From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id EDFFC3856973 for ; Thu, 27 Jul 2023 08:50:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EDFFC3856973 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-686e0213c0bso544727b3a.1 for ; Thu, 27 Jul 2023 01:50:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1690447854; x=1691052654; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=z0I7mYj/9EmPiAnvopEAWUPQU08AqWqd+rSkzOTSALY=; b=YzJPhSsouxLOd7487Z3KlpZlqAq5iZaiWmwWPWUvGQQtjgcw4qNCOrhU8zUtBNsFPv f9+95jVaoe3297US79pA1+W6fRSQUT6QZ/LAdwLKEovafyCZQv6axwbfK91nsxdqtmty ytVc+0zYyJ7tmWjBxiS0JFIoApqoUY1IN9+i3LW2bCsa/BQnaizdcnXEzAzN7FFBGmLa e3adaVr3Iq+g1b462jVMhvNc/ex6f5z9mL9XcKnB/UR0Ou8dYVGDcJkuFfSOjTBQMc2P tFw01Je55AuHHPOuEaVWuLT+2eCF8j3IZlBeuXhZMLp/b50AJBTAY7Tg7Q5ZmKHeFegr sHMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690447854; x=1691052654; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=z0I7mYj/9EmPiAnvopEAWUPQU08AqWqd+rSkzOTSALY=; b=mApVGyN1MPF3K8HCaJBHosYawdlLF3Z5ymU6MiHVWTDPaq/A/SrdoX754Rcg/kXmsv vFUY+fmdRqQ615gMHCYWA3XawCk4Ag59JRu2O7eZUcrZ/zcrwwKGRIUGR4XP9DJweodp mMMZenr7YiTaaaPw4Y9jANS7CQgUE1Va8Eimu44BVG8bADS4KuF2RdZXB0f7ZicWIxCS fYyPdixbqqdLiPZAMlxgjX4wLYNqY8WF5/ZMcRRBiHtrlSUj9Vj80Cj3nIdjuVmEq5GW 9FQiG6826PpS14R7WtxPhQRbecH502Tl/CRL8qqdYbI1lCX7oQBzasSPDha57f4Dzl20 3RLQ== X-Gm-Message-State: ABy/qLZReHEzNW8Oqd5xnzZMLdzenJgm6ntRB0x5QkVqGsX7ba8dgwGP fUlx++cX5EXqFWrknze8oC2TkE85Zp82IL9ZE1f5vAdcQ8egJ1PeX+h1J4iKydgxGshajCtjQJx KqMr5fx+rX6vOlf3/g6nkwcAdXGKTqvn511Otcm0I1Lj9heIrmyvdhcTHpxqTXjbiUQDHhzKjZY a7LB4qMhg= X-Google-Smtp-Source: APBJJlEQITAABRjYic0PtEPHpQmby6/Pyd5AwC7R5Sb3LZNXHn5myaLNC60USbrrLrIgk2amwpufEQ== X-Received: by 2002:a05:6a00:845:b0:66a:5466:25bd with SMTP id q5-20020a056a00084500b0066a546625bdmr5129486pfk.15.1690447854197; Thu, 27 Jul 2023 01:50:54 -0700 (PDT) Received: from sw04.internal.sifive.com ([64.62.193.194]) by smtp.gmail.com with ESMTPSA id 21-20020aa79255000000b00686bb3acfc2sm960625pfp.181.2023.07.27.01.50.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jul 2023 01:50:53 -0700 (PDT) From: Jesse Huang To: newlib@sourceware.org, kito.cheng@gmail.com Cc: Jesse Huang Subject: [PATCH] Fix rounding results in lrint() & llrint() when close to 0 Date: Thu, 27 Jul 2023 01:49:41 -0700 Message-Id: <20230727084940.2138765-1-jesse.huang@sifive.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_NUMSUBJECT,RCVD_IN_BARRACUDACENTRAL,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE 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: soft-fp should round floating pointer numbers according to the current rounding mode. However, in the current code of lrint() and llrint(), there are if statements before the actual rounding computation if(j0 < -1) return 0; Where j0 is the exponent of the floating point number. It means any number having a exponent less than -1 (i.e. interval (-0.5, 0.5)) will be rounded to 0 regardeless of the rounding mode. The bug already fixed in glibc in 2006 by moving the check afterwards the rounding computation, but still persists in newlib. This patch fixed it in a similar way to glibc Ref Commit in glibc: 6624dbc07b5a9fb316ed188ef01f65b8eea8b47c --- newlib/libm/common/s_llrint.c | 31 +++++++++++-------------------- newlib/libm/common/s_lrint.c | 31 +++++++++++-------------------- newlib/libm/common/sf_llrint.c | 6 ++---- newlib/libm/common/sf_lrint.c | 6 ++---- 4 files changed, 26 insertions(+), 48 deletions(-) diff --git a/newlib/libm/common/s_llrint.c b/newlib/libm/common/s_llrint.c index 72452dbe9..f2c48766e 100644 --- a/newlib/libm/common/s_llrint.c +++ b/newlib/libm/common/s_llrint.c @@ -66,26 +66,17 @@ long long int if(j0 < 20) { /* j0 in [-1023,19] */ - if(j0 < -1) - return 0; - else - { - /* j0 in [0,19] */ - /* shift amt in [0,19] */ - w = TWO52[sx] + x; - t = w - TWO52[sx]; - GET_HIGH_WORD(i0, t); - /* Detect the all-zeros representation of plus and - minus zero, which fails the calculation below. */ - if ((i0 & ~((__int32_t)1 << 31)) == 0) - return 0; - /* After round: j0 in [0,20] */ - j0 = ((i0 & 0x7ff00000) >> 20) - 1023; - i0 &= 0x000fffff; - i0 |= 0x00100000; - /* shift amt in [20,0] */ - result = i0 >> (20 - j0); - } + w = TWO52[sx] + x; + t = w - TWO52[sx]; + GET_HIGH_WORD(i0, t); + /* Detect the all-zeros representation of plus and + minus zero, which fails the calculation below. */ + if ((i0 & ~((__int32_t)1 << 31)) == 0) + return 0; + j0 = ((i0 & 0x7ff00000) >> 20) - 1023; + i0 &= 0x000fffff; + i0 |= 0x00100000; + result = (j0 < 0 ? 0 : i0 >> (20 - j0)); } else if (j0 < (int)(8 * sizeof (long long int)) - 1) { diff --git a/newlib/libm/common/s_lrint.c b/newlib/libm/common/s_lrint.c index b37f50fd4..b37b93aff 100644 --- a/newlib/libm/common/s_lrint.c +++ b/newlib/libm/common/s_lrint.c @@ -103,26 +103,17 @@ TWO52[2]={ if(j0 < 20) { /* j0 in [-1023,19] */ - if(j0 < -1) - return 0; - else - { - /* j0 in [0,19] */ - /* shift amt in [0,19] */ - w = TWO52[sx] + x; - t = w - TWO52[sx]; - GET_HIGH_WORD(i0, t); - /* Detect the all-zeros representation of plus and - minus zero, which fails the calculation below. */ - if ((i0 & ~(1L << 31)) == 0) - return 0; - /* After round: j0 in [0,20] */ - j0 = ((i0 & 0x7ff00000) >> 20) - 1023; - i0 &= 0x000fffff; - i0 |= 0x00100000; - /* shift amt in [20,0] */ - result = i0 >> (20 - j0); - } + w = TWO52[sx] + x; + t = w - TWO52[sx]; + GET_HIGH_WORD(i0, t); + /* Detect the all-zeros representation of plus and + minus zero, which fails the calculation below. */ + if ((i0 & ~(1L << 31)) == 0) + return 0; + j0 = ((i0 & 0x7ff00000) >> 20) - 1023; + i0 &= 0x000fffff; + i0 |= 0x00100000; + result = (j0 < 0 ? 0 : i0 >> (20 - j0)); } else if (j0 < (int)(8 * sizeof (long int)) - 1) { diff --git a/newlib/libm/common/sf_llrint.c b/newlib/libm/common/sf_llrint.c index 7558e89ac..905a5b21d 100644 --- a/newlib/libm/common/sf_llrint.c +++ b/newlib/libm/common/sf_llrint.c @@ -60,9 +60,7 @@ TWO23[2]={ if (j0 < (int)(sizeof (long long int) * 8) - 1) { - if (j0 < -1) - return 0; - else if (j0 >= 23) + if (j0 >= 23) result = (long long int) ((i0 & 0x7fffff) | 0x800000) << (j0 - 23); else { @@ -76,7 +74,7 @@ TWO23[2]={ j0 = ((i0 >> 23) & 0xff) - 0x7f; i0 &= 0x7fffff; i0 |= 0x800000; - result = i0 >> (23 - j0); + result = (j0 < 0 ? 0 : i0 >> (23 - j0)); } } else diff --git a/newlib/libm/common/sf_lrint.c b/newlib/libm/common/sf_lrint.c index 3c58c5d10..7fe47aefb 100644 --- a/newlib/libm/common/sf_lrint.c +++ b/newlib/libm/common/sf_lrint.c @@ -60,9 +60,7 @@ TWO23[2]={ if (j0 < (int)(sizeof (long int) * 8) - 1) { - if (j0 < -1) - return 0; - else if (j0 >= 23) + if (j0 >= 23) result = (long int) ((i0 & 0x7fffff) | 0x800000) << (j0 - 23); else { @@ -76,7 +74,7 @@ TWO23[2]={ j0 = ((i0 >> 23) & 0xff) - 0x7f; i0 &= 0x7fffff; i0 |= 0x800000; - result = i0 >> (23 - j0); + result = (j0 < 0 ? 0 : i0 >> (23 - j0)); } } else -- 2.31.1