s_cbrtf.c 1.9 KB

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