s_cbrt.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* @(#)s_cbrt.c 5.1 93/09/24 */
  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. #if defined(LIBM_SCCS) && !defined(lint)
  13. static char rcsid[] = "$NetBSD: s_cbrt.c,v 1.8 1995/05/10 20:46:49 jtc Exp $";
  14. #endif
  15. #include "math.h"
  16. #include "math_private.h"
  17. /* cbrt(x)
  18. * Return cube root of x
  19. */
  20. #ifdef __STDC__
  21. static const u_int32_t
  22. #else
  23. static u_int32_t
  24. #endif
  25. B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */
  26. B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */
  27. #ifdef __STDC__
  28. static const double
  29. #else
  30. static double
  31. #endif
  32. C = 5.42857142857142815906e-01, /* 19/35 = 0x3FE15F15, 0xF15F15F1 */
  33. D = -7.05306122448979611050e-01, /* -864/1225 = 0xBFE691DE, 0x2532C834 */
  34. E = 1.41428571428571436819e+00, /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */
  35. F = 1.60714285714285720630e+00, /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */
  36. G = 3.57142857142857150787e-01; /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */
  37. #ifdef __STDC__
  38. double cbrt(double x)
  39. #else
  40. double cbrt(x)
  41. double x;
  42. #endif
  43. {
  44. int32_t hx;
  45. double r,s,t=0.0,w;
  46. u_int32_t sign;
  47. u_int32_t high,low;
  48. GET_HIGH_WORD(hx,x);
  49. sign=hx&0x80000000; /* sign= sign(x) */
  50. hx ^=sign;
  51. if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */
  52. GET_LOW_WORD(low,x);
  53. if((hx|low)==0)
  54. return(x); /* cbrt(0) is itself */
  55. SET_HIGH_WORD(x,hx); /* x <- |x| */
  56. /* rough cbrt to 5 bits */
  57. if(hx<0x00100000) /* subnormal number */
  58. {SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */
  59. t*=x; GET_HIGH_WORD(high,t); SET_HIGH_WORD(t,high/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. GET_HIGH_WORD(high,t);
  69. INSERT_WORDS(t,high+0x00000001,0);
  70. /* one step newton iteration to 53 bits with error less than 0.667 ulps */
  71. s=t*t; /* t*t is exact */
  72. r=x/s;
  73. w=t+t;
  74. r=(r-t)/(w+r); /* r-s is exact */
  75. t=t+t*r;
  76. /* retore the sign bit */
  77. GET_HIGH_WORD(high,t);
  78. SET_HIGH_WORD(t,high|sign);
  79. return(t);
  80. }
  81. libm_hidden_def(cbrt)