mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] Properly simplified nextafter()
@ 2021-08-10  6:23 Stefan Kanthak
  2021-08-10 21:34 ` Szabolcs Nagy
  0 siblings, 1 reply; 34+ messages in thread
From: Stefan Kanthak @ 2021-08-10  6:23 UTC (permalink / raw)
  To: musl

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

<https://git.musl-libc.org/cgit/musl/plain/src/math/nextafter.c>
has quite some superfluous statements:

1. there's absolutely no need for 2 uint64_t holding |x| and |y|;
2. IEEE-754 specifies -0.0 == +0.0, so (x == y) is equivalent to
   (ax == 0) && (ay == 0): the latter 2 tests can be removed;
3. there's absolutely no need to compare the signs of x and y
   with the sign of the direction: its sufficient to test that
   direction and sign of x match;
4. a proper compiler/optimizer should be able to reuse the results
   of the comparision (x == y) for (x < y) or (x > y) and
   (x == 0.0) for (x < 0.0) or (x > 0.0).

   JFTR: if ((x < 0.0) == (x < y)) is equivalent to
         if ((x > 0.0) == (x > y))

--- -/src/math/nextafter.c
+++ +/src/math/nextafter.c
@@ -3,20 +3,15 @@
 double nextafter(double x, double y)
 {
        union {double f; uint64_t i;} ux={x}, uy={y};
-       uint64_t ax, ay;
        int e;

        if (isnan(x) || isnan(y))
                return x + y;
-       if (ux.i == uy.i)
+       if (x == y)
                return y;
-       ax = ux.i & -1ULL/2;
-       ay = uy.i & -1ULL/2;
-       if (ax == 0) {
-               if (ay == 0)
-                       return y;
+       if (x == 0.0)
                ux.i = (uy.i & 1ULL<<63) | 1;
-       } else if (ax > ay || ((ux.i ^ uy.i) & 1ULL<<63))
+       else if ((x < 0.0) == (x < y))
                ux.i--;
        else
                ux.i++;

[-- Attachment #2: nextafter.patch --]
[-- Type: application/octet-stream, Size: 565 bytes --]

--- -/src/math/nextafter.c
+++ +/src/math/nextafter.c
@@ -3,20 +3,15 @@
 double nextafter(double x, double y)
 {
 	union {double f; uint64_t i;} ux={x}, uy={y};
-	uint64_t ax, ay;
 	int e;
 
 	if (isnan(x) || isnan(y))
 		return x + y;
-	if (ux.i == uy.i)
+	if (x == y)
 		return y;
-	ax = ux.i & -1ULL/2;
-	ay = uy.i & -1ULL/2;
-	if (ax == 0) {
-		if (ay == 0)
-			return y;
+	if (x == 0.0)
 		ux.i = (uy.i & 1ULL<<63) | 1;
-	} else if (ax > ay || ((ux.i ^ uy.i) & 1ULL<<63))
+	else if ((x < 0.0) == (x < y))
 		ux.i--;
 	else
 		ux.i++;

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

end of thread, other threads:[~2021-08-15 21:49 UTC | newest]

Thread overview: 34+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-10  6:23 [musl] [PATCH] Properly simplified nextafter() Stefan Kanthak
2021-08-10 21:34 ` Szabolcs Nagy
2021-08-10 22:53   ` Stefan Kanthak
2021-08-11  2:40     ` Rich Felker
2021-08-11 15:44       ` Stefan Kanthak
2021-08-11 16:09         ` Rich Felker
2021-08-11 16:50           ` Stefan Kanthak
2021-08-11 17:57             ` Rich Felker
2021-08-11 22:16               ` Szabolcs Nagy
2021-08-11 22:43                 ` Stefan Kanthak
2021-08-12  0:59                   ` Rich Felker
2021-08-11  8:23     ` Szabolcs Nagy
2021-08-13 12:04   ` [musl] [PATCH #2] " Stefan Kanthak
2021-08-13 15:59     ` Rich Felker
2021-08-13 18:30       ` Stefan Kanthak
2021-08-14  4:07         ` Damian McGuckin
2021-08-14 22:45           ` Szabolcs Nagy
2021-08-14 23:46     ` Szabolcs Nagy
2021-08-15  7:04       ` Stefan Kanthak
2021-08-15  7:46         ` Ariadne Conill
2021-08-15 13:59           ` Rich Felker
2021-08-15 14:57             ` Ariadne Conill
2021-08-15  8:24         ` Damian McGuckin
2021-08-15 14:03           ` Rich Felker
2021-08-15 15:10             ` Damian McGuckin
2021-08-15 14:56         ` Szabolcs Nagy
2021-08-15 15:19           ` Stefan Kanthak
2021-08-15 15:48             ` Rich Felker
2021-08-15 16:29               ` Stefan Kanthak
2021-08-15 16:49                 ` Rich Felker
2021-08-15 20:52                   ` Stefan Kanthak
2021-08-15 21:48                     ` Rich Felker
2021-08-15 15:52             ` Ariadne Conill
2021-08-15 16:09               ` 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).