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