e_hypotl.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include "math_private_openbsd.h"
  46. long double
  47. hypotl(long double x, long double y)
  48. {
  49. long double a,b,t1,t2,yy1,y2,w;
  50. u_int32_t j,k,ea,eb;
  51. GET_LDOUBLE_EXP(ea,x);
  52. ea &= 0x7fff;
  53. GET_LDOUBLE_EXP(eb,y);
  54. eb &= 0x7fff;
  55. if(eb > ea) {a=y;b=x;j=ea; ea=eb;eb=j;} else {a=x;b=y;}
  56. SET_LDOUBLE_EXP(a,ea); /* a <- |a| */
  57. SET_LDOUBLE_EXP(b,eb); /* b <- |b| */
  58. if((ea-eb)>0x46) {return a+b;} /* x/y > 2**70 */
  59. k=0;
  60. if(ea > 0x5f3f) { /* a>2**8000 */
  61. if(ea == 0x7fff) { /* Inf or NaN */
  62. u_int32_t es,high,low;
  63. w = a+b; /* for sNaN */
  64. GET_LDOUBLE_WORDS(es,high,low,a);
  65. if(((high&0x7fffffff)|low)==0) w = a;
  66. GET_LDOUBLE_WORDS(es,high,low,b);
  67. if(((eb^0x7fff)|(high&0x7fffffff)|low)==0) w = b;
  68. return w;
  69. }
  70. /* scale a and b by 2**-9600 */
  71. ea -= 0x2580; eb -= 0x2580; k += 9600;
  72. SET_LDOUBLE_EXP(a,ea);
  73. SET_LDOUBLE_EXP(b,eb);
  74. }
  75. if(eb < 0x20bf) { /* b < 2**-8000 */
  76. if(eb == 0) { /* subnormal b or 0 */
  77. u_int32_t es,high,low;
  78. GET_LDOUBLE_WORDS(es,high,low,b);
  79. if((high|low)==0) return a;
  80. SET_LDOUBLE_WORDS(t1, 0x7ffd, 0, 0); /* t1=2^16382 */
  81. b *= t1;
  82. a *= t1;
  83. k -= 16382;
  84. } else { /* scale a and b by 2^9600 */
  85. ea += 0x2580; /* a *= 2^9600 */
  86. eb += 0x2580; /* b *= 2^9600 */
  87. k -= 9600;
  88. SET_LDOUBLE_EXP(a,ea);
  89. SET_LDOUBLE_EXP(b,eb);
  90. }
  91. }
  92. /* medium size a and b */
  93. w = a-b;
  94. if (w>b) {
  95. u_int32_t high;
  96. GET_LDOUBLE_MSW(high,a);
  97. SET_LDOUBLE_WORDS(t1,ea,high,0);
  98. t2 = a-t1;
  99. w = sqrtl(t1*t1-(b*(-b)-t2*(a+t1)));
  100. } else {
  101. u_int32_t high;
  102. GET_LDOUBLE_MSW(high,b);
  103. a = a+a;
  104. SET_LDOUBLE_WORDS(yy1,eb,high,0);
  105. y2 = b - yy1;
  106. GET_LDOUBLE_MSW(high,a);
  107. SET_LDOUBLE_WORDS(t1,ea+1,high,0);
  108. t2 = a - t1;
  109. w = sqrtl(t1*yy1-(w*(-w)-(t1*y2+t2*b)));
  110. }
  111. if(k!=0) {
  112. u_int32_t es;
  113. t1 = 1.0;
  114. GET_LDOUBLE_EXP(es,t1);
  115. SET_LDOUBLE_EXP(t1,es+k);
  116. return t1*w;
  117. } else return w;
  118. }