POK
e_hypot.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_hypot.c 5.1 93/09/24 */
18 /*
19  * ====================================================
20  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
21  *
22  * Developed at SunPro, a Sun Microsystems, Inc. business.
23  * Permission to use, copy, modify, and distribute this
24  * software is freely granted, provided that this notice
25  * is preserved.
26  * ====================================================
27  */
28 
29 /* __ieee754_hypot(x,y)
30  *
31  * Method :
32  * If (assume round-to-nearest) z=x*x+y*y
33  * has error less than sqrt(2)/2 ulp, than
34  * sqrt(z) has error less than 1 ulp (exercise).
35  *
36  * So, compute sqrt(x*x+y*y) with some care as
37  * follows to get the error below 1 ulp:
38  *
39  * Assume x>y>0;
40  * (if possible, set rounding to round-to-nearest)
41  * 1. if x > 2y use
42  * x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y
43  * where x1 = x with lower 32 bits cleared, x2 = x-x1; else
44  * 2. if x <= 2y use
45  * t1*yy1+((x-y)*(x-y)+(t1*y2+t2*y))
46  * where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1,
47  * yy1= y with lower 32 bits chopped, y2 = y-yy1.
48  *
49  * NOTE: scaling may be necessary if some argument is too
50  * large or too tiny
51  *
52  * Special cases:
53  * hypot(x,y) is INF if x or y is +INF or -INF; else
54  * hypot(x,y) is NAN if x or y is NAN.
55  *
56  * Accuracy:
57  * hypot(x,y) returns sqrt(x^2+y^2) with error less
58  * than 1 ulps (units in the last place)
59  */
60 
61 #ifdef POK_NEEDS_LIBMATH
62 
63 #include "math_private.h"
64 
65 double
66 __ieee754_hypot(double x, double y)
67 {
68  double a=x,b=y,t1,t2,yy1,y2,w;
69  int32_t j,k,ha,hb;
70 
71  GET_HIGH_WORD(ha,x);
72  ha &= 0x7fffffff;
73  GET_HIGH_WORD(hb,y);
74  hb &= 0x7fffffff;
75  if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
76  SET_HIGH_WORD(a,ha); /* a <- |a| */
77  SET_HIGH_WORD(b,hb); /* b <- |b| */
78  if((ha-hb)>0x3c00000) {return a+b;} /* x/y > 2**60 */
79  k=0;
80  if(ha > 0x5f300000) { /* a>2**500 */
81  if(ha >= 0x7ff00000) { /* Inf or NaN */
82  uint32_t low;
83  w = a+b; /* for sNaN */
84  GET_LOW_WORD(low,a);
85  if(((ha&0xfffff)|low)==0) w = a;
86  GET_LOW_WORD(low,b);
87  if(((hb^0x7ff00000)|low)==0) w = b;
88  return w;
89  }
90  /* scale a and b by 2**-600 */
91  ha -= 0x25800000; hb -= 0x25800000; k += 600;
92  SET_HIGH_WORD(a,ha);
93  SET_HIGH_WORD(b,hb);
94  }
95  if(hb < 0x20b00000) { /* b < 2**-500 */
96  if(hb <= 0x000fffff) { /* subnormal b or 0 */
97  uint32_t low;
98  GET_LOW_WORD(low,b);
99  if((hb|low)==0) return a;
100  t1=0;
101  SET_HIGH_WORD(t1,0x7fd00000); /* t1=2^1022 */
102  b *= t1;
103  a *= t1;
104  k -= 1022;
105  } else { /* scale a and b by 2^600 */
106  ha += 0x25800000; /* a *= 2^600 */
107  hb += 0x25800000; /* b *= 2^600 */
108  k -= 600;
109  SET_HIGH_WORD(a,ha);
110  SET_HIGH_WORD(b,hb);
111  }
112  }
113  /* medium size a and b */
114  w = a-b;
115  if (w>b) {
116  t1 = 0;
117  SET_HIGH_WORD(t1,ha);
118  t2 = a-t1;
119  w = __ieee754_sqrt(t1*t1-(b*(-b)-t2*(a+t1)));
120  } else {
121  a = a+a;
122  yy1 = 0;
123  SET_HIGH_WORD(yy1,hb);
124  y2 = b - yy1;
125  t1 = 0;
126  SET_HIGH_WORD(t1,ha+0x00100000);
127  t2 = a - t1;
128  w = __ieee754_sqrt(t1*yy1-(w*(-w)-(t1*y2+t2*b)));
129  }
130  if(k!=0) {
131  uint32_t high;
132  t1 = 1.0;
133  GET_HIGH_WORD(high,t1);
134  SET_HIGH_WORD(t1,high+(k<<20));
135  return t1*w;
136  } else return w;
137 }
138 
139 #endif
140