glibc/sysdeps
Letu Ren 0cc0033ef1 stdlib/strfrom: Add copysign to fix NAN issue on riscv (BZ #29501)
According to the specification of ISO/IEC TS 18661-1:2014,

The strfromd, strfromf, and strfroml functions are equivalent to
snprintf(s, n, format, fp) (7.21.6.5), except the format string contains only
the character %, an optional precision that does not contain an asterisk *, and
one of the conversion specifiers a, A, e, E, f, F, g, or G, which applies to
the type (double, float, or long double) indicated by the function suffix
(rather than  by a length modifier). Use of these functions with any other 20
format string results in undefined behavior.

strfromf will convert the arguement with type float to double first.

According to the latest version of IEEE754 which is published in 2019,

Conversion of a quiet NaN from a narrower format to a wider format in the same
radix, and then back to the same narrower format, should not change the quiet
NaN payload in any way except to make it canonical.

When either an input or result is a NaN, this standard does not interpret the
sign of a NaN. However, operations on bit strings—copy, negate, abs,
copySign—specify the sign bit of a NaN result, sometimes based upon the sign
bit of a NaN operand. The logical predicates totalOrder and isSignMinus are
also affected by the sign bit of a NaN operand. For all other operations, this
standard does not specify the sign bit of a NaN result, even when there is only
one input NaN, or when the NaN is produced from an invalid operation.

converting NAN or -NAN with type float to double doesn't need to keep
the signbit. As a result, this test case isn't mandatory.

The problem is that according to RISC-V ISA manual in chapter 11.3 of
riscv-isa-20191213,

Except when otherwise stated, if the result of a floating-point operation is
NaN, it is the canonical NaN. The canonical NaN has a positive sign and all
significand bits clear except the MSB, a.k.a. the quiet bit. For
single-precision floating-point, this corresponds to the pattern 0x7fc00000.

which means that conversion -NAN from float to double won't keep the signbit.

Since glibc ought to be consistent here between types and architectures, this
patch adds copysign to fix this problem if the string is NAN. This patch
adds two different functions under sysdeps directory to work around the
issue.

This patch has been tested on x86_64 and riscv64.

Resolves: BZ #29501

v2: Change from macros to different inline functions.
v3: Add unlikely check to isnan.
v4: Fix wrong commit message header.
v5: Fix style: add space before parentheses.
v6: Add copyright.
Signed-off-by: Letu Ren <fantasquex@gmail.com>
Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2022-10-28 11:35:20 -03:00
..
aarch64 aarch64: Don't build wordcopy 2022-10-28 11:14:54 +01:00
alpha Remove all assembly optimizations for htonl and htons 2022-10-24 11:40:08 -03:00
arc Use PTR_MANGLE and PTR_DEMANGLE unconditionally in C sources 2022-10-18 17:04:10 +02:00
arm sysdeps: arm: Fix preconfigure script for ARMv8/v9 targets [BZ #29698] 2022-10-20 11:23:05 -03:00
csky Use PTR_MANGLE and PTR_DEMANGLE unconditionally in C sources 2022-10-18 17:04:10 +02:00
generic stdlib/strfrom: Add copysign to fix NAN issue on riscv (BZ #29501) 2022-10-28 11:35:20 -03:00
gnu errlist: add missing entry for EDEADLOCK (bug 29545) 2022-09-08 11:40:24 +02:00
hppa hppa: Fix initialization of dp register [BZ 29635] 2022-10-01 19:49:25 +00:00
htl htl: Make pthread*_cond_timedwait register wref before releasing mutex 2022-08-22 22:27:24 +02:00
hurd hurd: Fix pthread_kill on exiting/ted thread 2022-01-15 15:11:54 +01:00
i386 Remove all assembly optimizations for htonl and htons 2022-10-24 11:40:08 -03:00
ia64 Remove all assembly optimizations for htonl and htons 2022-10-24 11:40:08 -03:00
ieee754 Avoid undefined behaviour in ibm128 implementation of llroundl (BZ #29488) 2022-10-24 20:48:02 +02:00
loongarch Use PTR_MANGLE and PTR_DEMANGLE unconditionally in C sources 2022-10-18 17:04:10 +02:00
m68k Use PTR_MANGLE and PTR_DEMANGLE unconditionally in C sources 2022-10-18 17:04:10 +02:00
mach Use PTR_MANGLE and PTR_DEMANGLE unconditionally in C sources 2022-10-18 17:04:10 +02:00
microblaze Use PTR_MANGLE and PTR_DEMANGLE unconditionally in C sources 2022-10-18 17:04:10 +02:00
mips Use PTR_MANGLE and PTR_DEMANGLE unconditionally in C sources 2022-10-18 17:04:10 +02:00
nios2 Use PTR_MANGLE and PTR_DEMANGLE unconditionally in C sources 2022-10-18 17:04:10 +02:00
nptl Use atomic_exchange_release/acquire 2022-09-26 16:58:08 +01:00
or1k Use atomic_exchange_release/acquire 2022-09-26 16:58:08 +01:00
posix get_nscd_addresses: Fix subscript typos [BZ #29605] 2022-09-28 12:47:10 -04:00
powerpc Use PTR_MANGLE and PTR_DEMANGLE unconditionally in C sources 2022-10-18 17:04:10 +02:00
pthread Do not define static_assert or thread_local in headers for C2x 2022-09-07 18:39:28 +00:00
riscv stdlib/strfrom: Add copysign to fix NAN issue on riscv (BZ #29501) 2022-10-28 11:35:20 -03:00
s390 Use PTR_MANGLE and PTR_DEMANGLE unconditionally in C sources 2022-10-18 17:04:10 +02:00
sh Use PTR_MANGLE and PTR_DEMANGLE unconditionally in C sources 2022-10-18 17:04:10 +02:00
sparc Use PTR_MANGLE and PTR_DEMANGLE unconditionally in C sources 2022-10-18 17:04:10 +02:00
unix aarch64: Fix the extension header write in getcontext and swapcontext 2022-10-28 11:14:54 +01:00
wordsize-32 Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
wordsize-64 Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
x86 elf: Remove _dl_string_hwcap 2022-10-06 07:59:48 -03:00
x86_64 x86_64: Implement evex512 version of strchrnul, strchr and wcschr 2022-10-25 22:39:35 -07:00