diff --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt index 0dda7d5c683ec..22a67283c2d26 100644 --- a/libc/config/gpu/amdgpu/entrypoints.txt +++ b/libc/config/gpu/amdgpu/entrypoints.txt @@ -451,6 +451,7 @@ set(TARGET_LIBM_ENTRYPOINTS libc.src.math.nextupl libc.src.math.pow libc.src.math.powf + libc.src.math.powf16 libc.src.math.remainder libc.src.math.remainderf libc.src.math.remainderl diff --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt index 6070fb5b17b3c..8a9bb724079c3 100644 --- a/libc/config/gpu/nvptx/entrypoints.txt +++ b/libc/config/gpu/nvptx/entrypoints.txt @@ -452,6 +452,7 @@ set(TARGET_LIBM_ENTRYPOINTS libc.src.math.nextupl libc.src.math.pow libc.src.math.powf + libc.src.math.powf16 libc.src.math.remainder libc.src.math.remainderf libc.src.math.remainderl diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index 714120a79e39a..2382448eb741b 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -592,6 +592,7 @@ set(TARGET_LIBM_ENTRYPOINTS libc.src.math.nextupl libc.src.math.pow libc.src.math.powf + libc.src.math.powf16 libc.src.math.remainder libc.src.math.remainderf libc.src.math.remainderl diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt index f04ac40145d3a..ff11969d9eaa7 100644 --- a/libc/config/linux/arm/entrypoints.txt +++ b/libc/config/linux/arm/entrypoints.txt @@ -405,6 +405,7 @@ set(TARGET_LIBM_ENTRYPOINTS libc.src.math.nextupl libc.src.math.pow libc.src.math.powf + libc.src.math.powf16 libc.src.math.remainder libc.src.math.remainderf libc.src.math.remainderl diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt index f6bbb346d10e5..9e8d74daa5c7a 100644 --- a/libc/config/linux/riscv/entrypoints.txt +++ b/libc/config/linux/riscv/entrypoints.txt @@ -600,6 +600,7 @@ set(TARGET_LIBM_ENTRYPOINTS libc.src.math.nextupl libc.src.math.pow libc.src.math.powf + libc.src.math.powf16 libc.src.math.remainder libc.src.math.remainderf libc.src.math.remainderl diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index 7a8d74a4e5da9..4e2ba8f4741d4 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -632,6 +632,7 @@ set(TARGET_LIBM_ENTRYPOINTS libc.src.math.nextupl libc.src.math.pow libc.src.math.powf + libc.src.math.powf16 libc.src.math.remainder libc.src.math.remainderf libc.src.math.remainderl diff --git a/libc/include/math.yaml b/libc/include/math.yaml index afd3ae33305c1..6358157189acc 100644 --- a/libc/include/math.yaml +++ b/libc/include/math.yaml @@ -2162,6 +2162,13 @@ functions: arguments: - type: float - type: float + - name: powf16 + standards: + - stdc + return_type: _Float16 + arguments: + - type: _Float16 + - type: _Float16 - name: powi standards: llvm_libc_ext return_type: double diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index e37e22fdb58e6..9238c440faae6 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -479,6 +479,7 @@ add_math_entrypoint_object(nextupbf16) add_math_entrypoint_object(pow) add_math_entrypoint_object(powf) +add_math_entrypoint_object(powf16) add_math_entrypoint_object(powi) add_math_entrypoint_object(powif) diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index c048a64db6bc2..eecf61ed878ac 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -1626,6 +1626,28 @@ add_entrypoint_object( libc.src.__support.math.expxf16_utils ) +add_entrypoint_object( + powf16 + SRCS + powf16.cpp + HDRS + ../powf16.h + DEPENDS + libc.hdr.errno_macros + libc.hdr.fenv_macros + libc.src.__support.CPP.bit + libc.src.__support.FPUtil.fenv_impl + libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.polyeval + libc.src.__support.FPUtil.cast + libc.src.__support.FPUtil.multiply_add + libc.src.__support.FPUtil.nearest_integer + libc.src.__support.FPUtil.sqrt + libc.src.__support.macros.optimization + libc.src.__support.math.exp10f_utils + libc.src.__support.math.common_constants +) + add_entrypoint_object( powf SRCS diff --git a/libc/src/math/generic/powf16.cpp b/libc/src/math/generic/powf16.cpp new file mode 100644 index 0000000000000..d5aa9773c07cb --- /dev/null +++ b/libc/src/math/generic/powf16.cpp @@ -0,0 +1,397 @@ +//===-- Half-precision x^y function ---------------------------------------===// +// +// 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 "src/math/powf16.h" +#include "hdr/errno_macros.h" +#include "hdr/fenv_macros.h" +#include "src/__support/CPP/bit.h" +#include "src/__support/FPUtil/FEnvImpl.h" +#include "src/__support/FPUtil/FPBits.h" +#include "src/__support/FPUtil/PolyEval.h" +#include "src/__support/FPUtil/cast.h" +#include "src/__support/FPUtil/multiply_add.h" +#include "src/__support/FPUtil/nearest_integer.h" +#include "src/__support/FPUtil/sqrt.h" +#include "src/__support/common.h" +#include "src/__support/macros/config.h" +#include "src/__support/macros/optimization.h" +#include "src/__support/macros/properties/types.h" +#include "src/__support/math/common_constants.h" +#include "src/__support/math/exp10f_utils.h" + +namespace LIBC_NAMESPACE_DECL { + +using namespace common_constants_internal; +namespace { +static inline double exp2_range_reduced(double x) { + // k = round(x * 32) => (hi + mid) * 2^5 + double kf = fputil::nearest_integer(x * 32.0); + int k = static_cast(kf); + // dx = lo = x - (hi + mid) = x - k * 2^(-5) + double dx = fputil::multiply_add(-0x1.0p-5, kf, x); // -2^-5 * k + x + + // hi = k >> MID_BITS + // exp_hi = hi shifted into double exponent field + int64_t hi = static_cast(k >> ExpBase::MID_BITS); + int64_t exp_hi = static_cast( + static_cast(hi) << fputil::FPBits::FRACTION_LEN); + + // mh_bits = bits for 2^hi * 2^mid (lookup contains base bits for 2^mid) + int tab_index = k & ExpBase::MID_MASK; // mid index in [0, 31] + int64_t mh_bits = ExpBase::EXP_2_MID[tab_index] + exp_hi; + + // mh = 2^(hi + mid) + double mh = fputil::FPBits(static_cast(mh_bits)).get_val(); + + // Degree-5 polynomial approximating (2^x - 1)/x generating by Sollya with: + // > P = fpminimax((2^x - 1)/x, 5, [|D...|], [-1/32. 1/32]); + constexpr double COEFFS[5] = {0x1.62e42fefa39efp-1, 0x1.ebfbdff8131c4p-3, + 0x1.c6b08d7061695p-5, 0x1.3b2b1bee74b2ap-7, + 0x1.5d88091198529p-10}; + + double dx_sq = dx * dx; + double c1 = fputil::multiply_add(dx, COEFFS[0], 1.0); // 1 + ln2*dx + double c2 = + fputil::multiply_add(dx, COEFFS[2], COEFFS[1]); // COEFF1 + COEFF2*dx + double c3 = + fputil::multiply_add(dx, COEFFS[4], COEFFS[3]); // COEFF3 + COEFF4*dx + double p = fputil::multiply_add(dx_sq, c3, c2); // c2 + c3*dx^2 + + // 2^x = 2^(hi+mid) * 2^dx + // ≈ mh * (1 + dx * P(dx)) + // = mh + (mh * dx) * P(dx) + double result = fputil::multiply_add(p, dx_sq * mh, c1 * mh); + + return result; +} +bool is_odd_integer(float16 x) { + using FPBits = fputil::FPBits; + FPBits xbits(x); + uint16_t x_u = xbits.uintval(); + unsigned x_e = static_cast(xbits.get_biased_exponent()); + unsigned lsb = static_cast( + cpp::countr_zero(static_cast(x_u | FPBits::EXP_MASK))); + constexpr unsigned UNIT_EXPONENT = + static_cast(FPBits::EXP_BIAS + FPBits::FRACTION_LEN); + return (x_e + lsb == UNIT_EXPONENT); +} + +bool is_integer(float16 x) { + using FPBits = fputil::FPBits; + FPBits xbits(x); + uint16_t x_u = xbits.uintval(); + unsigned x_e = static_cast(xbits.get_biased_exponent()); + unsigned lsb = static_cast( + cpp::countr_zero(static_cast(x_u | FPBits::EXP_MASK))); + constexpr unsigned UNIT_EXPONENT = + static_cast(FPBits::EXP_BIAS + FPBits::FRACTION_LEN); + return (x_e + lsb >= UNIT_EXPONENT); +} + +} // namespace + +LLVM_LIBC_FUNCTION(float16, powf16, (float16 x, float16 y)) { + using FPBits = fputil::FPBits; + + FPBits xbits(x), ybits(y); + bool x_sign = xbits.is_neg(); + bool y_sign = ybits.is_neg(); + + FPBits x_abs = xbits.abs(); + FPBits y_abs = ybits.abs(); + + uint16_t x_u = xbits.uintval(); + uint16_t x_a = x_abs.uintval(); + uint16_t y_a = y_abs.uintval(); + uint16_t y_u = ybits.uintval(); + bool result_sign = false; + + ///////// BEGIN - Check exceptional cases //////////////////////////////////// + // If x or y is signaling NaN + if (xbits.is_signaling_nan() || ybits.is_signaling_nan()) { + fputil::raise_except_if_required(FE_INVALID); + return FPBits::quiet_nan().get_val(); + } + if (LIBC_UNLIKELY( + ybits.is_zero() || x_u == FPBits::one().uintval() || xbits.is_nan() || + ybits.is_nan() || x_u == FPBits::one().uintval() || + x_u == FPBits::zero().uintval() || x_u >= FPBits::inf().uintval() || + y_u >= FPBits::inf().uintval() || + x_u < FPBits::min_normal().uintval() || y_a == 0x3400U || + y_a == 0x3800U || y_a == 0x3A00U || y_a == 0x3800U || + y_a == 0x3800U || y_a == 0x3D00U || y_a == 0x3E00U || + y_a == 0x4100U || y_a == 0x4300U || y_a == 0x3c00U || + y_a == 0x4000U || is_integer(y))) { + // pow(x, 0) = 1 + if (ybits.is_zero()) { + return 1.0f16; + } + + // pow(1, Y) = 1 + if (x_u == FPBits::one().uintval()) { + return 1.0f16; + } + // 4. Handle remaining NaNs + // pow(NaN, y) = NaN (for y != 0) + if (xbits.is_nan()) { + return x; + } + // pow(x, NaN) = NaN (for x != 1) + if (ybits.is_nan()) { + return y; + } + switch (y_a) { + case 0x3400U: // y = ±0.25 (1/4) + case 0x3800U: // y = ±0.5 (1/2) + case 0x3A00U: // y = ±0.75 (3/4) + case 0x3D00U: // y = ±1.25 (5/4) + case 0x3E00U: // y = ±1.5 (3/2) + case 0x4100U: // y = ±2.5 (5/2) + case 0x4300U: // y = ±3.5 (7/2) + { + if (xbits.is_zero()) { + if (y_sign) { + // pow(±0, negative) handled below + break; + } else { + // pow(±0, positive_fractional) = +0 + return FPBits::zero(Sign::POS).get_val(); + } + } + + if (x_sign && !xbits.is_zero()) { + break; // pow(negative, non-integer) = NaN + } + + double x_d = static_cast(x); + double sqrt_x = fputil::sqrt(x_d); + double fourth_root = fputil::sqrt(sqrt_x); + double result_d; + + // Compute based on exponent value + switch (y_a) { + case 0x3400U: // 0.25 = x^(1/4) + result_d = fourth_root; + break; + case 0x3800U: // 0.5 = x^(1/2) + result_d = sqrt_x; + break; + case 0x3A00U: // 0.75 = x^(1/2) * x^(1/4) + result_d = sqrt_x * fourth_root; + break; + case 0x3D00U: // 1.25 = x * x^(1/4) + result_d = x_d * fourth_root; + break; + case 0x3E00U: // 1.5 = x * x^(1/2) + result_d = x_d * sqrt_x; + break; + case 0x4100U: // 2.5 = x^2 * x^(1/2) + result_d = x_d * x_d * sqrt_x; + break; + case 0x4300U: // 3.5 = x^3 * x^(1/2) + result_d = x_d * x_d * x_d * sqrt_x; + break; + } + + result_d = y_sign ? (1.0 / result_d) : result_d; + return fputil::cast(result_d); + } + case 0x3c00U: // y = +-1.0 + return fputil::cast(y_sign ? (1.0 / x) : x); + + case 0x4000U: // y = +-2.0 + double result_d = static_cast(x) * static_cast(x); + return fputil::cast(y_sign ? (1.0 / (result_d)) : (result_d)); + } + // TODO: Speed things up with pow(2, y) = exp2(y) and pow(10, y) = exp10(y). + // + // pow(-1, y) for integer y + if (x_u == FPBits::one(Sign::NEG).uintval()) { + if (is_integer(y)) { + if (is_odd_integer(y)) { + return -1.0f16; + } else { + return 1.0f16; + } + } + // pow(-1, non-integer) = NaN + fputil::set_errno_if_required(EDOM); + fputil::raise_except_if_required(FE_INVALID); + return FPBits::quiet_nan().get_val(); + } + + // pow(±0, y) cases + if (xbits.is_zero()) { + if (y_sign) { + // pow(+-0, negative) = +-inf and raise FE_DIVBYZERO + fputil::raise_except_if_required(FE_DIVBYZERO); + bool result_neg = x_sign && ybits.is_finite() && is_odd_integer(y); + return FPBits::inf(result_neg ? Sign::NEG : Sign::POS).get_val(); + } else { + // pow(+-0, positive) = +-0 + bool out_is_neg = x_sign && is_odd_integer(y); + return out_is_neg ? FPBits::zero(Sign::NEG).get_val() + : FPBits::zero(Sign::POS).get_val(); + } + } + + if (xbits.is_inf()) { + bool out_is_neg = x_sign && ybits.is_finite() && is_odd_integer(y); + if (y_sign) // pow(+-inf, negative) = +-0 + return out_is_neg ? FPBits::zero(Sign::NEG).get_val() + : FPBits::zero(Sign::POS).get_val(); + // pow(+-inf, positive) = +-inf + return FPBits::inf(out_is_neg ? Sign::NEG : Sign::POS).get_val(); + } + + // y = +-inf cases + if (ybits.is_inf()) { + // pow(1, inf) handled above. + bool x_abs_less_than_one = x_a < FPBits::one().uintval(); + if ((x_abs_less_than_one && !y_sign) || + (!x_abs_less_than_one && y_sign)) { + // |x| < 1 and y = +inf => 0.0 + // |x| > 1 and y = -inf => 0.0 + return 0.0f16; + } else { + // |x| > 1 and y = +inf => +inf + // |x| < 1 and y = -inf => +inf + return FPBits::inf(Sign::POS).get_val(); + } + } + + // pow( negative, non-integer ) = NaN + if (x_sign && !is_integer(y)) { + fputil::set_errno_if_required(EDOM); + fputil::raise_except_if_required(FE_INVALID); + return FPBits::quiet_nan().get_val(); + } + + bool result_sign = false; + if (x_sign && is_integer(y)) { + result_sign = is_odd_integer(y); + } + + if (is_integer(y)) { + double base = x_abs.get_val(); + double res = 1.0; + int yi = static_cast(y_abs.get_val()); + + // Fast exponentiation by squaring + while (yi > 0) { + if (yi & 1) + res *= base; + base *= base; + yi = yi >> 1; + } + + if (y_sign) { + res = 1.0 / res; + } + + if (result_sign) { + res = -res; + } + + if (FPBits(fputil::cast(res)).is_inf()) { + fputil::raise_except_if_required(FE_OVERFLOW); + res = result_sign ? -0x1.0p20 : 0x1.0p20; + } + + float16 final_res = fputil::cast(res); + return final_res; + } + } + + ///////// END - Check exceptional cases ////////////////////////////////////// + + // Core computation: x^y = 2^( y * log2(x) ) + // We compute log2(x) = log(x) / log(2) using a polynomial approximation. + + // The exponent part (m) is added later to get the final log(x). + FPBits x_bits(x); + uint16_t x_u_log = x_bits.uintval(); + + // Extract exponent field of x. + int m = x_bits.get_exponent(); + + // When x is subnormal, normalize it by adjusting m. + if ((x_u_log & FPBits::EXP_MASK) == 0U) { + unsigned leading_zeros = + cpp::countl_zero(static_cast(x_u_log)) - (32 - 16); + + constexpr unsigned SUBNORMAL_SHIFT_CORRECTION = 5; + unsigned shift = leading_zeros - SUBNORMAL_SHIFT_CORRECTION; + + x_bits.set_mantissa(static_cast(x_u_log << shift)); + + m = 1 - FPBits::EXP_BIAS - static_cast(shift); + } + + // Extract the mantissa and index into small lookup tables. + uint16_t mant = x_bits.get_mantissa(); + // Use the highest 7 fractional bits of the mantissa as the index f. + int f = mant >> (FPBits::FRACTION_LEN - 7); + + // Reconstruct the mantissa value m_x so it's in the range [1.0, 2.0). + x_bits.set_biased_exponent(FPBits::EXP_BIAS); + double mant_d = x_bits.get_val(); + // Degree-5 polynomial approximation + // of log2 generated by Sollya with: + // > P = fpminimax(log2(1 + x)/x, 4, [|1, D...|], [-2^-8, 2^-7]); + constexpr double COEFFS[5] = {0x1.71547652b8133p0, -0x1.71547652d1e33p-1, + 0x1.ec70a098473dep-2, -0x1.7154c5ccdf121p-2, + 0x1.2514fd90a130ap-2}; + +#ifdef LIBC_TARGET_CPU_HAS_FMA_DOUBLE + double v = fputil::multiply_add(mant_d, RD[f], -1.0); +#else + double c = fputil::FPBits(fputil::FPBits(mant_d).uintval() & + 0x3fff'e000'0000'0000) + .get_val(); + double v = fputil::multiply_add(RD[f], mant_d - c, CD[f]); +#endif // LIBC_TARGET_CPU_HAS_FMA_DOUBLE + double extra_factor = static_cast(m) + LOG2_R[f]; + double vsq = v * v; + double c0 = fputil::multiply_add(v, COEFFS[0], 0.0); + double c1 = fputil::multiply_add(v, COEFFS[2], COEFFS[1]); + double c2 = fputil::multiply_add(v, COEFFS[4], COEFFS[3]); + + double log2_x = fputil::polyeval(vsq, c0, c1, c2); + + double y_d = fputil::cast(y); + double z = fputil::multiply_add(y_d, log2_x, y_d * extra_factor); + + // Check for underflow + // Float16 min normal is 2^-14, smallest subnormal is 2^-24 + if (LIBC_UNLIKELY(z < -25.0)) { + fputil::raise_except_if_required(FE_UNDERFLOW); + return result_sign ? FPBits::zero(Sign::NEG).get_val() + : FPBits::zero(Sign::POS).get_val(); + } + + // Check for overflow + // Float16 max is ~2^16 + double result_d; + if (LIBC_UNLIKELY(z > 16.0)) { + fputil::raise_except_if_required(FE_OVERFLOW); + result_d = result_sign ? -0x1.0p20 : 0x1.0p20; + } else { + result_d = exp2_range_reduced(z); + } + + if (result_sign) { + + result_d = -result_d; + } + + float16 result = fputil::cast((result_d)); + return result; +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/powf16.h b/libc/src/math/powf16.h new file mode 100644 index 0000000000000..bd50f16edeede --- /dev/null +++ b/libc/src/math/powf16.h @@ -0,0 +1,21 @@ +//===-- Implementation header for powf16 ------------------------*- 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_SRC_MATH_POWF16_H +#define LLVM_LIBC_SRC_MATH_POWF16_H + +#include "src/__support/macros/config.h" +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE_DECL { + +float16 powf16(float16 x, float16 y); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_MATH_POWF16_H diff --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt index ff5c511922171..afd5a72e0f87a 100644 --- a/libc/test/src/math/CMakeLists.txt +++ b/libc/test/src/math/CMakeLists.txt @@ -2648,6 +2648,17 @@ add_fp_unittest( libc.src.__support.FPUtil.fp_bits ) +add_fp_unittest( + powf16_test + NEED_MPFR + SUITE + libc-math-unittests + SRCS + powf16_test.cpp + DEPENDS + libc.src.math.powf16 + libc.src.__support.FPUtil.fp_bits +) add_fp_unittest( atan2f_test NEED_MPFR diff --git a/libc/test/src/math/exhaustive/CMakeLists.txt b/libc/test/src/math/exhaustive/CMakeLists.txt index 2ff4f02041776..4e56b31d398dc 100644 --- a/libc/test/src/math/exhaustive/CMakeLists.txt +++ b/libc/test/src/math/exhaustive/CMakeLists.txt @@ -357,6 +357,21 @@ add_fp_unittest( -lpthread ) +add_fp_unittest( + powf16_test + NO_RUN_POSTBUILD + NEED_MPFR + SUITE + libc_math_exhaustive_tests + SRCS + powf16_test.cpp + DEPENDS + .exhaustive_test + libc.src.math.powf16 + libc.src.__support.FPUtil.fp_bits + LINK_LIBRARIES + -lpthread +) add_fp_unittest( hypotf_test NO_RUN_POSTBUILD diff --git a/libc/test/src/math/exhaustive/powf16_test.cpp b/libc/test/src/math/exhaustive/powf16_test.cpp new file mode 100644 index 0000000000000..61c99baa77508 --- /dev/null +++ b/libc/test/src/math/exhaustive/powf16_test.cpp @@ -0,0 +1,72 @@ +//===-- Exhaustive test for powf16 ----------------------------------------===// +// +// 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 "exhaustive_test.h" +#include "src/__support/FPUtil/FPBits.h" +#include "src/math/powf16.h" +#include "test/UnitTest/FPMatcher.h" +#include "utils/MPFRWrapper/MPFRUtils.h" + +namespace mpfr = LIBC_NAMESPACE::testing::mpfr; + +struct Powf16Checker : public virtual LIBC_NAMESPACE::testing::Test { + using FloatType = float16; + using FPBits = LIBC_NAMESPACE::fputil::FPBits; + using StorageType = typename FPBits::StorageType; + + uint64_t check(uint16_t x_start, uint16_t x_stop, uint16_t y_start, + uint16_t y_stop, mpfr::RoundingMode rounding) { + mpfr::ForceRoundingMode r(rounding); + if (!r.success) + return true; + uint16_t xbits = x_start; + uint64_t failed = 0; + do { + float16 x = FPBits(xbits).get_val(); + uint16_t ybits = y_start; + do { + float16 y = FPBits(ybits).get_val(); + mpfr::BinaryInput input{x, y}; + bool correct = TEST_MPFR_MATCH_ROUNDING_SILENTLY( + mpfr::Operation::Pow, input, LIBC_NAMESPACE::powf16(x, y), 0.5, + rounding); + failed += (!correct); + } while (ybits++ < y_stop); + } while (xbits++ < x_stop); + return failed; + } +}; + +using LlvmLibcPowf16ExhaustiveTest = + LlvmLibcExhaustiveMathTest; + +// Range: x in [0, inf], y in [0, inf] +static constexpr uint16_t POS_START = 0x0000U; +static constexpr uint16_t POS_STOP = 0x7C00U; + +TEST_F(LlvmLibcPowf16ExhaustiveTest, PositiveRange) { + test_full_range_all_roundings(POS_START, POS_STOP, POS_START, POS_STOP); +} + +// Range: x in [-0, -inf], y in [0, inf] +static constexpr uint16_t NEG_START = 0x8000U; +static constexpr uint16_t NEG_STOP = 0xFC00U; + +TEST_F(LlvmLibcPowf16ExhaustiveTest, NegativeBasePositiveExponent) { + test_full_range_all_roundings(NEG_START, NEG_STOP, POS_START, POS_STOP); +} + +// Range: x in [0, inf], y in [-0, -inf] +TEST_F(LlvmLibcPowf16ExhaustiveTest, PositiveBaseNegativeExponent) { + test_full_range_all_roundings(POS_START, POS_STOP, NEG_START, NEG_STOP); +} + +// Range: x in [-0, -inf], y in [-0, -inf] +TEST_F(LlvmLibcPowf16ExhaustiveTest, NegativeRange) { + test_full_range_all_roundings(NEG_START, NEG_STOP, NEG_START, NEG_STOP); +} diff --git a/libc/test/src/math/powf16_test.cpp b/libc/test/src/math/powf16_test.cpp new file mode 100644 index 0000000000000..1610e66184026 --- /dev/null +++ b/libc/test/src/math/powf16_test.cpp @@ -0,0 +1,113 @@ +//===-- Unittests for powf16 ----------------------------------------------===// +// +// 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 "src/__support/CPP/bit.h" +#include "src/math/powf16.h" +#include "test/UnitTest/FPMatcher.h" +#include "test/UnitTest/Test.h" +#include "utils/MPFRWrapper/MPFRUtils.h" + +using LlvmLibcPowF16Test = LIBC_NAMESPACE::testing::FPTest; +using FPBits = LIBC_NAMESPACE::fputil::FPBits; + +namespace mpfr = LIBC_NAMESPACE::testing::mpfr; + +static constexpr float16 SELECTED_VALS[] = { + 0.83984375f16, 1.414f16, 0.0625f16, 2.5f16, + 3.140625f16, 15.5f16, 2.f16, 3.25f16}; + +// Test tricky inputs for selected x values against all possible y values. +TEST_F(LlvmLibcPowF16Test, TrickyInput_SelectedX_AllY) { + for (float16 x_base : SELECTED_VALS) { + // Only test non-negative x_base + if (FPBits(x_base).is_neg()) + continue; + + // Loop through normal and subnormal values only (0x0001 to 0x7BFF) + for (uint16_t y_u = 1; y_u <= 0x7BFFU; ++y_u) { + float16 y_base = FPBits(y_u).get_val(); + + // Case 1: (+x, +y) - Standard positive case + mpfr::BinaryInput input1{x_base, y_base}; + EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Pow, input1, + LIBC_NAMESPACE::powf16(x_base, y_base), + 0.5); + + // Case 2: (+x, -y) - Always valid for positive x + float16 y_neg = -y_base; + mpfr::BinaryInput input2{x_base, y_neg}; + EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Pow, input2, + LIBC_NAMESPACE::powf16(x_base, y_neg), + 0.5); + } + + // Case 3: (-x, +y) - Only test with positive integer y values + for (int y_int = 1; y_int <= 2048; ++y_int) { + float16 y_val = static_cast(y_int); + float16 x_neg = -x_base; + mpfr::BinaryInput input{x_neg, y_val}; + EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Pow, input, + LIBC_NAMESPACE::powf16(x_neg, y_val), 0.5); + } + + // Case 4: (-x, -y) - Only test with negative integer y values + for (int y_int = -2048; y_int < 0; ++y_int) { + float16 y_val = static_cast(y_int); + float16 x_neg = -x_base; + mpfr::BinaryInput input{x_neg, y_val}; + EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Pow, input, + LIBC_NAMESPACE::powf16(x_neg, y_val), 0.5); + } + } +} + +// Test tricky inputs for selected y values against all possible x values. +TEST_F(LlvmLibcPowF16Test, TrickyInput_SelectedY_AllX) { + for (float16 y_base : SELECTED_VALS) { + // Only test non-negative y_base + if (FPBits(y_base).is_neg()) + continue; + + // Loop through normal and subnormal values only (0x0001 to 0x7BFF) + for (uint16_t x_u = 1; x_u <= 0x7BFFU; ++x_u) { + float16 x_base = FPBits(x_u).get_val(); + + // Case 1: (+x, +y) - Standard positive case + mpfr::BinaryInput input1{x_base, y_base}; + EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Pow, input1, + LIBC_NAMESPACE::powf16(x_base, y_base), + 0.5); + + // Case 2: (+x, -y) - Always valid for positive x + float16 y_neg = -y_base; + mpfr::BinaryInput input2{x_base, y_neg}; + EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Pow, input2, + LIBC_NAMESPACE::powf16(x_base, y_neg), + 0.5); + } + + // Case 3: (-x, +y) - Only test with positive integer x values + for (int x_int = 1; x_int <= 2048; ++x_int) { + float16 x_val = static_cast(x_int); + float16 x_neg = -x_val; + mpfr::BinaryInput input{x_neg, y_base}; + EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Pow, input, + LIBC_NAMESPACE::powf16(x_neg, y_base), + 0.5); + } + + // Case 4: (-x, -y) - Only test with negative integer x values + for (int x_int = 1; x_int <= 2048; ++x_int) { + float16 x_val = static_cast(x_int); + float16 x_neg = -x_val; + float16 y_neg = -y_base; + mpfr::BinaryInput input{x_neg, y_neg}; + EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Pow, input, + LIBC_NAMESPACE::powf16(x_neg, y_neg), 0.5); + } + } +} diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt index 5afd3a9f22967..0de13713b2fa9 100644 --- a/libc/test/src/math/smoke/CMakeLists.txt +++ b/libc/test/src/math/smoke/CMakeLists.txt @@ -5095,6 +5095,16 @@ add_fp_unittest( libc.src.__support.FPUtil.fp_bits ) +add_fp_unittest( + powf16_test + SUITE + libc-math-smoke-tests + SRCS + powf16_test.cpp + DEPENDS + libc.src.math.powf16 + libc.src.__support.FPUtil.fp_bits +) add_fp_unittest( totalorder_test SUITE diff --git a/libc/test/src/math/smoke/powf16_test.cpp b/libc/test/src/math/smoke/powf16_test.cpp new file mode 100644 index 0000000000000..f74bb6e57877e --- /dev/null +++ b/libc/test/src/math/smoke/powf16_test.cpp @@ -0,0 +1,210 @@ +//===-- Unittests for powf16 ----------------------------------------------===// +// +// 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 "hdr/fenv_macros.h" +#include "src/math/powf16.h" +#include "test/UnitTest/FPMatcher.h" +#include "test/UnitTest/Test.h" + +using LlvmLibcPowF16Test = LIBC_NAMESPACE::testing::FPTest; +using LIBC_NAMESPACE::fputil::testing::ForceRoundingMode; +using LIBC_NAMESPACE::fputil::testing::RoundingMode; + +TEST_F(LlvmLibcPowF16Test, SpecialNumbers) { + constexpr float16 NEG_ODD_INTEGER = -3.0f16; + constexpr float16 NEG_EVEN_INTEGER = -6.0f16; + constexpr float16 NEG_NON_INTEGER = -1.5f16; + constexpr float16 POS_ODD_INTEGER = 5.0f16; + constexpr float16 POS_EVEN_INTEGER = 8.0f16; + constexpr float16 POS_NON_INTEGER = 1.5f16; + constexpr float16 ONE_HALF = 0.5f16; + + for (int i = 0; i < N_ROUNDING_MODES; ++i) { + + ForceRoundingMode __r(ROUNDING_MODES[i]); + if (!__r.success) + continue; + + // powf16( sNaN, exponent ) + EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::powf16(sNaN, sNaN), + FE_INVALID); + EXPECT_FP_EQ_WITH_EXCEPTION( + aNaN, LIBC_NAMESPACE::powf16(sNaN, NEG_ODD_INTEGER), FE_INVALID); + EXPECT_FP_EQ_WITH_EXCEPTION( + aNaN, LIBC_NAMESPACE::powf16(sNaN, NEG_EVEN_INTEGER), FE_INVALID); + EXPECT_FP_EQ_WITH_EXCEPTION( + aNaN, LIBC_NAMESPACE::powf16(sNaN, POS_ODD_INTEGER), FE_INVALID); + EXPECT_FP_EQ_WITH_EXCEPTION( + aNaN, LIBC_NAMESPACE::powf16(sNaN, POS_EVEN_INTEGER), FE_INVALID); + EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::powf16(sNaN, ONE_HALF), + FE_INVALID); + EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::powf16(sNaN, zero), + FE_INVALID); + EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::powf16(sNaN, neg_zero), + FE_INVALID); + EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::powf16(sNaN, inf), + FE_INVALID); + EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::powf16(sNaN, neg_inf), + FE_INVALID); + EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::powf16(sNaN, aNaN), + FE_INVALID); + + // powf16( 0.0f16, exponent ) + EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::powf16(zero, sNaN), + FE_INVALID); + EXPECT_FP_EQ_WITH_EXCEPTION( + inf, LIBC_NAMESPACE::powf16(zero, NEG_ODD_INTEGER), FE_DIVBYZERO); + EXPECT_FP_EQ_WITH_EXCEPTION( + inf, LIBC_NAMESPACE::powf16(zero, NEG_EVEN_INTEGER), FE_DIVBYZERO); + EXPECT_FP_EQ_WITH_EXCEPTION( + inf, LIBC_NAMESPACE::powf16(zero, NEG_NON_INTEGER), FE_DIVBYZERO); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(zero, POS_ODD_INTEGER)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(zero, POS_EVEN_INTEGER)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(zero, POS_NON_INTEGER)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(zero, ONE_HALF)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(zero, zero)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(zero, neg_zero)); + EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::powf16(zero, inf)); + EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::powf16(zero, neg_inf), + FE_DIVBYZERO); + EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf16(zero, aNaN)); + + // powf16( -0.0f16, exponent ) + EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::powf16(neg_zero, sNaN), + FE_INVALID); + EXPECT_FP_EQ_WITH_EXCEPTION( + neg_inf, LIBC_NAMESPACE::powf16(neg_zero, NEG_ODD_INTEGER), + FE_DIVBYZERO); + EXPECT_FP_EQ_WITH_EXCEPTION( + inf, LIBC_NAMESPACE::powf16(neg_zero, NEG_EVEN_INTEGER), FE_DIVBYZERO); + EXPECT_FP_EQ_WITH_EXCEPTION( + inf, LIBC_NAMESPACE::powf16(neg_zero, NEG_NON_INTEGER), FE_DIVBYZERO); + EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::powf16(neg_zero, POS_ODD_INTEGER)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(neg_zero, POS_EVEN_INTEGER)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(neg_zero, POS_NON_INTEGER)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(neg_zero, ONE_HALF)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(neg_zero, zero)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(neg_zero, neg_zero)); + EXPECT_FP_EQ(0.0f16, LIBC_NAMESPACE::powf16(neg_zero, inf)); + EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::powf16(neg_zero, neg_inf), + FE_DIVBYZERO); + EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf16(neg_zero, aNaN)); + + // powf16( 1.0f16, exponent ) + EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::powf16(1.0f16, sNaN), + FE_INVALID); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(1.0f16, zero)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(1.0f16, neg_zero)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(1.0f16, 1.0f16)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(1.0f16, -1.0f16)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(1.0f16, NEG_ODD_INTEGER)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(1.0f16, NEG_EVEN_INTEGER)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(1.0f16, NEG_NON_INTEGER)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(1.0f16, POS_ODD_INTEGER)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(1.0f16, POS_EVEN_INTEGER)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(1.0f16, POS_NON_INTEGER)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(1.0f16, inf)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(1.0f16, neg_inf)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(1.0f16, aNaN)); + + // powf16( -1.0f16, exponent ) + EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::powf16(-1.0f16, sNaN), + FE_INVALID); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(-1.0f16, zero)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(-1.0f16, neg_zero)); + EXPECT_FP_EQ(-1.0f16, LIBC_NAMESPACE::powf16(-1.0f16, 1.0f16)); + EXPECT_FP_EQ(-1.0f16, LIBC_NAMESPACE::powf16(-1.0f16, -1.0f16)); + EXPECT_FP_EQ(-1.0f16, LIBC_NAMESPACE::powf16(-1.0f16, NEG_ODD_INTEGER)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(-1.0f16, NEG_EVEN_INTEGER)); + EXPECT_FP_IS_NAN_WITH_EXCEPTION( + LIBC_NAMESPACE::powf16(-1.0f16, NEG_NON_INTEGER), FE_INVALID); + EXPECT_FP_EQ(-1.0f16, LIBC_NAMESPACE::powf16(-1.0f16, POS_ODD_INTEGER)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(-1.0f16, POS_EVEN_INTEGER)); + EXPECT_FP_IS_NAN_WITH_EXCEPTION( + LIBC_NAMESPACE::powf16(-1.0f16, POS_NON_INTEGER), FE_INVALID); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(-1.0f16, inf)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(-1.0f16, neg_inf)); + EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf16(-1.0f16, aNaN)); + + // powf16( inf, exponent ) + EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::powf16(inf, sNaN), + FE_INVALID); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(inf, zero)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(inf, neg_zero)); + EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf16(inf, 1.0f16)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(inf, -1.0f16)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(inf, NEG_ODD_INTEGER)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(inf, NEG_EVEN_INTEGER)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(inf, NEG_NON_INTEGER)); + EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf16(inf, POS_ODD_INTEGER)); + EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf16(inf, POS_EVEN_INTEGER)); + EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf16(inf, POS_NON_INTEGER)); + EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf16(inf, ONE_HALF)); + EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf16(inf, inf)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(inf, neg_inf)); + EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf16(inf, aNaN)); + + // powf16( -inf, exponent ) + EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::powf16(neg_inf, sNaN), + FE_INVALID); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(neg_inf, zero)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(neg_inf, neg_zero)); + EXPECT_FP_EQ(neg_inf, LIBC_NAMESPACE::powf16(neg_inf, 1.0f16)); + EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::powf16(neg_inf, -1.0f16)); + EXPECT_FP_EQ(neg_zero, LIBC_NAMESPACE::powf16(neg_inf, NEG_ODD_INTEGER)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(neg_inf, NEG_EVEN_INTEGER)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(neg_inf, NEG_NON_INTEGER)); + EXPECT_FP_EQ(neg_inf, LIBC_NAMESPACE::powf16(neg_inf, POS_ODD_INTEGER)); + EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf16(neg_inf, POS_EVEN_INTEGER)); + EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf16(neg_inf, POS_NON_INTEGER)); + EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf16(neg_inf, ONE_HALF)); + EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf16(neg_inf, inf)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(neg_inf, neg_inf)); + EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf16(neg_inf, aNaN)); + + // powf16 ( aNaN, exponent ) + EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, LIBC_NAMESPACE::powf16(aNaN, sNaN), + FE_INVALID); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(aNaN, zero)); + EXPECT_FP_EQ(1.0f16, LIBC_NAMESPACE::powf16(aNaN, neg_zero)); + EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf16(aNaN, 1.0f16)); + EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf16(aNaN, -1.0f16)); + EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf16(aNaN, NEG_ODD_INTEGER)); + EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf16(aNaN, NEG_EVEN_INTEGER)); + EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf16(aNaN, NEG_NON_INTEGER)); + EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf16(aNaN, POS_ODD_INTEGER)); + EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf16(aNaN, POS_EVEN_INTEGER)); + EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf16(aNaN, POS_NON_INTEGER)); + EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf16(aNaN, inf)); + EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf16(aNaN, neg_inf)); + EXPECT_FP_IS_NAN(LIBC_NAMESPACE::powf16(aNaN, aNaN)); + + // powf16 ( base, inf ) + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(0.1f16, inf)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(-0.1f16, inf)); + EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf16(1.1f16, inf)); + EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf16(-1.1f16, inf)); + + // powf16 ( base, -inf ) + EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf16(0.1f16, neg_inf)); + EXPECT_FP_EQ(inf, LIBC_NAMESPACE::powf16(-0.1f16, neg_inf)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(1.1f16, neg_inf)); + EXPECT_FP_EQ(zero, LIBC_NAMESPACE::powf16(-1.1f16, neg_inf)); + + // Overflow / Underflow: + if (ROUNDING_MODES[i] != RoundingMode::Downward && + ROUNDING_MODES[i] != RoundingMode::TowardZero) { + EXPECT_FP_EQ_WITH_EXCEPTION(inf, LIBC_NAMESPACE::powf16(3.1f16, 21.0f16), + FE_OVERFLOW); + } + if (ROUNDING_MODES[i] != RoundingMode::Upward) { + EXPECT_FP_EQ_WITH_EXCEPTION( + 0.0f16, LIBC_NAMESPACE::powf16(3.1f16, -21.0f16), FE_UNDERFLOW); + } + } +}