1#include "libm.h"
2#include <fenv.h>
3#include <limits.h>
4
5#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
6long lrintl(long double x) {
7    return lrint(x);
8}
9#elif defined(FE_INEXACT)
10/*
11see comments in lrint.c
12
13Note that if LONG_MAX == 0x7fffffffffffffff && LDBL_MANT_DIG == 64
14then x == 2**63 - 0.5 is the only input that overflows and
15raises inexact (with tonearest or upward rounding mode)
16*/
17long lrintl(long double x) {
18    PRAGMA_STDC_FENV_ACCESS_ON
19    int e;
20
21    e = fetestexcept(FE_INEXACT);
22    x = rintl(x);
23    if (!e && (x > LONG_MAX || x < LONG_MIN))
24        feclearexcept(FE_INEXACT);
25    /* conversion */
26    return x;
27}
28#else
29long lrintl(long double x) {
30    return rintl(x);
31}
32#endif
33