[libc-commits] [libc] [libc][math][c23] Add tanf16 function (PR #121018)
via libc-commits
libc-commits at lists.llvm.org
Mon Dec 30 11:00:47 PST 2024
================
@@ -0,0 +1,112 @@
+//===-- Half-precision tan(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/tanf16.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 = 9;
+
+constexpr fputil::ExceptValues<float16, N_EXCEPTS> TANF16_EXCEPTS{{
+ // (input, RZ output, RU offset, RD offset, RN offset)
+ {0x2894, 0x2894, 1, 0, 1},
+ {0x3091, 0x3099, 1, 0, 0},
+ {0x3098, 0x30a0, 1, 0, 0},
+ {0x55ed, 0x3911, 1, 0, 0},
+ {0x607b, 0xc638, 0, 1, 1},
+ {0x674e, 0x3b7d, 1, 0, 0},
+ {0x6807, 0x4014, 1, 0, 1},
+ {0x6f4d, 0xbe19, 0, 1, 1},
+ {0x7330, 0xcb62, 0, 1, 0},
+}};
+
+LLVM_LIBC_FUNCTION(float16, tanf16, (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 xf = x;
+
+ // Handle exceptional values
+ if (auto r = TANF16_EXCEPTS.lookup_odd(x_abs, x_sign);
+ LIBC_UNLIKELY(r.has_value()))
+ return r.value();
+
+ // |x| <= 0x1.d1p-5
+ if (LIBC_UNLIKELY(x_abs <= 0x2b44)) {
+ if (LIBC_UNLIKELY(x_abs <= 0x10e6)) {
+ // tan(+/-0) = +/-0
+ if (LIBC_UNLIKELY(x_abs == 0U))
+ return x;
+
+ int rounding = fputil::quick_get_round();
+
+ // Exhaustive tests show that, when:
+ // x > 0, and rounding upward or
+ // x < 0, and rounding downward then,
+ // tan(x) = x * 2^-11 + x
+ if ((xbits.is_pos() && rounding == FE_UPWARD) ||
+ (xbits.is_neg() && rounding == FE_DOWNWARD))
+ return fputil::cast<float16>(fputil::multiply_add(xf, 0x1.0p-11f, xf));
+ else
+ return x;
----------------
overmighty wrote:
Nit: https://llvm.org/docs/CodingStandards.html#don-t-use-else-after-a-return.
```suggestion
if ((xbits.is_pos() && rounding == FE_UPWARD) ||
(xbits.is_neg() && rounding == FE_DOWNWARD))
return fputil::cast<float16>(fputil::multiply_add(xf, 0x1.0p-11f, xf));
return x;
```
https://github.com/llvm/llvm-project/pull/121018
More information about the libc-commits
mailing list