1 | /* __gmp_scale2 -- Scale a double by exponent manipulation. |
---|

2 | |
---|

3 | Copyright 1996, 1997, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
---|

4 | |
---|

5 | This file is part of the GNU MP Library. |
---|

6 | |
---|

7 | The GNU MP Library is free software; you can redistribute it and/or modify |
---|

8 | it under the terms of the GNU Lesser General Public License as published by |
---|

9 | the Free Software Foundation; either version 2.1 of the License, or (at your |
---|

10 | option) any later version. |
---|

11 | |
---|

12 | The GNU MP Library is distributed in the hope that it will be useful, but |
---|

13 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|

14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|

15 | License for more details. |
---|

16 | |
---|

17 | You should have received a copy of the GNU Lesser General Public License |
---|

18 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|

19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|

20 | MA 02111-1307, USA. */ |
---|

21 | |
---|

22 | #include "gmp.h" |
---|

23 | #include "gmp-impl.h" |
---|

24 | |
---|

25 | #ifdef XDEBUG |
---|

26 | #undef _GMP_IEEE_FLOATS |
---|

27 | #endif |
---|

28 | |
---|

29 | #ifndef _GMP_IEEE_FLOATS |
---|

30 | #define _GMP_IEEE_FLOATS 0 |
---|

31 | #endif |
---|

32 | |
---|

33 | double |
---|

34 | __gmp_scale2 (double d, int exp) |
---|

35 | { |
---|

36 | #if _GMP_IEEE_FLOATS |
---|

37 | { |
---|

38 | #if defined (__alpha) && __GNUC__ == 2 && __GNUC_MINOR__ == 8 |
---|

39 | /* Work around alpha-specific bug in GCC 2.8.x. */ |
---|

40 | volatile |
---|

41 | #endif |
---|

42 | union ieee_double_extract x; |
---|

43 | x.d = d; |
---|

44 | exp += x.s.exp; |
---|

45 | x.s.exp = exp; |
---|

46 | if (exp >= 2047) |
---|

47 | { |
---|

48 | /* Return +-infinity */ |
---|

49 | x.s.exp = 2047; |
---|

50 | x.s.manl = x.s.manh = 0; |
---|

51 | } |
---|

52 | else if (exp < 1) |
---|

53 | { |
---|

54 | x.s.exp = 1; /* smallest exponent (biased) */ |
---|

55 | /* Divide result by 2 until we have scaled it to the right IEEE |
---|

56 | denormalized number, but stop if it becomes zero. */ |
---|

57 | while (exp < 1 && x.d != 0) |
---|

58 | { |
---|

59 | x.d *= 0.5; |
---|

60 | exp++; |
---|

61 | } |
---|

62 | } |
---|

63 | return x.d; |
---|

64 | } |
---|

65 | #else |
---|

66 | { |
---|

67 | double factor, r; |
---|

68 | |
---|

69 | factor = 2.0; |
---|

70 | if (exp < 0) |
---|

71 | { |
---|

72 | factor = 0.5; |
---|

73 | exp = -exp; |
---|

74 | } |
---|

75 | r = d; |
---|

76 | if (exp != 0) |
---|

77 | { |
---|

78 | if ((exp & 1) != 0) |
---|

79 | r *= factor; |
---|

80 | exp >>= 1; |
---|

81 | while (exp != 0) |
---|

82 | { |
---|

83 | factor *= factor; |
---|

84 | if ((exp & 1) != 0) |
---|

85 | r *= factor; |
---|

86 | exp >>= 1; |
---|

87 | } |
---|

88 | } |
---|

89 | return r; |
---|

90 | } |
---|

91 | #endif |
---|

92 | } |
---|