[libc] [llvm] [libc][math][c23] Implement higher math function `cbrtf16` in LLVM libc (PR #132484)

Krishna Pandey via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 22 08:53:59 PDT 2025


================
@@ -0,0 +1,164 @@
+//===-- Implementation of sqrtf16 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/cbrtf16.h"
+#include "hdr/fenv_macros.h"
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.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" // LIBC_UNLIKELY
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace {
+
+// Look up table for 2^(i/3) for i = 0, 1, 2 in single precision
+constexpr float CBRT2[3] = {0x1p0f, 0x1.428a3p0f, 0x1.965feap0f};
+
+// Degree-4 polynomials approximation of ((1 + x)^(1/3) - 1)/x for 0 <= x <= 1
+// generated by Sollya with:
+// > display=hexadecimal;
+// for i from 0 to 15 do {
+//   P = fpminimax(((1 + x)^(1/3) - 1)/x, 4, [|SG...|], [i/16, (i + 1)/16]);
+//   print("{", coeff(P, 0), ",", coeff(P, 1), ",", coeff(P, 2), ",",
+//         coeff(P, 3), coeff(P, 4),"},");
+// };
+// Then (1 + x)^(1/3) ~ 1 + x * P(x).
+// For example: for 0 <= x <= 1/8:
+// P(x) = 0x1.555556p-2 + x * (-0x1.c71d38p-4 + x * (0x1.f9b95ap-5 + x *
+// (-0x1.4ebe18p-5 + x * 0x1.9ca9d2p-6)))
+
+constexpr float COEFFS[16][5] = {
+    {0x1.555556p-2f, -0x1.c71ea4p-4f, 0x1.faa5f2p-5f, -0x1.64febep-5f,
+     0x1.733a46p-5f},
+    {0x1.55554ep-2f, -0x1.c715f6p-4f, 0x1.f88a9ep-5f, -0x1.4456e8p-5f,
+     0x1.5b5ef2p-6f},
+    {0x1.555508p-2f, -0x1.c6f404p-4f, 0x1.f56b7ap-5f, -0x1.33cff8p-5f,
+     0x1.18f146p-6f},
+    {0x1.5553fcp-2f, -0x1.c69bacp-4f, 0x1.efed98p-5f, -0x1.204706p-5f,
+     0x1.c90976p-7f},
+    {0x1.55517p-2f, -0x1.c5f996p-4f, 0x1.e85932p-5f, -0x1.0c0c0ep-5f,
+     0x1.77c766p-7f},
+    {0x1.554c96p-2f, -0x1.c501d2p-4f, 0x1.df0fc4p-5f, -0x1.f067f2p-6f,
+     0x1.380ab8p-7f},
+    {0x1.55448cp-2f, -0x1.c3ab1ep-4f, 0x1.d45876p-5f, -0x1.ca3988p-6f,
+     0x1.04f38ap-7f},
+    {0x1.5538aap-2f, -0x1.c1f886p-4f, 0x1.c8b11p-5f, -0x1.a6a16cp-6f,
+     0x1.b847c2p-8f},
+    {0x1.55278ap-2f, -0x1.bfd538p-4f, 0x1.bbde6p-5f, -0x1.846a8cp-6f,
+     0x1.73bfcp-8f},
+    {0x1.5511dp-2f, -0x1.bd6c88p-4f, 0x1.af0a3ap-5f, -0x1.660852p-6f,
+     0x1.3dbe34p-8f},
+    {0x1.54f82ap-2f, -0x1.bada56p-4f, 0x1.a2aa0ep-5f, -0x1.4b8c2ap-6f,
+     0x1.13379cp-8f},
+    {0x1.54d512p-2f, -0x1.b7a936p-4f, 0x1.94b91ep-5f, -0x1.30792cp-6f,
+     0x1.d7883cp-9f},
+    {0x1.54a8d8p-2f, -0x1.b3fde2p-4f, 0x1.861aeep-5f, -0x1.169484p-6f,
+     0x1.92b4cap-9f},
+    {0x1.548126p-2f, -0x1.b0f4a8p-4f, 0x1.7af574p-5f, -0x1.04644ep-6f,
+     0x1.662fb6p-9f},
+    {0x1.544b9p-2f, -0x1.ad2124p-4f, 0x1.6dd75p-5f, -0x1.e0cbecp-7f,
+     0x1.387692p-9f},
+    {0x1.5422c6p-2f, -0x1.aa61bp-4f, 0x1.64f4bap-5f, -0x1.c742b2p-7f,
+     0x1.1cf15ap-9f},
+};
+
+} // anonymous namespace
+
+LLVM_LIBC_FUNCTION(float16, cbrtf16, (float16 x)) {
+  using FPBits = fputil::FPBits<float16>;
+  using FloatBits = fputil::FPBits<float>;
+
+  FPBits x_bits(x);
+
+  uint16_t x_u = x_bits.uintval();
+  uint16_t x_abs = x_u & 0x7fff;
+  uint32_t sign_bit = (x_u >> 15) << FloatBits::EXP_LEN;
+
+  // cbrtf16(0) = 0, cbrtf16(NaN) = NaN
+  if (LIBC_UNLIKELY(x_abs == 0 || x_abs >= 0x7C00)) {
+    if (x_bits.is_signaling_nan()) {
+      fputil::raise_except(FE_INVALID);
+      return FPBits::quiet_nan().uintval();
+    }
+    return x;
+  }
+
+  float xf = static_cast<float>(x);
+  FloatBits xf_bits(xf);
+
+  unsigned x_e = static_cast<unsigned>(xf_bits.get_exponent());
+  unsigned out_e = x_e / 3 + 127;
----------------
krishna2803 wrote:

fixed in [4c9b493a](https://github.com/llvm/llvm-project/pull/132484/commits/4c9b493a2c7b993e4b742e57d08d6925a866bf5c)

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


More information about the llvm-commits mailing list