(root)/
glibc-2.38/
sysdeps/
powerpc/
fpu/
s_logb.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/dbl-64/s_logb.c>
      23  #else
      24  # include <math.h>
      25  # include <math_private.h>
      26  # include <math_ldbl_opt.h>
      27  # include <libm-alias-double.h>
      28  
      29  /* This implementation avoids FP to INT conversions by using VSX
      30     bitwise instructions over FP values.  */
      31  double
      32  __logb (double x)
      33  {
      34    double ret;
      35  
      36    if (__glibc_unlikely (x == 0.0))
      37      /* Raise FE_DIVBYZERO and return -HUGE_VAL[LF].  */
      38      return -1.0 / fabs (x);
      39  
      40    /* Mask to extract the exponent.  */
      41    asm ("xxland %x0,%x1,%x2\n"
      42         "fcfid  %0,%0"
      43         : "=d" (ret)
      44         : "d" (x), "d" (0x7ff0000000000000ULL));
      45    ret = (ret * 0x1p-52) - 1023.0;
      46    if (ret > 1023.0)
      47      /* Multiplication is used to set logb (+-INF) = INF.  */
      48      return (x * x);
      49    else if (ret == -1023.0)
      50      {
      51        /* POSIX specifies that denormal numbers are treated as
      52           though they were normalized.  */
      53        int64_t ix;
      54        EXTRACT_WORDS64 (ix, x);
      55        ix &= UINT64_C (0x7fffffffffffffff);
      56        return (double) (-1023 - (__builtin_clzll (ix) - 12));
      57      }
      58    /* Test to avoid logb_downward (0.0) == -0.0.  */
      59    return ret == -0.0 ? 0.0 : ret;
      60  }
      61  # ifndef __logb
      62  libm_alias_double (__logb, logb)
      63  # endif
      64  #endif