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