Blame sysdeps/ieee754/dbl-64/s_roundeven.c

Packit Service 82fcde
/* Round to nearest integer value, rounding halfway cases to even.
Packit Service 82fcde
   dbl-64 version.
Packit Service 82fcde
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <math.h>
Packit Service 82fcde
#include <math_private.h>
Packit Service 82fcde
#include <libm-alias-double.h>
Packit Service 82fcde
#include <stdint.h>
Packit Service 82fcde
Packit Service 82fcde
#define BIAS 0x3ff
Packit Service 82fcde
#define MANT_DIG 53
Packit Service 82fcde
#define MAX_EXP (2 * BIAS + 1)
Packit Service 82fcde
Packit Service 82fcde
double
Packit Service 82fcde
__roundeven (double x)
Packit Service 82fcde
{
Packit Service 82fcde
  uint32_t hx, lx, uhx;
Packit Service 82fcde
  EXTRACT_WORDS (hx, lx, x);
Packit Service 82fcde
  uhx = hx & 0x7fffffff;
Packit Service 82fcde
  int exponent = uhx >> (MANT_DIG - 1 - 32);
Packit Service 82fcde
  if (exponent >= BIAS + MANT_DIG - 1)
Packit Service 82fcde
    {
Packit Service 82fcde
      /* Integer, infinity or NaN.  */
Packit Service 82fcde
      if (exponent == MAX_EXP)
Packit Service 82fcde
	/* Infinity or NaN; quiet signaling NaNs.  */
Packit Service 82fcde
	return x + x;
Packit Service 82fcde
      else
Packit Service 82fcde
	return x;
Packit Service 82fcde
    }
Packit Service 82fcde
  else if (exponent >= BIAS + MANT_DIG - 32)
Packit Service 82fcde
    {
Packit Service 82fcde
      /* Not necessarily an integer; integer bit is in low word.
Packit Service 82fcde
	 Locate the bits with exponents 0 and -1.  */
Packit Service 82fcde
      int int_pos = (BIAS + MANT_DIG - 1) - exponent;
Packit Service 82fcde
      int half_pos = int_pos - 1;
Packit Service 82fcde
      uint32_t half_bit = 1U << half_pos;
Packit Service 82fcde
      uint32_t int_bit = 1U << int_pos;
Packit Service 82fcde
      if ((lx & (int_bit | (half_bit - 1))) != 0)
Packit Service 82fcde
	{
Packit Service 82fcde
	  /* Carry into the exponent works correctly.  No need to test
Packit Service 82fcde
	     whether HALF_BIT is set.  */
Packit Service 82fcde
	  lx += half_bit;
Packit Service 82fcde
	  hx += lx < half_bit;
Packit Service 82fcde
	}
Packit Service 82fcde
      lx &= ~(int_bit - 1);
Packit Service 82fcde
    }
Packit Service 82fcde
  else if (exponent == BIAS + MANT_DIG - 33)
Packit Service 82fcde
    {
Packit Service 82fcde
      /* Not necessarily an integer; integer bit is bottom of high
Packit Service 82fcde
	 word, half bit is top of low word.  */
Packit Service 82fcde
      if (((hx & 1) | (lx & 0x7fffffff)) != 0)
Packit Service 82fcde
	{
Packit Service 82fcde
	  lx += 0x80000000;
Packit Service 82fcde
	  hx += lx < 0x80000000;
Packit Service 82fcde
	}
Packit Service 82fcde
      lx = 0;
Packit Service 82fcde
    }
Packit Service 82fcde
  else if (exponent >= BIAS)
Packit Service 82fcde
    {
Packit Service 82fcde
      /* At least 1; not necessarily an integer, integer bit and half
Packit Service 82fcde
	 bit are in the high word.  Locate the bits with exponents 0
Packit Service 82fcde
	 and -1 (when the unbiased exponent is 0, the bit with
Packit Service 82fcde
	 exponent 0 is implicit, but as the bias is odd it is OK to
Packit Service 82fcde
	 take it from the low bit of the exponent).  */
Packit Service 82fcde
      int int_pos = (BIAS + MANT_DIG - 33) - exponent;
Packit Service 82fcde
      int half_pos = int_pos - 1;
Packit Service 82fcde
      uint32_t half_bit = 1U << half_pos;
Packit Service 82fcde
      uint32_t int_bit = 1U << int_pos;
Packit Service 82fcde
      if (((hx & (int_bit | (half_bit - 1))) | lx) != 0)
Packit Service 82fcde
	hx += half_bit;
Packit Service 82fcde
      hx &= ~(int_bit - 1);
Packit Service 82fcde
      lx = 0;
Packit Service 82fcde
    }
Packit Service 82fcde
  else if (exponent == BIAS - 1 && (uhx > 0x3fe00000 || lx != 0))
Packit Service 82fcde
    {
Packit Service 82fcde
      /* Interval (0.5, 1).  */
Packit Service 82fcde
      hx = (hx & 0x80000000) | 0x3ff00000;
Packit Service 82fcde
      lx = 0;
Packit Service 82fcde
    }
Packit Service 82fcde
  else
Packit Service 82fcde
    {
Packit Service 82fcde
      /* Rounds to 0.  */
Packit Service 82fcde
      hx &= 0x80000000;
Packit Service 82fcde
      lx = 0;
Packit Service 82fcde
    }
Packit Service 82fcde
  INSERT_WORDS (x, hx, lx);
Packit Service 82fcde
  return x;
Packit Service 82fcde
}
Packit Service 82fcde
hidden_def (__roundeven)
Packit Service 82fcde
libm_alias_double (__roundeven, roundeven)