diff options
| author | Michael Kruse <llvm-project@meinersbur.de> | 2025-01-03 10:22:51 +0100 |
|---|---|---|
| committer | Michael Kruse <llvm-project@meinersbur.de> | 2025-01-03 10:22:51 +0100 |
| commit | 38500d63e14ce340236840f60d356cdefb56a52c (patch) | |
| tree | 17edbec446ce9b50d2f215a483b83afb293a635d /libc/src/math | |
| parent | 1a3d5daaef7a6a63448a497da3eff7fc9e23df26 (diff) | |
| parent | 27f30029741ecf023baece7b3dde1ff9011ffefc (diff) | |
Merge branch 'main' into users/meinersbur/flang_runtime_split-headersusers/meinersbur/flang_runtime_split-headers
Diffstat (limited to 'libc/src/math')
| -rw-r--r-- | libc/src/math/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | libc/src/math/cosf16.h | 21 | ||||
| -rw-r--r-- | libc/src/math/docs/add_math_function.md | 2 | ||||
| -rw-r--r-- | libc/src/math/generic/CMakeLists.txt | 19 | ||||
| -rw-r--r-- | libc/src/math/generic/cosf16.cpp | 84 | ||||
| -rw-r--r-- | libc/src/math/generic/cospif16.cpp | 2 | ||||
| -rw-r--r-- | libc/src/math/generic/sinf16.cpp | 15 |
7 files changed, 133 insertions, 11 deletions
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt index 390a59d07a28..e4e2c49642f2 100644 --- a/libc/src/math/CMakeLists.txt +++ b/libc/src/math/CMakeLists.txt @@ -89,6 +89,7 @@ add_math_entrypoint_object(copysignf128) add_math_entrypoint_object(cos) add_math_entrypoint_object(cosf) +add_math_entrypoint_object(cosf16) add_math_entrypoint_object(cosh) add_math_entrypoint_object(coshf) diff --git a/libc/src/math/cosf16.h b/libc/src/math/cosf16.h new file mode 100644 index 000000000000..cc179a6e16c6 --- /dev/null +++ b/libc/src/math/cosf16.h @@ -0,0 +1,21 @@ +//===-- Implementation header for cosf16 ------------------------*- 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_COSF16_H +#define LLVM_LIBC_SRC_MATH_COSF16_H + +#include "src/__support/macros/config.h" +#include "src/__support/macros/properties/types.h" + +namespace LIBC_NAMESPACE_DECL { + +float16 cosf16(float16 x); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_MATH_COSF16_H diff --git a/libc/src/math/docs/add_math_function.md b/libc/src/math/docs/add_math_function.md index f02d502399e2..daaf1a3ec563 100644 --- a/libc/src/math/docs/add_math_function.md +++ b/libc/src/math/docs/add_math_function.md @@ -18,7 +18,7 @@ together with its specifications: ``` - Add function specs to the file: ``` - libc/hdrgen/yaml/math.yaml + libc/include/math.yaml ``` ## Implementation diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt index aeb758d4a092..b3d461291519 100644 --- a/libc/src/math/generic/CMakeLists.txt +++ b/libc/src/math/generic/CMakeLists.txt @@ -419,6 +419,25 @@ add_entrypoint_object( ) add_entrypoint_object( + cosf16 + SRCS + cosf16.cpp + HDRS + ../cosf16.h + DEPENDS + .sincosf16_utils + libc.hdr.errno_macros + libc.hdr.fenv_macros + libc.src.__support.FPUtil.cast + libc.src.__support.FPUtil.fenv_impl + libc.src.__support.FPUtil.fp_bits + libc.src.__support.FPUtil.except_value_utils + libc.src.__support.FPUtil.multiply_add + libc.src.__support.macros.optimization + libc.src.__support.macros.properties.types +) + +add_entrypoint_object( cospif SRCS cospif.cpp diff --git a/libc/src/math/generic/cosf16.cpp b/libc/src/math/generic/cosf16.cpp new file mode 100644 index 000000000000..534e07854474 --- /dev/null +++ b/libc/src/math/generic/cosf16.cpp @@ -0,0 +1,84 @@ +//===-- Half-precision cos(x) 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/cosf16.h" +#include "hdr/errno_macros.h" +#include "hdr/fenv_macros.h" +#include "sincosf16_utils.h" +#include "src/__support/FPUtil/FEnvImpl.h" +#include "src/__support/FPUtil/FPBits.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/macros/optimization.h" + +namespace LIBC_NAMESPACE_DECL { + +constexpr size_t N_EXCEPTS = 4; + +constexpr fputil::ExceptValues<float16, N_EXCEPTS> COSF16_EXCEPTS{{ + // (input, RZ output, RU offset, RD offset, RN offset) + {0x2b7c, 0x3bfc, 1, 0, 1}, + {0x4ac1, 0x38b5, 1, 0, 0}, + {0x5c49, 0xb8c6, 0, 1, 0}, + {0x7acc, 0xa474, 0, 1, 0}, +}}; + +LLVM_LIBC_FUNCTION(float16, cosf16, (float16 x)) { + using FPBits = fputil::FPBits<float16>; + FPBits xbits(x); + + uint16_t x_u = xbits.uintval(); + uint16_t x_abs = x_u & 0x7fff; + float xf = x; + + // Range reduction: + // For |x| > pi/32, we perform range reduction as follows: + // Find k and y such that: + // x = (k + y) * pi/32 + // k is an integer, |y| < 0.5 + // + // This is done by performing: + // k = round(x * 32/pi) + // y = x * 32/pi - k + // + // Once k and y are computed, we then deduce the answer by the cosine of sum + // formula: + // cos(x) = cos((k + y) * pi/32) + // = cos(k * pi/32) * cos(y * pi/32) - + // sin(k * pi/32) * sin(y * pi/32) + + // Handle exceptional values + if (auto r = COSF16_EXCEPTS.lookup(x_abs); LIBC_UNLIKELY(r.has_value())) + return r.value(); + + // cos(+/-0) = 1 + if (LIBC_UNLIKELY(x_abs == 0U)) + return fputil::cast<float16>(1.0f); + + // cos(+/-inf) = NaN, and cos(NaN) = NaN + if (xbits.is_inf_or_nan()) { + if (xbits.is_inf()) { + fputil::set_errno_if_required(EDOM); + fputil::raise_except_if_required(FE_INVALID); + } + + return x + FPBits::quiet_nan().get_val(); + } + + float sin_k, cos_k, sin_y, cosm1_y; + sincosf16_eval(xf, sin_k, cos_k, sin_y, cosm1_y); + // Since, cosm1_y = cos_y - 1, therefore: + // cos(x) = cos_k * cos_y - sin_k * sin_y + // = cos_k * (cos_y - 1 + 1) - sin_k * sin_y + // = cos_k * cosm1_y - sin_k * sin_y + cos_k + return fputil::cast<float16>(fputil::multiply_add( + cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k))); +} + +} // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/math/generic/cospif16.cpp b/libc/src/math/generic/cospif16.cpp index 384b39ff8e2c..ee74bdb4a369 100644 --- a/libc/src/math/generic/cospif16.cpp +++ b/libc/src/math/generic/cospif16.cpp @@ -73,7 +73,7 @@ LLVM_LIBC_FUNCTION(float16, cospif16, (float16 x)) { return fputil::cast<float16>(0.0f); // Since, cosm1_y = cos_y - 1, therefore: - // cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y + // cos(x * pi) = cos_k(cosm1_y) + cos_k - sin_k * sin_y return fputil::cast<float16>(fputil::multiply_add( cos_k, cosm1_y, fputil::multiply_add(-sin_k, sin_y, cos_k))); } diff --git a/libc/src/math/generic/sinf16.cpp b/libc/src/math/generic/sinf16.cpp index 86546348ba73..7c15951cccec 100644 --- a/libc/src/math/generic/sinf16.cpp +++ b/libc/src/math/generic/sinf16.cpp @@ -54,13 +54,10 @@ LLVM_LIBC_FUNCTION(float16, sinf16, (float16 x)) { // sin(y * pi/32) * cos(k * pi/32) // Handle exceptional values - if (LIBC_UNLIKELY(x_abs == 0x585c || x_abs == 0x5cb0 || x_abs == 0x51f5 || - x_abs == 0x2b45)) { - bool x_sign = x_u >> 15; - if (auto r = SINF16_EXCEPTS.lookup_odd(x_abs, x_sign); - LIBC_UNLIKELY(r.has_value())) - return r.value(); - } + bool x_sign = x_u >> 15; + if (auto r = SINF16_EXCEPTS.lookup_odd(x_abs, x_sign); + LIBC_UNLIKELY(r.has_value())) + return r.value(); int rounding = fputil::quick_get_round(); @@ -99,8 +96,8 @@ LLVM_LIBC_FUNCTION(float16, sinf16, (float16 x)) { if (LIBC_UNLIKELY(sin_y == 0 && sin_k == 0)) return FPBits::zero(xbits.sign()).get_val(); - // Since, cosm1_y = cos_y - 1, therfore: - // sin(x) = cos_k * sin_y + sin_k + (cosm1_y * sin_k) + // Since, cosm1_y = cos_y - 1, therefore: + // sin(x) = cos_k * sin_y + sin_k + (cosm1_y * sin_k) return fputil::cast<float16>(fputil::multiply_add( sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k))); } |
