|
Packit |
6c4009 |
/* Round to nearest integer value, rounding halfway cases to even.
|
|
Packit |
6c4009 |
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 <math.h>
|
|
Packit |
6c4009 |
#include <math_private.h>
|
|
Packit |
6c4009 |
#include <libm-alias-ldouble.h>
|
|
Packit |
6c4009 |
#include <stdint.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define BIAS 0x3fff
|
|
Packit |
6c4009 |
#define MANT_DIG 113
|
|
Packit |
6c4009 |
#define MAX_EXP (2 * BIAS + 1)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
_Float128
|
|
Packit |
6c4009 |
__roundevenl (_Float128 x)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
uint64_t hx, lx, uhx;
|
|
Packit |
6c4009 |
GET_LDOUBLE_WORDS64 (hx, lx, x);
|
|
Packit |
6c4009 |
uhx = hx & 0x7fffffffffffffffULL;
|
|
Packit |
6c4009 |
int exponent = uhx >> (MANT_DIG - 1 - 64);
|
|
Packit |
6c4009 |
if (exponent >= BIAS + MANT_DIG - 1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Integer, infinity or NaN. */
|
|
Packit |
6c4009 |
if (exponent == MAX_EXP)
|
|
Packit |
6c4009 |
/* Infinity or NaN; quiet signaling NaNs. */
|
|
Packit |
6c4009 |
return x + x;
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
return x;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (exponent >= BIAS + MANT_DIG - 64)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Not necessarily an integer; integer bit is in low word.
|
|
Packit |
6c4009 |
Locate the bits with exponents 0 and -1. */
|
|
Packit |
6c4009 |
int int_pos = (BIAS + MANT_DIG - 1) - exponent;
|
|
Packit |
6c4009 |
int half_pos = int_pos - 1;
|
|
Packit |
6c4009 |
uint64_t half_bit = 1ULL << half_pos;
|
|
Packit |
6c4009 |
uint64_t int_bit = 1ULL << int_pos;
|
|
Packit |
6c4009 |
if ((lx & (int_bit | (half_bit - 1))) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Carry into the exponent works correctly. No need to test
|
|
Packit |
6c4009 |
whether HALF_BIT is set. */
|
|
Packit |
6c4009 |
lx += half_bit;
|
|
Packit |
6c4009 |
hx += lx < half_bit;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
lx &= ~(int_bit - 1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (exponent == BIAS + MANT_DIG - 65)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Not necessarily an integer; integer bit is bottom of high
|
|
Packit |
6c4009 |
word, half bit is top of low word. */
|
|
Packit |
6c4009 |
if (((hx & 1) | (lx & 0x7fffffffffffffffULL)) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
lx += 0x8000000000000000ULL;
|
|
Packit |
6c4009 |
hx += lx < 0x8000000000000000ULL;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
lx = 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (exponent >= BIAS)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* At least 1; not necessarily an integer, integer bit and half
|
|
Packit |
6c4009 |
bit are in the high word. Locate the bits with exponents 0
|
|
Packit |
6c4009 |
and -1 (when the unbiased exponent is 0, the bit with
|
|
Packit |
6c4009 |
exponent 0 is implicit, but as the bias is odd it is OK to
|
|
Packit |
6c4009 |
take it from the low bit of the exponent). */
|
|
Packit |
6c4009 |
int int_pos = (BIAS + MANT_DIG - 65) - exponent;
|
|
Packit |
6c4009 |
int half_pos = int_pos - 1;
|
|
Packit |
6c4009 |
uint64_t half_bit = 1ULL << half_pos;
|
|
Packit |
6c4009 |
uint64_t int_bit = 1ULL << int_pos;
|
|
Packit |
6c4009 |
if (((hx & (int_bit | (half_bit - 1))) | lx) != 0)
|
|
Packit |
6c4009 |
hx += half_bit;
|
|
Packit |
6c4009 |
hx &= ~(int_bit - 1);
|
|
Packit |
6c4009 |
lx = 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (exponent == BIAS - 1 && (uhx > 0x3ffe000000000000ULL || lx != 0))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Interval (0.5, 1). */
|
|
Packit |
6c4009 |
hx = (hx & 0x8000000000000000ULL) | 0x3fff000000000000ULL;
|
|
Packit |
6c4009 |
lx = 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Rounds to 0. */
|
|
Packit |
6c4009 |
hx &= 0x8000000000000000ULL;
|
|
Packit |
6c4009 |
lx = 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
SET_LDOUBLE_WORDS64 (x, hx, lx);
|
|
Packit |
6c4009 |
return x;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
libm_alias_ldouble (__roundeven, roundeven)
|