2020-06-16 12:06:24 +08:00
|
|
|
//===-- Nearest integer floating-point operations ---------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_LIBC_UTILS_FPUTIL_NEAREST_INTEGER_OPERATIONS_H
|
|
|
|
#define LLVM_LIBC_UTILS_FPUTIL_NEAREST_INTEGER_OPERATIONS_H
|
|
|
|
|
2020-06-09 02:11:49 +08:00
|
|
|
#include "FPBits.h"
|
2020-06-16 12:06:24 +08:00
|
|
|
|
|
|
|
#include "utils/CPP/TypeTraits.h"
|
|
|
|
|
|
|
|
namespace __llvm_libc {
|
|
|
|
namespace fputil {
|
|
|
|
|
|
|
|
template <typename T,
|
|
|
|
cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
|
|
|
|
static inline T trunc(T x) {
|
2020-06-09 02:11:49 +08:00
|
|
|
FPBits<T> bits(x);
|
2020-06-16 12:06:24 +08:00
|
|
|
|
2020-06-09 02:11:49 +08:00
|
|
|
// If x is infinity or NaN, return it.
|
|
|
|
// If it is zero also we should return it as is, but the logic
|
|
|
|
// later in this function takes care of it. But not doing a zero
|
|
|
|
// check, we improve the run time of non-zero values.
|
|
|
|
if (bits.isInfOrNaN())
|
2020-06-16 12:06:24 +08:00
|
|
|
return x;
|
|
|
|
|
2020-06-09 02:11:49 +08:00
|
|
|
int exponent = bits.getExponent();
|
2020-06-16 12:06:24 +08:00
|
|
|
|
|
|
|
// If the exponent is greater than the most negative mantissa
|
|
|
|
// exponent, then x is already an integer.
|
2020-06-09 02:11:49 +08:00
|
|
|
if (exponent >= static_cast<int>(MantissaWidth<T>::value))
|
2020-06-16 12:06:24 +08:00
|
|
|
return x;
|
|
|
|
|
|
|
|
// If the exponent is such that abs(x) is less than 1, then return 0.
|
|
|
|
if (exponent <= -1) {
|
2020-06-09 02:11:49 +08:00
|
|
|
if (bits.sign)
|
2020-06-16 12:06:24 +08:00
|
|
|
return T(-0.0);
|
|
|
|
else
|
|
|
|
return T(0.0);
|
|
|
|
}
|
|
|
|
|
2020-06-09 02:11:49 +08:00
|
|
|
int trimSize = MantissaWidth<T>::value - exponent;
|
|
|
|
bits.mantissa = (bits.mantissa >> trimSize) << trimSize;
|
|
|
|
return bits;
|
2020-06-16 12:06:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T,
|
|
|
|
cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
|
|
|
|
static inline T ceil(T x) {
|
2020-06-26 03:09:02 +08:00
|
|
|
FPBits<T> bits(x);
|
2020-06-16 12:06:24 +08:00
|
|
|
|
|
|
|
// If x is infinity NaN or zero, return it.
|
2020-06-26 03:09:02 +08:00
|
|
|
if (bits.isInfOrNaN() || bits.isZero())
|
2020-06-16 12:06:24 +08:00
|
|
|
return x;
|
|
|
|
|
2020-06-26 03:09:02 +08:00
|
|
|
bool isNeg = bits.sign;
|
|
|
|
int exponent = bits.getExponent();
|
2020-06-16 12:06:24 +08:00
|
|
|
|
|
|
|
// If the exponent is greater than the most negative mantissa
|
|
|
|
// exponent, then x is already an integer.
|
2020-06-26 03:09:02 +08:00
|
|
|
if (exponent >= static_cast<int>(MantissaWidth<T>::value))
|
2020-06-16 12:06:24 +08:00
|
|
|
return x;
|
|
|
|
|
|
|
|
if (exponent <= -1) {
|
|
|
|
if (isNeg)
|
|
|
|
return T(-0.0);
|
|
|
|
else
|
|
|
|
return T(1.0);
|
|
|
|
}
|
|
|
|
|
2020-06-26 03:09:02 +08:00
|
|
|
uint32_t trimSize = MantissaWidth<T>::value - exponent;
|
|
|
|
bits.mantissa = (bits.mantissa >> trimSize) << trimSize;
|
|
|
|
T truncValue = T(bits);
|
|
|
|
|
2020-06-16 12:06:24 +08:00
|
|
|
// If x is already an integer, return it.
|
2020-06-26 03:09:02 +08:00
|
|
|
if (truncValue == x)
|
2020-06-16 12:06:24 +08:00
|
|
|
return x;
|
|
|
|
|
|
|
|
// If x is negative, the ceil operation is equivalent to the trunc operation.
|
|
|
|
if (isNeg)
|
|
|
|
return truncValue;
|
|
|
|
|
|
|
|
return truncValue + T(1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T,
|
|
|
|
cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
|
|
|
|
static inline T floor(T x) {
|
2020-06-26 03:09:02 +08:00
|
|
|
FPBits<T> bits(x);
|
|
|
|
if (bits.sign) {
|
2020-06-16 12:06:24 +08:00
|
|
|
return -ceil(-x);
|
|
|
|
} else {
|
|
|
|
return trunc(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T,
|
|
|
|
cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
|
|
|
|
static inline T round(T x) {
|
2020-06-26 03:09:02 +08:00
|
|
|
using UIntType = typename FPBits<T>::UIntType;
|
|
|
|
FPBits<T> bits(x);
|
2020-06-16 12:06:24 +08:00
|
|
|
|
2020-06-26 03:09:02 +08:00
|
|
|
// If x is infinity NaN or zero, return it.
|
|
|
|
if (bits.isInfOrNaN() || bits.isZero())
|
2020-06-16 12:06:24 +08:00
|
|
|
return x;
|
|
|
|
|
2020-06-26 03:09:02 +08:00
|
|
|
bool isNeg = bits.sign;
|
|
|
|
int exponent = bits.getExponent();
|
2020-06-16 12:06:24 +08:00
|
|
|
|
|
|
|
// If the exponent is greater than the most negative mantissa
|
|
|
|
// exponent, then x is already an integer.
|
2020-06-26 03:09:02 +08:00
|
|
|
if (exponent >= static_cast<int>(MantissaWidth<T>::value))
|
2020-06-16 12:06:24 +08:00
|
|
|
return x;
|
|
|
|
|
|
|
|
if (exponent == -1) {
|
|
|
|
// Absolute value of x is greater than equal to 0.5 but less than 1.
|
|
|
|
if (isNeg)
|
|
|
|
return T(-1.0);
|
|
|
|
else
|
|
|
|
return T(1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exponent <= -2) {
|
|
|
|
// Absolute value of x is less than 0.5.
|
|
|
|
if (isNeg)
|
|
|
|
return T(-0.0);
|
|
|
|
else
|
|
|
|
return T(0.0);
|
|
|
|
}
|
|
|
|
|
2020-06-26 03:09:02 +08:00
|
|
|
uint32_t trimSize = MantissaWidth<T>::value - exponent;
|
|
|
|
bool halfBitSet = bits.mantissa & (UIntType(1) << (trimSize - 1));
|
|
|
|
bits.mantissa = (bits.mantissa >> trimSize) << trimSize;
|
|
|
|
T truncValue = T(bits);
|
|
|
|
|
2020-06-16 12:06:24 +08:00
|
|
|
// If x is already an integer, return it.
|
2020-06-26 03:09:02 +08:00
|
|
|
if (truncValue == x)
|
2020-06-16 12:06:24 +08:00
|
|
|
return x;
|
|
|
|
|
2020-06-26 03:09:02 +08:00
|
|
|
if (!halfBitSet) {
|
2020-06-16 12:06:24 +08:00
|
|
|
// Franctional part is less than 0.5 so round value is the
|
|
|
|
// same as the trunc value.
|
|
|
|
return truncValue;
|
2020-06-26 03:09:02 +08:00
|
|
|
} else {
|
|
|
|
return isNeg ? truncValue - T(1.0) : truncValue + T(1.0);
|
2020-06-16 12:06:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace fputil
|
|
|
|
} // namespace __llvm_libc
|
|
|
|
|
|
|
|
#endif // LLVM_LIBC_UTILS_FPUTIL_NEAREST_INTEGER_OPERATIONS_H
|