Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 1 | #include "libm.h" |
| 2 | |
| 3 | #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024 |
| 4 | long double rintl(long double x) |
| 5 | { |
| 6 | return rint(x); |
| 7 | } |
| 8 | #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384 |
Szabolcs Nagy | 0ce946c | 2014-10-29 00:34:37 +0100 | [diff] [blame] | 9 | |
| 10 | static const long double toint = 1/LDBL_EPSILON; |
| 11 | |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 12 | long double rintl(long double x) |
| 13 | { |
Szabolcs Nagy | d1a2ead | 2013-09-03 03:27:02 +0000 | [diff] [blame] | 14 | union ldshape u = {x}; |
| 15 | int e = u.i.se & 0x7fff; |
| 16 | int s = u.i.se >> 15; |
| 17 | long double y; |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 18 | |
Szabolcs Nagy | d1a2ead | 2013-09-03 03:27:02 +0000 | [diff] [blame] | 19 | if (e >= 0x3fff+LDBL_MANT_DIG-1) |
| 20 | return x; |
| 21 | if (s) |
Szabolcs Nagy | 0ce946c | 2014-10-29 00:34:37 +0100 | [diff] [blame] | 22 | y = x - toint + toint; |
Szabolcs Nagy | d1a2ead | 2013-09-03 03:27:02 +0000 | [diff] [blame] | 23 | else |
Szabolcs Nagy | 0ce946c | 2014-10-29 00:34:37 +0100 | [diff] [blame] | 24 | y = x + toint - toint; |
Szabolcs Nagy | d1a2ead | 2013-09-03 03:27:02 +0000 | [diff] [blame] | 25 | if (y == 0) |
| 26 | return 0*x; |
| 27 | return y; |
Rich Felker | b69f695 | 2012-03-13 01:17:53 -0400 | [diff] [blame] | 28 | } |
| 29 | #endif |