glibc/sysdeps/riscv
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
..
bits Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
nofpu riscv: Update nofpu libm test ulps 2022-07-27 10:50:10 -03:00
nptl Use atomic_exchange_release/acquire 2022-09-26 16:58:08 +01:00
rv32 riscv: Remove RV32 floating point functions 2022-09-21 14:37:43 -04:00
rv64 riscv: Remove RV32 floating point functions 2022-09-21 14:37:43 -04:00
rvd stdlib/strfrom: Add copysign to fix NAN issue on riscv (BZ #29501) 2022-10-28 11:35:20 -03:00
rvf riscv: Remove RV32 floating point functions 2022-09-21 14:37:43 -04:00
sys Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
__longjmp.S Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
bsd-_setjmp.c RISC-V: ABI Implementation 2018-01-29 10:25:29 -08:00
bsd-setjmp.c RISC-V: ABI Implementation 2018-01-29 10:25:29 -08:00
configure elf: Replace PI_STATIC_AND_HIDDEN with opposite HIDDEN_VAR_NEEDS_DYNAMIC_RELOC 2022-04-26 09:26:22 -07:00
configure.ac elf: Replace PI_STATIC_AND_HIDDEN with opposite HIDDEN_VAR_NEEDS_DYNAMIC_RELOC 2022-04-26 09:26:22 -07:00
dl-irel.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
dl-machine.h riscv: Use memcpy to handle unaligned access when fixing R_RISCV_RELATIVE 2022-06-30 08:04:52 -07:00
dl-relocate-ld.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
dl-tls.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
dl-trampoline.S Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
e_sqrtl.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
fpu_control.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
gccframe.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
Implies Introduce <elf-initfini.h> and ELF_INITFINI for all architectures 2020-02-18 15:12:25 +01:00
jmpbuf-offsets.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
jmpbuf-unwind.h Use PTR_MANGLE and PTR_DEMANGLE unconditionally in C sources 2022-10-18 17:04:10 +02:00
ldsodefs.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
libc-tls.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
linkmap.h RISC-V: ABI Implementation 2018-01-29 10:25:29 -08:00
machine-gmon.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
Makefile riscv: Build with -mno-relax if linker does not support R_RISCV_ALIGN 2021-11-03 09:25:06 -03:00
math-tests-snan-payload.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
math-tests-trap.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
preconfigure RISC-V: Use an autoconf template to produce `preconfigure' 2022-05-13 17:07:23 +01:00
preconfigure.ac RISC-V: Use an autoconf template to produce `preconfigure' 2022-05-13 17:07:23 +01:00
setjmp.S Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
sfp-machine.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
sotruss-lib.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
stackinfo.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
start.S Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
tininess.h RISC-V: Generic <math.h> and soft-fp Routines 2018-01-29 10:26:35 -08:00
tst-audit.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00