e_rem_pio2l.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* From: @(#)e_rem_pio2.c 1.4 95/01/18 */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. * Copyright (c) 2008 Steven G. Kargl, David Schultz, Bruce D. Evans.
  6. *
  7. * Developed at SunSoft, a Sun Microsystems, Inc. business.
  8. * Permission to use, copy, modify, and distribute this
  9. * software is freely granted, provided that this notice
  10. * is preserved.
  11. * ====================================================
  12. *
  13. * Optimized by Bruce D. Evans.
  14. */
  15. #include <sys/cdefs.h>
  16. /* ld128 version of __ieee754_rem_pio2l(x,y)
  17. *
  18. * return the remainder of x rem pi/2 in y[0]+y[1]
  19. * use __kernel_rem_pio2()
  20. */
  21. #include <float.h>
  22. #include "openlibm.h"
  23. #include "math_private.h"
  24. #include "fpmath.h"
  25. #define BIAS (LDBL_MAX_EXP - 1)
  26. /*
  27. * XXX need to verify that nonzero integer multiples of pi/2 within the
  28. * range get no closer to a long double than 2**-140, or that
  29. * ilogb(x) + ilogb(min_delta) < 45 - -140.
  30. */
  31. /*
  32. * invpio2: 113 bits of 2/pi
  33. * pio2_1: first 68 bits of pi/2
  34. * pio2_1t: pi/2 - pio2_1
  35. * pio2_2: second 68 bits of pi/2
  36. * pio2_2t: pi/2 - (pio2_1+pio2_2)
  37. * pio2_3: third 68 bits of pi/2
  38. * pio2_3t: pi/2 - (pio2_1+pio2_2+pio2_3)
  39. */
  40. static const double
  41. zero = 0.00000000000000000000e+00, /* 0x00000000, 0x00000000 */
  42. two24 = 1.67772160000000000000e+07; /* 0x41700000, 0x00000000 */
  43. static const long double
  44. invpio2 = 6.3661977236758134307553505349005747e-01L, /* 0x145f306dc9c882a53f84eafa3ea6a.0p-113 */
  45. pio2_1 = 1.5707963267948966192292994253909555e+00L, /* 0x1921fb54442d18469800000000000.0p-112 */
  46. pio2_1t = 2.0222662487959507323996846200947577e-21L, /* 0x13198a2e03707344a4093822299f3.0p-181 */
  47. pio2_2 = 2.0222662487959507323994779168837751e-21L, /* 0x13198a2e03707344a400000000000.0p-181 */
  48. pio2_2t = 2.0670321098263988236496903051604844e-43L, /* 0x127044533e63a0105df531d89cd91.0p-254 */
  49. pio2_3 = 2.0670321098263988236499468110329591e-43L, /* 0x127044533e63a0105e00000000000.0p-254 */
  50. pio2_3t = -2.5650587247459238361625433492959285e-65L; /* -0x159c4ec64ddaeb5f78671cbfb2210.0p-327 */
  51. static inline __always_inline int
  52. __ieee754_rem_pio2l(long double x, long double *y)
  53. {
  54. union IEEEl2bits u,u1;
  55. long double z,w,t,r,fn;
  56. double tx[5],ty[3];
  57. int64_t n;
  58. int e0,ex,i,j,nx;
  59. int16_t expsign;
  60. u.e = x;
  61. expsign = u.xbits.expsign;
  62. ex = expsign & 0x7fff;
  63. if (ex < BIAS + 45 || ex == BIAS + 45 &&
  64. u.bits.manh < 0x921fb54442d1LL) {
  65. /* |x| ~< 2^45*(pi/2), medium size */
  66. /* Use a specialized rint() to get fn. Assume round-to-nearest. */
  67. fn = x*invpio2+0x1.8p112;
  68. fn = fn-0x1.8p112;
  69. #ifdef HAVE_EFFICIENT_I64RINT
  70. n = i64rint(fn);
  71. #else
  72. n = fn;
  73. #endif
  74. r = x-fn*pio2_1;
  75. w = fn*pio2_1t; /* 1st round good to 180 bit */
  76. {
  77. union IEEEl2bits u2;
  78. int ex1;
  79. j = ex;
  80. y[0] = r-w;
  81. u2.e = y[0];
  82. ex1 = u2.xbits.expsign & 0x7fff;
  83. i = j-ex1;
  84. if(i>51) { /* 2nd iteration needed, good to 248 */
  85. t = r;
  86. w = fn*pio2_2;
  87. r = t-w;
  88. w = fn*pio2_2t-((t-r)-w);
  89. y[0] = r-w;
  90. u2.e = y[0];
  91. ex1 = u2.xbits.expsign & 0x7fff;
  92. i = j-ex1;
  93. if(i>119) { /* 3rd iteration need, 316 bits acc */
  94. t = r; /* will cover all possible cases */
  95. w = fn*pio2_3;
  96. r = t-w;
  97. w = fn*pio2_3t-((t-r)-w);
  98. y[0] = r-w;
  99. }
  100. }
  101. }
  102. y[1] = (r-y[0])-w;
  103. return n;
  104. }
  105. /*
  106. * all other (large) arguments
  107. */
  108. if(ex==0x7fff) { /* x is inf or NaN */
  109. y[0]=y[1]=x-x; return 0;
  110. }
  111. /* set z = scalbn(|x|,ilogb(x)-23) */
  112. u1.e = x;
  113. e0 = ex - BIAS - 23; /* e0 = ilogb(|x|)-23; */
  114. u1.xbits.expsign = ex - e0;
  115. z = u1.e;
  116. for(i=0;i<4;i++) {
  117. tx[i] = (double)((int32_t)(z));
  118. z = (z-tx[i])*two24;
  119. }
  120. tx[4] = z;
  121. nx = 5;
  122. while(tx[nx-1]==zero) nx--; /* skip zero term */
  123. n = __kernel_rem_pio2(tx,ty,e0,nx,3);
  124. t = (long double)ty[2] + ty[1];
  125. r = t + ty[0];
  126. w = ty[0] - (r - t);
  127. if(expsign<0) {y[0] = -r; y[1] = -w; return -n;}
  128. y[0] = r; y[1] = w; return n;
  129. }