poly_2xm1.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*---------------------------------------------------------------------------+
  2. | poly_2xm1.c |
  3. | |
  4. | Function to compute 2^x-1 by a polynomial approximation. |
  5. | |
  6. | Copyright (C) 1992,1993,1994,1997 |
  7. | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
  8. | E-mail billm@suburbia.net |
  9. | |
  10. | |
  11. +---------------------------------------------------------------------------*/
  12. #include "exception.h"
  13. #include "reg_constant.h"
  14. #include "fpu_emu.h"
  15. #include "fpu_system.h"
  16. #include "control_w.h"
  17. #include "poly.h"
  18. #define HIPOWER 11
  19. static const unsigned long long lterms[HIPOWER] =
  20. {
  21. 0x0000000000000000LL, /* This term done separately as 12 bytes */
  22. 0xf5fdeffc162c7543LL,
  23. 0x1c6b08d704a0bfa6LL,
  24. 0x0276556df749cc21LL,
  25. 0x002bb0ffcf14f6b8LL,
  26. 0x0002861225ef751cLL,
  27. 0x00001ffcbfcd5422LL,
  28. 0x00000162c005d5f1LL,
  29. 0x0000000da96ccb1bLL,
  30. 0x0000000078d1b897LL,
  31. 0x000000000422b029LL
  32. };
  33. static const Xsig hiterm = MK_XSIG(0xb17217f7, 0xd1cf79ab, 0xc8a39194);
  34. /* Four slices: 0.0 : 0.25 : 0.50 : 0.75 : 1.0,
  35. These numbers are 2^(1/4), 2^(1/2), and 2^(3/4)
  36. */
  37. static const Xsig shiftterm0 = MK_XSIG(0, 0, 0);
  38. static const Xsig shiftterm1 = MK_XSIG(0x9837f051, 0x8db8a96f, 0x46ad2318);
  39. static const Xsig shiftterm2 = MK_XSIG(0xb504f333, 0xf9de6484, 0x597d89b3);
  40. static const Xsig shiftterm3 = MK_XSIG(0xd744fcca, 0xd69d6af4, 0x39a68bb9);
  41. static const Xsig *shiftterm[] = { &shiftterm0, &shiftterm1,
  42. &shiftterm2, &shiftterm3 };
  43. /*--- poly_2xm1() -----------------------------------------------------------+
  44. | Requires st(0) which is TAG_Valid and < 1. |
  45. +---------------------------------------------------------------------------*/
  46. int poly_2xm1(u_char sign, FPU_REG *arg, FPU_REG *result)
  47. {
  48. long int exponent, shift;
  49. unsigned long long Xll;
  50. Xsig accumulator, Denom, argSignif;
  51. u_char tag;
  52. exponent = exponent16(arg);
  53. #ifdef PARANOID
  54. if ( exponent >= 0 ) /* Don't want a |number| >= 1.0 */
  55. {
  56. /* Number negative, too large, or not Valid. */
  57. EXCEPTION(EX_INTERNAL|0x127);
  58. return 1;
  59. }
  60. #endif /* PARANOID */
  61. argSignif.lsw = 0;
  62. XSIG_LL(argSignif) = Xll = significand(arg);
  63. if ( exponent == -1 )
  64. {
  65. shift = (argSignif.msw & 0x40000000) ? 3 : 2;
  66. /* subtract 0.5 or 0.75 */
  67. exponent -= 2;
  68. XSIG_LL(argSignif) <<= 2;
  69. Xll <<= 2;
  70. }
  71. else if ( exponent == -2 )
  72. {
  73. shift = 1;
  74. /* subtract 0.25 */
  75. exponent--;
  76. XSIG_LL(argSignif) <<= 1;
  77. Xll <<= 1;
  78. }
  79. else
  80. shift = 0;
  81. if ( exponent < -2 )
  82. {
  83. /* Shift the argument right by the required places. */
  84. if ( FPU_shrx(&Xll, -2-exponent) >= 0x80000000U )
  85. Xll++; /* round up */
  86. }
  87. accumulator.lsw = accumulator.midw = accumulator.msw = 0;
  88. polynomial_Xsig(&accumulator, &Xll, lterms, HIPOWER-1);
  89. mul_Xsig_Xsig(&accumulator, &argSignif);
  90. shr_Xsig(&accumulator, 3);
  91. mul_Xsig_Xsig(&argSignif, &hiterm); /* The leading term */
  92. add_two_Xsig(&accumulator, &argSignif, &exponent);
  93. if ( shift )
  94. {
  95. /* The argument is large, use the identity:
  96. f(x+a) = f(a) * (f(x) + 1) - 1;
  97. */
  98. shr_Xsig(&accumulator, - exponent);
  99. accumulator.msw |= 0x80000000; /* add 1.0 */
  100. mul_Xsig_Xsig(&accumulator, shiftterm[shift]);
  101. accumulator.msw &= 0x3fffffff; /* subtract 1.0 */
  102. exponent = 1;
  103. }
  104. if ( sign != SIGN_POS )
  105. {
  106. /* The argument is negative, use the identity:
  107. f(-x) = -f(x) / (1 + f(x))
  108. */
  109. Denom.lsw = accumulator.lsw;
  110. XSIG_LL(Denom) = XSIG_LL(accumulator);
  111. if ( exponent < 0 )
  112. shr_Xsig(&Denom, - exponent);
  113. else if ( exponent > 0 )
  114. {
  115. /* exponent must be 1 here */
  116. XSIG_LL(Denom) <<= 1;
  117. if ( Denom.lsw & 0x80000000 )
  118. XSIG_LL(Denom) |= 1;
  119. (Denom.lsw) <<= 1;
  120. }
  121. Denom.msw |= 0x80000000; /* add 1.0 */
  122. div_Xsig(&accumulator, &Denom, &accumulator);
  123. }
  124. /* Convert to 64 bit signed-compatible */
  125. exponent += round_Xsig(&accumulator);
  126. result = &st(0);
  127. significand(result) = XSIG_LL(accumulator);
  128. setexponent16(result, exponent);
  129. tag = FPU_round(result, 1, 0, FULL_PRECISION, sign);
  130. setsign(result, sign);
  131. FPU_settag0(tag);
  132. return 0;
  133. }