1 /* From: @(#)e_hypot.c 1.3 95/01/18 */
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 * ====================================================
13 #include <sys/cdefs.h>
14 __FBSDID("$FreeBSD$");
16 /* long double version of hypot(). See e_hypot.c for most comments. */
22 #include "math_private.h"
24 #define GET_LDBL_MAN(h, l, v) do { \
25 union IEEEl2bits uv; \
33 #define GET_HIGH_WORD(i, v) GET_LDBL_EXPSIGN(i, v)
35 #define SET_HIGH_WORD(v, i) SET_LDBL_EXPSIGN(v, i)
37 #define DESW(exp) (exp) /* delta expsign word */
38 #define ESW(exp) (MAX_EXP - 1 + (exp)) /* expsign word */
39 #define MANT_DIG LDBL_MANT_DIG
40 #define MAX_EXP LDBL_MAX_EXP
42 #if LDBL_MANL_SIZE > 32
43 typedef uint64_t man_t;
45 typedef uint32_t man_t;
49 hypotl(long double x, long double y)
51 long double a=x,b=y,t1,t2,y1,y2,w;
58 if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
61 if((ha-hb)>DESW(MANT_DIG+7)) {return a+b;} /* x/y > 2**(MANT_DIG+7) */
63 if(ha > ESW(MAX_EXP/2-12)) { /* a>2**(MAX_EXP/2-12) */
64 if(ha >= ESW(MAX_EXP)) { /* Inf or NaN */
66 /* Use original arg order iff result is NaN; quieten sNaNs. */
67 w = fabsl(x+0.0)-fabsl(y+0.0);
68 GET_LDBL_MAN(manh,manl,a);
69 if (manh == LDBL_NBIT && manl == 0) w = a;
70 GET_LDBL_MAN(manh,manl,b);
71 if (hb >= ESW(MAX_EXP) && manh == LDBL_NBIT && manl == 0) w = b;
74 /* scale a and b by 2**-(MAX_EXP/2+88) */
75 ha -= DESW(MAX_EXP/2+88); hb -= DESW(MAX_EXP/2+88);
80 if(hb < ESW(-(MAX_EXP/2-12))) { /* b < 2**-(MAX_EXP/2-12) */
81 if(hb <= 0) { /* subnormal b or 0 */
83 GET_LDBL_MAN(manh,manl,b);
84 if((manh|manl)==0) return a;
86 SET_HIGH_WORD(t1,ESW(MAX_EXP-2)); /* t1=2^(MAX_EXP-2) */
90 } else { /* scale a and b by 2^(MAX_EXP/2+88) */
91 ha += DESW(MAX_EXP/2+88);
92 hb += DESW(MAX_EXP/2+88);
98 /* medium size a and b */
103 uv.e = t1; uv.bits.manl = 0; t1 = uv.e;
105 w = sqrtl(t1*t1-(b*(-b)-t2*(a+t1)));
110 uv.e = y1; uv.bits.manl = 0; y1 = uv.e;
113 uv.e = t1; uv.bits.manl = 0; t1 = uv.e;
115 w = sqrtl(t1*y1-(w*(-w)-(t1*y2+t2*b)));
120 GET_HIGH_WORD(high,t1);
121 SET_HIGH_WORD(t1,high+DESW(k));