e_hypotf.cpp (2220B)
1 /* e_hypotf.c -- float version of e_hypot.c. 2 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. 3 */ 4 5 /* 6 * ==================================================== 7 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 8 * 9 * Developed at SunPro, a Sun Microsystems, Inc. business. 10 * Permission to use, copy, modify, and distribute this 11 * software is freely granted, provided that this notice 12 * is preserved. 13 * ==================================================== 14 */ 15 16 //#include <sys/cdefs.h> 17 //__FBSDID("$FreeBSD$"); 18 19 #include <math.h> 20 21 #include "math.h" 22 #include "math_private.h" 23 24 float 25 __ieee754_hypotf(float x, float y) 26 { 27 float a,b,t1,t2,y1,y2,w; 28 int32_t j,k,ha,hb; 29 30 GET_FLOAT_WORD(ha,x); 31 ha &= 0x7fffffff; 32 GET_FLOAT_WORD(hb,y); 33 hb &= 0x7fffffff; 34 if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;} 35 a = fabsf(a); 36 b = fabsf(b); 37 if((ha-hb)>0xf000000) {return a+b;} /* x/y > 2**30 */ 38 k=0; 39 if(ha > 0x58800000) { /* a>2**50 */ 40 if(ha >= 0x7f800000) { /* Inf or NaN */ 41 /* Use original arg order iff result is NaN; quieten sNaNs. */ 42 w = fabsl(x+0.0L)-fabsf(y+0); 43 if(ha == 0x7f800000) w = a; 44 if(hb == 0x7f800000) w = b; 45 return w; 46 } 47 /* scale a and b by 2**-68 */ 48 ha -= 0x22000000; hb -= 0x22000000; k += 68; 49 SET_FLOAT_WORD(a,ha); 50 SET_FLOAT_WORD(b,hb); 51 } 52 if(hb < 0x26800000) { /* b < 2**-50 */ 53 if(hb <= 0x007fffff) { /* subnormal b or 0 */ 54 if(hb==0) return a; 55 SET_FLOAT_WORD(t1,0x7e800000); /* t1=2^126 */ 56 b *= t1; 57 a *= t1; 58 k -= 126; 59 } else { /* scale a and b by 2^68 */ 60 ha += 0x22000000; /* a *= 2^68 */ 61 hb += 0x22000000; /* b *= 2^68 */ 62 k -= 68; 63 SET_FLOAT_WORD(a,ha); 64 SET_FLOAT_WORD(b,hb); 65 } 66 } 67 /* medium size a and b */ 68 w = a-b; 69 if (w>b) { 70 SET_FLOAT_WORD(t1,ha&0xfffff000); 71 t2 = a-t1; 72 w = __ieee754_sqrtf(t1*t1-(b*(-b)-t2*(a+t1))); 73 } else { 74 a = a+a; 75 SET_FLOAT_WORD(y1,hb&0xfffff000); 76 y2 = b - y1; 77 SET_FLOAT_WORD(t1,(ha+0x00800000)&0xfffff000); 78 t2 = a - t1; 79 w = __ieee754_sqrtf(t1*y1-(w*(-w)-(t1*y2+t2*b))); 80 } 81 if(k!=0) { 82 SET_FLOAT_WORD(t1,(127+k)<<23); 83 return t1*w; 84 } else return w; 85 }