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