Skip to content

[libc][math] Refactor atanf16 implementation to header-only in src/__support/math folder. #150868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libc/shared/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "math/asinhf16.h"
#include "math/atan.h"
#include "math/atanf.h"
#include "math/atanf16.h"
#include "math/erff.h"
#include "math/exp.h"
#include "math/exp10.h"
Expand Down
28 changes: 28 additions & 0 deletions libc/shared/math/atanf16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===-- Shared atanf16 function ---------------------------------*- 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_SHARED_MATH_ATANF16_H
#define LLVM_LIBC_SHARED_MATH_ATANF16_H

#include "shared/libc_common.h"

#ifdef LIBC_TYPES_HAS_FLOAT16

#include "src/__support/math/atanf16.h"

namespace LIBC_NAMESPACE_DECL {
namespace shared {

using math::atanf16;

} // namespace shared
} // namespace LIBC_NAMESPACE_DECL

#endif // LIBC_TYPES_HAS_FLOAT16

#endif // LLVM_LIBC_SHARED_MATH_ATANF16_H
15 changes: 15 additions & 0 deletions libc/src/__support/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ add_header_library(
libc.src.__support.macros.optimization
)

add_header_library(
atanf16
HDRS
atanf16.h
DEPENDS
libc.src.__support.FPUtil.cast
libc.src.__support.FPUtil.except_value_utils
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.multiply_add
libc.src.__support.FPUtil.polyeval
libc.src.__support.FPUtil.sqrt
libc.src.__support.macros.optimization
)

add_header_library(
asinf
HDRS
Expand Down
119 changes: 119 additions & 0 deletions libc/src/__support/math/atanf16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//===-- Implementation header for atanf16 -----------------------*- 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___SUPPORT_MATH_ATANF16_H
#define LLVM_LIBC_SRC___SUPPORT_MATH_ATANF16_H

#include "include/llvm-libc-macros/float16-macros.h"

#ifdef LIBC_TYPES_HAS_FLOAT16

#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/FPUtil/sqrt.h"
#include "src/__support/macros/optimization.h"

namespace LIBC_NAMESPACE_DECL {

namespace math {

LIBC_INLINE static constexpr float16 atanf16(float16 x) {
// Generated by Solly using the following command:
// > round(pi/2, SG, RN);
constexpr float PI_2 = 0x1.921fb6p0;

#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
constexpr size_t N_EXCEPTS = 6;

constexpr fputil::ExceptValues<float16, N_EXCEPTS> ATANF16_EXCEPTS{{
// (input, RZ output, RU offset, RD offset, RN offset)
{0x2745, 0x2744, 1, 0, 1},
{0x3099, 0x3090, 1, 0, 1},
{0x3c6c, 0x3aae, 1, 0, 1},
{0x466e, 0x3daa, 1, 0, 1},
{0x48ae, 0x3ddb, 1, 0, 0},
{0x5619, 0x3e3d, 1, 0, 1},
}};
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS

using FPBits = fputil::FPBits<float16>;
FPBits xbits(x);

uint16_t x_u = xbits.uintval();
uint16_t x_abs = x_u & 0x7fff;
bool x_sign = x_u >> 15;
float sign = (x_sign ? -1.0 : 1.0);

// |x| >= +/-inf
if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
if (xbits.is_nan()) {
if (xbits.is_signaling_nan()) {
fputil::raise_except_if_required(FE_INVALID);
return FPBits::quiet_nan().get_val();
}
return x;
}

// atanf16(+/-inf) = +/-pi/2
return fputil::cast<float16>(sign * PI_2);
}

float xf = x;
float xsq = xf * xf;
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
// Handle exceptional values
if (auto r = ATANF16_EXCEPTS.lookup_odd(x_abs, x_sign);
LIBC_UNLIKELY(r.has_value()))
return r.value();
#endif

// |x| <= 0x1p0, |x| <= 1
if (x_abs <= 0x3c00) {
// atanf16(+/-0) = +/-0
if (LIBC_UNLIKELY(x_abs == 0))
return x;

// Degree-14 minimax odd polynomial of atan(x) generated by Sollya with:
// > P = fpminimax(atan(x)/x, [|0, 2, 4, 6, 8, 10, 12, 14|], [|SG...|],
// [0, 1]);
float result = fputil::polyeval(
xsq, 0x1.fffffcp-1f, -0x1.55519ep-2f, 0x1.98f6a8p-3f, -0x1.1f0a92p-3f,
0x1.95b654p-4f, -0x1.e65492p-5f, 0x1.8c0c36p-6f, -0x1.32316ep-8f);
return fputil::cast<float16>(xf * result);
}

// If |x| > 1
// y = atan(x) = sign(x) * atan(|x|)
// atan(|x|) = pi/2 - atan(1/|x|)
// Recall, 1/|x| < 1
float x_inv_sq = 1.0f / xsq;
float x_inv = fputil::sqrt<float>(x_inv_sq);

// Degree-14 minimax odd polynomial of atan(x) generated by Sollya with:
// > P = fpminimax(atan(x)/x, [|0, 2, 4, 6, 8, 10, 12, 14|], [|SG...|],
// [0, 1]);
float interm =
fputil::polyeval(x_inv_sq, 0x1.fffffcp-1f, -0x1.55519ep-2f,
0x1.98f6a8p-3f, -0x1.1f0a92p-3f, 0x1.95b654p-4f,
-0x1.e65492p-5f, 0x1.8c0c36p-6f, -0x1.32316ep-8f);

return fputil::cast<float16>(sign *
fputil::multiply_add(x_inv, -interm, PI_2));
}

} // namespace math

} // namespace LIBC_NAMESPACE_DECL

#endif // LIBC_TYPES_HAS_FLOAT16

#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ATANF16_H
12 changes: 1 addition & 11 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4037,17 +4037,7 @@ add_entrypoint_object(
HDRS
../atanf16.h
DEPENDS
libc.hdr.errno_macros
libc.hdr.fenv_macros
libc.src.__support.FPUtil.cast
libc.src.__support.FPUtil.except_value_utils
libc.src.__support.FPUtil.fenv_impl
libc.src.__support.FPUtil.fp_bits
libc.src.__support.FPUtil.multiply_add
libc.src.__support.FPUtil.polyeval
libc.src.__support.FPUtil.sqrt
libc.src.__support.macros.optimization
libc.src.__support.macros.properties.types
libc.src.__support.math.atanf16
)

add_entrypoint_object(
Expand Down
95 changes: 2 additions & 93 deletions libc/src/math/generic/atanf16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,101 +7,10 @@
//===----------------------------------------------------------------------===//

#include "src/math/atanf16.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/FPUtil/sqrt.h"
#include "src/__support/macros/optimization.h"
#include "src/__support/math/atanf16.h"

namespace LIBC_NAMESPACE_DECL {

// Generated by Solly using the following command:
// > round(pi/2, SG, RN);
static constexpr float PI_2 = 0x1.921fb6p0;

#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
static constexpr size_t N_EXCEPTS = 6;

static constexpr fputil::ExceptValues<float16, N_EXCEPTS> ATANF16_EXCEPTS{{
// (input, RZ output, RU offset, RD offset, RN offset)
{0x2745, 0x2744, 1, 0, 1},
{0x3099, 0x3090, 1, 0, 1},
{0x3c6c, 0x3aae, 1, 0, 1},
{0x466e, 0x3daa, 1, 0, 1},
{0x48ae, 0x3ddb, 1, 0, 0},
{0x5619, 0x3e3d, 1, 0, 1},
}};
#endif // !LIBC_MATH_HAS_SKIP_ACCURATE_PASS

LLVM_LIBC_FUNCTION(float16, atanf16, (float16 x)) {
using FPBits = fputil::FPBits<float16>;
FPBits xbits(x);

uint16_t x_u = xbits.uintval();
uint16_t x_abs = x_u & 0x7fff;
bool x_sign = x_u >> 15;
float sign = (x_sign ? -1.0 : 1.0);

// |x| >= +/-inf
if (LIBC_UNLIKELY(x_abs >= 0x7c00)) {
if (xbits.is_nan()) {
if (xbits.is_signaling_nan()) {
fputil::raise_except_if_required(FE_INVALID);
return FPBits::quiet_nan().get_val();
}
return x;
}

// atanf16(+/-inf) = +/-pi/2
return fputil::cast<float16>(sign * PI_2);
}

float xf = x;
float xsq = xf * xf;
#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
// Handle exceptional values
if (auto r = ATANF16_EXCEPTS.lookup_odd(x_abs, x_sign);
LIBC_UNLIKELY(r.has_value()))
return r.value();
#endif

// |x| <= 0x1p0, |x| <= 1
if (x_abs <= 0x3c00) {
// atanf16(+/-0) = +/-0
if (LIBC_UNLIKELY(x_abs == 0))
return x;

// Degree-14 minimax odd polynomial of atan(x) generated by Sollya with:
// > P = fpminimax(atan(x)/x, [|0, 2, 4, 6, 8, 10, 12, 14|], [|SG...|],
// [0, 1]);
float result = fputil::polyeval(
xsq, 0x1.fffffcp-1f, -0x1.55519ep-2f, 0x1.98f6a8p-3f, -0x1.1f0a92p-3f,
0x1.95b654p-4f, -0x1.e65492p-5f, 0x1.8c0c36p-6f, -0x1.32316ep-8f);
return fputil::cast<float16>(xf * result);
}

// If |x| > 1
// y = atan(x) = sign(x) * atan(|x|)
// atan(|x|) = pi/2 - atan(1/|x|)
// Recall, 1/|x| < 1
float x_inv_sq = 1.0f / xsq;
float x_inv = fputil::sqrt<float>(x_inv_sq);

// Degree-14 minimax odd polynomial of atan(x) generated by Sollya with:
// > P = fpminimax(atan(x)/x, [|0, 2, 4, 6, 8, 10, 12, 14|], [|SG...|],
// [0, 1]);
float interm =
fputil::polyeval(x_inv_sq, 0x1.fffffcp-1f, -0x1.55519ep-2f,
0x1.98f6a8p-3f, -0x1.1f0a92p-3f, 0x1.95b654p-4f,
-0x1.e65492p-5f, 0x1.8c0c36p-6f, -0x1.32316ep-8f);

return fputil::cast<float16>(sign *
fputil::multiply_add(x_inv, -interm, PI_2));
}
LLVM_LIBC_FUNCTION(float16, atanf16, (float16 x)) { return math::atanf16(x); }

} // namespace LIBC_NAMESPACE_DECL
1 change: 1 addition & 0 deletions libc/test/shared/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_fp_unittest(
libc.src.__support.math.asinhf16
libc.src.__support.math.atan
libc.src.__support.math.atanf
libc.src.__support.math.atanf16
libc.src.__support.math.erff
libc.src.__support.math.exp
libc.src.__support.math.exp10
Expand Down
1 change: 1 addition & 0 deletions libc/test/shared/shared_math_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ TEST(LlvmLibcSharedMathTest, AllFloat16) {
EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::acospif16(1.0f16));
EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::asinf16(0.0f16));
EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::asinhf16(0.0f16));
EXPECT_FP_EQ(0x0p+0f16, LIBC_NAMESPACE::shared::atanf16(0.0f16));

EXPECT_FP_EQ(0x1p+0f16, LIBC_NAMESPACE::shared::exp10f16(0.0f16));

Expand Down
22 changes: 22 additions & 0 deletions utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2288,6 +2288,21 @@ libc_support_library(
],
)

libc_support_library(
name = "__support_math_atanf16",
hdrs = ["src/__support/math/atanf16.h"],
deps = [
":__support_fputil_cast",
":__support_fputil_except_value_utils",
":__support_fputil_fenv_impl",
":__support_fputil_fp_bits",
":__support_fputil_polyeval",
":__support_fputil_multiply_add",
":__support_fputil_sqrt",
":__support_macros_optimization",
],
)

libc_support_library(
name = "__support_math_asinf",
hdrs = ["src/__support/math/asinf.h"],
Expand Down Expand Up @@ -2904,6 +2919,13 @@ libc_math_function(
],
)

libc_math_function(
name = "atanf16",
additional_deps = [
":__support_math_atanf16"
],
)

libc_math_function(
name = "atan",
additional_deps = [
Expand Down
Loading