1 /* ef_remainder.c -- float version of e_remainder.c.
2  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3  */
4 
5 /*
6  * ====================================================
7  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8  *
9  * Developed at SunPro, a Sun Microsystems, Inc. business.
10  * Permission to use, copy, modify, and distribute this
11  * software is freely granted, provided that this notice
12  * is preserved.
13  * ====================================================
14  */
15 
16 #include "fdlibm.h"
17 
18 static const float zero = 0.0;
19 
20 float
remainderf(float x,float p)21 remainderf(float x, float p)
22 {
23     __int32_t hx, hp;
24     __uint32_t sx;
25     float p_half;
26 
27     GET_FLOAT_WORD(hx, x);
28     GET_FLOAT_WORD(hp, p);
29     sx = hx & 0x80000000;
30     hp &= 0x7fffffff;
31     hx &= 0x7fffffff;
32 
33     /* purge off exception values */
34     if (FLT_UWORD_IS_NAN(hx) || FLT_UWORD_IS_NAN(hp))
35         return x + p;
36 
37     if (isinf(x) || hp == 0)
38         return __math_invalidf(x);
39 
40     if (hp <= FLT_UWORD_HALF_MAX)
41         x = fmodf(x, p + p); /* now x < 2p */
42     if ((hx - hp) == 0)
43         return zero * x;
44     x = fabsf(x);
45     p = fabsf(p);
46     if (hp < 0x01000000) {
47         if (x + x > p) {
48             x -= p;
49             if (x + x >= p)
50                 x -= p;
51         }
52     } else {
53         p_half = (float)0.5 * p;
54         if (x > p_half) {
55             x -= p;
56             if (x >= p_half)
57                 x -= p;
58         }
59     }
60     GET_FLOAT_WORD(hx, x);
61     SET_FLOAT_WORD(x, hx ^ sx);
62     return x;
63 }
64 
65 _MATH_ALIAS_f_ff(remainder)
66