xref: /netbsd-src/lib/libm/src/e_hypotf.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
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 #if defined(LIBM_SCCS) && !defined(lint)
18 __RCSID("$NetBSD: e_hypotf.c,v 1.7 1999/07/02 15:37:39 simonb Exp $");
19 #endif
20 
21 #include "math.h"
22 #include "math_private.h"
23 
24 #ifdef __STDC__
25 	float __ieee754_hypotf(float x, float y)
26 #else
27 	float __ieee754_hypot(x,y)
28 	float x, y;
29 #endif
30 {
31 	float a=x,b=y,t1,t2,y1,y2,w;
32 	int32_t j,k,ha,hb;
33 
34 	GET_FLOAT_WORD(ha,x);
35 	ha &= 0x7fffffff;
36 	GET_FLOAT_WORD(hb,y);
37 	hb &= 0x7fffffff;
38 	if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
39 	SET_FLOAT_WORD(a,ha);	/* a <- |a| */
40 	SET_FLOAT_WORD(b,hb);	/* b <- |b| */
41 	if((ha-hb)>0xf000000) {return a+b;} /* x/y > 2**30 */
42 	k=0;
43 	if(ha > 0x58800000) {	/* a>2**50 */
44 	   if(ha >= 0x7f800000) {	/* Inf or NaN */
45 	       w = a+b;			/* for sNaN */
46 	       if(ha == 0x7f800000) w = a;
47 	       if(hb == 0x7f800000) w = b;
48 	       return w;
49 	   }
50 	   /* scale a and b by 2**-60 */
51 	   ha -= 0x5d800000; hb -= 0x5d800000;	k += 60;
52 	   SET_FLOAT_WORD(a,ha);
53 	   SET_FLOAT_WORD(b,hb);
54 	}
55 	if(hb < 0x26800000) {	/* b < 2**-50 */
56 	    if(hb <= 0x007fffff) {	/* subnormal b or 0 */
57 	        if(hb==0) return a;
58 		SET_FLOAT_WORD(t1,0x3f000000);	/* t1=2^126 */
59 		b *= t1;
60 		a *= t1;
61 		k -= 126;
62 	    } else {		/* scale a and b by 2^60 */
63 	        ha += 0x5d800000; 	/* a *= 2^60 */
64 		hb += 0x5d800000;	/* b *= 2^60 */
65 		k -= 60;
66 		SET_FLOAT_WORD(a,ha);
67 		SET_FLOAT_WORD(b,hb);
68 	    }
69 	}
70     /* medium size a and b */
71 	w = a-b;
72 	if (w>b) {
73 	    SET_FLOAT_WORD(t1,ha&0xfffff000);
74 	    t2 = a-t1;
75 	    w  = __ieee754_sqrtf(t1*t1-(b*(-b)-t2*(a+t1)));
76 	} else {
77 	    a  = a+a;
78 	    SET_FLOAT_WORD(y1,hb&0xfffff000);
79 	    y2 = b - y1;
80 	    SET_FLOAT_WORD(t1,ha+0x00800000);
81 	    t2 = a - t1;
82 	    w  = __ieee754_sqrtf(t1*y1-(w*(-w)-(t1*y2+t2*b)));
83 	}
84 	if(k!=0) {
85 	    SET_FLOAT_WORD(t1,0x3f800000+(k<<23));
86 	    return t1*w;
87 	} else return w;
88 }
89