1 /* origin: FreeBSD /usr/src/lib/msun/src/e_hypotl.c */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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
10 * ====================================================
12 /* long double version of hypot(). See comments in hypot.c. */
16 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
17 long double hypotl(long double x, long double y)
21 #elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
23 #define GET_LDBL_EXPSIGN(i, v) do { \
24 union IEEEl2bits uv; \
27 i = uv.xbits.expsign; \
30 #define GET_LDBL_MAN(h, l, v) do { \
31 union IEEEl2bits uv; \
38 #define SET_LDBL_EXPSIGN(v, i) do { \
39 union IEEEl2bits uv; \
42 uv.xbits.expsign = i; \
47 #define GET_HIGH_WORD(i, v) GET_LDBL_EXPSIGN(i, v)
49 #define SET_HIGH_WORD(v, i) SET_LDBL_EXPSIGN(v, i)
51 #define DESW(exp) (exp) /* delta expsign word */
52 #define ESW(exp) (MAX_EXP - 1 + (exp)) /* expsign word */
53 #define MANT_DIG LDBL_MANT_DIG
54 #define MAX_EXP LDBL_MAX_EXP
56 #if LDBL_MANL_SIZE > 32
57 typedef uint64_t man_t;
59 typedef uint32_t man_t;
62 long double hypotl(long double x, long double y)
64 long double a=x,b=y,t1,t2,y1,y2,w;
81 if (ha - hb > DESW(MANT_DIG+7)) /* x/y > 2**(MANT_DIG+7) */
84 if (ha > ESW(MAX_EXP/2-12)) { /* a>2**(MAX_EXP/2-12) */
85 if (ha >= ESW(MAX_EXP)) { /* Inf or NaN */
87 /* Use original arg order iff result is NaN; quieten sNaNs. */
88 w = fabsl(x+0.0)-fabsl(y+0.0);
89 GET_LDBL_MAN(manh,manl,a);
90 if (manh == LDBL_NBIT && manl == 0) w = a;
91 GET_LDBL_MAN(manh,manl,b);
92 if (hb >= ESW(MAX_EXP) && manh == LDBL_NBIT && manl == 0) w = b;
95 /* scale a and b by 2**-(MAX_EXP/2+88) */
96 ha -= DESW(MAX_EXP/2+88); hb -= DESW(MAX_EXP/2+88);
101 if (hb < ESW(-(MAX_EXP/2-12))) { /* b < 2**-(MAX_EXP/2-12) */
102 if (hb <= 0) { /* subnormal b or 0 */
104 GET_LDBL_MAN(manh,manl,b);
105 if ((manh|manl) == 0)
108 SET_HIGH_WORD(t1, ESW(MAX_EXP-2)); /* t1 = 2^(MAX_EXP-2) */
112 } else { /* scale a and b by 2^(MAX_EXP/2+88) */
113 ha += DESW(MAX_EXP/2+88);
114 hb += DESW(MAX_EXP/2+88);
116 SET_HIGH_WORD(a, ha);
117 SET_HIGH_WORD(b, hb);
120 /* medium size a and b */
125 uv.e = t1; uv.bits.manl = 0; t1 = uv.e;
127 w = sqrtl(t1*t1-(b*(-b)-t2*(a+t1)));
132 uv.e = y1; uv.bits.manl = 0; y1 = uv.e;
135 uv.e = t1; uv.bits.manl = 0; t1 = uv.e;
137 w = sqrtl(t1*y1-(w*(-w)-(t1*y2+t2*b)));
142 GET_HIGH_WORD(high, t1);
143 SET_HIGH_WORD(t1, high+DESW(k));