mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] math: optimize lrint on 32bit targets
@ 2019-09-21 15:52 Szabolcs Nagy
  2019-09-22 20:43 ` Szabolcs Nagy
  0 siblings, 1 reply; 8+ messages in thread
From: Szabolcs Nagy @ 2019-09-21 15:52 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 27 bytes --]

this was discussed on irc.

[-- Attachment #2: 0001-math-optimize-lrint-on-32bit-targets.patch --]
[-- Type: text/x-diff, Size: 2000 bytes --]

From 902e0db0a7a9d4f11870e1f784e1d14b5491ce8c Mon Sep 17 00:00:00 2001
From: Szabolcs Nagy <nsz@port70.net>
Date: Mon, 16 Sep 2019 20:33:11 +0000
Subject: [PATCH] math: optimize lrint on 32bit targets

lrint in (LONG_MAX, 1/DBL_EPSILON) and in (-1/DBL_EPSILON, LONG_MIN)
is not trivial: rounding to int may be inexact, but the conversion to
int may overflow and then the inexact flag must not be raised. (the
overflow threshold is rounding mode dependent).

this matters on 32bit targets (without single instruction lrint or
rint), so the common case (when there is no overflow) is optimized by
inlining the lrint logic, otherwise the old code is kept as a fallback.

(on my laptop an i486 lrint call is asm:10ns, old c:31ns, new c:22ns)
---
 src/math/lrint.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/src/math/lrint.c b/src/math/lrint.c
index bdca8b7c..08abf326 100644
--- a/src/math/lrint.c
+++ b/src/math/lrint.c
@@ -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,24 @@ long lrint(double x)
 	/* conversion */
 	return x;
 }
+
+long lrint(double x)
+{
+	union {double f; uint64_t i;} u = {x};
+	uint32_t abstop = u.i>>32 & 0x7fffffff;
+
+	if (abstop < 0x41dfffff) {
+		/* |x| < 0x7ffffc00 */
+		double_t y, t;
+		union {double f; uint64_t i;} toint = {1/EPS};
+		toint.i |= u.i & (1ULL << 63);
+		t = toint.f;
+		y = x + t - t;
+		/* conversion */
+		return y;
+	}
+	return lrint_slow(x);
+}
 #else
 long lrint(double x)
 {
-- 
2.21.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-09-23 20:42 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-21 15:52 [PATCH] math: optimize lrint on 32bit targets Szabolcs Nagy
2019-09-22 20:43 ` Szabolcs Nagy
2019-09-23 14:24   ` Rich Felker
2019-09-23 14:54     ` Szabolcs Nagy
2019-09-23 16:08       ` Rich Felker
2019-09-23 17:40   ` Rich Felker
2019-09-23 18:38     ` Szabolcs Nagy
2019-09-23 20:42       ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).