diff options
| author | Damien George <damien.p.george@gmail.com> | 2017-06-23 15:52:00 +1000 |
|---|---|---|
| committer | Damien George <damien.p.george@gmail.com> | 2017-06-28 15:12:04 +1000 |
| commit | 045116551ec06d2619250cffff5e8b56a5f827d1 (patch) | |
| tree | 314e63ab9307cb0bc4d33486f173ced228209186 /lib/libm_dbl/ceil.c | |
| parent | 409fc8f9c1c5f07bcbdb6229196775deb4133e03 (diff) | |
lib: Add libm_dbl, a double-precision math library, from musl-1.1.16.
Diffstat (limited to 'lib/libm_dbl/ceil.c')
| -rw-r--r-- | lib/libm_dbl/ceil.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/libm_dbl/ceil.c b/lib/libm_dbl/ceil.c new file mode 100644 index 000000000..b13e6f2d6 --- /dev/null +++ b/lib/libm_dbl/ceil.c @@ -0,0 +1,31 @@ +#include "libm.h" + +#if FLT_EVAL_METHOD==0 || FLT_EVAL_METHOD==1 +#define EPS DBL_EPSILON +#elif FLT_EVAL_METHOD==2 +#define EPS LDBL_EPSILON +#endif +static const double_t toint = 1/EPS; + +double ceil(double x) +{ + union {double f; uint64_t i;} u = {x}; + int e = u.i >> 52 & 0x7ff; + double_t y; + + if (e >= 0x3ff+52 || x == 0) + return x; + /* y = int(x) - x, where int(x) is an integer neighbor of x */ + if (u.i >> 63) + y = x - toint + toint - x; + else + y = x + toint - toint - x; + /* special case because of non-nearest rounding modes */ + if (e <= 0x3ff-1) { + FORCE_EVAL(y); + return u.i >> 63 ? -0.0 : 1; + } + if (y < 0) + return x + y + 1; + return x + y; +} |
