e_sqrtf.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* e_sqrtf.c -- float version of e_sqrt.c.
  2. * Conversion to float by Ian Lance Taylor, Cygnus Support, [email protected].
  3. */
  4. /*
  5. * ====================================================
  6. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  7. *
  8. * Developed at SunPro, a Sun Microsystems, Inc. business.
  9. * Permission to use, copy, modify, and distribute this
  10. * software is freely granted, provided that this notice
  11. * is preserved.
  12. * ====================================================
  13. */
  14. #include "openlibm.h"
  15. #include "math_private.h"
  16. static const float one = 1.0, tiny=1.0e-30;
  17. DLLEXPORT float
  18. __ieee754_sqrtf(float x)
  19. {
  20. float z;
  21. int32_t sign = (int)0x80000000;
  22. int32_t ix,s,q,m,t,i;
  23. u_int32_t r;
  24. GET_FLOAT_WORD(ix,x);
  25. /* take care of Inf and NaN */
  26. if((ix&0x7f800000)==0x7f800000) {
  27. return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
  28. sqrt(-inf)=sNaN */
  29. }
  30. /* take care of zero */
  31. if(ix<=0) {
  32. if((ix&(~sign))==0) return x;/* sqrt(+-0) = +-0 */
  33. else if(ix<0)
  34. return (x-x)/(x-x); /* sqrt(-ve) = sNaN */
  35. }
  36. /* normalize x */
  37. m = (ix>>23);
  38. if(m==0) { /* subnormal x */
  39. for(i=0;(ix&0x00800000)==0;i++) ix<<=1;
  40. m -= i-1;
  41. }
  42. m -= 127; /* unbias exponent */
  43. ix = (ix&0x007fffff)|0x00800000;
  44. if(m&1) /* odd m, double x to make it even */
  45. ix += ix;
  46. m >>= 1; /* m = [m/2] */
  47. /* generate sqrt(x) bit by bit */
  48. ix += ix;
  49. q = s = 0; /* q = sqrt(x) */
  50. r = 0x01000000; /* r = moving bit from right to left */
  51. while(r!=0) {
  52. t = s+r;
  53. if(t<=ix) {
  54. s = t+r;
  55. ix -= t;
  56. q += r;
  57. }
  58. ix += ix;
  59. r>>=1;
  60. }
  61. /* use floating add to find out rounding direction */
  62. if(ix!=0) {
  63. z = one-tiny; /* trigger inexact flag */
  64. if (z>=one) {
  65. z = one+tiny;
  66. if (z>one)
  67. q += 2;
  68. else
  69. q += (q&1);
  70. }
  71. }
  72. ix = (q>>1)+0x3f000000;
  73. ix += (m <<23);
  74. SET_FLOAT_WORD(z,ix);
  75. return z;
  76. }