From: cosarara <cosa@cosarara.me>
To: 9front@9front.org
Subject: [9front] Bug converting vlong to double on 32bit architectures
Date: Fri, 30 Dec 2022 14:19:59 +0100 [thread overview]
Message-ID: <6d3b100d-1732-efa2-f01c-728440859ba2@cosarara.me> (raw)
Hi,
While trying to run kvik's lu9 on ARM, I found that when converting
LLONG_MIN to a double on 32bit systems, the result is wrong (positive
instead of negative).
Given the following test program:
#include <u.h>
#include <libc.h>
#define LLONG_MAX
#define LLONG_MIN (-LLONG_MAX-1)
void main() {
vlong min = LLONG_MIN;
double dmin = min;
print("minint %lld\n", min);
print("minint as double %f\n", dmin);
if (dmin > 0.0) {
exits("int min as double turned positive");
}
exits(0);
}
The output on x86_64 will be:
minint -9223372036854775808
minint as double -9223372036854776400.000000
But on arm or 386 (and I expect also spim, 68000, mips, 68020, sparc,
power, since they all use the same _v2d):
minint -9223372036854775808
minint as double 9223372036854776400.000000
And the value turned positive in the conversion.
The function used for the cast to double is (in /sys/src/libc/arm/vlrt.c):
#define SIGN(n) (1UL<<(n-1))
double
_v2d(Vlong x)
{
if(x.hi & SIGN(32)) {
if(x.lo) {
x.lo = -x.lo;
x.hi = ~x.hi;
} else
x.hi = -x.hi;
return -((long)x.hi*4294967296. + x.lo);
}
return (long)x.hi*4294967296. + x.lo;
}
If I understand correctly, the issue is that where it tries to flip the
sign for x.hi (x.hi = -x.hi), 0x80000000 has no positive, thus stays the
same (it stays negative). Then when we get to the negative return, we
get a positive out.
What came to my mind then, is that in the case that there is no x.lo, we
can keep the x.hi sign and cast directly, thus:
double
_v2d(Vlong x)
{
if(!x.lo) {
return (long)x.hi*4294967296.;
}
if(x.hi & SIGN(32)) {
x.lo = -x.lo;
x.hi = ~x.hi;
return -((long)x.hi*4294967296. + x.lo);
}
return (long)x.hi*4294967296. + x.lo;
}
This looks correct to me, but I don't trust myself to not make mistakes
in such critical code, so I would like some feedback on the change.
Happy new year in advance,
cosa
next reply other threads:[~2022-12-30 13:21 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-12-30 13:19 cosarara [this message]
2022-12-30 16:57 ` Jaume Delclòs Coll
2022-12-30 18:32 ` cinap_lenrek
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=6d3b100d-1732-efa2-f01c-728440859ba2@cosarara.me \
--to=cosa@cosarara.me \
--cc=9front@9front.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 NNTP newsgroup(s).