Fix a wrong constant in powerpc hypot implementation
[glibc.git] / sysdeps / powerpc / fpu / e_hypot.c
blob3731c58a30fd63f3e86a85639c0383e16836cd17
1 /* Pythagorean addition using doubles
2 Copyright (C) 2011 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 not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "math.h"
22 #include "math_private.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 pdnum = 2.225073858507201e-308;
32 /* __ieee754_hypot(x,y)
34 * This a FP only version without any FP->INT conversion.
35 * It is similar to default C version, making appropriates
36 * overflow and underflows checks as well scaling when it
37 * is needed.
40 #ifdef _ARCH_PWR7
41 /* POWER7 isinf and isnan optimization are fast. */
42 # define TEST_INF_NAN(x, y) \
43 if (isinf(x) || isinf(y)) \
44 return INFINITY; \
45 if (isnan(x) || isnan(y)) \
46 return NAN;
47 # else
48 /* For POWER6 and below isinf/isnan triggers LHS and PLT calls are
49 * costly (especially for POWER6). */
50 # define GET_TW0_HIGH_WORD(d1,d2,i1,i2) \
51 do { \
52 ieee_double_shape_type gh_u1; \
53 ieee_double_shape_type gh_u2; \
54 gh_u1.value = (d1); \
55 gh_u2.value = (d2); \
56 (i1) = gh_u1.parts.msw; \
57 (i2) = gh_u2.parts.msw; \
58 } while (0)
60 # define TEST_INF_NAN(x, y) \
61 do { \
62 int32_t hx, hy; \
63 GET_TW0_HIGH_WORD(x, y, hx, hy); \
64 if (hy > hx) { \
65 uint32_t ht = hx; hx = hy; hy = ht; \
66 } \
67 if (hx >= 0x7ff00000) { \
68 if (hx == 0x7ff00000 || hy == 0x7ff00000) \
69 return INFINITY; \
70 return NAN; \
71 } \
72 } while (0)
74 #endif
77 double
78 __ieee754_hypot (double x, double y)
80 x = fabs (x);
81 y = fabs (y);
83 TEST_INF_NAN (x, y);
85 if (y > x)
87 double t = x;
88 x = y;
89 y = t;
91 if (y == 0.0 || (x / y) > two60)
93 return x + y;
95 if (x > two500)
97 x *= twoM600;
98 y *= twoM600;
99 return __ieee754_sqrt (x * x + y * y) / twoM600;
101 if (y < twoM500)
103 if (y <= pdnum)
105 x *= two1022;
106 y *= two1022;
107 return __ieee754_sqrt (x * x + y * y) / two1022;
109 else
111 x *= two600;
112 y *= two600;
113 return __ieee754_sqrt (x * x + y * y) / two600;
116 return __ieee754_sqrt (x * x + y * y);
118 strong_alias (__ieee754_hypot, __hypot_finite)