cbrtf.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* origin: FreeBSD /usr/src/lib/msun/src/s_cbrtf.c */
  2. /*
  3. * Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
  4. * Debugged and optimized by Bruce D. Evans.
  5. */
  6. /*
  7. * ====================================================
  8. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  9. *
  10. * Developed at SunPro, a Sun Microsystems, Inc. business.
  11. * Permission to use, copy, modify, and distribute this
  12. * software is freely granted, provided that this notice
  13. * is preserved.
  14. * ====================================================
  15. */
  16. /* cbrtf(x)
  17. * Return cube root of x
  18. */
  19. #include "libm.h"
  20. static const unsigned
  21. B1 = 709958130, /* B1 = (127-127.0/3-0.03306235651)*2**23 */
  22. B2 = 642849266; /* B2 = (127-127.0/3-24/3-0.03306235651)*2**23 */
  23. float cbrtf(float x)
  24. {
  25. double r,T;
  26. float t;
  27. int32_t hx;
  28. uint32_t sign;
  29. uint32_t high;
  30. GET_FLOAT_WORD(hx, x);
  31. sign = hx & 0x80000000;
  32. hx ^= sign;
  33. if (hx >= 0x7f800000) /* cbrt(NaN,INF) is itself */
  34. return x + x;
  35. /* rough cbrt to 5 bits */
  36. if (hx < 0x00800000) { /* zero or subnormal? */
  37. if (hx == 0)
  38. return x; /* cbrt(+-0) is itself */
  39. SET_FLOAT_WORD(t, 0x4b800000); /* set t = 2**24 */
  40. t *= x;
  41. GET_FLOAT_WORD(high, t);
  42. SET_FLOAT_WORD(t, sign|((high&0x7fffffff)/3+B2));
  43. } else
  44. SET_FLOAT_WORD(t, sign|(hx/3+B1));
  45. /*
  46. * First step Newton iteration (solving t*t-x/t == 0) to 16 bits. In
  47. * double precision so that its terms can be arranged for efficiency
  48. * without causing overflow or underflow.
  49. */
  50. T = t;
  51. r = T*T*T;
  52. T = T*((double)x+x+r)/(x+r+r);
  53. /*
  54. * Second step Newton iteration to 47 bits. In double precision for
  55. * efficiency and accuracy.
  56. */
  57. r = T*T*T;
  58. T = T*((double)x+x+r)/(x+r+r);
  59. /* rounding to 24 bits is perfect in round-to-nearest mode */
  60. return T;
  61. }