e_hypotl.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* From: @(#)e_hypot.c 1.3 95/01/18 */
  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. #include "cdefs-compat.h"
  13. //__FBSDID("$FreeBSD: src/lib/msun/src/e_hypotl.c,v 1.3 2011/10/16 05:36:39 das Exp $");
  14. /* long double version of hypot(). See e_hypot.c for most comments. */
  15. #include <float.h>
  16. #include <openlibm_math.h>
  17. #include "fpmath.h"
  18. #include "math_private.h"
  19. #define GET_LDBL_MAN(h, l, v) do { \
  20. union IEEEl2bits uv; \
  21. \
  22. uv.e = v; \
  23. h = uv.bits.manh; \
  24. l = uv.bits.manl; \
  25. } while (0)
  26. #undef GET_HIGH_WORD
  27. #define GET_HIGH_WORD(i, v) GET_LDBL_EXPSIGN(i, v)
  28. #undef SET_HIGH_WORD
  29. #define SET_HIGH_WORD(v, i) SET_LDBL_EXPSIGN(v, i)
  30. #define DESW(exp) (exp) /* delta expsign word */
  31. #define ESW(exp) (MAX_EXP - 1 + (exp)) /* expsign word */
  32. #define MANT_DIG LDBL_MANT_DIG
  33. #define MAX_EXP LDBL_MAX_EXP
  34. #if LDBL_MANL_SIZE > 32
  35. typedef u_int64_t man_t;
  36. #else
  37. typedef u_int32_t man_t;
  38. #endif
  39. OLM_DLLEXPORT long double
  40. hypotl(long double x, long double y)
  41. {
  42. long double a=x,b=y,t1,t2,y1,y2,w;
  43. int32_t j,k,ha,hb;
  44. GET_HIGH_WORD(ha,x);
  45. ha &= 0x7fff;
  46. GET_HIGH_WORD(hb,y);
  47. hb &= 0x7fff;
  48. if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
  49. a = fabsl(a);
  50. b = fabsl(b);
  51. if((ha-hb)>DESW(MANT_DIG+7)) {return a+b;} /* x/y > 2**(MANT_DIG+7) */
  52. k=0;
  53. if(ha > ESW(MAX_EXP/2-12)) { /* a>2**(MAX_EXP/2-12) */
  54. if(ha >= ESW(MAX_EXP)) { /* Inf or NaN */
  55. man_t manh, manl;
  56. /* Use original arg order iff result is NaN; quieten sNaNs. */
  57. w = fabsl(x+0.0)-fabsl(y+0.0);
  58. GET_LDBL_MAN(manh,manl,a);
  59. if (manh == LDBL_NBIT && manl == 0) w = a;
  60. GET_LDBL_MAN(manh,manl,b);
  61. if (hb >= ESW(MAX_EXP) && manh == LDBL_NBIT && manl == 0) w = b;
  62. return w;
  63. }
  64. /* scale a and b by 2**-(MAX_EXP/2+88) */
  65. ha -= DESW(MAX_EXP/2+88); hb -= DESW(MAX_EXP/2+88);
  66. k += MAX_EXP/2+88;
  67. SET_HIGH_WORD(a,ha);
  68. SET_HIGH_WORD(b,hb);
  69. }
  70. if(hb < ESW(-(MAX_EXP/2-12))) { /* b < 2**-(MAX_EXP/2-12) */
  71. if(hb <= 0) { /* subnormal b or 0 */
  72. man_t manh, manl;
  73. GET_LDBL_MAN(manh,manl,b);
  74. if((manh|manl)==0) return a;
  75. t1=0;
  76. SET_HIGH_WORD(t1,ESW(MAX_EXP-2)); /* t1=2^(MAX_EXP-2) */
  77. b *= t1;
  78. a *= t1;
  79. k -= MAX_EXP-2;
  80. } else { /* scale a and b by 2^(MAX_EXP/2+88) */
  81. ha += DESW(MAX_EXP/2+88);
  82. hb += DESW(MAX_EXP/2+88);
  83. k -= MAX_EXP/2+88;
  84. SET_HIGH_WORD(a,ha);
  85. SET_HIGH_WORD(b,hb);
  86. }
  87. }
  88. /* medium size a and b */
  89. w = a-b;
  90. if (w>b) {
  91. t1 = a;
  92. union IEEEl2bits uv;
  93. uv.e = t1; uv.bits.manl = 0; t1 = uv.e;
  94. t2 = a-t1;
  95. w = sqrtl(t1*t1-(b*(-b)-t2*(a+t1)));
  96. } else {
  97. a = a+a;
  98. y1 = b;
  99. union IEEEl2bits uv;
  100. uv.e = y1; uv.bits.manl = 0; y1 = uv.e;
  101. y2 = b - y1;
  102. t1 = a;
  103. uv.e = t1; uv.bits.manl = 0; t1 = uv.e;
  104. t2 = a - t1;
  105. w = sqrtl(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+DESW(k));
  112. return t1*w;
  113. } else return w;
  114. }