From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by sourceware.org (Postfix) with ESMTPS id 8103E384D160 for ; Thu, 6 Oct 2022 05:27:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8103E384D160 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pj1-x1035.google.com with SMTP id x1-20020a17090ab00100b001fda21bbc90so3349515pjq.3 for ; Wed, 05 Oct 2022 22:27:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date; bh=DgQkVaT29HRK6+3jLzhtzyrdGwiJBRulp8np6BWq/TQ=; b=HcBQ5LuZvPC00Mu+kQ0BPE9cTvAMxN4FgaEM81rN2SZxSdHENDr0zwg8Fw6S+a42mq 1+MnpdBAmeu/dAXBZiQaowIw9Dhn6thEv7WXO+eYQMKmCwhl3msRnE7fpl/R9+y7RpoH TD1H4Frwi9SZrtt/jV66Veu6nE2RtMVelNa7jBgnfu78cTRiX+7+1/LuxEAxJThphXRW meK+WPj/KU+uvsGtZi+Io3M7hYxWG92qvXLWVlWZmeIijG8YQSr958w9ASvZ8yzbU73k gzGDPbhOfwUguyAeUCXlhZTTdhvuui6CYGgSoA0P6hMAuZ+7CkuvTkHhSNSnJVL3yucZ h2Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=DgQkVaT29HRK6+3jLzhtzyrdGwiJBRulp8np6BWq/TQ=; b=lKupyQ4pdZZ0rxMhUEXggLmBo9TLWdkEZ/26K4d87StfStfDx0a18I8DM3jjL6QSjD 2Szu7qqQSmBl9hxeGEiyTNd8t6xF4Eu0E7AuNZu2NqPZzh6IWsTuizVZfdqSjzD+A1hE /FFO7WYDfsaJBm9BAu8EPyGirbr34YfDYbAfnj3yirFR78XATttR7efmTTodm/V1II/H FeQ2PaPnMgyIMSrmXFQMrxRPRJCmSDfwsVIz25Z77vgIpzt8FK45QE0v6+wg7RMHr4G8 uRBPgNZtx+J1nkIYVN/eW+BCKynEKpAvaXHQDHz6zFAD24cqWv7mBGWojbnCoy9YvF9j xL7Q== X-Gm-Message-State: ACrzQf1adptcAv4byHiCsnLLskQGEUPvi85yvHBjuMFq825IAtaeaqyb oXWwjrX80j/QvxA/APVQviVbZDVFTBTQGUFe X-Google-Smtp-Source: AMsMyM6vTc+eYoW7jnSrtlJfVS+DOtzBRitApTo5IWjQQ7ALzgcjKVUYITuu6xUk8O8sKjI+fNSaNw== X-Received: by 2002:a17:903:11c7:b0:178:af17:e93e with SMTP id q7-20020a17090311c700b00178af17e93emr2960885plh.78.1665034026351; Wed, 05 Oct 2022 22:27:06 -0700 (PDT) Received: from fanta-arch.tsinghua.edu.cn (ec2-18-162-45-232.ap-east-1.compute.amazonaws.com. [18.162.45.232]) by smtp.gmail.com with ESMTPSA id j12-20020a170903024c00b0017a04542a45sm11513568plh.159.2022.10.05.22.27.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 22:27:05 -0700 (PDT) From: Letu Ren To: libc-alpha@sourceware.org Cc: fweimer@redhat.com, joseph@codesourcery.com, Letu Ren Subject: [PATCH v2] stdlib/strfrom: Add copysign to fix NAN issue on riscv (BZ #29501) Date: Thu, 6 Oct 2022 13:26:46 +0800 Message-Id: <20221006052646.8277-1-fantasquex@gmail.com> X-Mailer: git-send-email 2.38.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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. Signed-off-by: Letu Ren --- I'm not sure about copyright of the new file I created and the name of the new function. Please give me some advice. Thanks. stdlib/strfrom-skeleton.c | 3 ++- .../generic/fix-float-double-convert-nan.h | 15 +++++++++++++ .../riscv/rvd/fix-float-double-convert-nan.h | 22 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 sysdeps/generic/fix-float-double-convert-nan.h create mode 100644 sysdeps/riscv/rvd/fix-float-double-convert-nan.h diff --git a/stdlib/strfrom-skeleton.c b/stdlib/strfrom-skeleton.c index 1fba04bf6a..a14df8d424 100644 --- a/stdlib/strfrom-skeleton.c +++ b/stdlib/strfrom-skeleton.c @@ -27,6 +27,7 @@ #include #include #include +#include #define UCHAR_T char #define L_(Str) Str @@ -61,7 +62,7 @@ STRFROM (char *dest, size_t size, const char *format, FLOAT f) because __printf_fp and __printf_fphex only accept double and long double as the floating-point argument. */ if (__builtin_types_compatible_p (FLOAT, float)) - fpnum.flt = f; + fpnum.flt = keep_sign_conversion(f); else fpnum.value = f; diff --git a/sysdeps/generic/fix-float-double-convert-nan.h b/sysdeps/generic/fix-float-double-convert-nan.h new file mode 100644 index 0000000000..0949dda102 --- /dev/null +++ b/sysdeps/generic/fix-float-double-convert-nan.h @@ -0,0 +1,15 @@ +/* Fix for conversion of float NAN to double. Generic version. */ + +#ifndef FIX_FLOAT_DOUBLE_CONVERT_NAN_H +#define FIX_FLOAT_DOUBLE_CONVERT_NAN_H + +/* This function aims to work around conversions of float -NAN + to double returning NAN instead of the correct -NAN in some + architectures. */ +static inline double __attribute__ ((always_inline)) +keep_sign_conversion (float flt) +{ + return flt; +} + +#endif diff --git a/sysdeps/riscv/rvd/fix-float-double-convert-nan.h b/sysdeps/riscv/rvd/fix-float-double-convert-nan.h new file mode 100644 index 0000000000..b393e7435b --- /dev/null +++ b/sysdeps/riscv/rvd/fix-float-double-convert-nan.h @@ -0,0 +1,22 @@ +/* Fix for conversion of float NAN to double. RISC-V version. */ + +#ifndef FIX_FLOAT_DOUBLE_CONVERT_NAN_H +#define FIX_FLOAT_DOUBLE_CONVERT_NAN_H + +#include + +/* RISC-V rvd instructions do not preserve the signbit of NAN + when converting from float to double. */ +static inline double +keep_sign_conversion (float flt) +{ + if (isnan (flt)) + { + float x = copysignf (1.f, flt); + return copysign ((double) flt, (double) x); + } + else + return flt; +} + +#endif -- 2.38.0