(root)/
glibc-2.38/
sysdeps/
powerpc/
fpu/
s_logbl.c
       1  /* Get exponent of a floating-point value.  PowerPC version.
       2     Copyright (C) 2012-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       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.
       9  
      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.
      14  
      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     <https://www.gnu.org/licenses/>.  */
      18  
      19  /* ISA 2.07 provides fast GPR to FP instruction (mfvsr{d,wz}) which make
      20     generic implementation faster.  */
      21  #if defined(_ARCH_PWR8) || !defined(_ARCH_PWR7)
      22  # include <./sysdeps/ieee754/ldbl-128ibm/s_logbl.c>
      23  #else
      24  # include <math.h>
      25  # include <math_private.h>
      26  # include <math_ldbl_opt.h>
      27  
      28  /* This implementation avoids FP to INT conversions by using VSX
      29     bitwise instructions over FP values.  */
      30  long double
      31  __logbl (long double x)
      32  {
      33    double xh, xl;
      34    double ret;
      35    int64_t hx;
      36  
      37    if (__glibc_unlikely (x == 0.0))
      38      /* Raise FE_DIVBYZERO and return -HUGE_VAL[LF].  */
      39      return -1.0L / __builtin_fabsl (x);
      40  
      41    ldbl_unpack (x, &xh, &xl);
      42    EXTRACT_WORDS64 (hx, xh);
      43  
      44    /* Mask to extract the exponent.  */
      45    asm ("xxland %x0,%x1,%x2\n"
      46         "fcfid  %0,%0"
      47         : "=d" (ret)
      48         : "d" (xh), "d" (0x7ff0000000000000ULL));
      49    ret = (ret * 0x1p-52) - 1023.0;
      50    if (ret > 1023.0)
      51      /* Multiplication is used to set logb (+-INF) = INF.  */
      52      return (xh * xh);
      53    else if (ret == -1023.0)
      54      {
      55        /* POSIX specifies that denormal number is treated as
      56           though it were normalized.  */
      57        return (long double) (- (__builtin_clzll (hx & 0x7fffffffffffffffLL) \
      58  			       - 12) - 1023);
      59      }
      60    else if ((hx & 0x000fffffffffffffLL) == 0)
      61      {
      62        /* If the high part is a power of 2, and the low part is nonzero
      63  	 with the opposite sign, the low part affects the
      64  	 exponent.  */
      65        int64_t lx, rhx;
      66        EXTRACT_WORDS64 (lx, xl);
      67        rhx = (hx & 0x7ff0000000000000LL) >> 52;
      68        if ((hx ^ lx) < 0 && (lx & 0x7fffffffffffffffLL) != 0)
      69  	rhx--;
      70        return (long double) (rhx - 1023);
      71      }
      72    /* Test to avoid logb_downward (0.0) == -0.0.  */
      73    return ret == -0.0 ? 0.0 : ret;
      74  }
      75  # ifndef __logbl
      76  long_double_symbol (libm, __logbl, logbl);
      77  # endif
      78  #endif