POK
e_hypotf.c
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Fri Jan 30 14:41:34 2009
15  */
16 
17 /* e_hypotf.c -- float version of e_hypot.c.
18  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
19  */
20 
21 /*
22  * ====================================================
23  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
24  *
25  * Developed at SunPro, a Sun Microsystems, Inc. business.
26  * Permission to use, copy, modify, and distribute this
27  * software is freely granted, provided that this notice
28  * is preserved.
29  * ====================================================
30  */
31 
32 #ifdef POK_NEEDS_LIBMATH
33 
34 #include "math_private.h"
35 
36 float
37 __ieee754_hypotf(float x, float y)
38 {
39  float a=x,b=y,t1,t2,yy1,y2,w;
40  int32_t j,k,ha,hb;
41 
42  GET_FLOAT_WORD(ha,x);
43  ha &= 0x7fffffff;
44  GET_FLOAT_WORD(hb,y);
45  hb &= 0x7fffffff;
46  if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
47  SET_FLOAT_WORD(a,ha); /* a <- |a| */
48  SET_FLOAT_WORD(b,hb); /* b <- |b| */
49  if((ha-hb)>0xf000000) {return a+b;} /* x/y > 2**30 */
50  k=0;
51  if(ha > 0x58800000) { /* a>2**50 */
52  if(ha >= 0x7f800000) { /* Inf or NaN */
53  w = a+b; /* for sNaN */
54  if(ha == 0x7f800000) w = a;
55  if(hb == 0x7f800000) w = b;
56  return w;
57  }
58  /* scale a and b by 2**-60 */
59  ha -= 0x5d800000; hb -= 0x5d800000; k += 60;
60  SET_FLOAT_WORD(a,ha);
61  SET_FLOAT_WORD(b,hb);
62  }
63  if(hb < 0x26800000) { /* b < 2**-50 */
64  if(hb <= 0x007fffff) { /* subnormal b or 0 */
65  if(hb==0) return a;
66  SET_FLOAT_WORD(t1,0x3f000000); /* t1=2^126 */
67  b *= t1;
68  a *= t1;
69  k -= 126;
70  } else { /* scale a and b by 2^60 */
71  ha += 0x5d800000; /* a *= 2^60 */
72  hb += 0x5d800000; /* b *= 2^60 */
73  k -= 60;
74  SET_FLOAT_WORD(a,ha);
75  SET_FLOAT_WORD(b,hb);
76  }
77  }
78  /* medium size a and b */
79  w = a-b;
80  if (w>b) {
81  SET_FLOAT_WORD(t1,ha&0xfffff000);
82  t2 = a-t1;
83  w = __ieee754_sqrtf(t1*t1-(b*(-b)-t2*(a+t1)));
84  } else {
85  a = a+a;
86  SET_FLOAT_WORD(yy1,hb&0xfffff000);
87  y2 = b - yy1;
88  SET_FLOAT_WORD(t1,ha+0x00800000);
89  t2 = a - t1;
90  w = __ieee754_sqrtf(t1*yy1-(w*(-w)-(t1*y2+t2*b)));
91  }
92  if(k!=0) {
93  SET_FLOAT_WORD(t1,0x3f800000+(k<<23));
94  return t1*w;
95  } else return w;
96 }
97 #endif
98