Merge branch 'vendor/MPFR'
[dragonfly.git] / lib / libm / src / e_hypotf.c
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  * $NetBSD: e_hypotf.c,v 1.9 2008/04/25 22:21:53 christos Exp $
16  */
17
18 #include <math.h>
19 #include "math_private.h"
20
21 float
22 hypotf(float x, float y)
23 {
24         float a=x,b=y,t1,t2,yy1,y2,w;
25         int32_t j,k,ha,hb;
26
27         GET_FLOAT_WORD(ha,x);
28         ha &= 0x7fffffff;
29         GET_FLOAT_WORD(hb,y);
30         hb &= 0x7fffffff;
31         if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
32         SET_FLOAT_WORD(a,ha);   /* a <- |a| */
33         SET_FLOAT_WORD(b,hb);   /* b <- |b| */
34         if((ha-hb)>0xf000000) {return a+b;} /* x/y > 2**30 */
35         k=0;
36         if(ha > 0x58800000) {   /* a>2**50 */
37            if(ha >= 0x7f800000) {       /* Inf or NaN */
38                w = a+b;                 /* for sNaN */
39                if(ha == 0x7f800000) w = a;
40                if(hb == 0x7f800000) w = b;
41                return w;
42            }
43            /* scale a and b by 2**-60 */
44            ha -= 0x5d800000; hb -= 0x5d800000;  k += 60;
45            SET_FLOAT_WORD(a,ha);
46            SET_FLOAT_WORD(b,hb);
47         }
48         if(hb < 0x26800000) {   /* b < 2**-50 */
49             if(hb <= 0x007fffff) {      /* subnormal b or 0 */
50                 if(hb==0) return a;
51                 SET_FLOAT_WORD(t1,0x3f000000);  /* t1=2^126 */
52                 b *= t1;
53                 a *= t1;
54                 k -= 126;
55             } else {            /* scale a and b by 2^60 */
56                 ha += 0x5d800000;       /* a *= 2^60 */
57                 hb += 0x5d800000;       /* b *= 2^60 */
58                 k -= 60;
59                 SET_FLOAT_WORD(a,ha);
60                 SET_FLOAT_WORD(b,hb);
61             }
62         }
63     /* medium size a and b */
64         w = a-b;
65         if (w>b) {
66             SET_FLOAT_WORD(t1,ha&0xfffff000);
67             t2 = a-t1;
68             w  = sqrtf(t1*t1-(b*(-b)-t2*(a+t1)));
69         } else {
70             a  = a+a;
71             SET_FLOAT_WORD(yy1,hb&0xfffff000);
72             y2 = b - yy1;
73             SET_FLOAT_WORD(t1,ha+0x00800000);
74             t2 = a - t1;
75             w  = sqrtf(t1*yy1-(w*(-w)-(t1*y2+t2*b)));
76         }
77         if(k!=0) {
78             SET_FLOAT_WORD(t1,0x3f800000+(k<<23));
79             return t1*w;
80         } else return w;
81 }