e_hypotl.c 3.2 KB

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