mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Szabolcs Nagy <nsz@port70.net>
To: musl@lists.openwall.com
Subject: [musl] [PATCH] math: avoid runtime conversions of floating-point constants
Date: Fri, 4 Feb 2022 21:04:45 +0100	[thread overview]
Message-ID: <20220204200445.GF1320090@port70.net> (raw)

gcc-12 with -frounding-mode will do inexact constant conversions at
runtime according to the runtime rounding mode.

in the math library we want constants to be rounding mode independent
so this patch fixes cases where new runtime conversions happen with
gcc-12.

fortunately this only affects two minor cases, the fix uses global
initializers where rounding mode does not apply.

after the patch the same amount of conversions happen with gcc-12 as
with gcc-11.
---
 src/complex/cacosf.c | 4 +++-
 src/complex/catanf.c | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/complex/cacosf.c b/src/complex/cacosf.c
index 2e048540..ed8acf0f 100644
--- a/src/complex/cacosf.c
+++ b/src/complex/cacosf.c
@@ -2,8 +2,10 @@
 
 // FIXME
 
+static const float float_pi_2 = M_PI_2;
+
 float complex cacosf(float complex z)
 {
 	z = casinf(z);
-	return CMPLXF((float)M_PI_2 - crealf(z), -cimagf(z));
+	return CMPLXF(float_pi_2 - crealf(z), -cimagf(z));
 }
diff --git a/src/complex/catanf.c b/src/complex/catanf.c
index ef3907a5..1d569f2d 100644
--- a/src/complex/catanf.c
+++ b/src/complex/catanf.c
@@ -61,13 +61,15 @@ static const double DP1 = 3.140625;
 static const double DP2 = 9.67502593994140625E-4;
 static const double DP3 = 1.509957990978376432E-7;
 
+static const float float_pi = M_PI;
+
 static float _redupif(float xx)
 {
 	float x, t;
 	long i;
 
 	x = xx;
-	t = x/(float)M_PI;
+	t = x/float_pi;
 	if (t >= 0.0f)
 		t += 0.5f;
 	else
-- 
2.35.1


                 reply	other threads:[~2022-02-04 20:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220204200445.GF1320090@port70.net \
    --to=nsz@port70.net \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).