From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 26193 invoked from network); 10 Aug 2021 06:26:26 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 10 Aug 2021 06:26:26 -0000 Received: (qmail 12150 invoked by uid 550); 10 Aug 2021 06:26:22 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 12118 invoked from network); 10 Aug 2021 06:26:22 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nexgo.de; s=vfde-smtpout-mb-15sep; t=1628576650; bh=2INlY6peUQc+JJonCd6d8+WB+f/0LT47hKJbuyeM3iw=; h=From:To:Subject:Date; b=ktHKYP6zcI3fi6P1U26CHFnrO5eF8iHZNsyUkytqvSGrjHOwVTgRxaAZDhhL3sfgt /q6KwYvcfPaXHXe3sfTSHcx6TFn4CHrwLgYbAt0pH0cjWS/LreDFnBhoL7T3QV+sj4 3xUnZG3j1/WFlMkYB/q6TlBsMuE9roSKGp14G1qM= Message-ID: <0C6AAAD55DA44C6189B2FF4F5FB2C3E7@H270> From: "Stefan Kanthak" To: Date: Tue, 10 Aug 2021 08:23:46 +0200 Organization: Me, myself & IT MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_8CED_01D78DC1.0A02F710" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Windows Mail 6.0.6002.18197 X-MimeOLE: Produced By Microsoft MimeOLE V6.1.7601.24158 X-purgate-type: clean X-purgate-Ad: Categorized by eleven eXpurgate (R) http://www.eleven.de X-purgate: This mail is considered clean (visit http://www.eleven.de for further information) X-purgate: clean X-purgate-size: 2622 X-purgate-ID: 155817::1628576650-00003C24-8A7C3590/0/0 Subject: [musl] [PATCH] Properly simplified nextafter() This is a multi-part message in MIME format. ------=_NextPart_000_8CED_01D78DC1.0A02F710 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit 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++; ------=_NextPart_000_8CED_01D78DC1.0A02F710 Content-Type: application/octet-stream; name="nextafter.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="nextafter.patch" --- -/src/math/nextafter.c=0A= +++ +/src/math/nextafter.c=0A= @@ -3,20 +3,15 @@=0A= double nextafter(double x, double y)=0A= {=0A= union {double f; uint64_t i;} ux=3D{x}, uy=3D{y};=0A= - uint64_t ax, ay;=0A= int e;=0A= =0A= if (isnan(x) || isnan(y))=0A= return x + y;=0A= - if (ux.i =3D=3D uy.i)=0A= + if (x =3D=3D y)=0A= return y;=0A= - ax =3D ux.i & -1ULL/2;=0A= - ay =3D uy.i & -1ULL/2;=0A= - if (ax =3D=3D 0) {=0A= - if (ay =3D=3D 0)=0A= - return y;=0A= + if (x =3D=3D 0.0)=0A= ux.i =3D (uy.i & 1ULL<<63) | 1;=0A= - } else if (ax > ay || ((ux.i ^ uy.i) & 1ULL<<63))=0A= + else if ((x < 0.0) =3D=3D (x < y))=0A= ux.i--;=0A= else=0A= ux.i++;=0A= ------=_NextPart_000_8CED_01D78DC1.0A02F710--