Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / powerpc / power7 / fpu / s_logbl.c
blob251e99d5564566246f02bd8902a48814aa3880cb
1 /* logbl(). PowerPC/POWER7 version.
2 Copyright (C) 2012-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <math.h>
20 #include <math_private.h>
21 #include <math_ldbl_opt.h>
23 /* This implementation avoids FP to INT conversions by using VSX
24 bitwise instructions over FP values. */
26 static const double two1div52 = 2.220446049250313e-16; /* 1/2**52 */
27 static const double two10m1 = -1023.0; /* 2**10 -1 */
29 /* FP mask to extract the exponent. */
30 static const union {
31 unsigned long long mask;
32 double d;
33 } mask = { 0x7ff0000000000000ULL };
35 long double
36 __logbl (long double x)
38 double xh;
39 double ret;
41 if (__builtin_expect (x == 0.0L, 0))
42 /* Raise FE_DIVBYZERO and return -HUGE_VAL[LF]. */
43 return -1.0L / __builtin_fabsl (x);
45 xh = ldbl_high (x);
46 /* ret = x & 0x7ff0000000000000; */
47 asm (
48 "xxland %x0,%x1,%x2\n"
49 "fcfid %0,%0"
50 : "=f" (ret)
51 : "f" (xh), "f" (mask.d));
52 /* ret = (ret >> 52) - 1023.0; */
53 ret = (ret * two1div52) + two10m1;
54 if (__builtin_expect (ret > -two10m1, 0))
55 /* Multiplication is used to set logb (+-INF) = INF. */
56 return (xh * xh);
57 else if (__builtin_expect (ret == two10m1, 0))
59 /* POSIX specifies that denormal number is treated as
60 though it were normalized. */
61 int64_t hx;
63 EXTRACT_WORDS64 (hx, xh);
64 return (long double) (-1023 - (__builtin_clzll (hx) - 12));
66 /* Test to avoid logb_downward (0.0) == -0.0. */
67 return ret == -0.0 ? 0.0 : ret;
69 #ifndef __logbl
70 long_double_symbol (libm, __logbl, logbl);
71 #endif