[libc-commits] [libc] [llvm] [libc][math][c23] Add atan2f16 function (PR #183531)

Xinlong Chen via libc-commits libc-commits at lists.llvm.org
Sat Feb 28 00:42:50 PST 2026


================
@@ -0,0 +1,153 @@
+//===-- Implementation header for atan2f16 ----------------------*- 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_ATAN2F16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ATAN2F16_H
+
+#include "include/llvm-libc-macros/float16-macros.h"
+
+#ifdef LIBC_TYPES_HAS_FLOAT16
+
+#include "src/__support/CPP/optional.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/macros/optimization.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace math {
+
+namespace atan2f16_internal {
+
+#ifndef LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+// (x_abs, y_abs) keys for exceptional atan2f16 inputs;
+// index i -> ExceptValues input.
+LIBC_INLINE_VAR constexpr size_t N_ATAN2F16_EXCEPTS = 11;
+LIBC_INLINE_VAR constexpr uint16_t ATAN2F16_EXCEPT_X[] = {
+    0x37DA, 0x3A01, 0x3EFD, 0x4059, 0x40CD, 0x3D84,
+    0x38C2, 0x3814, 0x3596, 0x41B5, 0x3C62};
+LIBC_INLINE_VAR constexpr uint16_t ATAN2F16_EXCEPT_Y[] = {
+    0x3631, 0x3BFE, 0x398B, 0x3E2F, 0x4378, 0x354A,
+    0x3A93, 0x3C1F, 0x4189, 0x3CA3, 0x3EB3};
+
+// (input = index, RZ result, RU offset, RD offset, RN offset).
+LIBC_INLINE_VAR constexpr fputil::ExceptValues<float16, N_ATAN2F16_EXCEPTS>
+    ATAN2F16_EXCEPTS{{
+        {0, 0x3957, 1, 0, 0},
+        {1, 0x3B69, 1, 0, 0},
+        {2, 0x360A, 1, 0, 1},
+        {3, 0x38F2, 1, 0, 0},
+        {4, 0x3BFE, 1, 0, 1},
+        {5, 0x3387, 1, 0, 0},
+        {6, 0x3B8D, 1, 0, 1},
+        {7, 0x3C71, 1, 0, 1},
+        {8, 0x3DC7, 1, 0, 1},
+        {9, 0x362C, 1, 0, 1},
+        {10, 0x3BEE, 1, 0, 0},
+    }};
+
+LIBC_INLINE constexpr cpp::optional<float16>
+lookup_atan2f16_except(uint16_t x_abs, uint16_t y_abs) {
+  for (size_t i = 0; i < N_ATAN2F16_EXCEPTS; ++i) {
+    if (x_abs == ATAN2F16_EXCEPT_X[i] && y_abs == ATAN2F16_EXCEPT_Y[i])
+      return ATAN2F16_EXCEPTS.lookup(static_cast<uint16_t>(i));
+  }
+  return cpp::nullopt;
+}
+
+#endif // LIBC_MATH_HAS_SKIP_ACCURATE_PASS
+
+// atan(u) for u in (0, 1]: atan(u) = u * P(u^2).
----------------
Xinlong-Chen wrote:

This is Taylor polynomial, which is no longer needed in the new implementation

https://github.com/llvm/llvm-project/pull/183531


More information about the libc-commits mailing list