From: Michael Hudson-Doyle <michael.hudson@canonical.com>
To: libc-alpha@sourceware.org
Subject: [PATCH v2] Avoid undefined behaviour in ibm128 implementation of llroundl
Date: Mon, 22 Aug 2022 14:04:20 +1200 [thread overview]
Message-ID: <20220822020419.137775-1-michael.hudson@canonical.com> (raw)
In-Reply-To: <20220821104446.46521-1-michael.hudson@canonical.com>
Detecting an overflow edge case depended on signed overflow of a long
long. Replace the signed long long with unsigned and cast it back to
unsigned before comparisons (which is implementation defined behaviour,
but I guess glibc does not support any one's complement
architectures...).
BZ #29488
---
v2: added casts to some references to 'res' I missed in v1. This
version passes all tests on ppc64el with gcc 12 with both -O2 and -O3.
---
sysdeps/ieee754/ldbl-128ibm/s_llroundl.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c b/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c
index d85154e73a..5f54f92767 100644
--- a/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c
+++ b/sysdeps/ieee754/ldbl-128ibm/s_llroundl.c
@@ -28,7 +28,8 @@ long long
__llroundl (long double x)
{
double xh, xl;
- long long res, hi, lo;
+ unsigned long long res;
+ long long hi, lo;
ldbl_unpack (x, &xh, &xl);
@@ -69,7 +70,7 @@ __llroundl (long double x)
res = hi + lo;
/* This is just sign(hi) == sign(lo) && sign(res) != sign(hi). */
- if (__glibc_unlikely (((~(hi ^ lo) & (res ^ hi)) < 0)))
+ if (__glibc_unlikely (((~(hi ^ lo) & (((long long)res) ^ hi)) < 0)))
goto overflow;
xh -= lo;
@@ -82,7 +83,7 @@ __llroundl (long double x)
}
else if (xh == 0.5)
{
- if (xl > 0.0 || (xl == 0.0 && res >= 0))
+ if (xl > 0.0 || (xl == 0.0 && ((long long)res) >= 0))
res += 1;
}
else if (-xh > 0.5)
@@ -91,11 +92,11 @@ __llroundl (long double x)
}
else if (-xh == 0.5)
{
- if (xl < 0.0 || (xl == 0.0 && res <= 0))
+ if (xl < 0.0 || (xl == 0.0 && ((long long)res) <= 0))
res -= 1;
}
- if (__glibc_unlikely (((~(hi ^ (res - hi)) & (res ^ hi)) < 0)))
+ if (__glibc_unlikely (((~(hi ^ (((long long)res) - hi)) & (((long long)res) ^ hi)) < 0)))
goto overflow;
return res;
--
2.34.1
next prev parent reply other threads:[~2022-08-22 2:04 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-21 10:44 [PATCH] " Michael Hudson-Doyle
2022-08-21 19:24 ` Michael Hudson-Doyle
2022-08-22 2:04 ` Michael Hudson-Doyle [this message]
2022-08-22 7:54 ` [PATCH v2] " Florian Weimer
2022-08-22 8:34 ` Andreas Schwab
2022-08-22 8:52 ` Michael Hudson-Doyle
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=20220822020419.137775-1-michael.hudson@canonical.com \
--to=michael.hudson@canonical.com \
--cc=libc-alpha@sourceware.org \
/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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).