Merge from vendor branch LIBPCAP:
[dragonfly.git] / lib / msun / src / e_remainder.c
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  * $FreeBSD: src/lib/msun/src/e_remainder.c,v 1.6 1999/08/28 00:06:38 peter Exp $
13  * $DragonFly: src/lib/msun/src/Attic/e_remainder.c,v 1.2 2003/06/17 04:26:53 dillon Exp $
14  */
15
16 /* __ieee754_remainder(x,p)
17  * Return :
18  *      returns  x REM p  =  x - [x/p]*p as if in infinite
19  *      precise arithmetic, where [x/p] is the (infinite bit)
20  *      integer nearest x/p (in half way case choose the even one).
21  * Method :
22  *      Based on fmod() return x-[x/p]chopped*p exactlp.
23  */
24
25 #include "math.h"
26 #include "math_private.h"
27
28 #ifdef __STDC__
29 static const double zero = 0.0;
30 #else
31 static double zero = 0.0;
32 #endif
33
34
35 #ifdef __STDC__
36         double __generic___ieee754_remainder(double x, double p)
37 #else
38         double __generic___ieee754_remainder(x,p)
39         double x,p;
40 #endif
41 {
42         int32_t hx,hp;
43         u_int32_t sx,lx,lp;
44         double p_half;
45
46         EXTRACT_WORDS(hx,lx,x);
47         EXTRACT_WORDS(hp,lp,p);
48         sx = hx&0x80000000;
49         hp &= 0x7fffffff;
50         hx &= 0x7fffffff;
51
52     /* purge off exception values */
53         if((hp|lp)==0) return (x*p)/(x*p);      /* p = 0 */
54         if((hx>=0x7ff00000)||                   /* x not finite */
55           ((hp>=0x7ff00000)&&                   /* p is NaN */
56           (((hp-0x7ff00000)|lp)!=0)))
57             return (x*p)/(x*p);
58
59
60         if (hp<=0x7fdfffff) x = __ieee754_fmod(x,p+p);  /* now x < 2p */
61         if (((hx-hp)|(lx-lp))==0) return zero*x;
62         x  = fabs(x);
63         p  = fabs(p);
64         if (hp<0x00200000) {
65             if(x+x>p) {
66                 x-=p;
67                 if(x+x>=p) x -= p;
68             }
69         } else {
70             p_half = 0.5*p;
71             if(x>p_half) {
72                 x-=p;
73                 if(x>=p_half) x -= p;
74             }
75         }
76         GET_HIGH_WORD(hx,x);
77         SET_HIGH_WORD(x,hx^sx);
78         return x;
79 }