hypot.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* origin: FreeBSD /usr/src/lib/msun/src/e_hypot.c */
  2. /*
  3. * ====================================================
  4. * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  5. *
  6. * Developed at SunSoft, 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. /* hypot(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, then
  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*y1+((x-y)*(x-y)+(t1*y2+t2*y))
  29. * where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1,
  30. * y1= y with lower 32 bits chopped, y2 = y-y1.
  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 "libm.h"
  44. double hypot(double x, double y)
  45. {
  46. double a,b,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) {
  53. a = y;
  54. b = x;
  55. j=ha; ha=hb; hb=j;
  56. } else {
  57. a = x;
  58. b = y;
  59. }
  60. a = fabs(a);
  61. b = fabs(b);
  62. if (ha - hb > 0x3c00000) /* x/y > 2**60 */
  63. return a+b;
  64. k = 0;
  65. if (ha > 0x5f300000) { /* a > 2**500 */
  66. if(ha >= 0x7ff00000) { /* Inf or NaN */
  67. uint32_t low;
  68. /* Use original arg order iff result is NaN; quieten sNaNs. */
  69. w = fabs(x+0.0) - fabs(y+0.0);
  70. GET_LOW_WORD(low, a);
  71. if (((ha&0xfffff)|low) == 0) w = a;
  72. GET_LOW_WORD(low, b);
  73. if (((hb^0x7ff00000)|low) == 0) w = b;
  74. return w;
  75. }
  76. /* scale a and b by 2**-600 */
  77. ha -= 0x25800000; hb -= 0x25800000; k += 600;
  78. SET_HIGH_WORD(a, ha);
  79. SET_HIGH_WORD(b, hb);
  80. }
  81. if (hb < 0x20b00000) { /* b < 2**-500 */
  82. if (hb <= 0x000fffff) { /* subnormal b or 0 */
  83. uint32_t low;
  84. GET_LOW_WORD(low, b);
  85. if ((hb|low) == 0)
  86. return a;
  87. t1 = 0;
  88. SET_HIGH_WORD(t1, 0x7fd00000); /* t1 = 2^1022 */
  89. b *= t1;
  90. a *= t1;
  91. k -= 1022;
  92. } else { /* scale a and b by 2^600 */
  93. ha += 0x25800000; /* a *= 2^600 */
  94. hb += 0x25800000; /* b *= 2^600 */
  95. k -= 600;
  96. SET_HIGH_WORD(a, ha);
  97. SET_HIGH_WORD(b, hb);
  98. }
  99. }
  100. /* medium size a and b */
  101. w = a - b;
  102. if (w > b) {
  103. t1 = 0;
  104. SET_HIGH_WORD(t1, ha);
  105. t2 = a-t1;
  106. w = sqrt(t1*t1-(b*(-b)-t2*(a+t1)));
  107. } else {
  108. a = a + a;
  109. y1 = 0;
  110. SET_HIGH_WORD(y1, hb);
  111. y2 = b - y1;
  112. t1 = 0;
  113. SET_HIGH_WORD(t1, ha+0x00100000);
  114. t2 = a - t1;
  115. w = sqrt(t1*y1-(w*(-w)-(t1*y2+t2*b)));
  116. }
  117. if (k)
  118. w = scalbn(w, k);
  119. return w;
  120. }