|
Packit Service |
bc2dc3 |
/* Round to integer generic implementation.
|
|
Packit Service |
bc2dc3 |
Copyright (C) 2019 Free Software Foundation, Inc.
|
|
Packit Service |
bc2dc3 |
This file is part of the GNU C Library
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
bc2dc3 |
modify it under the terms of the GNU Library General Public License as
|
|
Packit Service |
bc2dc3 |
published by the Free Software Foundation; either version 2 of the
|
|
Packit Service |
bc2dc3 |
License, or (at your option) any later version.
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
bc2dc3 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
bc2dc3 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
bc2dc3 |
Library General Public License for more details.
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
You should have received a copy of the GNU Library General Public
|
|
Packit Service |
bc2dc3 |
License along with the GNU C Library; see the file COPYING.LIB. If
|
|
Packit Service |
bc2dc3 |
not, see <http://www.gnu.org/licenses/>. */
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
#ifndef _ROUND_TO_INTEGER_H
|
|
Packit Service |
bc2dc3 |
#define _ROUND_TO_INTEGER_H
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
#include <fenv_private.h>
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
enum round_mode
|
|
Packit Service |
bc2dc3 |
{
|
|
Packit Service |
bc2dc3 |
CEIL
|
|
Packit Service |
bc2dc3 |
};
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
static inline void
|
|
Packit Service |
bc2dc3 |
set_fenv_mode (enum round_mode mode)
|
|
Packit Service |
bc2dc3 |
{
|
|
Packit Service |
bc2dc3 |
int rmode;
|
|
Packit Service |
bc2dc3 |
switch (mode)
|
|
Packit Service |
bc2dc3 |
{
|
|
Packit Service |
bc2dc3 |
case CEIL: rmode = FE_UPWARD; break;
|
|
Packit Service |
bc2dc3 |
default: rmode = FE_TONEAREST; break;
|
|
Packit Service |
bc2dc3 |
}
|
|
Packit Service |
bc2dc3 |
__fesetround_inline_nocheck (rmode);
|
|
Packit Service |
bc2dc3 |
}
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
static inline float
|
|
Packit Service |
bc2dc3 |
round_to_integer_float (enum round_mode mode, float x)
|
|
Packit Service |
bc2dc3 |
{
|
|
Packit Service |
bc2dc3 |
/* Ensure sNaN input is converted to qNaN. */
|
|
Packit Service |
bc2dc3 |
if (__glibc_unlikely (isnan (x)))
|
|
Packit Service |
bc2dc3 |
return x + x;
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
if (fabs (x) > 0x1p+23)
|
|
Packit Service |
bc2dc3 |
return x;
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
float r = x;
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
/* Save current FPU rounding mode and inexact state. */
|
|
Packit Service |
bc2dc3 |
fenv_t fe = fegetenv_register ();
|
|
Packit Service |
bc2dc3 |
set_fenv_mode (mode);
|
|
Packit Service |
bc2dc3 |
if (x > 0.0)
|
|
Packit Service |
bc2dc3 |
{
|
|
Packit Service |
bc2dc3 |
r += 0x1p+23;
|
|
Packit Service |
bc2dc3 |
r -= 0x1p+23;
|
|
Packit Service |
bc2dc3 |
r = fabs (r);
|
|
Packit Service |
bc2dc3 |
}
|
|
Packit Service |
bc2dc3 |
else if (x < 0.0)
|
|
Packit Service |
bc2dc3 |
{
|
|
Packit Service |
bc2dc3 |
r -= 0x1p+23;
|
|
Packit Service |
bc2dc3 |
r += 0x1p+23;
|
|
Packit Service |
bc2dc3 |
r = -fabs (r);
|
|
Packit Service |
bc2dc3 |
}
|
|
Packit Service |
bc2dc3 |
__builtin_mtfsf (0xff, fe);
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
return r;
|
|
Packit Service |
bc2dc3 |
}
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
static inline double
|
|
Packit Service |
bc2dc3 |
round_to_integer_double (enum round_mode mode, double x)
|
|
Packit Service |
bc2dc3 |
{
|
|
Packit Service |
bc2dc3 |
/* Ensure sNaN input is converted to qNaN. */
|
|
Packit Service |
bc2dc3 |
if (__glibc_unlikely (isnan (x)))
|
|
Packit Service |
bc2dc3 |
return x + x;
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
if (fabs (x) > 0x1p+52)
|
|
Packit Service |
bc2dc3 |
return x;
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
double r = x;
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
/* Save current FPU rounding mode and inexact state. */
|
|
Packit Service |
bc2dc3 |
fenv_t fe = fegetenv_register ();
|
|
Packit Service |
bc2dc3 |
set_fenv_mode (mode);
|
|
Packit Service |
bc2dc3 |
if (x > 0.0)
|
|
Packit Service |
bc2dc3 |
{
|
|
Packit Service |
bc2dc3 |
r += 0x1p+52;
|
|
Packit Service |
bc2dc3 |
r -= 0x1p+52;
|
|
Packit Service |
bc2dc3 |
r = fabs (r);
|
|
Packit Service |
bc2dc3 |
}
|
|
Packit Service |
bc2dc3 |
else if (x < 0.0)
|
|
Packit Service |
bc2dc3 |
{
|
|
Packit Service |
bc2dc3 |
r -= 0x1p+52;
|
|
Packit Service |
bc2dc3 |
r += 0x1p+52;
|
|
Packit Service |
bc2dc3 |
r = -fabs (r);
|
|
Packit Service |
bc2dc3 |
}
|
|
Packit Service |
bc2dc3 |
__builtin_mtfsf (0xff, fe);
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
return r;
|
|
Packit Service |
bc2dc3 |
}
|
|
Packit Service |
bc2dc3 |
|
|
Packit Service |
bc2dc3 |
#endif
|