cbrt.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_cbrt.c */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunPro, a Sun Microsystems, Inc. business.
  7. * Permission to use, copy, modify, and distribute this
  8. * software is freely granted, provided that this notice
  9. * is preserved.
  10. * ====================================================
  11. *
  12. * Optimized by Bruce D. Evans.
  13. */
  14. /* cbrt(x)
  15. * Return cube root of x
  16. */
  17. #include "libm.h"
  18. static const uint32_t
  19. B1 = 715094163, /* B1 = (1023-1023/3-0.03306235651)*2**20 */
  20. B2 = 696219795; /* B2 = (1023-1023/3-54/3-0.03306235651)*2**20 */
  21. /* |1/cbrt(x) - p(x)| < 2**-23.5 (~[-7.93e-8, 7.929e-8]). */
  22. static const double
  23. P0 = 1.87595182427177009643, /* 0x3ffe03e6, 0x0f61e692 */
  24. P1 = -1.88497979543377169875, /* 0xbffe28e0, 0x92f02420 */
  25. P2 = 1.621429720105354466140, /* 0x3ff9f160, 0x4a49d6c2 */
  26. P3 = -0.758397934778766047437, /* 0xbfe844cb, 0xbee751d9 */
  27. P4 = 0.145996192886612446982; /* 0x3fc2b000, 0xd4e4edd7 */
  28. double cbrt(double x)
  29. {
  30. int32_t hx;
  31. union dshape u;
  32. double r,s,t=0.0,w;
  33. uint32_t sign;
  34. uint32_t high,low;
  35. EXTRACT_WORDS(hx, low, x);
  36. sign = hx & 0x80000000;
  37. hx ^= sign;
  38. if (hx >= 0x7ff00000) /* cbrt(NaN,INF) is itself */
  39. return x+x;
  40. /*
  41. * Rough cbrt to 5 bits:
  42. * cbrt(2**e*(1+m) ~= 2**(e/3)*(1+(e%3+m)/3)
  43. * where e is integral and >= 0, m is real and in [0, 1), and "/" and
  44. * "%" are integer division and modulus with rounding towards minus
  45. * infinity. The RHS is always >= the LHS and has a maximum relative
  46. * error of about 1 in 16. Adding a bias of -0.03306235651 to the
  47. * (e%3+m)/3 term reduces the error to about 1 in 32. With the IEEE
  48. * floating point representation, for finite positive normal values,
  49. * ordinary integer divison of the value in bits magically gives
  50. * almost exactly the RHS of the above provided we first subtract the
  51. * exponent bias (1023 for doubles) and later add it back. We do the
  52. * subtraction virtually to keep e >= 0 so that ordinary integer
  53. * division rounds towards minus infinity; this is also efficient.
  54. */
  55. if (hx < 0x00100000) { /* zero or subnormal? */
  56. if ((hx|low) == 0)
  57. return x; /* cbrt(0) is itself */
  58. SET_HIGH_WORD(t, 0x43500000); /* set t = 2**54 */
  59. t *= x;
  60. GET_HIGH_WORD(high, t);
  61. INSERT_WORDS(t, sign|((high&0x7fffffff)/3+B2), 0);
  62. } else
  63. INSERT_WORDS(t, sign|(hx/3+B1), 0);
  64. /*
  65. * New cbrt to 23 bits:
  66. * cbrt(x) = t*cbrt(x/t**3) ~= t*P(t**3/x)
  67. * where P(r) is a polynomial of degree 4 that approximates 1/cbrt(r)
  68. * to within 2**-23.5 when |r - 1| < 1/10. The rough approximation
  69. * has produced t such than |t/cbrt(x) - 1| ~< 1/32, and cubing this
  70. * gives us bounds for r = t**3/x.
  71. *
  72. * Try to optimize for parallel evaluation as in k_tanf.c.
  73. */
  74. r = (t*t)*(t/x);
  75. t = t*((P0+r*(P1+r*P2))+((r*r)*r)*(P3+r*P4));
  76. /*
  77. * Round t away from zero to 23 bits (sloppily except for ensuring that
  78. * the result is larger in magnitude than cbrt(x) but not much more than
  79. * 2 23-bit ulps larger). With rounding towards zero, the error bound
  80. * would be ~5/6 instead of ~4/6. With a maximum error of 2 23-bit ulps
  81. * in the rounded t, the infinite-precision error in the Newton
  82. * approximation barely affects third digit in the final error
  83. * 0.667; the error in the rounded t can be up to about 3 23-bit ulps
  84. * before the final error is larger than 0.667 ulps.
  85. */
  86. u.value = t;
  87. u.bits = (u.bits + 0x80000000) & 0xffffffffc0000000ULL;
  88. t = u.value;
  89. /* one step Newton iteration to 53 bits with error < 0.667 ulps */
  90. s = t*t; /* t*t is exact */
  91. r = x/s; /* error <= 0.5 ulps; |r| < |t| */
  92. w = t+t; /* t+t is exact */
  93. r = (r-t)/(w+r); /* r-t is exact; w+r ~= 3*t */
  94. t = t+t*r; /* error <= 0.5 + 0.5/3 + epsilon */
  95. return t;
  96. }