POK
/home/jaouen/pok_official/pok/trunk/libpok/libm/e_remainder.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 /* @(#)e_remainder.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 
00030 /* __ieee754_remainder(x,p)
00031  * Return :
00032  *      returns  x REM p  =  x - [x/p]*p as if in infinite
00033  *      precise arithmetic, where [x/p] is the (infinite bit)
00034  *      integer nearest x/p (in half way case choose the even one).
00035  * Method :
00036  *      Based on fmod() return x-[x/p]chopped*p exactlp.
00037  */
00038 
00039 #ifdef POK_NEEDS_LIBMATH
00040 #include <libm.h>
00041 #include "math_private.h"
00042 
00043 static const double zero = 0.0;
00044 
00045 
00046 double
00047 __ieee754_remainder(double x, double p)
00048 {
00049         int32_t hx,hp;
00050         uint32_t sx,lx,lp;
00051         double p_half;
00052 
00053         EXTRACT_WORDS(hx,lx,x);
00054         EXTRACT_WORDS(hp,lp,p);
00055         sx = hx&0x80000000;
00056         hp &= 0x7fffffff;
00057         hx &= 0x7fffffff;
00058 
00059     /* purge off exception values */
00060         if((hp|lp)==0) return (x*p)/(x*p);      /* p = 0 */
00061         if((hx>=0x7ff00000)||                   /* x not finite */
00062           ((hp>=0x7ff00000)&&                   /* p is NaN */
00063           (((hp-0x7ff00000)|lp)!=0)))
00064             return (x*p)/(x*p);
00065 
00066 
00067         if (hp<=0x7fdfffff) x = __ieee754_fmod(x,p+p);  /* now x < 2p */
00068         if (((hx-hp)|(lx-lp))==0) return zero*x;
00069         x  = fabs(x);
00070         p  = fabs(p);
00071         if (hp<0x00200000) {
00072             if(x+x>p) {
00073                 x-=p;
00074                 if(x+x>=p) x -= p;
00075             }
00076         } else {
00077             p_half = 0.5*p;
00078             if(x>p_half) {
00079                 x-=p;
00080                 if(x>=p_half) x -= p;
00081             }
00082         }
00083         GET_HIGH_WORD(hx,x);
00084         SET_HIGH_WORD(x,hx^sx);
00085         return x;
00086 }
00087 #endif
00088