[libc-commits] [libc] [libc][math] Implement isnanbf16 function (PR #238777)

Vaibhav Agrawal via libc-commits libc-commits at lists.llvm.org
Wed Nov 4 05:03:32 PST 2026


https://github.com/competitive-programmer04 created https://github.com/llvm/llvm-project/pull/238777

Add support for the isnanbf16 function for bfloat16 floating point type.
Uses fputil::FPBits<bfloat16>::is_nan() for the underlying implementation. 
Includes unit smoke tests covering special constants (NaNs, infinities, zeros, subnormals) and rounded numbers.

Author: competitive-programmer04

>From c6ac49a803286d076a685878444148af1f577101 Mon Sep 17 00:00:00 2001
From: competitive-programmer04 <coder8809 at gmail.com>
Date: Wed, 4 Nov 2026 18:18:52 +0530
Subject: [PATCH] [libc][math] Implement isnanbf16 function

Add support for the isnanbf16 function for bfloat16 floating point type.
Uses fputil::FPBits<bfloat16>::is_nan() for the underlying implementation.
Includes unit smoke tests covering special constants (NaNs, infinities, zeros, subnormals) and rounded numbers.
---
 libc/config/linux/aarch64/entrypoints.txt   |  1 +
 libc/config/linux/x86_64/entrypoints.txt    |  1 +
 libc/src/__support/math/CMakeLists.txt      | 10 ++++++++
 libc/src/__support/math/isnanbf16.h         | 27 +++++++++++++++++++++
 libc/src/math/CMakeLists.txt                |  1 +
 libc/src/math/generic/CMakeLists.txt        | 12 +++++++--
 libc/src/math/generic/isnanbf16.cpp         | 15 ++++++++++++
 libc/src/math/isnanbf16.h                   | 22 +++++++++++++++++
 libc/test/src/math/smoke/CMakeLists.txt     | 13 ++++++++++
 libc/test/src/math/smoke/isnanbf16_test.cpp | 15 ++++++++++++
 10 files changed, 115 insertions(+), 2 deletions(-)
 create mode 100644 libc/src/__support/math/isnanbf16.h
 create mode 100644 libc/src/math/generic/isnanbf16.cpp
 create mode 100644 libc/src/math/isnanbf16.h
 create mode 100644 libc/test/src/math/smoke/isnanbf16_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 4b04f8995be49f..04e52d35ea181d 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -964,6 +964,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.ilogbf16
     libc.src.math.iscanonicalf16
     libc.src.math.isnanf16
+    libc.src.math.isnanbf16
     libc.src.math.issignalingf16
     libc.src.math.ldexpf16
     libc.src.math.lgammaf16
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index f31143b6965a6a..5d4b62eb277cfd 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1060,6 +1060,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
     libc.src.math.ilogbf16
     libc.src.math.iscanonicalf16
     libc.src.math.isnanf16
+    libc.src.math.isnanbf16
     libc.src.math.issignalingf16
     libc.src.math.ldexpf16
     libc.src.math.lgammaf16
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index ba3edda2843149..efa335bc5408a7 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -1846,6 +1846,16 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+    isnanbf16
+    HDRS
+       isnanbf16.h
+    DEPENDS
+       libc.src.__support.FPUtil.bfloat16
+       libc.src.__support.FPUtil.fp_bits
+       libc.src.__support.macros.config
+   )
+
 add_header_library(
   issignaling
   HDRS
diff --git a/libc/src/__support/math/isnanbf16.h b/libc/src/__support/math/isnanbf16.h
new file mode 100644
index 00000000000000..48e6af724f9ba2
--- /dev/null
+++ b/libc/src/__support/math/isnanbf16.h
@@ -0,0 +1,27 @@
+//===-- Implementation header for isnanbf16 ----------------------*- 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_ISNANBF16_H
+#define LLVM_LIBC_SRC___SUPPORT_MATH_ISNANBF16_H
+
+#include "src/__support/FPUtil/FPBits.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace math {
+
+LIBC_INLINE constexpr int isnanbf16(bfloat16 x) {
+  return fputil::FPBits<bfloat16>(x).is_nan();
+}
+
+} // namespace math
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_MATH_ISNANBF16_H
diff --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index 08728a02809359..273af2c2796d83 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -385,6 +385,7 @@ add_math_entrypoint_object(ilogbbf16)
 add_math_entrypoint_object(isnan)
 add_math_entrypoint_object(isnanf)
 add_math_entrypoint_object(isnanf16)
+add_math_entrypoint_object(isnanbf16)
 add_math_entrypoint_object(isnanf128)
 add_math_entrypoint_object(isnanl)
 
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index b7444dd916d006..f8eed6332a83f1 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1,5 +1,3 @@
-
-
 add_entrypoint_object(
   canonicalize
   SRCS
@@ -3263,6 +3261,16 @@ add_entrypoint_object(
     libc.src.__support.math.isnanf16
 )
 
+add_entrypoint_object(
+    isnanbf16
+    SRCS
+       isnanbf16.cpp
+    HDRS
+       ../isnanbf16.h
+    DEPENDS
+      libc.src.__support.math.isnanbf16
+)
+
 add_entrypoint_object(
   nan
   SRCS
diff --git a/libc/src/math/generic/isnanbf16.cpp b/libc/src/math/generic/isnanbf16.cpp
new file mode 100644
index 00000000000000..80c99e42a23440
--- /dev/null
+++ b/libc/src/math/generic/isnanbf16.cpp
@@ -0,0 +1,15 @@
+//===-- Implementation of isnanbf16 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/isnanbf16.h"
+#include "src/__support/math/isnanbf16.h"
+
+namespace LIBC_NAMESPACE_DECL {
+LLVM_LIBC_FUNCTION(int, isnanbf16, (bfloat16 x)) { return math::isnanbf16(x); }
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/math/isnanbf16.h b/libc/src/math/isnanbf16.h
new file mode 100644
index 00000000000000..7fe6afe38abad0
--- /dev/null
+++ b/libc/src/math/isnanbf16.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for isnanbf16 ----------------------*- 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_ISNANBF16_H
+#define LLVM_LIBC_SRC_MATH_ISNANBF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int isnanbf16(bfloat16 x);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_ISNANBF16_H
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index b3315c10db0d01..52025833cc4a48 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -2254,6 +2254,19 @@ add_fp_unittest(
     libc.src.math.isnanf16
 )
 
+add_fp_unittest(
+    isnanbf16_test
+    SUITE
+       libc-math-smoke-tests
+    SRCS
+       isnanbf16_test.cpp
+    HDRS
+       IsNanTest.h
+    DEPENDS
+       libc.src.math.isnanbf16
+       libc.src.__support.FPUtil.bfloat16
+   )
+
 add_fp_unittest(
   llogb_test
   SUITE
diff --git a/libc/test/src/math/smoke/isnanbf16_test.cpp b/libc/test/src/math/smoke/isnanbf16_test.cpp
new file mode 100644
index 00000000000000..bdd553b3112c9a
--- /dev/null
+++ b/libc/test/src/math/smoke/isnanbf16_test.cpp
@@ -0,0 +1,15 @@
+//===-- Unittests for isnanbf16
+//--------------------------------------------===//
+//
+// 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 "IsNanTest.h"
+#include "src/__support/FPUtil/bfloat16.h"
+
+#include "src/math/isnanbf16.h"
+
+LIST_ISNAN_TESTS(Isnanbf16, bfloat16, LIBC_NAMESPACE::isnanbf16)



More information about the libc-commits mailing list