4
0
mirror of git://sourceware.org/git/newlib-cygwin.git synced 2025-02-28 12:05:47 +08:00

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.
This commit is contained in:
Jeff Johnston 2010-08-03 18:21:20 +00:00
parent 0b6fbd396c
commit 0de7cf04f3
5 changed files with 53 additions and 7 deletions

View File

@ -1,3 +1,12 @@
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.
2010-07-30 Anthony Green <green@moxielogic.com> 2010-07-30 Anthony Green <green@moxielogic.com>
* libc/sys/rtems/machine/_types.h: Add moxie support. * libc/sys/rtems/machine/_types.h: Add moxie support.

View File

@ -61,40 +61,56 @@ long long int
/* Extract exponent field. */ /* Extract exponent field. */
j0 = ((i0 & 0x7ff00000) >> 20) - 1023; j0 = ((i0 & 0x7ff00000) >> 20) - 1023;
/* j0 in [-1023,1024] */
if(j0 < 20) if(j0 < 20)
{ {
/* j0 in [-1023,19] */
if(j0 < -1) if(j0 < -1)
return 0; return 0;
else else
{ {
/* j0 in [0,19] */
/* shift amt in [0,19] */
w = TWO52[sx] + x; w = TWO52[sx] + x;
t = w - TWO52[sx]; t = w - TWO52[sx];
GET_HIGH_WORD(i0, t); GET_HIGH_WORD(i0, t);
/* Detect the all-zeros representation of plus and /* Detect the all-zeros representation of plus and
minus zero, which fails the calculation below. */ minus zero, which fails the calculation below. */
if ((i0 & ~(1 << 31)) == 0) if ((i0 & ~((__int32_t)1 << 31)) == 0)
return 0; return 0;
/* After round: j0 in [0,20] */
j0 = ((i0 & 0x7ff00000) >> 20) - 1023; j0 = ((i0 & 0x7ff00000) >> 20) - 1023;
i0 &= 0x000fffff; i0 &= 0x000fffff;
i0 |= 0x00100000; i0 |= 0x00100000;
/* shift amt in [20,0] */
result = i0 >> (20 - j0); result = i0 >> (20 - j0);
} }
} }
else if (j0 < (int)(8 * sizeof (long long int)) - 1) else if (j0 < (int)(8 * sizeof (long long int)) - 1)
{ {
/* 64bit return: j0 in [20,62] */
if (j0 >= 52) 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)) | result = ((long long int) ((i0 & 0x000fffff) | 0x0010000) << (j0 - 20)) |
/* 64bit return: right shift amt in [0,10] */
(i1 << (j0 - 52)); (i1 << (j0 - 52));
else else
{ {
/* 64bit return: j0 in [20,51] */
w = TWO52[sx] + x; w = TWO52[sx] + x;
t = w - TWO52[sx]; t = w - TWO52[sx];
EXTRACT_WORDS (i0, i1, t); EXTRACT_WORDS (i0, i1, t);
j0 = ((i0 & 0x7ff00000) >> 20) - 1023; j0 = ((i0 & 0x7ff00000) >> 20) - 1023;
i0 &= 0x000fffff; i0 &= 0x000fffff;
i0 |= 0x00100000; 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 else

View File

@ -31,10 +31,10 @@ llround(double x)
msw &= 0x000fffff; msw &= 0x000fffff;
msw |= 0x00100000; msw |= 0x00100000;
/* exponent_less_1023 in [-1024,1023] */ /* exponent_less_1023 in [-1023,1024] */
if (exponent_less_1023 < 20) 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 < 0)
{ {
if (exponent_less_1023 < -1) if (exponent_less_1023 < -1)

View File

@ -98,13 +98,17 @@ TWO52[2]={
/* Extract exponent field. */ /* Extract exponent field. */
j0 = ((i0 & 0x7ff00000) >> 20) - 1023; j0 = ((i0 & 0x7ff00000) >> 20) - 1023;
/* j0 in [-1023,1024] */
if(j0 < 20) if(j0 < 20)
{ {
/* j0 in [-1023,19] */
if(j0 < -1) if(j0 < -1)
return 0; return 0;
else else
{ {
/* j0 in [0,19] */
/* shift amt in [0,19] */
w = TWO52[sx] + x; w = TWO52[sx] + x;
t = w - TWO52[sx]; t = w - TWO52[sx];
GET_HIGH_WORD(i0, t); GET_HIGH_WORD(i0, t);
@ -112,26 +116,43 @@ TWO52[2]={
minus zero, which fails the calculation below. */ minus zero, which fails the calculation below. */
if ((i0 & ~(1L << 31)) == 0) if ((i0 & ~(1L << 31)) == 0)
return 0; return 0;
/* After round: j0 in [0,20] */
j0 = ((i0 & 0x7ff00000) >> 20) - 1023; j0 = ((i0 & 0x7ff00000) >> 20) - 1023;
i0 &= 0x000fffff; i0 &= 0x000fffff;
i0 |= 0x00100000; i0 |= 0x00100000;
/* shift amt in [20,0] */
result = i0 >> (20 - j0); result = i0 >> (20 - j0);
} }
} }
else if (j0 < (int)(8 * sizeof (long int)) - 1) else if (j0 < (int)(8 * sizeof (long int)) - 1)
{ {
/* 32bit return: j0 in [20,30] */
/* 64bit return: j0 in [20,62] */
if (j0 >= 52) 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)) | result = ((long int) ((i0 & 0x000fffff) | 0x0010000) << (j0 - 20)) |
/* 64bit return: right shift amt in [0,10] */
(i1 << (j0 - 52)); (i1 << (j0 - 52));
else else
{ {
/* 32bit return: j0 in [20,30] */
/* 64bit return: j0 in [20,51] */
w = TWO52[sx] + x; w = TWO52[sx] + x;
t = w - TWO52[sx]; t = w - TWO52[sx];
EXTRACT_WORDS (i0, i1, t); EXTRACT_WORDS (i0, i1, t);
j0 = ((i0 & 0x7ff00000) >> 20) - 1023; j0 = ((i0 & 0x7ff00000) >> 20) - 1023;
i0 &= 0x000fffff; i0 &= 0x000fffff;
i0 |= 0x00100000; 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 else

View File

@ -71,10 +71,10 @@ ANSI C, POSIX
exponent_less_1023 = ((msw & 0x7ff00000) >> 20) - 1023; exponent_less_1023 = ((msw & 0x7ff00000) >> 20) - 1023;
msw &= 0x000fffff; msw &= 0x000fffff;
msw |= 0x00100000; msw |= 0x00100000;
/* exponent_less_1023 in [-1024,1023] */ /* exponent_less_1023 in [-1023,1024] */
if (exponent_less_1023 < 20) 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 < 0)
{ {
if (exponent_less_1023 < -1) if (exponent_less_1023 < -1)