(root)/
glibc-2.38/
sysdeps/
ieee754/
ldbl-128ibm/
s_roundl.c
       1  /* Round to 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  /* This has been coded in assembler because GCC makes such a mess of it
      21     when it's coded in C.  */
      22  
      23  #define NO_MATH_REDIRECT
      24  #include <math.h>
      25  #include <math_private.h>
      26  #include <math_ldbl_opt.h>
      27  #include <float.h>
      28  #include <ieee754.h>
      29  
      30  double round (double) asm ("__round");
      31  
      32  
      33  long double
      34  __roundl (long double x)
      35  {
      36    double xh, xl, hi, lo;
      37  
      38    ldbl_unpack (x, &xh, &xl);
      39  
      40    /* Return Inf, Nan, +/-0 unchanged.  */
      41    if (__builtin_expect (xh != 0.0
      42  			&& __builtin_isless (__builtin_fabs (xh),
      43  					     __builtin_inf ()), 1))
      44      {
      45        hi = round (xh);
      46        if (hi != xh)
      47  	{
      48  	  /* The high part is not an integer; the low part only
      49  	     affects the result if the high part is exactly half way
      50  	     between two integers and the low part is nonzero with the
      51  	     opposite sign.  */
      52  	  if (fabs (hi - xh) == 0.5)
      53  	    {
      54  	      if (xh > 0 && xl < 0)
      55  		xh = hi - 1;
      56  	      else if (xh < 0 && xl > 0)
      57  		xh = hi + 1;
      58  	      else
      59  		xh = hi;
      60  	    }
      61  	  else
      62  	    xh = hi;
      63  	  xl = 0;
      64  	}
      65        else
      66  	{
      67  	  /* The high part is a nonzero integer.  */
      68  	  lo = round (xl);
      69  	  if (fabs (lo - xl) == 0.5)
      70  	    {
      71  	      if (xh > 0 && xl < 0)
      72  		xl = lo + 1;
      73  	      else if (xh < 0 && lo > 0)
      74  		xl = lo - 1;
      75  	      else
      76  		xl = lo;
      77  	    }
      78  	  else
      79  	    xl = lo;
      80  	  xh = hi;
      81  	  ldbl_canonicalize_int (&xh, &xl);
      82  	}
      83      }
      84    else
      85      /* Quiet signaling NaN arguments.  */
      86      xh += xh;
      87  
      88    return ldbl_pack (xh, xl);
      89  }
      90  
      91  long_double_symbol (libm, __roundl, roundl);