]>
Commit | Line | Data |
---|---|---|
77a2a8b4 | 1 | /* Pythagorean addition using doubles |
f7a9f785 | 2 | Copyright (C) 2011-2016 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 PE |
17 | License along with the GNU C Library; see the file COPYING.LIB. If |
18 | not, see <http://www.gnu.org/licenses/>. */ | |
77a2a8b4 | 19 | |
1ed0291c RH |
20 | #include <math.h> |
21 | #include <math_private.h> | |
e054f494 | 22 | #include <stdint.h> |
77a2a8b4 AZ |
23 | |
24 | static const double two60 = 1.152921504606847e+18; | |
25 | static const double two500 = 3.2733906078961419e+150; | |
26 | static const double two600 = 4.149515568880993e+180; | |
27 | static const double two1022 = 4.49423283715579e+307; | |
28 | static const double twoM500 = 3.054936363499605e-151; | |
850fb039 | 29 | static const double twoM600 = 2.4099198651028841e-181; |
16e616a7 | 30 | static const double two60factor = 1.5592502418239997e+290; |
77a2a8b4 AZ |
31 | static const double pdnum = 2.225073858507201e-308; |
32 | ||
33 | /* __ieee754_hypot(x,y) | |
34 | * | |
35 | * This a FP only version without any FP->INT conversion. | |
36 | * It is similar to default C version, making appropriates | |
37 | * overflow and underflows checks as well scaling when it | |
38 | * is needed. | |
39 | */ | |
40 | ||
41 | #ifdef _ARCH_PWR7 | |
42 | /* POWER7 isinf and isnan optimization are fast. */ | |
43 | # define TEST_INF_NAN(x, y) \ | |
44 | if (isinf(x) || isinf(y)) \ | |
45 | return INFINITY; \ | |
46 | if (isnan(x) || isnan(y)) \ | |
47 | return NAN; | |
48 | # else | |
49 | /* For POWER6 and below isinf/isnan triggers LHS and PLT calls are | |
50 | * costly (especially for POWER6). */ | |
51 | # define GET_TW0_HIGH_WORD(d1,d2,i1,i2) \ | |
52 | do { \ | |
53 | ieee_double_shape_type gh_u1; \ | |
54 | ieee_double_shape_type gh_u2; \ | |
55 | gh_u1.value = (d1); \ | |
56 | gh_u2.value = (d2); \ | |
13d3b41a AZ |
57 | (i1) = gh_u1.parts.msw & 0x7fffffff; \ |
58 | (i2) = gh_u2.parts.msw & 0x7fffffff; \ | |
77a2a8b4 AZ |
59 | } while (0) |
60 | ||
61 | # define TEST_INF_NAN(x, y) \ | |
62 | do { \ | |
13d3b41a | 63 | uint32_t hx, hy; \ |
77a2a8b4 AZ |
64 | GET_TW0_HIGH_WORD(x, y, hx, hy); \ |
65 | if (hy > hx) { \ | |
66 | uint32_t ht = hx; hx = hy; hy = ht; \ | |
67 | } \ | |
68 | if (hx >= 0x7ff00000) { \ | |
69 | if (hx == 0x7ff00000 || hy == 0x7ff00000) \ | |
70 | return INFINITY; \ | |
71 | return NAN; \ | |
72 | } \ | |
73 | } while (0) | |
74 | ||
75 | #endif | |
76 | ||
77 | ||
78 | double | |
79 | __ieee754_hypot (double x, double y) | |
80 | { | |
81 | x = fabs (x); | |
82 | y = fabs (y); | |
83 | ||
84 | TEST_INF_NAN (x, y); | |
85 | ||
86 | if (y > x) | |
87 | { | |
88 | double t = x; | |
89 | x = y; | |
90 | y = t; | |
91 | } | |
16e616a7 AZ |
92 | if (y == 0.0) |
93 | return x; | |
94 | /* if y is higher enough, y * 2^60 might overflow. The tests if | |
95 | y >= 1.7976931348623157e+308/2^60 (two60factor) and uses the | |
96 | appropriate check to avoid the overflow exception generation. */ | |
97 | if (y > two60factor) | |
77a2a8b4 | 98 | { |
16e616a7 AZ |
99 | if ((x / y) > two60) |
100 | return x + y; | |
101 | } | |
102 | else | |
103 | { | |
104 | if (x > (y * two60)) | |
105 | return x + y; | |
77a2a8b4 AZ |
106 | } |
107 | if (x > two500) | |
108 | { | |
109 | x *= twoM600; | |
110 | y *= twoM600; | |
edc121be | 111 | return __ieee754_sqrt (x * x + y * y) / twoM600; |
77a2a8b4 AZ |
112 | } |
113 | if (y < twoM500) | |
114 | { | |
115 | if (y <= pdnum) | |
116 | { | |
117 | x *= two1022; | |
118 | y *= two1022; | |
f6987f5a JM |
119 | double ret = __ieee754_sqrt (x * x + y * y) / two1022; |
120 | math_check_force_underflow_nonneg (ret); | |
121 | return ret; | |
77a2a8b4 AZ |
122 | } |
123 | else | |
124 | { | |
125 | x *= two600; | |
126 | y *= two600; | |
edc121be | 127 | return __ieee754_sqrt (x * x + y * y) / two600; |
77a2a8b4 AZ |
128 | } |
129 | } | |
edc121be | 130 | return __ieee754_sqrt (x * x + y * y); |
77a2a8b4 | 131 | } |
0ac5ae23 | 132 | strong_alias (__ieee754_hypot, __hypot_finite) |