s_cbrt.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* @(#)s_cbrt.c 1.3 95/01/18 */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunSoft, 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. */
  13. #include "fdlibm.h"
  14. #ifndef _DOUBLE_IS_32BITS
  15. /* cbrt(x)
  16. * Return cube root of x
  17. */
  18. #ifdef __STDC__
  19. static const uint32_t
  20. #else
  21. static uint32_t
  22. #endif
  23. B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */
  24. B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
  25. #ifdef __STDC__
  26. static const double
  27. #else
  28. static double
  29. #endif
  30. C = 5.42857142857142815906e-01, /* 19/35 = 0x3FE15F15, 0xF15F15F1 */
  31. D = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */
  32. E = 1.41428571428571436819e+00, /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */
  33. F = 1.60714285714285720630e+00, /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */
  34. G = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */
  35. #ifdef __STDC__
  36. double cbrt(double x)
  37. #else
  38. double cbrt(x)
  39. double x;
  40. #endif
  41. {
  42. int32_t hx, lx, ht;
  43. double r,s,t=0.0,w;
  44. uint32_t sign;
  45. GET_HIGH_WORD(hx,x); /* high word of x */
  46. sign=hx&0x80000000; /* sign= sign(x) */
  47. hx ^=sign;
  48. if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */
  49. GET_LOW_WORD(lx, x);
  50. if((hx|lx)==0)
  51. return(x); /* cbrt(0) is itself */
  52. SET_HIGH_WORD(x,hx); /* x <- |x| */
  53. /* rough cbrt to 5 bits */
  54. if(hx<0x00100000) /* subnormal number */
  55. {
  56. SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */
  57. t*=x;
  58. GET_HIGH_WORD(ht,t);
  59. SET_HIGH_WORD(t,ht/3+B2);
  60. }
  61. else
  62. SET_HIGH_WORD(t,hx/3+B1);
  63. /* new cbrt to 23 bits, may be implemented in single precision */
  64. r=t*t/x;
  65. s=C+r*t;
  66. t*=G+F/(s+E+D/s);
  67. /* chopped to 20 bits and make it larger than cbrt(x) */
  68. SET_LOW_WORD(t,0);
  69. GET_HIGH_WORD(ht,t);
  70. SET_HIGH_WORD(t,ht + 0x00000001);
  71. /* one step newton iteration to 53 bits with error less than 0.667 ulps */
  72. s=t*t; /* t*t is exact */
  73. r=x/s;
  74. w=t+t;
  75. r=(r-t)/(w+r); /* r-s is exact */
  76. t=t+t*r;
  77. /* retore the sign bit */
  78. GET_HIGH_WORD(ht,t);
  79. SET_HIGH_WORD(t,ht|sign);
  80. return(t);
  81. }
  82. #endif /* _DOUBLE_IS_32BITS */