poly_2xm1.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*---------------------------------------------------------------------------+
  3. | poly_2xm1.c |
  4. | |
  5. | Function to compute 2^x-1 by a polynomial approximation. |
  6. | |
  7. | Copyright (C) 1992,1993,1994,1997 |
  8. | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
  9. | E-mail billm@suburbia.net |
  10. | |
  11. | |
  12. +---------------------------------------------------------------------------*/
  13. #include "exception.h"
  14. #include "reg_constant.h"
  15. #include "fpu_emu.h"
  16. #include "fpu_system.h"
  17. #include "control_w.h"
  18. #include "poly.h"
  19. #define HIPOWER 11
  20. static const unsigned long long lterms[HIPOWER] = {
  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. };
  44. /*--- poly_2xm1() -----------------------------------------------------------+
  45. | Requires st(0) which is TAG_Valid and < 1. |
  46. +---------------------------------------------------------------------------*/
  47. int poly_2xm1(u_char sign, FPU_REG *arg, FPU_REG *result)
  48. {
  49. long int exponent, shift;
  50. unsigned long long Xll;
  51. Xsig accumulator, Denom, argSignif;
  52. u_char tag;
  53. exponent = exponent16(arg);
  54. #ifdef PARANOID
  55. if (exponent >= 0) { /* Don't want a |number| >= 1.0 */
  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. shift = (argSignif.msw & 0x40000000) ? 3 : 2;
  65. /* subtract 0.5 or 0.75 */
  66. exponent -= 2;
  67. XSIG_LL(argSignif) <<= 2;
  68. Xll <<= 2;
  69. } else if (exponent == -2) {
  70. shift = 1;
  71. /* subtract 0.25 */
  72. exponent--;
  73. XSIG_LL(argSignif) <<= 1;
  74. Xll <<= 1;
  75. } else
  76. shift = 0;
  77. if (exponent < -2) {
  78. /* Shift the argument right by the required places. */
  79. if (FPU_shrx(&Xll, -2 - exponent) >= 0x80000000U)
  80. Xll++; /* round up */
  81. }
  82. accumulator.lsw = accumulator.midw = accumulator.msw = 0;
  83. polynomial_Xsig(&accumulator, &Xll, lterms, HIPOWER - 1);
  84. mul_Xsig_Xsig(&accumulator, &argSignif);
  85. shr_Xsig(&accumulator, 3);
  86. mul_Xsig_Xsig(&argSignif, &hiterm); /* The leading term */
  87. add_two_Xsig(&accumulator, &argSignif, &exponent);
  88. if (shift) {
  89. /* The argument is large, use the identity:
  90. f(x+a) = f(a) * (f(x) + 1) - 1;
  91. */
  92. shr_Xsig(&accumulator, -exponent);
  93. accumulator.msw |= 0x80000000; /* add 1.0 */
  94. mul_Xsig_Xsig(&accumulator, shiftterm[shift]);
  95. accumulator.msw &= 0x3fffffff; /* subtract 1.0 */
  96. exponent = 1;
  97. }
  98. if (sign != SIGN_POS) {
  99. /* The argument is negative, use the identity:
  100. f(-x) = -f(x) / (1 + f(x))
  101. */
  102. Denom.lsw = accumulator.lsw;
  103. XSIG_LL(Denom) = XSIG_LL(accumulator);
  104. if (exponent < 0)
  105. shr_Xsig(&Denom, -exponent);
  106. else if (exponent > 0) {
  107. /* exponent must be 1 here */
  108. XSIG_LL(Denom) <<= 1;
  109. if (Denom.lsw & 0x80000000)
  110. XSIG_LL(Denom) |= 1;
  111. (Denom.lsw) <<= 1;
  112. }
  113. Denom.msw |= 0x80000000; /* add 1.0 */
  114. div_Xsig(&accumulator, &Denom, &accumulator);
  115. }
  116. /* Convert to 64 bit signed-compatible */
  117. exponent += round_Xsig(&accumulator);
  118. result = &st(0);
  119. significand(result) = XSIG_LL(accumulator);
  120. setexponent16(result, exponent);
  121. tag = FPU_round(result, 1, 0, FULL_PRECISION, sign);
  122. setsign(result, sign);
  123. FPU_settag0(tag);
  124. return 0;
  125. }