e_hypot.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * ====================================================
  3. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  4. *
  5. * Developed at SunPro, a Sun Microsystems, Inc. business.
  6. * Permission to use, copy, modify, and distribute this
  7. * software is freely granted, provided that this notice
  8. * is preserved.
  9. * ====================================================
  10. */
  11. /* __ieee754_hypot(x,y)
  12. *
  13. * Method :
  14. * If (assume round-to-nearest) z=x*x+y*y
  15. * has error less than sqrt(2)/2 ulp, than
  16. * sqrt(z) has error less than 1 ulp (exercise).
  17. *
  18. * So, compute sqrt(x*x+y*y) with some care as
  19. * follows to get the error below 1 ulp:
  20. *
  21. * Assume x>y>0;
  22. * (if possible, set rounding to round-to-nearest)
  23. * 1. if x > 2y use
  24. * x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y
  25. * where x1 = x with lower 32 bits cleared, x2 = x-x1; else
  26. * 2. if x <= 2y use
  27. * t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))
  28. * where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1,
  29. * y1= y with lower 32 bits chopped, y2 = y-y1.
  30. *
  31. * NOTE: scaling may be necessary if some argument is too
  32. * large or too tiny
  33. *
  34. * Special cases:
  35. * hypot(x,y) is INF if x or y is +INF or -INF; else
  36. * hypot(x,y) is NAN if x or y is NAN.
  37. *
  38. * Accuracy:
  39. * hypot(x,y) returns sqrt(x^2+y^2) with error less
  40. * than 1 ulps (units in the last place)
  41. */
  42. #include "math.h"
  43. #include "math_private.h"
  44. double __ieee754_hypot(double x, double y)
  45. {
  46. double a=x,b=y,t1,t2,_y1,y2,w;
  47. int32_t j,k,ha,hb;
  48. GET_HIGH_WORD(ha,x);
  49. ha &= 0x7fffffff;
  50. GET_HIGH_WORD(hb,y);
  51. hb &= 0x7fffffff;
  52. if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
  53. SET_HIGH_WORD(a,ha); /* a <- |a| */
  54. SET_HIGH_WORD(b,hb); /* b <- |b| */
  55. if((ha-hb)>0x3c00000) {return a+b;} /* x/y > 2**60 */
  56. k=0;
  57. if(ha > 0x5f300000) { /* a>2**500 */
  58. if(ha >= 0x7ff00000) { /* Inf or NaN */
  59. u_int32_t low;
  60. w = a+b; /* for sNaN */
  61. GET_LOW_WORD(low,a);
  62. if(((ha&0xfffff)|low)==0) w = a;
  63. GET_LOW_WORD(low,b);
  64. if(((hb^0x7ff00000)|low)==0) w = b;
  65. return w;
  66. }
  67. /* scale a and b by 2**-600 */
  68. ha -= 0x25800000; hb -= 0x25800000; k += 600;
  69. SET_HIGH_WORD(a,ha);
  70. SET_HIGH_WORD(b,hb);
  71. }
  72. if(hb < 0x20b00000) { /* b < 2**-500 */
  73. if(hb <= 0x000fffff) { /* subnormal b or 0 */
  74. u_int32_t low;
  75. GET_LOW_WORD(low,b);
  76. if((hb|low)==0) return a;
  77. t1=0;
  78. SET_HIGH_WORD(t1,0x7fd00000); /* t1=2^1022 */
  79. b *= t1;
  80. a *= t1;
  81. k -= 1022;
  82. } else { /* scale a and b by 2^600 */
  83. ha += 0x25800000; /* a *= 2^600 */
  84. hb += 0x25800000; /* b *= 2^600 */
  85. k -= 600;
  86. SET_HIGH_WORD(a,ha);
  87. SET_HIGH_WORD(b,hb);
  88. }
  89. }
  90. /* medium size a and b */
  91. w = a-b;
  92. if (w>b) {
  93. t1 = 0;
  94. SET_HIGH_WORD(t1,ha);
  95. t2 = a-t1;
  96. w = __ieee754_sqrt(t1*t1-(b*(-b)-t2*(a+t1)));
  97. } else {
  98. a = a+a;
  99. _y1 = 0;
  100. SET_HIGH_WORD(_y1,hb);
  101. y2 = b - _y1;
  102. t1 = 0;
  103. SET_HIGH_WORD(t1,ha+0x00100000);
  104. t2 = a - t1;
  105. w = __ieee754_sqrt(t1*_y1-(w*(-w)-(t1*y2+t2*b)));
  106. }
  107. if(k!=0) {
  108. u_int32_t high;
  109. t1 = 1.0;
  110. GET_HIGH_WORD(high,t1);
  111. SET_HIGH_WORD(t1,high+(k<<20));
  112. return t1*w;
  113. } else return w;
  114. }
  115. strong_alias(__ieee754_hypot, hypot)
  116. libm_hidden_def(hypot)