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