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