blob: 374327db2282d450ceb79e12286db767eb452148 [file] [log] [blame]
Rich Felkerb69f6952012-03-13 01:17:53 -04001#include "libm.h"
2
3#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
4long 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 Nagy0ce946c2014-10-29 00:34:37 +01009
10static const long double toint = 1/LDBL_EPSILON;
11
Rich Felkerb69f6952012-03-13 01:17:53 -040012long double rintl(long double x)
13{
Szabolcs Nagyd1a2ead2013-09-03 03:27:02 +000014 union ldshape u = {x};
15 int e = u.i.se & 0x7fff;
16 int s = u.i.se >> 15;
17 long double y;
Rich Felkerb69f6952012-03-13 01:17:53 -040018
Szabolcs Nagyd1a2ead2013-09-03 03:27:02 +000019 if (e >= 0x3fff+LDBL_MANT_DIG-1)
20 return x;
21 if (s)
Szabolcs Nagy0ce946c2014-10-29 00:34:37 +010022 y = x - toint + toint;
Szabolcs Nagyd1a2ead2013-09-03 03:27:02 +000023 else
Szabolcs Nagy0ce946c2014-10-29 00:34:37 +010024 y = x + toint - toint;
Szabolcs Nagyd1a2ead2013-09-03 03:27:02 +000025 if (y == 0)
26 return 0*x;
27 return y;
Rich Felkerb69f6952012-03-13 01:17:53 -040028}
29#endif