]>
Commit | Line | Data |
---|---|---|
77a2a8b4 | 1 | /* Pythagorean addition using doubles |
04277e02 | 2 | Copyright (C) 2011-2019 Free Software Foundation, Inc. |
77a2a8b4 AZ |
3 | This file is part of the GNU C Library |
4 | Contributed by Adhemerval Zanella <azanella@br.ibm.com>, 2011 | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
7 | modify it under the terms of the GNU Library General Public License as | |
8 | published by the Free Software Foundation; either version 2 of the | |
9 | License, or (at your option) any later version. | |
10 | ||
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Library General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Library General Public | |
59ba27a6 | 17 | License along with the GNU C Library; see the file COPYING.LIB. If |
5a82c748 | 18 | not, see <https://www.gnu.org/licenses/>. */ |
77a2a8b4 | 19 | |
1ed0291c RH |
20 | #include <math.h> |
21 | #include <math_private.h> | |
8f5b00d3 | 22 | #include <math-underflow.h> |
e054f494 | 23 | #include <stdint.h> |
77a2a8b4 | 24 | |
77a2a8b4 AZ |
25 | /* __ieee754_hypot(x,y) |
26 | * | |
27 | * This a FP only version without any FP->INT conversion. | |
28 | * It is similar to default C version, making appropriates | |
29 | * overflow and underflows checks as well scaling when it | |
30 | * is needed. | |
31 | */ | |
32 | ||
77a2a8b4 AZ |
33 | double |
34 | __ieee754_hypot (double x, double y) | |
35 | { | |
69461d98 AZ |
36 | if ((isinf (x) || isinf (y)) |
37 | && !issignaling (x) && !issignaling (y)) | |
38 | return INFINITY; | |
39 | if (isnan (x) || isnan (y)) | |
40 | return x + y; | |
41 | ||
77a2a8b4 AZ |
42 | x = fabs (x); |
43 | y = fabs (y); | |
44 | ||
77a2a8b4 AZ |
45 | if (y > x) |
46 | { | |
47 | double t = x; | |
48 | x = y; | |
49 | y = t; | |
50 | } | |
16e616a7 AZ |
51 | if (y == 0.0) |
52 | return x; | |
69461d98 | 53 | |
16e616a7 AZ |
54 | /* if y is higher enough, y * 2^60 might overflow. The tests if |
55 | y >= 1.7976931348623157e+308/2^60 (two60factor) and uses the | |
56 | appropriate check to avoid the overflow exception generation. */ | |
69461d98 AZ |
57 | if (y <= 0x1.fffffffffffffp+963 && x > (y * 0x1p+60)) |
58 | return x + y; | |
59 | ||
60 | if (x > 0x1p+500) | |
77a2a8b4 | 61 | { |
69461d98 AZ |
62 | x *= 0x1p-600; |
63 | y *= 0x1p-600; | |
64 | return sqrt (x * x + y * y) / 0x1p-600; | |
77a2a8b4 | 65 | } |
69461d98 | 66 | if (y < 0x1p-500) |
77a2a8b4 | 67 | { |
69461d98 | 68 | if (y <= 0x0.fffffffffffffp-1022) |
77a2a8b4 | 69 | { |
69461d98 AZ |
70 | x *= 0x1p+1022; |
71 | y *= 0x1p+1022; | |
72 | double ret = sqrt (x * x + y * y) / 0x1p+1022; | |
f6987f5a JM |
73 | math_check_force_underflow_nonneg (ret); |
74 | return ret; | |
77a2a8b4 AZ |
75 | } |
76 | else | |
77 | { | |
69461d98 AZ |
78 | x *= 0x1p+600; |
79 | y *= 0x1p+600; | |
80 | return sqrt (x * x + y * y) / 0x1p+600; | |
77a2a8b4 AZ |
81 | } |
82 | } | |
f67a8147 | 83 | return sqrt (x * x + y * y); |
77a2a8b4 | 84 | } |
0ac5ae23 | 85 | strong_alias (__ieee754_hypot, __hypot_finite) |