xref: /freebsd/lib/msun/src/e_hypotl.c (revision 0dd5a5603e7a33d976f8e6015620bbc79839c609)
1d23166b0SDavid Schultz /*
2d23166b0SDavid Schultz  * ====================================================
3d23166b0SDavid Schultz  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
4d23166b0SDavid Schultz  *
5d23166b0SDavid Schultz  * Developed at SunSoft, a Sun Microsystems, Inc. business.
6d23166b0SDavid Schultz  * Permission to use, copy, modify, and distribute this
7d23166b0SDavid Schultz  * software is freely granted, provided that this notice
8d23166b0SDavid Schultz  * is preserved.
9d23166b0SDavid Schultz  * ====================================================
10d23166b0SDavid Schultz  */
11d23166b0SDavid Schultz 
12d23166b0SDavid Schultz /* long double version of hypot().  See e_hypot.c for most comments. */
13d23166b0SDavid Schultz 
14d23166b0SDavid Schultz #include <float.h>
15d23166b0SDavid Schultz 
16d23166b0SDavid Schultz #include "fpmath.h"
17d23166b0SDavid Schultz #include "math.h"
18d23166b0SDavid Schultz #include "math_private.h"
19d23166b0SDavid Schultz 
20d23166b0SDavid Schultz #define	GET_LDBL_MAN(h, l, v) do {	\
21d23166b0SDavid Schultz 	union IEEEl2bits uv;		\
22d23166b0SDavid Schultz 					\
23d23166b0SDavid Schultz 	uv.e = v;			\
24d23166b0SDavid Schultz 	h = uv.bits.manh;		\
25d23166b0SDavid Schultz 	l = uv.bits.manl;		\
26d23166b0SDavid Schultz } while (0)
27d23166b0SDavid Schultz 
28d23166b0SDavid Schultz #undef GET_HIGH_WORD
29d23166b0SDavid Schultz #define	GET_HIGH_WORD(i, v)	GET_LDBL_EXPSIGN(i, v)
30d23166b0SDavid Schultz #undef SET_HIGH_WORD
31d23166b0SDavid Schultz #define	SET_HIGH_WORD(v, i)	SET_LDBL_EXPSIGN(v, i)
32d23166b0SDavid Schultz 
33d23166b0SDavid Schultz #define	DESW(exp)	(exp)		/* delta expsign word */
34d23166b0SDavid Schultz #define	ESW(exp)	(MAX_EXP - 1 + (exp))	/* expsign word */
35d23166b0SDavid Schultz #define	MANT_DIG	LDBL_MANT_DIG
36d23166b0SDavid Schultz #define	MAX_EXP		LDBL_MAX_EXP
37d23166b0SDavid Schultz 
38d23166b0SDavid Schultz #if LDBL_MANL_SIZE > 32
39d23166b0SDavid Schultz typedef	uint64_t man_t;
40d23166b0SDavid Schultz #else
41d23166b0SDavid Schultz typedef	uint32_t man_t;
42d23166b0SDavid Schultz #endif
43d23166b0SDavid Schultz 
44d23166b0SDavid Schultz long double
hypotl(long double x,long double y)45d23166b0SDavid Schultz hypotl(long double x, long double y)
46d23166b0SDavid Schultz {
47d23166b0SDavid Schultz 	long double a=x,b=y,t1,t2,y1,y2,w;
48d23166b0SDavid Schultz 	int32_t j,k,ha,hb;
49d23166b0SDavid Schultz 
50d23166b0SDavid Schultz 	GET_HIGH_WORD(ha,x);
51d23166b0SDavid Schultz 	ha &= 0x7fff;
52d23166b0SDavid Schultz 	GET_HIGH_WORD(hb,y);
53d23166b0SDavid Schultz 	hb &= 0x7fff;
54d23166b0SDavid Schultz 	if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
55d23166b0SDavid Schultz 	a = fabsl(a);
56d23166b0SDavid Schultz 	b = fabsl(b);
57d23166b0SDavid Schultz 	if((ha-hb)>DESW(MANT_DIG+7)) {return a+b;} /* x/y > 2**(MANT_DIG+7) */
58d23166b0SDavid Schultz 	k=0;
59d23166b0SDavid Schultz 	if(ha > ESW(MAX_EXP/2-12)) {	/* a>2**(MAX_EXP/2-12) */
60d23166b0SDavid Schultz 	   if(ha >= ESW(MAX_EXP)) {	/* Inf or NaN */
61d23166b0SDavid Schultz 	       man_t manh, manl;
62d23166b0SDavid Schultz 	       /* Use original arg order iff result is NaN; quieten sNaNs. */
636f1b8a07SBruce Evans 	       w = fabsl(x+0.0L)-fabsl(y+0);
64d23166b0SDavid Schultz 	       GET_LDBL_MAN(manh,manl,a);
65d23166b0SDavid Schultz 	       if (manh == LDBL_NBIT && manl == 0) w = a;
66d23166b0SDavid Schultz 	       GET_LDBL_MAN(manh,manl,b);
67d23166b0SDavid Schultz 	       if (hb >= ESW(MAX_EXP) && manh == LDBL_NBIT && manl == 0) w = b;
68d23166b0SDavid Schultz 	       return w;
69d23166b0SDavid Schultz 	   }
70d23166b0SDavid Schultz 	   /* scale a and b by 2**-(MAX_EXP/2+88) */
71d23166b0SDavid Schultz 	   ha -= DESW(MAX_EXP/2+88); hb -= DESW(MAX_EXP/2+88);
72d23166b0SDavid Schultz 	   k += MAX_EXP/2+88;
73d23166b0SDavid Schultz 	   SET_HIGH_WORD(a,ha);
74d23166b0SDavid Schultz 	   SET_HIGH_WORD(b,hb);
75d23166b0SDavid Schultz 	}
76d23166b0SDavid Schultz 	if(hb < ESW(-(MAX_EXP/2-12))) {	/* b < 2**-(MAX_EXP/2-12) */
77d23166b0SDavid Schultz 	    if(hb <= 0) {		/* subnormal b or 0 */
78d23166b0SDavid Schultz 	        man_t manh, manl;
79d23166b0SDavid Schultz 		GET_LDBL_MAN(manh,manl,b);
80d23166b0SDavid Schultz 		if((manh|manl)==0) return a;
81*d3338f33SDimitry Andric 		t1=1;
82d23166b0SDavid Schultz 		SET_HIGH_WORD(t1,ESW(MAX_EXP-2));	/* t1=2^(MAX_EXP-2) */
83d23166b0SDavid Schultz 		b *= t1;
84d23166b0SDavid Schultz 		a *= t1;
85d23166b0SDavid Schultz 		k -= MAX_EXP-2;
86d23166b0SDavid Schultz 	    } else {		/* scale a and b by 2^(MAX_EXP/2+88) */
87d23166b0SDavid Schultz 		ha += DESW(MAX_EXP/2+88);
88d23166b0SDavid Schultz 		hb += DESW(MAX_EXP/2+88);
89d23166b0SDavid Schultz 		k -= MAX_EXP/2+88;
90d23166b0SDavid Schultz 		SET_HIGH_WORD(a,ha);
91d23166b0SDavid Schultz 		SET_HIGH_WORD(b,hb);
92d23166b0SDavid Schultz 	    }
93d23166b0SDavid Schultz 	}
94d23166b0SDavid Schultz     /* medium size a and b */
95d23166b0SDavid Schultz 	w = a-b;
96d23166b0SDavid Schultz 	if (w>b) {
97d23166b0SDavid Schultz 	    t1 = a;
98d23166b0SDavid Schultz 	    union IEEEl2bits uv;
998087c515SDavid Schultz 	    uv.e = t1; uv.bits.manl = 0; t1 = uv.e;
100d23166b0SDavid Schultz 	    t2 = a-t1;
101d23166b0SDavid Schultz 	    w  = sqrtl(t1*t1-(b*(-b)-t2*(a+t1)));
102d23166b0SDavid Schultz 	} else {
103d23166b0SDavid Schultz 	    a  = a+a;
104d23166b0SDavid Schultz 	    y1 = b;
105d23166b0SDavid Schultz 	    union IEEEl2bits uv;
106d23166b0SDavid Schultz 	    uv.e = y1; uv.bits.manl = 0; y1 = uv.e;
107d23166b0SDavid Schultz 	    y2 = b - y1;
108d23166b0SDavid Schultz 	    t1 = a;
109d23166b0SDavid Schultz 	    uv.e = t1; uv.bits.manl = 0; t1 = uv.e;
110d23166b0SDavid Schultz 	    t2 = a - t1;
111d23166b0SDavid Schultz 	    w  = sqrtl(t1*y1-(w*(-w)-(t1*y2+t2*b)));
112d23166b0SDavid Schultz 	}
113d23166b0SDavid Schultz 	if(k!=0) {
114d23166b0SDavid Schultz 	    u_int32_t high;
115d23166b0SDavid Schultz 	    t1 = 1.0;
116d23166b0SDavid Schultz 	    GET_HIGH_WORD(high,t1);
117d23166b0SDavid Schultz 	    SET_HIGH_WORD(t1,high+DESW(k));
118d23166b0SDavid Schultz 	    return t1*w;
119d23166b0SDavid Schultz 	} else return w;
120d23166b0SDavid Schultz }
121