72 changes: 72 additions & 0 deletions libc/test/src/math/ceill_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//===-- Unittests for ceill -----------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "include/math.h"
#include "src/math/ceill.h"
#include "utils/FPUtil/FPBits.h"
#include "utils/MPFRWrapper/MPFRUtils.h"
#include "utils/UnitTest/Test.h"

using FPBits = __llvm_libc::fputil::FPBits<long double>;

namespace mpfr = __llvm_libc::testing::mpfr;

// Zero tolerance; As in, exact match with MPFR result.
static constexpr mpfr::Tolerance tolerance{mpfr::Tolerance::doublePrecision, 0,
0};
TEST(CeillTest, SpecialNumbers) {
ASSERT_TRUE(FPBits::zero() == __llvm_libc::ceill(FPBits::zero()));
ASSERT_TRUE(FPBits::negZero() == __llvm_libc::ceill(FPBits::negZero()));

ASSERT_TRUE(FPBits::inf() == __llvm_libc::ceill(FPBits::inf()));
ASSERT_TRUE(FPBits::negInf() == __llvm_libc::ceill(FPBits::negInf()));

long double nan = FPBits::buildNaN(1);
ASSERT_NE(isnan(nan), 0);
ASSERT_NE(isnan(__llvm_libc::ceill(nan)), 0);
}

TEST(CeillTest, RoundedNumbers) {
ASSERT_TRUE(FPBits(1.0l) == __llvm_libc::ceill(1.0l));
ASSERT_TRUE(FPBits(-1.0l) == __llvm_libc::ceill(-1.0l));
ASSERT_TRUE(FPBits(10.0l) == __llvm_libc::ceill(10.0l));
ASSERT_TRUE(FPBits(-10.0l) == __llvm_libc::ceill(-10.0l));
ASSERT_TRUE(FPBits(1234.0l) == __llvm_libc::ceill(1234.0l));
ASSERT_TRUE(FPBits(-1234.0l) == __llvm_libc::ceill(-1234.0l));
}

TEST(CeillTest, Fractions) {
ASSERT_TRUE(FPBits(2.0l) == __llvm_libc::ceill(1.3l));
ASSERT_TRUE(FPBits(-1.0l) == __llvm_libc::ceill(-1.3l));
ASSERT_TRUE(FPBits(2.0l) == __llvm_libc::ceill(1.5l));
ASSERT_TRUE(FPBits(-1.0l) == __llvm_libc::ceill(-1.5l));
ASSERT_TRUE(FPBits(2.0l) == __llvm_libc::ceill(1.75l));
ASSERT_TRUE(FPBits(-1.0l) == __llvm_libc::ceill(-1.75l));
ASSERT_TRUE(FPBits(11.0l) == __llvm_libc::ceill(10.32l));
ASSERT_TRUE(FPBits(-10.0l) == __llvm_libc::ceill(-10.32l));
ASSERT_TRUE(FPBits(11.0l) == __llvm_libc::ceill(10.65l));
ASSERT_TRUE(FPBits(-10.0l) == __llvm_libc::ceill(-10.65l));
ASSERT_TRUE(FPBits(1235.0l) == __llvm_libc::ceill(1234.18l));
ASSERT_TRUE(FPBits(-1234.0l) == __llvm_libc::ceill(-1234.18l));
ASSERT_TRUE(FPBits(1235.0l) == __llvm_libc::ceill(1234.96l));
ASSERT_TRUE(FPBits(-1234.0l) == __llvm_libc::ceill(-1234.96l));
}

TEST(CeillTest, InLongDoubleRange) {
using UIntType = FPBits::UIntType;
constexpr UIntType count = 10000000;
constexpr UIntType step = UIntType(-1) / count;
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
long double x = FPBits(v);
if (isnan(x) || isinf(x))
continue;

ASSERT_MPFR_MATCH(mpfr::Operation::Ceil, x, __llvm_libc::ceill(x),
tolerance);
}
}
72 changes: 72 additions & 0 deletions libc/test/src/math/floorl_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//===-- Unittests for floorl ----------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "include/math.h"
#include "src/math/floorl.h"
#include "utils/FPUtil/FPBits.h"
#include "utils/MPFRWrapper/MPFRUtils.h"
#include "utils/UnitTest/Test.h"

using FPBits = __llvm_libc::fputil::FPBits<long double>;

namespace mpfr = __llvm_libc::testing::mpfr;

// Zero tolerance; As in, exact match with MPFR result.
static constexpr mpfr::Tolerance tolerance{mpfr::Tolerance::doublePrecision, 0,
0};
TEST(FloorlTest, SpecialNumbers) {
ASSERT_TRUE(FPBits::zero() == __llvm_libc::floorl(FPBits::zero()));
ASSERT_TRUE(FPBits::negZero() == __llvm_libc::floorl(FPBits::negZero()));

ASSERT_TRUE(FPBits::inf() == __llvm_libc::floorl(FPBits::inf()));
ASSERT_TRUE(FPBits::negInf() == __llvm_libc::floorl(FPBits::negInf()));

long double nan = FPBits::buildNaN(1);
ASSERT_NE(isnan(nan), 0);
ASSERT_NE(isnan(__llvm_libc::floorl(nan)), 0);
}

TEST(FloorlTest, RoundedNumbers) {
ASSERT_TRUE(FPBits(1.0l) == __llvm_libc::floorl(1.0l));
ASSERT_TRUE(FPBits(-1.0l) == __llvm_libc::floorl(-1.0l));
ASSERT_TRUE(FPBits(10.0l) == __llvm_libc::floorl(10.0l));
ASSERT_TRUE(FPBits(-10.0l) == __llvm_libc::floorl(-10.0l));
ASSERT_TRUE(FPBits(1234.0l) == __llvm_libc::floorl(1234.0l));
ASSERT_TRUE(FPBits(-1234.0l) == __llvm_libc::floorl(-1234.0l));
}

TEST(FloorlTest, Fractions) {
ASSERT_TRUE(FPBits(1.0l) == __llvm_libc::floorl(1.3l));
ASSERT_TRUE(FPBits(-2.0l) == __llvm_libc::floorl(-1.3l));
ASSERT_TRUE(FPBits(1.0l) == __llvm_libc::floorl(1.5l));
ASSERT_TRUE(FPBits(-2.0l) == __llvm_libc::floorl(-1.5l));
ASSERT_TRUE(FPBits(1.0l) == __llvm_libc::floorl(1.75l));
ASSERT_TRUE(FPBits(-2.0l) == __llvm_libc::floorl(-1.75l));
ASSERT_TRUE(FPBits(10.0l) == __llvm_libc::floorl(10.32l));
ASSERT_TRUE(FPBits(-11.0l) == __llvm_libc::floorl(-10.32l));
ASSERT_TRUE(FPBits(10.0l) == __llvm_libc::floorl(10.65l));
ASSERT_TRUE(FPBits(-11.0l) == __llvm_libc::floorl(-10.65l));
ASSERT_TRUE(FPBits(1234.0l) == __llvm_libc::floorl(1234.18l));
ASSERT_TRUE(FPBits(-1235.0l) == __llvm_libc::floorl(-1234.18l));
ASSERT_TRUE(FPBits(1234.0l) == __llvm_libc::floorl(1234.96l));
ASSERT_TRUE(FPBits(-1235.0l) == __llvm_libc::floorl(-1234.96l));
}

TEST(FloorlTest, InLongDoubleRange) {
using UIntType = FPBits::UIntType;
constexpr UIntType count = 10000000;
constexpr UIntType step = UIntType(-1) / count;
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
long double x = FPBits(v);
if (isnan(x) || isinf(x))
continue;

ASSERT_MPFR_MATCH(mpfr::Operation::Floor, x, __llvm_libc::floorl(x),
tolerance);
}
}
72 changes: 72 additions & 0 deletions libc/test/src/math/roundl_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//===-- Unittests for roundl ----------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "include/math.h"
#include "src/math/roundl.h"
#include "utils/FPUtil/FPBits.h"
#include "utils/MPFRWrapper/MPFRUtils.h"
#include "utils/UnitTest/Test.h"

using FPBits = __llvm_libc::fputil::FPBits<long double>;

namespace mpfr = __llvm_libc::testing::mpfr;

// Zero tolerance; As in, exact match with MPFR result.
static constexpr mpfr::Tolerance tolerance{mpfr::Tolerance::floatPrecision, 0,
0};
TEST(RoundlTest, SpecialNumbers) {
ASSERT_TRUE(FPBits::zero() == __llvm_libc::roundl(FPBits::zero()));
ASSERT_TRUE(FPBits::negZero() == __llvm_libc::roundl(FPBits::negZero()));

ASSERT_TRUE(FPBits::inf() == __llvm_libc::roundl(FPBits::inf()));
ASSERT_TRUE(FPBits::negInf() == __llvm_libc::roundl(FPBits::negInf()));

long double nan = FPBits::buildNaN(1);
ASSERT_NE(isnan(nan), 0);
ASSERT_NE(isnan(__llvm_libc::roundl(nan)), 0);
}

TEST(RoundlTest, RoundedNumbers) {
ASSERT_TRUE(FPBits(1.0l) == __llvm_libc::roundl(1.0l));
ASSERT_TRUE(FPBits(-1.0l) == __llvm_libc::roundl(-1.0l));
ASSERT_TRUE(FPBits(10.0l) == __llvm_libc::roundl(10.0l));
ASSERT_TRUE(FPBits(-10.0l) == __llvm_libc::roundl(-10.0l));
ASSERT_TRUE(FPBits(1234.0l) == __llvm_libc::roundl(1234.0l));
ASSERT_TRUE(FPBits(-1234.0l) == __llvm_libc::roundl(-1234.0l));
}

TEST(RoundlTest, Fractions) {
ASSERT_TRUE(FPBits(1.0l) == __llvm_libc::roundl(1.3l));
ASSERT_TRUE(FPBits(-1.0l) == __llvm_libc::roundl(-1.3l));
ASSERT_TRUE(FPBits(2.0l) == __llvm_libc::roundl(1.5l));
ASSERT_TRUE(FPBits(-2.0l) == __llvm_libc::roundl(-1.5l));
ASSERT_TRUE(FPBits(2.0l) == __llvm_libc::roundl(1.75l));
ASSERT_TRUE(FPBits(-2.0l) == __llvm_libc::roundl(-1.75l));
ASSERT_TRUE(FPBits(10.0l) == __llvm_libc::roundl(10.32l));
ASSERT_TRUE(FPBits(-10.0l) == __llvm_libc::roundl(-10.32l));
ASSERT_TRUE(FPBits(11.0l) == __llvm_libc::roundl(10.65l));
ASSERT_TRUE(FPBits(-11.0l) == __llvm_libc::roundl(-10.65l));
ASSERT_TRUE(FPBits(1234.0l) == __llvm_libc::roundl(1234.38l));
ASSERT_TRUE(FPBits(-1234.0l) == __llvm_libc::roundl(-1234.38l));
ASSERT_TRUE(FPBits(1235.0l) == __llvm_libc::roundl(1234.96l));
ASSERT_TRUE(FPBits(-1235.0l) == __llvm_libc::roundl(-1234.96l));
}

TEST(RoundlTest, InLongDoubleRange) {
using UIntType = FPBits::UIntType;
constexpr UIntType count = 10000000;
constexpr UIntType step = UIntType(-1) / count;
for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
long double x = FPBits(v);
if (isnan(x) || isinf(x))
continue;

ASSERT_MPFR_MATCH(mpfr::Operation::Round, x, __llvm_libc::roundl(x),
tolerance);
}
}
66 changes: 28 additions & 38 deletions libc/utils/FPUtil/NearestIntegerOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
#ifndef LLVM_LIBC_UTILS_FPUTIL_NEAREST_INTEGER_OPERATIONS_H
#define LLVM_LIBC_UTILS_FPUTIL_NEAREST_INTEGER_OPERATIONS_H

#include "ClassificationFunctions.h"
#include "FPBits.h"
#include "FloatOperations.h"
#include "FloatProperties.h"

#include "utils/CPP/TypeTraits.h"

Expand Down Expand Up @@ -54,21 +51,18 @@ static inline T trunc(T x) {
template <typename T,
cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline T ceil(T x) {
using Properties = FloatProperties<T>;
using BitsType = typename FloatProperties<T>::BitsType;

BitsType bits = valueAsBits(x);
FPBits<T> bits(x);

// If x is infinity NaN or zero, return it.
if (bitsAreInfOrNaN(bits) || bitsAreZero(bits))
if (bits.isInfOrNaN() || bits.isZero())
return x;

bool isNeg = bits & Properties::signMask;
int exponent = getExponentFromBits(bits);
bool isNeg = bits.sign;
int exponent = bits.getExponent();

// If the exponent is greater than the most negative mantissa
// exponent, then x is already an integer.
if (exponent >= static_cast<int>(Properties::mantissaWidth))
if (exponent >= static_cast<int>(MantissaWidth<T>::value))
return x;

if (exponent <= -1) {
Expand All @@ -78,14 +72,14 @@ static inline T ceil(T x) {
return T(1.0);
}

uint32_t trimSize = Properties::mantissaWidth - exponent;
uint32_t trimSize = MantissaWidth<T>::value - exponent;
bits.mantissa = (bits.mantissa >> trimSize) << trimSize;
T truncValue = T(bits);

// If x is already an integer, return it.
if ((bits << (Properties::bitWidth - trimSize)) == 0)
if (truncValue == x)
return x;

BitsType truncBits = (bits >> trimSize) << trimSize;
T truncValue = valueFromBits(truncBits);

// If x is negative, the ceil operation is equivalent to the trunc operation.
if (isNeg)
return truncValue;
Expand All @@ -96,8 +90,8 @@ static inline T ceil(T x) {
template <typename T,
cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline T floor(T x) {
auto bits = valueAsBits(x);
if (FloatProperties<T>::signMask & bits) {
FPBits<T> bits(x);
if (bits.sign) {
return -ceil(-x);
} else {
return trunc(x);
Expand All @@ -107,21 +101,19 @@ static inline T floor(T x) {
template <typename T,
cpp::EnableIfType<cpp::IsFloatingPointType<T>::Value, int> = 0>
static inline T round(T x) {
using Properties = FloatProperties<T>;
using BitsType = typename FloatProperties<T>::BitsType;

BitsType bits = valueAsBits(x);
using UIntType = typename FPBits<T>::UIntType;
FPBits<T> bits(x);

// If x is infinity, NaN or zero, return it.
if (bitsAreInfOrNaN(bits) || bitsAreZero(bits))
// If x is infinity NaN or zero, return it.
if (bits.isInfOrNaN() || bits.isZero())
return x;

bool isNeg = bits & Properties::signMask;
int exponent = getExponentFromBits(bits);
bool isNeg = bits.sign;
int exponent = bits.getExponent();

// If the exponent is greater than the most negative mantissa
// exponent, then x is already an integer.
if (exponent >= static_cast<int>(Properties::mantissaWidth))
if (exponent >= static_cast<int>(MantissaWidth<T>::value))
return x;

if (exponent == -1) {
Expand All @@ -140,24 +132,22 @@ static inline T round(T x) {
return T(0.0);
}

uint32_t trimSize = Properties::mantissaWidth - exponent;
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);

// If x is already an integer, return it.
if ((bits << (Properties::bitWidth - trimSize)) == 0)
if (truncValue == x)
return x;

BitsType truncBits = (bits >> trimSize) << trimSize;
T truncValue = valueFromBits(truncBits);

if ((bits & (BitsType(1) << (trimSize - 1))) == 0) {
if (!halfBitSet) {
// Franctional part is less than 0.5 so round value is the
// same as the trunc value.
return truncValue;
} else {
return isNeg ? truncValue - T(1.0) : truncValue + T(1.0);
}

if (isNeg)
return truncValue - T(1.0);
else
return truncValue + T(1.0);
}

} // namespace fputil
Expand Down