math: rewrite remainder functions (remainder, remquo, fmod, modf)
[musl] / src / math / round.c
1 #include "libm.h"
2
3 double round(double x)
4 {
5         union {double f; uint64_t i;} u = {x};
6         int e = u.i >> 52 & 0x7ff;
7         double_t y;
8
9         if (e >= 0x3ff+52)
10                 return x;
11         if (u.i >> 63)
12                 x = -x;
13         if (e < 0x3ff-1) {
14                 /* raise inexact if x!=0 */
15                 FORCE_EVAL(x + 0x1p52);
16                 return 0*u.f;
17         }
18         y = (double)(x + 0x1p52) - 0x1p52 - x;
19         if (y > 0.5)
20                 y = y + x - 1;
21         else if (y <= -0.5)
22                 y = y + x + 1;
23         else
24                 y = y + x;
25         if (u.i >> 63)
26                 y = -y;
27         return y;
28 }