e_sqrtf.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* e_sqrtf.c -- float version of e_sqrt.c.
  2. * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
  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. #ifndef lint
  15. static char rcsid[] = "$FreeBSD: src/lib/msun/src/e_sqrtf.c,v 1.7 2002/05/28 18:15:04 alfred Exp $";
  16. #endif
  17. #include "openlibm.h"
  18. #include "math_private.h"
  19. static const float one = 1.0, tiny=1.0e-30;
  20. float
  21. __ieee754_sqrtf(float x)
  22. {
  23. float z;
  24. int32_t sign = (int)0x80000000;
  25. int32_t ix,s,q,m,t,i;
  26. u_int32_t r;
  27. GET_FLOAT_WORD(ix,x);
  28. /* take care of Inf and NaN */
  29. if((ix&0x7f800000)==0x7f800000) {
  30. return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
  31. sqrt(-inf)=sNaN */
  32. }
  33. /* take care of zero */
  34. if(ix<=0) {
  35. if((ix&(~sign))==0) return x;/* sqrt(+-0) = +-0 */
  36. else if(ix<0)
  37. return (x-x)/(x-x); /* sqrt(-ve) = sNaN */
  38. }
  39. /* normalize x */
  40. m = (ix>>23);
  41. if(m==0) { /* subnormal x */
  42. for(i=0;(ix&0x00800000)==0;i++) ix<<=1;
  43. m -= i-1;
  44. }
  45. m -= 127; /* unbias exponent */
  46. ix = (ix&0x007fffff)|0x00800000;
  47. if(m&1) /* odd m, double x to make it even */
  48. ix += ix;
  49. m >>= 1; /* m = [m/2] */
  50. /* generate sqrt(x) bit by bit */
  51. ix += ix;
  52. q = s = 0; /* q = sqrt(x) */
  53. r = 0x01000000; /* r = moving bit from right to left */
  54. while(r!=0) {
  55. t = s+r;
  56. if(t<=ix) {
  57. s = t+r;
  58. ix -= t;
  59. q += r;
  60. }
  61. ix += ix;
  62. r>>=1;
  63. }
  64. /* use floating add to find out rounding direction */
  65. if(ix!=0) {
  66. z = one-tiny; /* trigger inexact flag */
  67. if (z>=one) {
  68. z = one+tiny;
  69. if (z>one)
  70. q += 2;
  71. else
  72. q += (q&1);
  73. }
  74. }
  75. ix = (q>>1)+0x3f000000;
  76. ix += (m <<23);
  77. SET_FLOAT_WORD(z,ix);
  78. return z;
  79. }