cbrtl.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_cbrtl.c */
  2. /*-
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. * Copyright (c) 2009-2011, Bruce D. Evans, Steven G. Kargl, David Schultz.
  6. *
  7. * Developed at SunPro, 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. * The argument reduction and testing for exceptional cases was
  14. * written by Steven G. Kargl with input from Bruce D. Evans
  15. * and David A. Schultz.
  16. */
  17. #include "libm.h"
  18. #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
  19. long double cbrtl(long double x)
  20. {
  21. return cbrt(x);
  22. }
  23. #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
  24. #define BIAS (LDBL_MAX_EXP - 1)
  25. static const unsigned B1 = 709958130; /* B1 = (127-127.0/3-0.03306235651)*2**23 */
  26. long double cbrtl(long double x)
  27. {
  28. union IEEEl2bits u, v;
  29. long double r, s, t, w;
  30. double dr, dt, dx;
  31. float ft, fx;
  32. uint32_t hx;
  33. uint16_t expsign;
  34. int k;
  35. u.e = x;
  36. expsign = u.xbits.expsign;
  37. k = expsign & 0x7fff;
  38. /*
  39. * If x = +-Inf, then cbrt(x) = +-Inf.
  40. * If x = NaN, then cbrt(x) = NaN.
  41. */
  42. if (k == BIAS + LDBL_MAX_EXP)
  43. return x + x;
  44. if (k == 0) {
  45. /* If x = +-0, then cbrt(x) = +-0. */
  46. if ((u.bits.manh | u.bits.manl) == 0)
  47. return x;
  48. /* Adjust subnormal numbers. */
  49. u.e *= 0x1.0p514;
  50. k = u.bits.exp;
  51. k -= BIAS + 514;
  52. } else
  53. k -= BIAS;
  54. u.xbits.expsign = BIAS;
  55. v.e = 1;
  56. x = u.e;
  57. switch (k % 3) {
  58. case 1:
  59. case -2:
  60. x = 2*x;
  61. k--;
  62. break;
  63. case 2:
  64. case -1:
  65. x = 4*x;
  66. k -= 2;
  67. break;
  68. }
  69. v.xbits.expsign = (expsign & 0x8000) | (BIAS + k / 3);
  70. /*
  71. * The following is the guts of s_cbrtf, with the handling of
  72. * special values removed and extra care for accuracy not taken,
  73. * but with most of the extra accuracy not discarded.
  74. */
  75. /* ~5-bit estimate: */
  76. fx = x;
  77. GET_FLOAT_WORD(hx, fx);
  78. SET_FLOAT_WORD(ft, ((hx & 0x7fffffff) / 3 + B1));
  79. /* ~16-bit estimate: */
  80. dx = x;
  81. dt = ft;
  82. dr = dt * dt * dt;
  83. dt = dt * (dx + dx + dr) / (dx + dr + dr);
  84. /* ~47-bit estimate: */
  85. dr = dt * dt * dt;
  86. dt = dt * (dx + dx + dr) / (dx + dr + dr);
  87. #if LDBL_MANT_DIG == 64
  88. /*
  89. * dt is cbrtl(x) to ~47 bits (after x has been reduced to 1 <= x < 8).
  90. * Round it away from zero to 32 bits (32 so that t*t is exact, and
  91. * away from zero for technical reasons).
  92. */
  93. t = dt + (0x1.0p32L + 0x1.0p-31L) - 0x1.0p32;
  94. #elif LDBL_MANT_DIG == 113
  95. /*
  96. * Round dt away from zero to 47 bits. Since we don't trust the 47,
  97. * add 2 47-bit ulps instead of 1 to round up. Rounding is slow and
  98. * might be avoidable in this case, since on most machines dt will
  99. * have been evaluated in 53-bit precision and the technical reasons
  100. * for rounding up might not apply to either case in cbrtl() since
  101. * dt is much more accurate than needed.
  102. */
  103. t = dt + 0x2.0p-46 + 0x1.0p60L - 0x1.0p60;
  104. #endif
  105. /*
  106. * Final step Newton iteration to 64 or 113 bits with
  107. * error < 0.667 ulps
  108. */
  109. s = t*t; /* t*t is exact */
  110. r = x/s; /* error <= 0.5 ulps; |r| < |t| */
  111. w = t+t; /* t+t is exact */
  112. r = (r-t)/(w+r); /* r-t is exact; w+r ~= 3*t */
  113. t = t+t*r; /* error <= 0.5 + 0.5/3 + epsilon */
  114. t *= v.e;
  115. return t;
  116. }
  117. #endif