From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x32d.google.com (mail-ot1-x32d.google.com [IPv6:2607:f8b0:4864:20::32d]) by sourceware.org (Postfix) with ESMTPS id 7B70A3856974 for ; Fri, 21 Oct 2022 17:10:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7B70A3856974 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-ot1-x32d.google.com with SMTP id r13-20020a056830418d00b0065601df69c0so2185231otu.7 for ; Fri, 21 Oct 2022 10:10:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=D0EbIoI/sctIcWaKI/LKNEIZC/sVaV75uizK76cqSVY=; b=Owx+iLvBuhuxiDDIOjfJtpBJcI6zR5ZP04mfKrEhnfGEdoMmdh8qdZCpCFVrNtoGX3 zvhmk78DRI5fUYwiHA5u2I1HbjRzTQRcxxzfwcbamQOQIOixwGFYVkwHW9TI7Rfsvfcl Fy+f1MkJOP7vroS6W29LJz9wId0gp/BLTbeSTj5SHI50FmGPmP1WUmOSb1MLTvYj8trV WCCLJwjChubRcdjia9xt6hK4C/rwymRQAibkK5xQQ5ziMQpwxZ+S2bbI0BankVU6cza0 VCmd2QscKJ/kj3Zer7Pkf8BgaoNX9iZ3FJBD5GNV+i6Mwv/vY9dJw/xRS8j0/Th+ppjG Smbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=D0EbIoI/sctIcWaKI/LKNEIZC/sVaV75uizK76cqSVY=; b=xTbx7jtDd07IPsKhXEx33s11XwYJ6oYR+DkVMUz7BFkKl5BfEm+9R6LqVgAZG0BdPK 2/4z9kPC/zCdz2l1hB0LJa34y5trpLMRuKedKj+i5jiMTnNjQpvUp3PmI2hr0UBxvAn7 ZS0hEskE89Gmp4jaB2n0ch/A5Nd8NJK4g235Uov9xjtaonb52g/Wrj3SzoFyIYu9wUZx 1NhNHXQe9wauJjcqmX8X+c83vT+FHVgO+Hz5AyXt72IbaSF4y2Vb4tdnAxwR5jfWyFI/ h1EdmU3Bf6jIRNaANj2cdjx4wVCmnMFByaOHjz7tDyhKjGhzBPzA+WOCKfvuC+EZDZpi i6nQ== X-Gm-Message-State: ACrzQf3VWPJ1N0czvtREw3HfocPhXKpXZdBV44/dJrH7Aq4/zMOve0So gy1Wsnd/7JKt+VmCM94PKESC6S4p3CJ2bnbj X-Google-Smtp-Source: AMsMyM7Jm0NoOf1QruRWYIzjor4qkrQaDFWVz+kzcLH7ufv+9atzLjxDaO9IGQ0wqa3XU3rG7CX+iA== X-Received: by 2002:a05:6830:1cc2:b0:661:dc05:41d7 with SMTP id p2-20020a0568301cc200b00661dc0541d7mr9391206otg.294.1666372230681; Fri, 21 Oct 2022 10:10:30 -0700 (PDT) Received: from ?IPV6:2804:1b3:a7c3:7d19:5909:4ed0:fbc6:5cfd? ([2804:1b3:a7c3:7d19:5909:4ed0:fbc6:5cfd]) by smtp.gmail.com with ESMTPSA id t14-20020a056870f20e00b00132756f2c94sm3426525oao.12.2022.10.21.10.10.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 21 Oct 2022 10:10:30 -0700 (PDT) Message-ID: <9f0725fe-98b0-0b9e-1bd8-e51275ad5561@linaro.org> Date: Fri, 21 Oct 2022 14:10:27 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.3.3 Subject: Re: [PATCH v6] stdlib/strfrom: Add copysign to fix NAN issue on riscv (BZ #29501) Content-Language: en-US To: Letu Ren , libc-alpha@sourceware.org Cc: fweimer@redhat.com, joseph@codesourcery.com, schwab@suse.de References: <20221021145450.5803-1-fantasquex@gmail.com> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: <20221021145450.5803-1-fantasquex@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,NICE_REPLY_A,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: On 21/10/22 11:54, Letu Ren wrote: > 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 LGTM, thanks. Really minor nits below that I did not catch before, there is no need to send a v7 (just fix it before install). Reviewed-by: Adhemerval Zanella > --- > stdlib/strfrom-skeleton.c | 3 +- > .../generic/fix-float-double-convert-nan.h | 31 ++++++++++++++++ > .../riscv/rvd/fix-float-double-convert-nan.h | 37 +++++++++++++++++++ > 3 files changed, 70 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..36e9adcad5 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..a42c3d7363 > --- /dev/null > +++ b/sysdeps/generic/fix-float-double-convert-nan.h > @@ -0,0 +1,31 @@ > +/* Fix for conversion of float NAN to double. Generic version. Double space after period. > + Copyright (C) 2022 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library. If not, see > + . */ > + > +#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. */ Double space before close comment. > +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..0d2d871a70 > --- /dev/null > +++ b/sysdeps/riscv/rvd/fix-float-double-convert-nan.h > @@ -0,0 +1,37 @@ > +/* Fix for conversion of float NAN to double. RISC-V version.. Double space after period and remove last final period. > + Copyright (C) 2022 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library. If not, see > + . */ > + > +#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 (__glibc_unlikely (isnan (flt))) > + { > + float x = copysignf (1.f, flt); > + return copysign ((double) flt, (double) x); > + } > + return flt; > +} > + > +#endif