e_hypotl.c 3.3 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 sqrtl(2)/2 ulp, than
  17. * sqrtl(z) has error less than 1 ulp (exercise).
  18. *
  19. * So, compute sqrtl(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 64 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 64 bits cleared, t2 = 2x-t1,
  30. * yy1= y with lower 64 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. * hypotl(x,y) is INF if x or y is +INF or -INF; else
  37. * hypotl(x,y) is NAN if x or y is NAN.
  38. *
  39. * Accuracy:
  40. * hypotl(x,y) returns sqrtl(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. int64_t j,k,ha,hb;
  50. GET_LDOUBLE_MSW64(ha,x);
  51. ha &= 0x7fffffffffffffffLL;
  52. GET_LDOUBLE_MSW64(hb,y);
  53. hb &= 0x7fffffffffffffffLL;
  54. if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
  55. SET_LDOUBLE_MSW64(a,ha); /* a <- |a| */
  56. SET_LDOUBLE_MSW64(b,hb); /* b <- |b| */
  57. if((ha-hb)>0x78000000000000LL) {return a+b;} /* x/y > 2**120 */
  58. k=0;
  59. if(ha > 0x5f3f000000000000LL) { /* a>2**8000 */
  60. if(ha >= 0x7fff000000000000LL) { /* Inf or NaN */
  61. u_int64_t low;
  62. w = a+b; /* for sNaN */
  63. GET_LDOUBLE_LSW64(low,a);
  64. if(((ha&0xffffffffffffLL)|low)==0) w = a;
  65. GET_LDOUBLE_LSW64(low,b);
  66. if(((hb^0x7fff000000000000LL)|low)==0) w = b;
  67. return w;
  68. }
  69. /* scale a and b by 2**-9600 */
  70. ha -= 0x2580000000000000LL;
  71. hb -= 0x2580000000000000LL; k += 9600;
  72. SET_LDOUBLE_MSW64(a,ha);
  73. SET_LDOUBLE_MSW64(b,hb);
  74. }
  75. if(hb < 0x20bf000000000000LL) { /* b < 2**-8000 */
  76. if(hb <= 0x0000ffffffffffffLL) { /* subnormal b or 0 */
  77. u_int64_t low;
  78. GET_LDOUBLE_LSW64(low,b);
  79. if((hb|low)==0) return a;
  80. t1=0;
  81. SET_LDOUBLE_MSW64(t1,0x7ffd000000000000LL); /* t1=2^16382 */
  82. b *= t1;
  83. a *= t1;
  84. k -= 16382;
  85. } else { /* scale a and b by 2^9600 */
  86. ha += 0x2580000000000000LL; /* a *= 2^9600 */
  87. hb += 0x2580000000000000LL; /* b *= 2^9600 */
  88. k -= 9600;
  89. SET_LDOUBLE_MSW64(a,ha);
  90. SET_LDOUBLE_MSW64(b,hb);
  91. }
  92. }
  93. /* medium size a and b */
  94. w = a-b;
  95. if (w>b) {
  96. t1 = 0;
  97. SET_LDOUBLE_MSW64(t1,ha);
  98. t2 = a-t1;
  99. w = sqrtl(t1*t1-(b*(-b)-t2*(a+t1)));
  100. } else {
  101. a = a+a;
  102. yy1 = 0;
  103. SET_LDOUBLE_MSW64(yy1,hb);
  104. y2 = b - yy1;
  105. t1 = 0;
  106. SET_LDOUBLE_MSW64(t1,ha+0x0001000000000000LL);
  107. t2 = a - t1;
  108. w = sqrtl(t1*yy1-(w*(-w)-(t1*y2+t2*b)));
  109. }
  110. if(k!=0) {
  111. u_int64_t high;
  112. t1 = 1.0L;
  113. GET_LDOUBLE_MSW64(high,t1);
  114. SET_LDOUBLE_MSW64(t1,high+(k<<48));
  115. return t1*w;
  116. } else return w;
  117. }