#include <gtest/gtest.h>
#include <c10/util/generic_math.h>
#include <torch/headeronly/util/BFloat16-math.h>
#include <torch/headeronly/util/NumericUtils.h>
#include <torch/headeronly/util/TypeSafeSignMath.h>
#include <cmath>
#include <limits>
namespace torch {
namespace aot_inductor {
TEST(TestMath, TestDivFloor) {
EXPECT_EQ(c10::div_floor_floating(5., 0.), INFINITY);
EXPECT_DOUBLE_EQ(c10::div_floor_floating(5., 2.), 2.);
EXPECT_DOUBLE_EQ(c10::div_floor_floating(5., -2.), -3.);
EXPECT_EQ(c10::div_floor_integer(5, 2), 2);
EXPECT_EQ(c10::div_floor_integer(5, -2), -3);
}
TEST(TestMath, TestIsNan) {
using torch::headeronly::_isnan;
const float fnan = std::nanf("");
EXPECT_FALSE(_isnan(1));
EXPECT_FALSE(_isnan(1.0f));
EXPECT_TRUE(_isnan(fnan));
EXPECT_FALSE(_isnan(1.0));
EXPECT_TRUE(_isnan(std::nan("")));
EXPECT_FALSE(_isnan(torch::headeronly::complex<float>(1.0f, 2.0f)));
EXPECT_TRUE(_isnan(torch::headeronly::complex<float>(fnan, 2.0f)));
EXPECT_TRUE(_isnan(torch::headeronly::complex<float>(1.0f, fnan)));
EXPECT_FALSE(_isnan(torch::headeronly::Half(1.0f)));
EXPECT_TRUE(_isnan(torch::headeronly::Half(fnan)));
EXPECT_FALSE(_isnan(torch::headeronly::BFloat16(1.0f)));
EXPECT_TRUE(_isnan(torch::headeronly::BFloat16(fnan)));
EXPECT_FALSE(_isnan(torch::headeronly::Float8_e5m2(1.0f)));
EXPECT_TRUE(_isnan(torch::headeronly::Float8_e5m2(fnan)));
EXPECT_TRUE(_isnan(torch::headeronly::Float8_e4m3fn(fnan)));
EXPECT_TRUE(_isnan(torch::headeronly::Float8_e5m2fnuz(fnan)));
EXPECT_TRUE(_isnan(torch::headeronly::Float8_e4m3fnuz(fnan)));
}
TEST(TestMath, TestIsInf) {
using torch::headeronly::_isinf;
const float finf = std::numeric_limits<float>::infinity();
EXPECT_FALSE(_isinf(1));
EXPECT_FALSE(_isinf(1.0f));
EXPECT_TRUE(_isinf(finf));
EXPECT_TRUE(_isinf(std::numeric_limits<double>::infinity()));
EXPECT_FALSE(_isinf(torch::headeronly::Half(1.0f)));
EXPECT_TRUE(_isinf(torch::headeronly::Half(finf)));
EXPECT_FALSE(_isinf(torch::headeronly::BFloat16(1.0f)));
EXPECT_TRUE(_isinf(torch::headeronly::BFloat16(finf)));
EXPECT_TRUE(_isinf(torch::headeronly::Float8_e5m2(finf)));
EXPECT_FALSE(_isinf(torch::headeronly::Float8_e4m3fn(1.0f)));
EXPECT_FALSE(_isinf(torch::headeronly::Float8_e5m2fnuz(1.0f)));
EXPECT_FALSE(_isinf(torch::headeronly::Float8_e4m3fnuz(1.0f)));
}
TEST(TestMath, TestSignbit) {
using torch::headeronly::_signbit;
using torch::headeronly::BFloat16;
using torch::headeronly::Half;
const float fnan = std::numeric_limits<float>::quiet_NaN();
const double dnan = std::numeric_limits<double>::quiet_NaN();
EXPECT_TRUE(_signbit(-1));
EXPECT_FALSE(_signbit(0));
EXPECT_FALSE(_signbit(1u));
EXPECT_TRUE(_signbit(-1.0f));
EXPECT_TRUE(_signbit(-0.0f));
EXPECT_FALSE(_signbit(0.0f));
EXPECT_TRUE(_signbit(-fnan));
EXPECT_FALSE(_signbit(fnan));
EXPECT_TRUE(_signbit(-0.0));
EXPECT_TRUE(_signbit(-dnan));
EXPECT_TRUE(_signbit(Half(-0.0f)));
EXPECT_FALSE(_signbit(Half(0.0f)));
EXPECT_TRUE(_signbit(Half(0xFE00, Half::from_bits())));
EXPECT_TRUE(_signbit(BFloat16(-0.0f)));
EXPECT_FALSE(_signbit(BFloat16(0.0f)));
EXPECT_TRUE(_signbit(BFloat16(0xFFC0, BFloat16::from_bits())));
}
TEST(TestMath, TestIsReducedFloatingPoint) {
using torch::headeronly::BFloat16;
using torch::headeronly::Half;
using torch::headeronly::is_reduced_floating_point;
using torch::headeronly::is_reduced_floating_point_v;
static_assert(is_reduced_floating_point_v<Half>);
static_assert(is_reduced_floating_point_v<BFloat16>);
static_assert(!is_reduced_floating_point_v<float>);
EXPECT_TRUE(is_reduced_floating_point<Half>::value);
EXPECT_TRUE(is_reduced_floating_point<BFloat16>::value);
EXPECT_FALSE(is_reduced_floating_point<double>::value);
EXPECT_NEAR(static_cast<float>(std::acos(Half(0.5f))), 1.0472f, 1e-3);
EXPECT_NEAR(static_cast<float>(std::sqrt(BFloat16(4.0f))), 2.0f, 1e-3);
EXPECT_NEAR(static_cast<float>(std::rsqrt(BFloat16(4.0f))), 0.5f, 1e-3);
EXPECT_NEAR(static_cast<float>(std::pow(Half(2.0f), 3.0)), 8.0f, 1e-3);
EXPECT_NEAR(
static_cast<float>(std::pow(BFloat16(2.0f), BFloat16(3.0f))), 8.0f, 1e-3);
BFloat16 from(1.0f);
BFloat16 toward(2.0f);
BFloat16 stepped = std::nextafter(from, toward);
EXPECT_GT(static_cast<float>(stepped), 1.0f);
EXPECT_LT(static_cast<float>(stepped), 2.0f);
BFloat16 pos_zero(uint16_t{0}, BFloat16::from_bits());
BFloat16 neg_zero(uint16_t{0x8000}, BFloat16::from_bits());
EXPECT_EQ(std::nextafter(pos_zero, neg_zero).x, neg_zero.x);
BFloat16 nan(std::numeric_limits<float>::quiet_NaN());
EXPECT_TRUE(std::isnan(static_cast<float>(std::nextafter(nan, from))));
}
}
}