poly_2xm1.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. 0x0000000000000000LL, /* This term done separately as 12 bytes */
  21. 0xf5fdeffc162c7543LL,
  22. 0x1c6b08d704a0bfa6LL,
  23. 0x0276556df749cc21LL,
  24. 0x002bb0ffcf14f6b8LL,
  25. 0x0002861225ef751cLL,
  26. 0x00001ffcbfcd5422LL,
  27. 0x00000162c005d5f1LL,
  28. 0x0000000da96ccb1bLL,
  29. 0x0000000078d1b897LL,
  30. 0x000000000422b029LL
  31. };
  32. static const Xsig hiterm = MK_XSIG(0xb17217f7, 0xd1cf79ab, 0xc8a39194);
  33. /* Four slices: 0.0 : 0.25 : 0.50 : 0.75 : 1.0,
  34. These numbers are 2^(1/4), 2^(1/2), and 2^(3/4)
  35. */
  36. static const Xsig shiftterm0 = MK_XSIG(0, 0, 0);
  37. static const Xsig shiftterm1 = MK_XSIG(0x9837f051, 0x8db8a96f, 0x46ad2318);
  38. static const Xsig shiftterm2 = MK_XSIG(0xb504f333, 0xf9de6484, 0x597d89b3);
  39. static const Xsig shiftterm3 = MK_XSIG(0xd744fcca, 0xd69d6af4, 0x39a68bb9);
  40. static const Xsig *shiftterm[] = { &shiftterm0, &shiftterm1,
  41. &shiftterm2, &shiftterm3
  42. };
  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. /* Number negative, too large, or not Valid. */
  56. EXCEPTION(EX_INTERNAL | 0x127);
  57. return 1;
  58. }
  59. #endif /* PARANOID */
  60. argSignif.lsw = 0;
  61. XSIG_LL(argSignif) = Xll = significand(arg);
  62. if (exponent == -1) {
  63. shift = (argSignif.msw & 0x40000000) ? 3 : 2;
  64. /* subtract 0.5 or 0.75 */
  65. exponent -= 2;
  66. XSIG_LL(argSignif) <<= 2;
  67. Xll <<= 2;
  68. } else if (exponent == -2) {
  69. shift = 1;
  70. /* subtract 0.25 */
  71. exponent--;
  72. XSIG_LL(argSignif) <<= 1;
  73. Xll <<= 1;
  74. } else
  75. shift = 0;
  76. if (exponent < -2) {
  77. /* Shift the argument right by the required places. */
  78. if (FPU_shrx(&Xll, -2 - exponent) >= 0x80000000U)
  79. Xll++; /* round up */
  80. }
  81. accumulator.lsw = accumulator.midw = accumulator.msw = 0;
  82. polynomial_Xsig(&accumulator, &Xll, lterms, HIPOWER - 1);
  83. mul_Xsig_Xsig(&accumulator, &argSignif);
  84. shr_Xsig(&accumulator, 3);
  85. mul_Xsig_Xsig(&argSignif, &hiterm); /* The leading term */
  86. add_two_Xsig(&accumulator, &argSignif, &exponent);
  87. if (shift) {
  88. /* The argument is large, use the identity:
  89. f(x+a) = f(a) * (f(x) + 1) - 1;
  90. */
  91. shr_Xsig(&accumulator, -exponent);
  92. accumulator.msw |= 0x80000000; /* add 1.0 */
  93. mul_Xsig_Xsig(&accumulator, shiftterm[shift]);
  94. accumulator.msw &= 0x3fffffff; /* subtract 1.0 */
  95. exponent = 1;
  96. }
  97. if (sign != SIGN_POS) {
  98. /* The argument is negative, use the identity:
  99. f(-x) = -f(x) / (1 + f(x))
  100. */
  101. Denom.lsw = accumulator.lsw;
  102. XSIG_LL(Denom) = XSIG_LL(accumulator);
  103. if (exponent < 0)
  104. shr_Xsig(&Denom, -exponent);
  105. else if (exponent > 0) {
  106. /* exponent must be 1 here */
  107. XSIG_LL(Denom) <<= 1;
  108. if (Denom.lsw & 0x80000000)
  109. XSIG_LL(Denom) |= 1;
  110. (Denom.lsw) <<= 1;
  111. }
  112. Denom.msw |= 0x80000000; /* add 1.0 */
  113. div_Xsig(&accumulator, &Denom, &accumulator);
  114. }
  115. /* Convert to 64 bit signed-compatible */
  116. exponent += round_Xsig(&accumulator);
  117. result = &st(0);
  118. significand(result) = XSIG_LL(accumulator);
  119. setexponent16(result, exponent);
  120. tag = FPU_round(result, 1, 0, FULL_PRECISION, sign);
  121. setsign(result, sign);
  122. FPU_settag0(tag);
  123. return 0;
  124. }