From 0de7cf04f3064a543adc0ed73d16c49bfb2777fc Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Tue, 3 Aug 2010 18:21:20 +0000 Subject: [PATCH] 2010-08-03 Craig Howland * libm/common/s_lrint.c (lrint): Annotate shift operations with possible shift amount ranges, and use SAFE_RIGHT_SHIFT to avoid undefined behaviour. * libm/common/s_llrint.c (llrint): Likewise. * libm/common/s_lround.c (lround): Correct some comments on ranges. * libm/common/s_llround.c (llround): Likewise. --- newlib/ChangeLog | 9 +++++++++ newlib/libm/common/s_llrint.c | 20 ++++++++++++++++++-- newlib/libm/common/s_llround.c | 4 ++-- newlib/libm/common/s_lrint.c | 23 ++++++++++++++++++++++- newlib/libm/common/s_lround.c | 4 ++-- 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/newlib/ChangeLog b/newlib/ChangeLog index e52f306d3..294b2c98a 100644 --- a/newlib/ChangeLog +++ b/newlib/ChangeLog @@ -1,3 +1,12 @@ +2010-08-03 Craig Howland + + * libm/common/s_lrint.c (lrint): Annotate shift operations with + possible shift amount ranges, and use SAFE_RIGHT_SHIFT to avoid + undefined behaviour. + * libm/common/s_llrint.c (llrint): Likewise. + * libm/common/s_lround.c (lround): Correct some comments on ranges. + * libm/common/s_llround.c (llround): Likewise. + 2010-07-30 Anthony Green * libc/sys/rtems/machine/_types.h: Add moxie support. diff --git a/newlib/libm/common/s_llrint.c b/newlib/libm/common/s_llrint.c index 2239c4667..8b8a846ae 100644 --- a/newlib/libm/common/s_llrint.c +++ b/newlib/libm/common/s_llrint.c @@ -61,40 +61,56 @@ long long int /* Extract exponent field. */ j0 = ((i0 & 0x7ff00000) >> 20) - 1023; + /* j0 in [-1023,1024] */ 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 & ~(1 << 31)) == 0) + 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); } } else if (j0 < (int)(8 * sizeof (long long int)) - 1) { + /* 64bit return: j0 in [20,62] */ if (j0 >= 52) + /* 64bit return: j0 in [52,62] */ + /* 64bit return: left shift amt in [32,42] */ result = ((long long int) ((i0 & 0x000fffff) | 0x0010000) << (j0 - 20)) | + /* 64bit return: right shift amt in [0,10] */ (i1 << (j0 - 52)); else { + /* 64bit return: j0 in [20,51] */ w = TWO52[sx] + x; t = w - TWO52[sx]; EXTRACT_WORDS (i0, i1, t); j0 = ((i0 & 0x7ff00000) >> 20) - 1023; i0 &= 0x000fffff; i0 |= 0x00100000; - result = ((long long int) i0 << (j0 - 20)) | (i1 >> (52 - j0)); + /* After round: + * 64bit return: j0 in [20,52] */ + /* 64bit return: left shift amt in [0,32] */ + /* ***64bit return: right shift amt in [32,0] */ + result = ((long long int) i0 << (j0 - 20)) + | SAFE_RIGHT_SHIFT (i1, (52 - j0)); } } else diff --git a/newlib/libm/common/s_llround.c b/newlib/libm/common/s_llround.c index 3dcb5ff92..0bc35ac6e 100644 --- a/newlib/libm/common/s_llround.c +++ b/newlib/libm/common/s_llround.c @@ -31,10 +31,10 @@ llround(double x) msw &= 0x000fffff; msw |= 0x00100000; - /* exponent_less_1023 in [-1024,1023] */ + /* exponent_less_1023 in [-1023,1024] */ if (exponent_less_1023 < 20) { - /* exponent_less_1023 in [-1024,19] */ + /* exponent_less_1023 in [-1023,19] */ if (exponent_less_1023 < 0) { if (exponent_less_1023 < -1) diff --git a/newlib/libm/common/s_lrint.c b/newlib/libm/common/s_lrint.c index af94af709..cf35dcfbd 100644 --- a/newlib/libm/common/s_lrint.c +++ b/newlib/libm/common/s_lrint.c @@ -98,13 +98,17 @@ TWO52[2]={ /* Extract exponent field. */ j0 = ((i0 & 0x7ff00000) >> 20) - 1023; + /* j0 in [-1023,1024] */ 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); @@ -112,26 +116,43 @@ TWO52[2]={ 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); } } else if (j0 < (int)(8 * sizeof (long int)) - 1) { + /* 32bit return: j0 in [20,30] */ + /* 64bit return: j0 in [20,62] */ if (j0 >= 52) + /* 64bit return: j0 in [52,62] */ + /* 64bit return: left shift amt in [32,42] */ result = ((long int) ((i0 & 0x000fffff) | 0x0010000) << (j0 - 20)) | + /* 64bit return: right shift amt in [0,10] */ (i1 << (j0 - 52)); else { + /* 32bit return: j0 in [20,30] */ + /* 64bit return: j0 in [20,51] */ w = TWO52[sx] + x; t = w - TWO52[sx]; EXTRACT_WORDS (i0, i1, t); j0 = ((i0 & 0x7ff00000) >> 20) - 1023; i0 &= 0x000fffff; i0 |= 0x00100000; - result = ((long int) i0 << (j0 - 20)) | (i1 >> (52 - j0)); + /* After round: + * 32bit return: j0 in [20,31]; + * 64bit return: j0 in [20,52] */ + /* 32bit return: left shift amt in [0,11] */ + /* 64bit return: left shift amt in [0,32] */ + /* ***32bit return: right shift amt in [32,21] */ + /* ***64bit return: right shift amt in [32,0] */ + result = ((long int) i0 << (j0 - 20)) + | SAFE_RIGHT_SHIFT (i1, (52 - j0)); } } else diff --git a/newlib/libm/common/s_lround.c b/newlib/libm/common/s_lround.c index fecc62772..7c1eb3144 100644 --- a/newlib/libm/common/s_lround.c +++ b/newlib/libm/common/s_lround.c @@ -71,10 +71,10 @@ ANSI C, POSIX exponent_less_1023 = ((msw & 0x7ff00000) >> 20) - 1023; msw &= 0x000fffff; msw |= 0x00100000; - /* exponent_less_1023 in [-1024,1023] */ + /* exponent_less_1023 in [-1023,1024] */ if (exponent_less_1023 < 20) { - /* exponent_less_1023 in [-1024,19] */ + /* exponent_less_1023 in [-1023,19] */ if (exponent_less_1023 < 0) { if (exponent_less_1023 < -1)