(root)/
glibc-2.38/
sysdeps/
ieee754/
ldbl-128ibm/
s_llroundl.c
       1  /* Round to long long int long double floating-point values.
       2     IBM extended format long double version.
       3     Copyright (C) 2006-2023 Free Software Foundation, Inc.
       4     This file is part of the GNU C Library.
       5  
       6     The GNU C Library is free software; you can redistribute it and/or
       7     modify it under the terms of the GNU Lesser General Public
       8     License as published by the Free Software Foundation; either
       9     version 2.1 of the License, or (at your option) any later version.
      10  
      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     Lesser General Public License for more details.
      15  
      16     You should have received a copy of the GNU Lesser General Public
      17     License along with the GNU C Library; if not, see
      18     <https://www.gnu.org/licenses/>.  */
      19  
      20  #include <math.h>
      21  #include <fenv.h>
      22  #include <math_private.h>
      23  #include <math_ldbl_opt.h>
      24  #include <float.h>
      25  #include <ieee754.h>
      26  
      27  long long
      28  __llroundl (long double x)
      29  {
      30    double xh, xl;
      31    long long res, hi, lo;
      32  
      33    ldbl_unpack (x, &xh, &xl);
      34  
      35    /* Limit the range of values handled by the conversion to long long.
      36       We do this because we aren't sure whether that conversion properly
      37       raises FE_INVALID.  */
      38    if (__builtin_expect
      39        ((__builtin_fabs (xh) <= -(double) (-__LONG_LONG_MAX__ - 1)), 1)
      40  #if !defined (FE_INVALID)
      41        || 1
      42  #endif
      43      )
      44      {
      45        if (__glibc_unlikely ((xh == -(double) (-__LONG_LONG_MAX__ - 1))))
      46  	{
      47  	  /* When XH is 9223372036854775808.0, converting to long long will
      48  	     overflow, resulting in an invalid operation.  However, XL might
      49  	     be negative and of sufficient magnitude that the overall long
      50  	     double is in fact in range.  Avoid raising an exception.  In any
      51  	     case we need to convert this value specially, because
      52  	     the converted value is not exactly represented as a double
      53  	     thus subtracting HI from XH suffers rounding error.  */
      54  	  hi = __LONG_LONG_MAX__;
      55  	  xh = 1.0;
      56  	}
      57        else
      58  	{
      59  	  hi = (long long) xh;
      60  	  xh -= hi;
      61  	}
      62        ldbl_canonicalize (&xh, &xl);
      63  
      64        lo = (long long) xh;
      65  
      66        /* Peg at max/min values, assuming that the above conversions do so.
      67           Strictly speaking, we can return anything for values that overflow,
      68           but this is more useful.  */
      69        if (__glibc_unlikely (__builtin_add_overflow (hi, lo, &res)))
      70  	goto overflow;
      71  
      72        xh -= lo;
      73        ldbl_canonicalize (&xh, &xl);
      74  
      75        if (xh > 0.5)
      76  	{
      77  	  if (__glibc_unlikely (__builtin_add_overflow (res, 1, &res)))
      78  	    goto overflow;
      79  	}
      80        else if (xh == 0.5)
      81  	{
      82  	  if (xl > 0.0 || (xl == 0.0 && res >= 0))
      83  	    if (__glibc_unlikely (__builtin_add_overflow (res, 1, &res)))
      84  	      goto overflow;
      85  	}
      86        else if (-xh > 0.5)
      87  	{
      88  	  if (__glibc_unlikely (__builtin_add_overflow (res, -1, &res)))
      89  	    goto overflow;
      90  	}
      91        else if (-xh == 0.5)
      92  	{
      93  	  if (xl < 0.0 || (xl == 0.0 && res <= 0))
      94  	    if (__glibc_unlikely (__builtin_add_overflow (res, -1, &res)))
      95  	      goto overflow;
      96  	}
      97  
      98        return res;
      99      }
     100    else
     101      {
     102        if (xh > 0.0)
     103  	hi = __LONG_LONG_MAX__;
     104        else if (xh < 0.0)
     105  	hi = -__LONG_LONG_MAX__ - 1;
     106        else
     107  	/* Nan */
     108  	hi = 0;
     109      }
     110  
     111  overflow:
     112  #ifdef FE_INVALID
     113    feraiseexcept (FE_INVALID);
     114  #endif
     115    return hi;
     116  }
     117  
     118  long_double_symbol (libm, __llroundl, llroundl);