|
Packit |
6c4009 |
/* Round to integer type. ldbl-128 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-ldouble.h>
|
|
Packit |
6c4009 |
#include <stdbool.h>
|
|
Packit |
6c4009 |
#include <stdint.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define BIAS 0x3fff
|
|
Packit |
6c4009 |
#define MANT_DIG 113
|
|
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 (_Float128 x, int round, unsigned int width)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (width > INTMAX_WIDTH)
|
|
Packit |
6c4009 |
width = INTMAX_WIDTH;
|
|
Packit |
6c4009 |
uint64_t hx, lx;
|
|
Packit |
6c4009 |
GET_LDOUBLE_WORDS64 (hx, lx, x);
|
|
Packit |
6c4009 |
bool negative = (hx & 0x8000000000000000ULL) != 0;
|
|
Packit |
6c4009 |
if (width == 0)
|
|
Packit |
6c4009 |
return fromfp_domain_error (negative, width);
|
|
Packit |
6c4009 |
hx &= 0x7fffffffffffffffULL;
|
|
Packit |
6c4009 |
if ((hx | lx) == 0)
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
int exponent = hx >> (MANT_DIG - 1 - 64);
|
|
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 |
hx &= ((1ULL << (MANT_DIG - 1 - 64)) - 1);
|
|
Packit |
6c4009 |
hx |= 1ULL << (MANT_DIG - 1 - 64);
|
|
Packit |
6c4009 |
uintmax_t uret;
|
|
Packit |
6c4009 |
bool half_bit, more_bits;
|
|
Packit |
6c4009 |
/* The exponent is at most 63, so we are shifting right by at least
|
|
Packit |
6c4009 |
49 bits. */
|
|
Packit |
6c4009 |
if (exponent >= -1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int shift = MANT_DIG - 1 - exponent;
|
|
Packit |
6c4009 |
if (shift <= 64)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
uint64_t h = 1ULL << (shift - 1);
|
|
Packit |
6c4009 |
half_bit = (lx & h) != 0;
|
|
Packit |
6c4009 |
more_bits = (lx & (h - 1)) != 0;
|
|
Packit |
6c4009 |
uret = hx << (64 - shift);
|
|
Packit |
6c4009 |
if (shift != 64)
|
|
Packit |
6c4009 |
uret |= lx >> shift;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
uint64_t h = 1ULL << (shift - 1 - 64);
|
|
Packit |
6c4009 |
half_bit = (hx & h) != 0;
|
|
Packit |
6c4009 |
more_bits = ((hx & (h - 1)) | lx) != 0;
|
|
Packit |
6c4009 |
uret = hx >> (shift - 64);
|
|
Packit |
6c4009 |
}
|
|
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 |
}
|