diff options
author | Jeff Johnston <jjohnstn@redhat.com> | 2010-08-03 22:21:20 +0400 |
---|---|---|
committer | Jeff Johnston <jjohnstn@redhat.com> | 2010-08-03 22:21:20 +0400 |
commit | 0de7cf04f3064a543adc0ed73d16c49bfb2777fc (patch) | |
tree | 20425317096c120eeaa64a360838a05c8feba8cf /newlib/libm/common | |
parent | 0b6fbd396ca2f5448acb925a0b7f718676980d74 (diff) |
2010-08-03 Craig Howland <howland@LGSInnovations.com>
* 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.
Diffstat (limited to 'newlib/libm/common')
-rw-r--r-- | newlib/libm/common/s_llrint.c | 20 | ||||
-rw-r--r-- | newlib/libm/common/s_llround.c | 4 | ||||
-rw-r--r-- | newlib/libm/common/s_lrint.c | 23 | ||||
-rw-r--r-- | newlib/libm/common/s_lround.c | 4 |
4 files changed, 44 insertions, 7 deletions
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) |