POK
/home/jaouen/pok_official/pok/trunk/libpok/libm/rint.c
00001 /*
00002  *                               POK header
00003  * 
00004  * The following file is a part of the POK project. Any modification should
00005  * made according to the POK licence. You CANNOT use this file or a part of
00006  * this file is this part of a file for your own project
00007  *
00008  * For more information on the POK licence, please see our LICENCE FILE
00009  *
00010  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
00011  *
00012  *                                      Copyright (c) 2007-2009 POK team 
00013  *
00014  * Created by julien on Fri Jan 30 14:41:34 2009 
00015  */
00016 
00017 /* @(#)s_rint.c 5.1 93/09/24 */
00018 /*
00019  * ====================================================
00020  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00021  *
00022  * Developed at SunPro, a Sun Microsystems, Inc. business.
00023  * Permission to use, copy, modify, and distribute this
00024  * software is freely granted, provided that this notice
00025  * is preserved.
00026  * ====================================================
00027  */
00028 
00029 #ifdef POK_NEEDS_LIBMATH
00030 
00031 /*
00032  * rint(x)
00033  * Return x rounded to integral value according to the prevailing
00034  * rounding mode.
00035  * Method:
00036  *      Using floating addition.
00037  * Exception:
00038  *      Inexact flag raised if x not equal to rint(x).
00039  */
00040 
00041 #include <libm.h>
00042 #include "math_private.h"
00043 
00044 static const double
00045 TWO52[2]={
00046   4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
00047  -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */
00048 };
00049 
00050 double
00051 rint(double x)
00052 {
00053         int32_t i0,jj0,sx;
00054         uint32_t i,i1;
00055         double w,t;
00056         EXTRACT_WORDS(i0,i1,x);
00057         sx = (i0>>31)&1;
00058         jj0 = ((i0>>20)&0x7ff)-0x3ff;
00059         if(jj0<20) {
00060             if(jj0<0) {
00061                 if(((i0&0x7fffffff)|i1)==0) return x;
00062                 i1 |= (i0&0x0fffff);
00063                 i0 &= 0xfffe0000;
00064                 i0 |= ((i1|-i1)>>12)&0x80000;
00065                 SET_HIGH_WORD(x,i0);
00066                 w = TWO52[sx]+x;
00067                 t =  w-TWO52[sx];
00068                 GET_HIGH_WORD(i0,t);
00069                 SET_HIGH_WORD(t,(i0&0x7fffffff)|(sx<<31));
00070                 return t;
00071             } else {
00072                 i = (0x000fffff)>>jj0;
00073                 if(((i0&i)|i1)==0) return x; /* x is integral */
00074                 i>>=1;
00075                 if(((i0&i)|i1)!=0) {
00076                     if(jj0==19) i1 = 0x40000000; else
00077                     i0 = (i0&(~i))|((0x20000)>>jj0);
00078                 }
00079             }
00080         } else if (jj0>51) {
00081             if(jj0==0x400) return x+x;  /* inf or NaN */
00082             else return x;              /* x is integral */
00083         } else {
00084             i = ((uint32_t)(0xffffffff))>>(jj0-20);
00085             if((i1&i)==0) return x;     /* x is integral */
00086             i>>=1;
00087             if((i1&i)!=0) i1 = (i1&(~i))|((0x40000000)>>(jj0-20));
00088         }
00089         INSERT_WORDS(x,i0,i1);
00090         w = TWO52[sx]+x;
00091         return w-TWO52[sx];
00092 }
00093 
00094 #endif
00095