math: rewrite remainder functions (remainder, remquo, fmod, modf)
[musl] / src / math / roundl.c
1 #include "libm.h"
2
3 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
4 long double roundl(long double x)
5 {
6         return round(x);
7 }
8 #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
9 #if LDBL_MANT_DIG == 64
10 #define TOINT 0x1p63
11 #elif LDBL_MANT_DIG == 113
12 #define TOINT 0x1p112
13 #endif
14 long double roundl(long double x)
15 {
16         union ldshape u = {x};
17         int e = u.i.se & 0x7fff;
18         long double y;
19
20         if (e >= 0x3fff+LDBL_MANT_DIG-1)
21                 return x;
22         if (u.i.se >> 15)
23                 x = -x;
24         if (e < 0x3fff-1) {
25                 FORCE_EVAL(x + TOINT);
26                 return 0*u.f;
27         }
28         y = x + TOINT - TOINT - x;
29         if (y > 0.5)
30                 y = y + x - 1;
31         else if (y <= -0.5)
32                 y = y + x + 1;
33         else
34                 y = y + x;
35         if (u.i.se >> 15)
36                 y = -y;
37         return y;
38 }
39 #endif