Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / powerpc / fpu / e_hypot.c
blobb64d326e21a19d46d79e970d3917449272a692ad
1 /* Pythagorean addition using doubles
2 Copyright (C) 2011-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library
4 Contributed by Adhemerval Zanella <azanella@br.ibm.com>, 2011
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.
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.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, see <http://www.gnu.org/licenses/>. */
20 #include <math.h>
21 #include <math_private.h>
22 #include <stdint.h>
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;
29 static const double twoM600 = 2.4099198651028841e-181;
30 static const double two60factor = 1.5592502418239997e+290;
31 static const double pdnum = 2.225073858507201e-308;
33 /* __ieee754_hypot(x,y)
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.
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); \
57 (i1) = gh_u1.parts.msw & 0x7fffffff; \
58 (i2) = gh_u2.parts.msw & 0x7fffffff; \
59 } while (0)
61 # define TEST_INF_NAN(x, y) \
62 do { \
63 uint32_t hx, hy; \
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)
75 #endif
78 double
79 __ieee754_hypot (double x, double y)
81 x = fabs (x);
82 y = fabs (y);
84 TEST_INF_NAN (x, y);
86 if (y > x)
88 double t = x;
89 x = y;
90 y = t;
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)
99 if ((x / y) > two60)
100 return x + y;
102 else
104 if (x > (y * two60))
105 return x + y;
107 if (x > two500)
109 x *= twoM600;
110 y *= twoM600;
111 return __ieee754_sqrt (x * x + y * y) / twoM600;
113 if (y < twoM500)
115 if (y <= pdnum)
117 x *= two1022;
118 y *= two1022;
119 return __ieee754_sqrt (x * x + y * y) / two1022;
121 else
123 x *= two600;
124 y *= two600;
125 return __ieee754_sqrt (x * x + y * y) / two600;
128 return __ieee754_sqrt (x * x + y * y);
130 strong_alias (__ieee754_hypot, __hypot_finite)