1/*
2 * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/* __ieee754_remainder(x,p)
27 * Return :
28 *      returns  x REM p  =  x - [x/p]*p as if in infinite
29 *      precise arithmetic, where [x/p] is the (infinite bit)
30 *      integer nearest x/p (in half way case choose the even one).
31 * Method :
32 *      Based on fmod() return x-[x/p]chopped*p exactlp.
33 */
34
35#include "fdlibm.h"
36
37#ifdef __STDC__
38static const double zero = 0.0;
39#else
40static double zero = 0.0;
41#endif
42
43
44#ifdef __STDC__
45        double __ieee754_remainder(double x, double p)
46#else
47        double __ieee754_remainder(x,p)
48        double x,p;
49#endif
50{
51        int hx,hp;
52        unsigned sx,lx,lp;
53        double p_half;
54
55        hx = __HI(x);           /* high word of x */
56        lx = __LO(x);           /* low  word of x */
57        hp = __HI(p);           /* high word of p */
58        lp = __LO(p);           /* low  word of p */
59        sx = hx&0x80000000;
60        hp &= 0x7fffffff;
61        hx &= 0x7fffffff;
62
63    /* purge off exception values */
64        if((hp|lp)==0) return (x*p)/(x*p);      /* p = 0 */
65        if((hx>=0x7ff00000)||                   /* x not finite */
66          ((hp>=0x7ff00000)&&                   /* p is NaN */
67          (((hp-0x7ff00000)|lp)!=0)))
68            return (x*p)/(x*p);
69
70
71        if (hp<=0x7fdfffff) x = __ieee754_fmod(x,p+p);  /* now x < 2p */
72        if (((hx-hp)|(lx-lp))==0) return zero*x;
73        x  = fabs(x);
74        p  = fabs(p);
75        if (hp<0x00200000) {
76            if(x+x>p) {
77                x-=p;
78                if(x+x>=p) x -= p;
79            }
80        } else {
81            p_half = 0.5*p;
82            if(x>p_half) {
83                x-=p;
84                if(x>=p_half) x -= p;
85            }
86        }
87        __HI(x) ^= sx;
88        return x;
89}
90