[libc-commits] [libc] [llvm] [libc][math][c23] Add asinbf16 math function (PR #184170)
Mohamed Emad via libc-commits
libc-commits at lists.llvm.org
Tue Mar 3 06:42:25 PST 2026
================
@@ -0,0 +1,101 @@
+//===-- Implementation header for asinbf16 ----------------------*- 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_ASINBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ASINBF16_H
+
+#include "src/__support/FPUtil/FEnvImpl.h"
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/PolyEval.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/FPUtil/cast.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 constexpr bfloat16 asinbf16(bfloat16 x) {
+ // Generated by Sollya using the following command:
+ // > display = hexadecimal;
+ // > round(pi/2, SG, RN);
+ constexpr float PI_2 = 0x1.921fb6p0f;
+
+ using FPBits = fputil::FPBits<bfloat16>;
+ FPBits xbits(x);
+
+ uint16_t x_u = xbits.uintval();
+ uint16_t x_abs = x_u & 0x7fff;
+ float x_sign = (xbits.sign() == Sign::NEG) ? -1 : 1;
+ float xf = x;
+
+ // case 1: |x|>=1, NaN or Inf
+ if (LIBC_UNLIKELY(x_abs >= 0x3F80)) {
+ if (x_abs == 0x3F80) {
+ return bfloat16(x_sign * PI_2);
+ }
+ // NaN
+ if (xbits.is_nan()) {
+ if (xbits.is_signaling_nan()) {
+ fputil::raise_except_if_required(FE_INVALID);
+ return FPBits::quiet_nan().get_val();
+ }
+ return x; // quiet NaN
+ }
+ // |x|>1 & inf
+ fputil::raise_except_if_required(FE_INVALID);
+ fputil::set_errno_if_required(EDOM); // Domain is bounded
+ return FPBits::quiet_nan().get_val();
+ }
+
+ // case 2: |x| = {0}
+ if (LIBC_UNLIKELY(x_abs == 0))
+ return x; // with sign
+
+ if (LIBC_UNLIKELY(x_abs <= 0x3D00)) {
+ int rounding = fputil::quick_get_round();
+ if ((xbits.is_pos() && rounding == FE_UPWARD) ||
+ (xbits.is_neg() && rounding == FE_DOWNWARD)) {
+ return bfloat16(fputil::multiply_add(xf, 0x1.0p-13f, xf));
+ }
+ return x;
+ }
+
+ float xf_abs = (xf < 0 ? -xf : xf);
+ float x_sq = xf_abs * xf_abs;
+
+ // Degree 6 polynomial of asin(x) generated using Sollya with command :
+ // > display = hexadecimal
+ // > P = fpminimax(asin(x)/x, [|0,2,4,6|], [|SG,SG,SG,SG|], [0, 0.5]);
+ auto asin_poly = [](float u) {
----------------
hulxv wrote:
use [`asin_eval`](https://github.com/llvm/llvm-project/blob/c782e2d40572ce4ff3a71562265bf6f01857c132/libc/src/__support/math/inv_trigf_utils.h#L171) from `inv_tragf_utils`
https://github.com/llvm/llvm-project/pull/184170
More information about the libc-commits
mailing list