aboutsummaryrefslogtreecommitdiff
path: root/newlib/libm/common/s_lrint.c
diff options
context:
space:
mode:
authorJesse Huang via Newlib <newlib@sourceware.org>2023-07-27 01:49:41 -0700
committerCorinna Vinschen <corinna@vinschen.de>2023-07-27 11:14:12 +0200
commit9e329b544ac04b389e12984362e06b92e1506399 (patch)
treea2f043282ac2058f773ea7c3f5861cf52b632994 /newlib/libm/common/s_lrint.c
parent4fbcc8c5fe7d655de4bf04f2f123e77506011030 (diff)
downloadnewlib-9e329b544ac04b389e12984362e06b92e1506399.zip
newlib-9e329b544ac04b389e12984362e06b92e1506399.tar.gz
newlib-9e329b544ac04b389e12984362e06b92e1506399.tar.bz2
Fix rounding results in lrint() & llrint() when close to 0
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
Diffstat (limited to 'newlib/libm/common/s_lrint.c')
-rw-r--r--newlib/libm/common/s_lrint.c31
1 files changed, 11 insertions, 20 deletions
diff --git a/newlib/libm/common/s_lrint.c b/newlib/libm/common/s_lrint.c
index b37f50f..b37b93a 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)
{