amath  1.8.5
Simple command line calculator
hypot.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Project homepage:
26  * https://amath.innolan.net
27  *
28  * The original source code can be obtained from:
29  * http://www.netlib.org/fdlibm/e_hypot.c
30  *
31  * =================================================================
32  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
33  *
34  * Developed at SunSoft, a Sun Microsystems, Inc. business.
35  * Permission to use, copy, modify, and distribute this
36  * software is freely granted, provided that this notice
37  * is preserved.
38  * =================================================================
39  */
40 
41 /**
42  * @file hypot.c
43  */
44 
45 #include "prim.h"
46 
47 /**
48  * @brief hypot
49  * @details
50  * <pre>
51  * Method
52  * If (assume round-to-nearest) z=x*x+y*y
53  * has error less than sqrt(2)/2 ulp, than
54  * sqrt(z) has error less than 1 ulp (exercise).
55  *
56  * So, compute sqrt(x*x+y*y) with some care as
57  * follows to get the error below 1 ulp:
58  *
59  * Assume x>y>0;
60  * (if possible, set rounding to round-to-nearest)
61  * 1. if x > 2y use
62  * x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y
63  * where x1 = x with lower 32 bits cleared, x2 = x-x1; else
64  * 2. if x <= 2y use
65  * t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))
66  * where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1,
67  * y1= y with lower 32 bits chopped, y2 = y-y1.
68  *
69  * NOTE: scaling may be necessary if some argument is too
70  * large or too tiny
71  *
72  * Special cases:
73  * hypot(x,y) is INF if x or y is +INF or -INF; else
74  * hypot(x,y) is NAN if x or y is NAN.
75  *
76  * Accuracy:
77  * hypot(x,y) returns sqrt(x^2+y^2) with error less
78  * than 1 ulps (units in the last place)
79  * </pre>
80  */
81 double hypot(double x, double y)
82 {
83  double a = x, b = y, t1, t2, y1, y2, w;
84  uint32_t j, k, ha, hb, hx, hy;
85 
86  GET_HIGH_WORD(hx, x);
87  GET_HIGH_WORD(hy, y);
88  ha = hx & 0x7FFFFFFF; // high word of x
89  hb = hy & 0x7FFFFFFF; // high word of y
90 
91  if (hb > ha)
92  {
93  a = y;
94  b = x;
95  j = ha;
96  ha = hb;
97  hb = j;
98  }
99  else
100  {
101  a = x;
102  b = y;
103  }
104 
105  SET_HIGH_WORD(a, ha); // a <- |a|
106  SET_HIGH_WORD(b, hb); // b <- |b|
107 
108  // x/y > 2**60
109  if ((ha - hb) > 0x3C00000)
110  {
111  return a + b;
112  }
113 
114  k = 0;
115 
116  // a>2**500
117  if (ha > 0x5F300000)
118  {
119  // Inf or NaN
120  if (ha >= 0x7FF00000)
121  {
122  uint32_t la, lb;
123  w = a + b; // for sNaN
124  GET_LOW_WORD(la, a);
125  GET_LOW_WORD(lb, b);
126 
127  if (((ha & 0xFFFFF) | la) == 0)
128  {
129  w = a;
130  }
131 
132  if (((hb ^ 0x7FF00000) | lb) == 0)
133  {
134  w = b;
135  }
136 
137  return w;
138  }
139 
140  // scale a and b by 2**-600
141  ha -= 0x25800000;
142  hb -= 0x25800000;
143  k += 600;
144  SET_HIGH_WORD(a, ha);
145  SET_HIGH_WORD(b, hb);
146  }
147 
148  // b < 2**-500
149  if (hb < 0x20B00000)
150  {
151  // subnormal b or 0
152  if (hb <= 0x000FFFFF)
153  {
154  uint32_t lb;
155  GET_LOW_WORD(lb, b);
156  if ((hb | lb) == 0)
157  {
158  return a;
159  }
160 
161  t1 = 0;
162  SET_HIGH_WORD(t1, 0x7FD00000); /* t1=2^1022 */
163  b *= t1;
164  a *= t1;
165  k -= 1022;
166  }
167  else
168  { /* scale a and b by 2^600 */
169  ha += 0x25800000; /* a *= 2^600 */
170  hb += 0x25800000; /* b *= 2^600 */
171  k -= 600;
172 
173  SET_HIGH_WORD(a, ha);
174  SET_HIGH_WORD(b, hb);
175  }
176  }
177 
178  // medium size a and b
179  w = a - b;
180  if (w > b)
181  {
182  t1 = 0;
183  SET_HIGH_WORD(t1, ha);
184  t2 = a - t1;
185  w = sqrt(t1 * t1 - (b * (-b) - t2 * (a + t1)));
186  }
187  else
188  {
189  a = a + a;
190  y1 = 0;
191  SET_HIGH_WORD(y1, hb);
192  y2 = b - y1;
193  t1 = 0;
194  SET_HIGH_WORD(t1, ha + 0x00100000);
195  t2 = a - t1;
196  w = sqrt(t1 * y1 - (w * (-w) - (t1 * y2 + t2 * b)));
197  }
198 
199  if (k != 0)
200  {
201  uint32_t ht1;
202  t1 = 1.0;
203  GET_HIGH_WORD(ht1, t1);
204  SET_HIGH_WORD(t1, ht1 + (k << 20));
205  return t1 * w;
206  }
207 
208  return w;
209 }
double sqrt(double x)
Square root function.
Definition: sqrt.c:119
#define GET_HIGH_WORD(i, d)
Get the more significant 32 bit int from a double.
Definition: prim.h:167
double hypot(double x, double y)
hypot
Definition: hypot.c:81
#define GET_LOW_WORD(i, d)
Get the less significant 32 bit int from a double.
Definition: prim.h:177
#define SET_HIGH_WORD(d, v)
Set the more significant 32 bits of a double from an int.
Definition: prim.h:198