1 /* @(#)e_remainder.c 5.1 93/09/24 */ 2 /* 3 * ==================================================== 4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 * 6 * Developed at SunPro, a Sun Microsystems, Inc. business. 7 * Permission to use, copy, modify, and distribute this 8 * software is freely granted, provided that this notice 9 * is preserved. 10 * ==================================================== 11 */ 12 13 #include <sys/cdefs.h> 14 #if defined(LIBM_SCCS) && !defined(lint) 15 __RCSID("$NetBSD: e_remainder.c,v 1.10 1999/07/02 15:37:41 simonb Exp $"); 16 #endif 17 18 /* __ieee754_remainder(x,p) 19 * Return : 20 * returns x REM p = x - [x/p]*p as if in infinite 21 * precise arithmetic, where [x/p] is the (infinite bit) 22 * integer nearest x/p (in half way case choose the even one). 23 * Method : 24 * Based on fmod() return x-[x/p]chopped*p exactlp. 25 */ 26 27 #include "math.h" 28 #include "math_private.h" 29 30 #ifdef __STDC__ 31 static const double zero = 0.0; 32 #else 33 static double zero = 0.0; 34 #endif 35 36 37 #ifdef __STDC__ 38 double __ieee754_remainder(double x, double p) 39 #else 40 double __ieee754_remainder(x,p) 41 double x,p; 42 #endif 43 { 44 int32_t hx,hp; 45 u_int32_t sx,lx,lp; 46 double p_half; 47 48 EXTRACT_WORDS(hx,lx,x); 49 EXTRACT_WORDS(hp,lp,p); 50 sx = hx&0x80000000; 51 hp &= 0x7fffffff; 52 hx &= 0x7fffffff; 53 54 /* purge off exception values */ 55 if((hp|lp)==0) return (x*p)/(x*p); /* p = 0 */ 56 if((hx>=0x7ff00000)|| /* x not finite */ 57 ((hp>=0x7ff00000)&& /* p is NaN */ 58 (((hp-0x7ff00000)|lp)!=0))) 59 return (x*p)/(x*p); 60 61 62 if (hp<=0x7fdfffff) x = __ieee754_fmod(x,p+p); /* now x < 2p */ 63 if (((hx-hp)|(lx-lp))==0) return zero*x; 64 x = fabs(x); 65 p = fabs(p); 66 if (hp<0x00200000) { 67 if(x+x>p) { 68 x-=p; 69 if(x+x>=p) x -= p; 70 } 71 } else { 72 p_half = 0.5*p; 73 if(x>p_half) { 74 x-=p; 75 if(x>=p_half) x -= p; 76 } 77 } 78 GET_HIGH_WORD(hx,x); 79 SET_HIGH_WORD(x,hx^sx); 80 return x; 81 } 82