rework langinfo code for ABI compat and for use by time code
[musl] / src / math / remainderf.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/e_remainderf.c */
2 /*
3  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
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 "libm.h"
17
18 float remainderf(float x, float p)
19 {
20         int32_t hx,hp;
21         uint32_t sx;
22         float p_half;
23
24         GET_FLOAT_WORD(hx, x);
25         GET_FLOAT_WORD(hp, p);
26         sx = hx & 0x80000000;
27         hp &= 0x7fffffff;
28         hx &= 0x7fffffff;
29
30         /* purge off exception values */
31         if (hp == 0 || hx >= 0x7f800000 || hp > 0x7f800000)  /* p = 0, x not finite, p is NaN */
32                 return (x*p)/(x*p);
33
34         if (hp <= 0x7effffff)
35                 x = fmodf(x, p + p);  /* now x < 2p */
36         if (hx - hp == 0)
37                 return 0.0f*x;
38         x = fabsf(x);
39         p = fabsf(p);
40         if (hp < 0x01000000) {
41                 if (x + x > p) {
42                         x -= p;
43                         if (x + x >= p)
44                                 x -= p;
45                 }
46         } else {
47                 p_half = 0.5f*p;
48                 if (x > p_half) {
49                         x -= p;
50                         if (x >= p_half)
51                                 x -= p;
52                 }
53         }
54         GET_FLOAT_WORD(hx, x);
55         if ((hx & 0x7fffffff) == 0)
56                 hx = 0;
57         SET_FLOAT_WORD(x, hx ^ sx);
58         return x;
59 }