[Bug libgcc/116017] libgcc/riscv/softfp: Fix loss of sign when truncating NaN values
keithp at keithp dot com
gcc-bugzilla@gcc.gnu.org
Sat Jul 20 22:38:24 GMT 2024
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116017
--- Comment #5 from keithp at keithp dot com <keithp at keithp dot com> ---
You're quite correct; conversion from double to float also loses the sign bit.
It never occurred to me that RISC-V would be different from every other GCC
target in this regard. I'll go stick a whole pile of risc-v specific code where
I needs to preserve the sign; I'm glad I don't also have to preserve the
significand. Yay for standards!
Soft float, double, long double:
test/test-nansign_rv64imac_lp64
ld: -nan (sign 1) d: nan (sign 0) f: nan (sign 0)
Hard float, soft double, long double:
test/test-nansign_rv64imafc_lp64f
ld: -nan (sign 1) d: nan (sign 0) f: nan (sign 0)
Hard float, hard double, soft long double:
test/test-nansign_rv64ifd_lp64d
ld: -nan (sign 1) d: nan (sign 0) f: nan (sign 0)
Here's the code which generated the above:
#define __STDC_WANT_IEC_60559_BFP_EXT__
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
volatile long double ld;
volatile double d;
volatile float f;
int ldbit, dbit, fbit;
char ldstr[64];
char dstr[64];
char fstr[64];
ld = (long double) NAN;
ld = -ld;
ldbit = signbit(ld);
strfroml(ldstr, sizeof(ldstr), "%a", ld);
d = (double) ld;
dbit = signbit(d);
strfromd(dstr, sizeof(dstr), "%a", d);
d = (double) NAN;
d = -d;
f = (float) d;
fbit = signbit(f);
strfromf(fstr, sizeof(fstr), "%a", f);
printf("ld: %s (sign %d) d: %s (sign %d) f: %s (sign %d)\n",
ldstr, ldbit, dstr, dbit, fstr, fbit);
if (ldbit == 0 || dbit == 0 || fbit == 0)
return 1;
return 0;
}
More information about the Gcc-bugs
mailing list