#include "src/math/log10f16.h"
#include "hdr/errno_macros.h"
#include "hdr/fenv_macros.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/except_value_utils.h"
#include "src/__support/FPUtil/multiply_add.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/macros/optimization.h"
#include "src/__support/macros/properties/cpu_features.h"
#include "src/__support/math/expxf16_utils.h"
namespace LIBC_NAMESPACE_DECL {
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
#ifdef LIBC_TARGET_CPU_HAS_FMA_FLOAT
static constexpr size_t N_LOG10F16_EXCEPTS = 11;
#else
static constexpr size_t N_LOG10F16_EXCEPTS = 17;
#endif
static constexpr fputil::ExceptValues<float16, N_LOG10F16_EXCEPTS>
LOG10F16_EXCEPTS = {{
{0x338fU, 0xb903U, 0U, 1U, 0U},
{0x33f8U, 0xb8d4U, 0U, 1U, 1U},
#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
{0x38e5U, 0xb2d3U, 0U, 1U, 1U},
#endif
{0x3baaU, 0xa4c4U, 0U, 1U, 1U},
{0x3bacU, 0xa4a7U, 0U, 1U, 1U},
{0x3bccU, 0xa1b7U, 0U, 1U, 1U},
#ifndef LIBC_TARGET_CPU_HAS_FMA_FLOAT
{0x3bceU, 0xa17eU, 0U, 1U, 1U},
{0x3bf6U, 0x985aU, 0U, 1U, 1U},
{0x3bfeU, 0x8ef3U, 0U, 1U, 1U},
{0x3cddU, 0x2d6eU, 1U, 0U, 1U},
{0x40fbU, 0x3656U, 1U, 0U, 1U},
#endif
{0x4900U, 0x3c00U, 0U, 0U, 0U},
{0x5640U, 0x4000U, 0U, 0U, 0U},
{0x57e1U, 0x4033U, 1U, 0U, 0U},
{0x63d0U, 0x4200U, 0U, 0U, 0U},
{0x70e2U, 0x4400U, 0U, 0U, 0U},
{0x719dU, 0x440fU, 1U, 0U, 0U},
}};
#endif
LLVM_LIBC_FUNCTION(float16, log10f16, (float16 x)) {
using namespace math::expxf16_internal;
using FPBits = fputil::FPBits<float16>;
FPBits x_bits(x);
uint16_t x_u = x_bits.uintval();
if (LIBC_UNLIKELY(x_u == 0U || x_u == 0x3c00U || x_u >= 0x7c00U)) {
if (x_bits.is_nan()) {
if (x_bits.is_signaling_nan()) {
fputil::raise_except_if_required(FE_INVALID);
return FPBits::quiet_nan().get_val();
}
return x;
}
if ((x_u & 0x7fffU) == 0U) {
fputil::raise_except_if_required(FE_DIVBYZERO);
return FPBits::inf(Sign::NEG).get_val();
}
if (x_u == 0x3c00U)
return FPBits::zero().get_val();
if (x_u > 0x8000U) {
fputil::set_errno_if_required(EDOM);
fputil::raise_except_if_required(FE_INVALID);
return FPBits::quiet_nan().get_val();
}
return FPBits::inf().get_val();
}
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
if (auto r = LOG10F16_EXCEPTS.lookup(x_u); LIBC_UNLIKELY(r.has_value()))
return r.value();
#endif
int m = -FPBits::EXP_BIAS;
if ((x_u & FPBits::EXP_MASK) == 0U) {
constexpr float NORMALIZE_EXP = 1U << FPBits::FRACTION_LEN;
x_bits = FPBits(x_bits.get_val() * fputil::cast<float16>(NORMALIZE_EXP));
x_u = x_bits.uintval();
m -= FPBits::FRACTION_LEN;
}
uint16_t mant = x_bits.get_mantissa();
int f = mant >> 5;
m += x_u >> FPBits::FRACTION_LEN;
x_bits.set_biased_exponent(FPBits::EXP_BIAS);
float mant_f = x_bits.get_val();
float v = fputil::multiply_add(mant_f, ONE_OVER_F_F[f], -1.0f);
float log10p1_d_over_f =
v * fputil::polyeval(v, 0x1.bcb7bp-2f, -0x1.bce168p-3f, 0x1.28acb8p-3f);
float log10_1_mant = LOG10F_F[f] + log10p1_d_over_f;
return fputil::cast<float16>(
fputil::multiply_add(static_cast<float>(m), LOG10F_2, log10_1_mant));
}
}