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