1#include "libm.h"
2
3#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
4long double roundl(long double x) {
5    return round(x);
6}
7#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
8
9static const long double toint = 1 / LDBL_EPSILON;
10
11long double roundl(long double x) {
12    union ldshape u = {x};
13    int e = u.i.se & 0x7fff;
14    long double y;
15
16    if (e >= 0x3fff + LDBL_MANT_DIG - 1)
17        return x;
18    if (u.i.se >> 15)
19        x = -x;
20    if (e < 0x3fff - 1) {
21        FORCE_EVAL(x + toint);
22        return 0 * u.f;
23    }
24    y = x + toint - toint - x;
25    if (y > 0.5)
26        y = y + x - 1;
27    else if (y <= -0.5)
28        y = y + x + 1;
29    else
30        y = y + x;
31    if (u.i.se >> 15)
32        y = -y;
33    return y;
34}
35#endif
36