forwarding a patch that uses the fetch macros to pull in acpica and build it (NicJA).
[AROS.git] / compiler / stdc / math / e_hypotl.c
blob4e98d375c8ab54e897706049296695c6e9def391
1 /*
2 * ====================================================
3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 * Developed at SunPro, 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 * ====================================================
12 #ifndef lint
13 static char rcsid[] = "$FreeBSD: src/lib/msun/src/e_hypotl.c,v 1.3 2011/10/16 05:36:39 das Exp $";
14 #endif
16 #include <float.h>
17 #include "math.h"
19 #include "fpmath.h"
20 #include "math_private.h"
22 #define GET_LDBL_MAN(h, l, v) do { \
23 union IEEEl2bits uv; \
25 uv.e = v; \
26 h = uv.bits.manh; \
27 l = uv.bits.manl; \
28 } while (0)
30 #undef GET_HIGH_WORD
31 #define GET_HIGH_WORD(i, v) GET_LDBL_EXPSIGN(i, v)
32 #undef SET_HIGH_WORD
33 #define SET_HIGH_WORD(v, i) SET_LDBL_EXPSIGN(v, i)
35 #define DESW(exp) (exp) /* delta expsign word */
36 #define ESW(exp) (MAX_EXP - 1 + (exp)) /* expsign word */
37 #define MANT_DIG LDBL_MANT_DIG
38 #define MAX_EXP LDBL_MAX_EXP
40 #if LDBL_MANL_SIZE > 32
41 typedef uint64_t man_t;
42 #else
43 typedef uint32_t man_t;
44 #endif
46 long double
47 hypotl(long double x, long double y)
49 long double a=x,b=y,t1,t2,y1,y2,w;
50 int32_t j,k,ha,hb;
52 GET_HIGH_WORD(ha,x);
53 ha &= 0x7fff;
54 GET_HIGH_WORD(hb,y);
55 hb &= 0x7fff;
56 if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
57 a = fabsl(a);
58 b = fabsl(b);
59 if((ha-hb)>DESW(MANT_DIG+7)) {return a+b;} /* x/y > 2**(MANT_DIG+7) */
60 k=0;
61 if(ha > ESW(MAX_EXP/2-12)) { /* a>2**(MAX_EXP/2-12) */
62 if(ha >= ESW(MAX_EXP)) { /* Inf or NaN */
63 man_t manh, manl;
64 /* Use original arg order iff result is NaN; quieten sNaNs. */
65 w = fabsl(x+0.0)-fabsl(y+0.0);
66 GET_LDBL_MAN(manh,manl,a);
67 if (manh == LDBL_NBIT && manl == 0) w = a;
68 GET_LDBL_MAN(manh,manl,b);
69 if (hb >= ESW(MAX_EXP) && manh == LDBL_NBIT && manl == 0) w = b;
70 return w;
72 /* scale a and b by 2**-(MAX_EXP/2+88) */
73 ha -= DESW(MAX_EXP/2+88); hb -= DESW(MAX_EXP/2+88);
74 k += MAX_EXP/2+88;
75 SET_HIGH_WORD(a,ha);
76 SET_HIGH_WORD(b,hb);
78 if(hb < ESW(-(MAX_EXP/2-12))) { /* b < 2**-(MAX_EXP/2-12) */
79 if(hb <= 0) { /* subnormal b or 0 */
80 man_t manh, manl;
81 GET_LDBL_MAN(manh,manl,b);
82 if((manh|manl)==0) return a;
83 t1=0;
84 SET_HIGH_WORD(t1,ESW(MAX_EXP-2)); /* t1=2^(MAX_EXP-2) */
85 b *= t1;
86 a *= t1;
87 k -= MAX_EXP-2;
88 } else { /* scale a and b by 2^(MAX_EXP/2+88) */
89 ha += DESW(MAX_EXP/2+88);
90 hb += DESW(MAX_EXP/2+88);
91 k -= MAX_EXP/2+88;
92 SET_HIGH_WORD(a,ha);
93 SET_HIGH_WORD(b,hb);
96 /* medium size a and b */
97 w = a-b;
98 if (w>b) {
99 t1 = a;
100 union IEEEl2bits uv;
101 uv.e = t1; uv.bits.manl = 0; t1 = uv.e;
102 t2 = a-t1;
103 w = sqrtl(t1*t1-(b*(-b)-t2*(a+t1)));
104 } else {
105 a = a+a;
106 y1 = b;
107 union IEEEl2bits uv;
108 uv.e = y1; uv.bits.manl = 0; y1 = uv.e;
109 y2 = b - y1;
110 t1 = a;
111 uv.e = t1; uv.bits.manl = 0; t1 = uv.e;
112 t2 = a - t1;
113 w = sqrtl(t1*y1-(w*(-w)-(t1*y2+t2*b)));
115 if(k!=0) {
116 uint32_t high;
117 t1 = 1.0;
118 GET_HIGH_WORD(high,t1);
119 SET_HIGH_WORD(t1,high+DESW(k));
120 return t1*w;
121 } else return w;