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

Packit 6c4009
/* Round to integer type.  dbl-64 version.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fenv.h>
Packit 6c4009
#include <math.h>
Packit 6c4009
#include <math_private.h>
Packit 6c4009
#include <libm-alias-double.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
Packit 6c4009
#define BIAS 0x3ff
Packit 6c4009
#define MANT_DIG 53
Packit 6c4009
Packit 6c4009
#if UNSIGNED
Packit 6c4009
# define RET_TYPE uintmax_t
Packit 6c4009
#else
Packit 6c4009
# define RET_TYPE intmax_t
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#include <fromfp.h>
Packit 6c4009
Packit 6c4009
RET_TYPE
Packit 6c4009
FUNC (double x, int round, unsigned int width)
Packit 6c4009
{
Packit 6c4009
  if (width > INTMAX_WIDTH)
Packit 6c4009
    width = INTMAX_WIDTH;
Packit 6c4009
  uint64_t ix;
Packit 6c4009
  EXTRACT_WORDS64 (ix, x);
Packit 6c4009
  bool negative = (ix & 0x8000000000000000ULL) != 0;
Packit 6c4009
  if (width == 0)
Packit 6c4009
    return fromfp_domain_error (negative, width);
Packit 6c4009
  ix &= 0x7fffffffffffffffULL;
Packit 6c4009
  if (ix == 0)
Packit 6c4009
    return 0;
Packit 6c4009
  int exponent = ix >> (MANT_DIG - 1);
Packit 6c4009
  exponent -= BIAS;
Packit 6c4009
  int max_exponent = fromfp_max_exponent (negative, width);
Packit 6c4009
  if (exponent > max_exponent)
Packit 6c4009
    return fromfp_domain_error (negative, width);
Packit 6c4009
Packit 6c4009
  ix &= ((1ULL << (MANT_DIG - 1)) - 1);
Packit 6c4009
  ix |= 1ULL << (MANT_DIG - 1);
Packit 6c4009
  uintmax_t uret;
Packit 6c4009
  bool half_bit, more_bits;
Packit 6c4009
  if (exponent >= MANT_DIG - 1)
Packit 6c4009
    {
Packit 6c4009
      uret = ix;
Packit 6c4009
      uret <<= exponent - (MANT_DIG - 1);
Packit 6c4009
      half_bit = false;
Packit 6c4009
      more_bits = false;
Packit 6c4009
    }
Packit 6c4009
  else if (exponent >= -1)
Packit 6c4009
    {
Packit 6c4009
      uint64_t h = 1ULL << (MANT_DIG - 2 - exponent);
Packit 6c4009
      half_bit = (ix & h) != 0;
Packit 6c4009
      more_bits = (ix & (h - 1)) != 0;
Packit 6c4009
      uret = ix >> (MANT_DIG - 1 - exponent);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      uret = 0;
Packit 6c4009
      half_bit = false;
Packit 6c4009
      more_bits = true;
Packit 6c4009
    }
Packit 6c4009
  return fromfp_round_and_return (negative, uret, half_bit, more_bits, round,
Packit 6c4009
				  exponent, max_exponent, width);
Packit 6c4009
}