fix double-processing of DT_RELR relocations in ldso relocating itself
[musl] / src / math / lrint.c
index bdca8b7..ddee7a0 100644 (file)
@@ -1,5 +1,6 @@
 #include <limits.h>
 #include <fenv.h>
+#include <math.h>
 #include "libm.h"
 
 /*
@@ -26,7 +27,18 @@ as a double.
 */
 
 #if LONG_MAX < 1U<<53 && defined(FE_INEXACT)
-long lrint(double x)
+#include <float.h>
+#include <stdint.h>
+#if FLT_EVAL_METHOD==0 || FLT_EVAL_METHOD==1
+#define EPS DBL_EPSILON
+#elif FLT_EVAL_METHOD==2
+#define EPS LDBL_EPSILON
+#endif
+#ifdef __GNUC__
+/* avoid stack frame in lrint */
+__attribute__((noinline))
+#endif
+static long lrint_slow(double x)
 {
        #pragma STDC FENV_ACCESS ON
        int e;
@@ -38,6 +50,20 @@ long lrint(double x)
        /* conversion */
        return x;
 }
+
+long lrint(double x)
+{
+       uint32_t abstop = asuint64(x)>>32 & 0x7fffffff;
+       uint64_t sign = asuint64(x) & (1ULL << 63);
+
+       if (abstop < 0x41dfffff) {
+               /* |x| < 0x7ffffc00, no overflow */
+               double_t toint = asdouble(asuint64(1/EPS) | sign);
+               double_t y = x + toint - toint;
+               return (long)y;
+       }
+       return lrint_slow(x);
+}
 #else
 long lrint(double x)
 {