Viet-Trung Luu | 96b05c1 | 2016-01-11 11:26:36 -0800 | [diff] [blame] | 1 | #include "libm.h" |
| 2 | |
| 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 |
George Kulakowski | 17e3b04 | 2016-02-18 15:59:50 -0800 | [diff] [blame] | 4 | long double floorl(long double x) { |
| 5 | return floor(x); |
Viet-Trung Luu | 96b05c1 | 2016-01-11 11:26:36 -0800 | [diff] [blame] | 6 | } |
| 7 | #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 |
| 8 | |
George Kulakowski | 17e3b04 | 2016-02-18 15:59:50 -0800 | [diff] [blame] | 9 | static const long double toint = 1 / LDBL_EPSILON; |
Viet-Trung Luu | 96b05c1 | 2016-01-11 11:26:36 -0800 | [diff] [blame] | 10 | |
George Kulakowski | 17e3b04 | 2016-02-18 15:59:50 -0800 | [diff] [blame] | 11 | long double floorl(long double x) { |
| 12 | union ldshape u = {x}; |
| 13 | int e = u.i.se & 0x7fff; |
| 14 | long double y; |
Viet-Trung Luu | 96b05c1 | 2016-01-11 11:26:36 -0800 | [diff] [blame] | 15 | |
George Kulakowski | 17e3b04 | 2016-02-18 15:59:50 -0800 | [diff] [blame] | 16 | if (e >= 0x3fff + LDBL_MANT_DIG - 1 || x == 0) |
| 17 | return x; |
| 18 | /* y = int(x) - x, where int(x) is an integer neighbor of x */ |
| 19 | if (u.i.se >> 15) |
| 20 | y = x - toint + toint - x; |
| 21 | else |
| 22 | y = x + toint - toint - x; |
| 23 | /* special case because of non-nearest rounding modes */ |
| 24 | if (e <= 0x3fff - 1) { |
| 25 | FORCE_EVAL(y); |
| 26 | return u.i.se >> 15 ? -1 : 0; |
| 27 | } |
| 28 | if (y > 0) |
| 29 | return x + y - 1; |
| 30 | return x + y; |
Viet-Trung Luu | 96b05c1 | 2016-01-11 11:26:36 -0800 | [diff] [blame] | 31 | } |
| 32 | #endif |