[libc-commits] [libc] 691f231 - [libc][math][c++23] Add totalorder{, mag}bf16 math functions (#155568)

via libc-commits libc-commits at lists.llvm.org
Thu Aug 28 07:07:11 PDT 2025


Author: Krishna Pandey
Date: 2025-08-28T19:37:07+05:30
New Revision: 691f2315793dbd07a0e4ecbd970284ade4188db3

URL: https://github.com/llvm/llvm-project/commit/691f2315793dbd07a0e4ecbd970284ade4188db3
DIFF: https://github.com/llvm/llvm-project/commit/691f2315793dbd07a0e4ecbd970284ade4188db3.diff

LOG: [libc][math][c++23] Add totalorder{,mag}bf16 math functions (#155568)

This PR adds the following basic math functions for BFloat16 type along
with the tests:
- totalorderbf16
- totalordermagbf16

---------

Signed-off-by: Krishna Pandey <kpandey81930 at gmail.com>

Added: 
    libc/src/math/generic/totalorderbf16.cpp
    libc/src/math/generic/totalordermagbf16.cpp
    libc/src/math/totalorderbf16.h
    libc/src/math/totalordermagbf16.h
    libc/test/src/math/smoke/totalorderbf16_test.cpp
    libc/test/src/math/smoke/totalordermagbf16_test.cpp

Modified: 
    libc/config/baremetal/aarch64/entrypoints.txt
    libc/config/baremetal/arm/entrypoints.txt
    libc/config/baremetal/riscv/entrypoints.txt
    libc/config/darwin/aarch64/entrypoints.txt
    libc/config/darwin/x86_64/entrypoints.txt
    libc/config/gpu/amdgpu/entrypoints.txt
    libc/config/gpu/nvptx/entrypoints.txt
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/arm/entrypoints.txt
    libc/config/linux/riscv/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/config/windows/entrypoints.txt
    libc/docs/headers/math/index.rst
    libc/src/math/CMakeLists.txt
    libc/src/math/generic/CMakeLists.txt
    libc/test/src/math/smoke/CMakeLists.txt
    libc/test/src/math/smoke/TotalOrderMagTest.h
    libc/test/src/math/smoke/TotalOrderTest.h

Removed: 
    


################################################################################
diff  --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index 27f8cfb4df18f..ff3a6bfa8a2fc 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -817,6 +817,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
+  libc.src.math.totalorderbf16
+  libc.src.math.totalordermagbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
 )

diff  --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index 7de180d65832f..f8916d71f86aa 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -820,6 +820,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
+  libc.src.math.totalorderbf16
+  libc.src.math.totalordermagbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
 )

diff  --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 2ba13a1123f7d..8a72fa6692869 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -820,6 +820,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
+  libc.src.math.totalorderbf16
+  libc.src.math.totalordermagbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
 )

diff  --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 804f9235e0d60..796c0d3c8a4ff 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -650,6 +650,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
+  libc.src.math.totalorderbf16
+  libc.src.math.totalordermagbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
 )

diff  --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index f4830edc0a292..fa1d8def8936c 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -293,6 +293,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
+  libc.src.math.totalorderbf16
+  libc.src.math.totalordermagbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
 )

diff  --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 1843b727de478..49c41411cb5d2 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -676,6 +676,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
+  libc.src.math.totalorderbf16
+  libc.src.math.totalordermagbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
 )

diff  --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 49d322ae32a92..06a2c9addb7fb 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -678,6 +678,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
+  libc.src.math.totalorderbf16
+  libc.src.math.totalordermagbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
 )

diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 8d6d4ca5e10d9..feea7ff69c166 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -905,6 +905,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
+  libc.src.math.totalorderbf16
+  libc.src.math.totalordermagbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
 )

diff  --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 2752bd0da56d0..8e92fac863c6b 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -520,6 +520,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
+  libc.src.math.totalorderbf16
+  libc.src.math.totalordermagbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
 )

diff  --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index e3759dba921d6..fb08cfe7bd3f8 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -923,6 +923,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
+  libc.src.math.totalorderbf16
+  libc.src.math.totalordermagbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
 )

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index a82faa4d6a141..37af75454daa3 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -956,6 +956,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
+  libc.src.math.totalorderbf16
+  libc.src.math.totalordermagbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
 )

diff  --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 20f7a187591d4..c09e6b259fa42 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -366,6 +366,8 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16
+  libc.src.math.totalorderbf16
+  libc.src.math.totalordermagbf16
   libc.src.math.ufromfpbf16
   libc.src.math.ufromfpxbf16
 )

diff  --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 818386aa18f02..89624460c3af5 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -241,9 +241,9 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | setpayloadsig    | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | F.10.13.3              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| totalorder       | |check|          | |check|         | |check|                | |check|              | |check|                |                        | F.10.12.1              | N/A                        |
+| totalorder       | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | F.10.12.1              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| totalordermag    | |check|          | |check|         | |check|                | |check|              | |check|                |                        | F.10.12.2              | N/A                        |
+| totalordermag    | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | F.10.12.2              | N/A                        |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | trunc            | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.9.9               | F.10.6.9                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+

diff  --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index e6a2f868e46b5..1edb517421e28 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -579,12 +579,14 @@ add_math_entrypoint_object(totalorderf)
 add_math_entrypoint_object(totalorderl)
 add_math_entrypoint_object(totalorderf16)
 add_math_entrypoint_object(totalorderf128)
+add_math_entrypoint_object(totalorderbf16)
 
 add_math_entrypoint_object(totalordermag)
 add_math_entrypoint_object(totalordermagf)
 add_math_entrypoint_object(totalordermagl)
 add_math_entrypoint_object(totalordermagf16)
 add_math_entrypoint_object(totalordermagf128)
+add_math_entrypoint_object(totalordermagbf16)
 
 add_math_entrypoint_object(trunc)
 add_math_entrypoint_object(truncf)

diff  --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 70ca5c51990de..904f8d2eb4704 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -4784,6 +4784,21 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.basic_operations
     libc.src.__support.macros.properties.types
 )
+
+add_entrypoint_object(
+  totalorderbf16
+  SRCS
+    totalorderbf16.cpp
+  HDRS
+    ../totalorderbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_entrypoint_object(
   totalordermag
   SRCS
@@ -4835,6 +4850,20 @@ add_entrypoint_object(
     libc.src.__support.macros.properties.types
 )
 
+add_entrypoint_object(
+  totalordermagbf16
+  SRCS
+    totalordermagbf16.cpp
+  HDRS
+    ../totalordermagbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.basic_operations
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_entrypoint_object(
   getpayload
   SRCS

diff  --git a/libc/src/math/generic/totalorderbf16.cpp b/libc/src/math/generic/totalorderbf16.cpp
new file mode 100644
index 0000000000000..bb9c86e281f6b
--- /dev/null
+++ b/libc/src/math/generic/totalorderbf16.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of totalorderbf16 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/totalorderbf16.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, totalorderbf16,
+                   (const bfloat16 *x, const bfloat16 *y)) {
+  return static_cast<int>(fputil::totalorder(*x, *y));
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/math/generic/totalordermagbf16.cpp b/libc/src/math/generic/totalordermagbf16.cpp
new file mode 100644
index 0000000000000..3fc61d9d8bcb4
--- /dev/null
+++ b/libc/src/math/generic/totalordermagbf16.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of totalordermagbf16 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/totalordermagbf16.h"
+#include "src/__support/FPUtil/BasicOperations.h"
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, totalordermagbf16,
+                   (const bfloat16 *x, const bfloat16 *y)) {
+  return static_cast<int>(fputil::totalordermag(*x, *y));
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/math/totalorderbf16.h b/libc/src/math/totalorderbf16.h
new file mode 100644
index 0000000000000..2414852db92b8
--- /dev/null
+++ b/libc/src/math/totalorderbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for totalorderbf16 ----------------*- 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_TOTALORDERF16_H
+#define LLVM_LIBC_SRC_MATH_TOTALORDERF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int totalorderbf16(const bfloat16 *x, const bfloat16 *y);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_TOTALORDERF16_H

diff  --git a/libc/src/math/totalordermagbf16.h b/libc/src/math/totalordermagbf16.h
new file mode 100644
index 0000000000000..c48de1ca8e540
--- /dev/null
+++ b/libc/src/math/totalordermagbf16.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for totalordermagbf16 -------------*- 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_TOTALORDERMAGF16_H
+#define LLVM_LIBC_SRC_MATH_TOTALORDERMAGF16_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/properties/types.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int totalordermagbf16(const bfloat16 *x, const bfloat16 *y);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_MATH_TOTALORDERMAGF16_H

diff  --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 0497d5e0c06ed..166802fd9f9cc 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -5050,6 +5050,19 @@ add_fp_unittest(
     libc.src.math.totalorderf128
 )
 
+add_fp_unittest(
+  totalorderbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    totalorderbf16_test.cpp
+  HDRS
+    TotalOrderTest.h
+  DEPENDS
+    libc.src.math.totalorderbf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   totalordermag_test
   SUITE
@@ -5110,6 +5123,19 @@ add_fp_unittest(
     libc.src.math.totalordermagf128
 )
 
+add_fp_unittest(
+  totalordermagbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    totalordermagbf16_test.cpp
+  HDRS
+    TotalOrderMagTest.h
+  DEPENDS
+    libc.src.math.totalordermagbf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   getpayload_test
   SUITE

diff  --git a/libc/test/src/math/smoke/TotalOrderMagTest.h b/libc/test/src/math/smoke/TotalOrderMagTest.h
index 0a13fd2922e4c..b5c26bb4bcd37 100644
--- a/libc/test/src/math/smoke/TotalOrderMagTest.h
+++ b/libc/test/src/math/smoke/TotalOrderMagTest.h
@@ -106,24 +106,46 @@ class TotalOrderMagTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
   }
 
   void testNaNPayloads(TotalOrderMagFunc func) {
-    T qnan_0x42 = FPBits::quiet_nan(Sign::POS, 0x42).get_val();
-    T neg_qnan_0x42 = FPBits::quiet_nan(Sign::NEG, 0x42).get_val();
-    T snan_0x42 = FPBits::signaling_nan(Sign::POS, 0x42).get_val();
-    T neg_snan_0x42 = FPBits::signaling_nan(Sign::NEG, 0x42).get_val();
 
     EXPECT_TRUE(funcWrapper(func, aNaN, aNaN));
     EXPECT_TRUE(funcWrapper(func, sNaN, sNaN));
-    EXPECT_TRUE(funcWrapper(func, aNaN, qnan_0x42));
-    EXPECT_FALSE(funcWrapper(func, sNaN, snan_0x42));
-    EXPECT_FALSE(funcWrapper(func, qnan_0x42, aNaN));
-    EXPECT_TRUE(funcWrapper(func, snan_0x42, sNaN));
 
     EXPECT_TRUE(funcWrapper(func, neg_aNaN, neg_aNaN));
     EXPECT_TRUE(funcWrapper(func, neg_sNaN, neg_sNaN));
-    EXPECT_TRUE(funcWrapper(func, neg_aNaN, neg_qnan_0x42));
-    EXPECT_FALSE(funcWrapper(func, neg_sNaN, neg_snan_0x42));
-    EXPECT_FALSE(funcWrapper(func, neg_qnan_0x42, neg_aNaN));
-    EXPECT_TRUE(funcWrapper(func, neg_snan_0x42, neg_sNaN));
+
+    if constexpr (FPBits::FRACTION_LEN - 1 >= 5) {
+      T qnan_0x15 = FPBits::quiet_nan(Sign::POS, 0x15).get_val();
+      T neg_qnan_0x15 = FPBits::quiet_nan(Sign::NEG, 0x15).get_val();
+      T snan_0x15 = FPBits::signaling_nan(Sign::POS, 0x15).get_val();
+      T neg_snan_0x15 = FPBits::signaling_nan(Sign::NEG, 0x15).get_val();
+
+      EXPECT_TRUE(funcWrapper(func, aNaN, qnan_0x15));
+      EXPECT_FALSE(funcWrapper(func, sNaN, snan_0x15));
+      EXPECT_FALSE(funcWrapper(func, qnan_0x15, aNaN));
+      EXPECT_TRUE(funcWrapper(func, snan_0x15, sNaN));
+
+      EXPECT_TRUE(funcWrapper(func, neg_aNaN, neg_qnan_0x15));
+      EXPECT_FALSE(funcWrapper(func, neg_sNaN, neg_snan_0x15));
+      EXPECT_FALSE(funcWrapper(func, neg_qnan_0x15, neg_aNaN));
+      EXPECT_TRUE(funcWrapper(func, neg_snan_0x15, neg_sNaN));
+    }
+
+    if constexpr (FPBits::FRACTION_LEN - 1 >= 7) {
+      T qnan_0x42 = FPBits::quiet_nan(Sign::POS, 0x42).get_val();
+      T neg_qnan_0x42 = FPBits::quiet_nan(Sign::NEG, 0x42).get_val();
+      T snan_0x42 = FPBits::signaling_nan(Sign::POS, 0x42).get_val();
+      T neg_snan_0x42 = FPBits::signaling_nan(Sign::NEG, 0x42).get_val();
+
+      EXPECT_TRUE(funcWrapper(func, aNaN, qnan_0x42));
+      EXPECT_FALSE(funcWrapper(func, sNaN, snan_0x42));
+      EXPECT_FALSE(funcWrapper(func, qnan_0x42, aNaN));
+      EXPECT_TRUE(funcWrapper(func, snan_0x42, sNaN));
+
+      EXPECT_TRUE(funcWrapper(func, neg_aNaN, neg_qnan_0x42));
+      EXPECT_FALSE(funcWrapper(func, neg_sNaN, neg_snan_0x42));
+      EXPECT_FALSE(funcWrapper(func, neg_qnan_0x42, neg_aNaN));
+      EXPECT_TRUE(funcWrapper(func, neg_snan_0x42, neg_sNaN));
+    }
   }
 };
 

diff  --git a/libc/test/src/math/smoke/TotalOrderTest.h b/libc/test/src/math/smoke/TotalOrderTest.h
index e426eb35016b9..4281a9a0b987d 100644
--- a/libc/test/src/math/smoke/TotalOrderTest.h
+++ b/libc/test/src/math/smoke/TotalOrderTest.h
@@ -104,24 +104,46 @@ class TotalOrderTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
   }
 
   void testNaNPayloads(TotalOrderFunc func) {
-    T qnan_0x42 = FPBits::quiet_nan(Sign::POS, 0x42).get_val();
-    T neg_qnan_0x42 = FPBits::quiet_nan(Sign::NEG, 0x42).get_val();
-    T snan_0x42 = FPBits::signaling_nan(Sign::POS, 0x42).get_val();
-    T neg_snan_0x42 = FPBits::signaling_nan(Sign::NEG, 0x42).get_val();
 
     EXPECT_TRUE(funcWrapper(func, aNaN, aNaN));
     EXPECT_TRUE(funcWrapper(func, sNaN, sNaN));
-    EXPECT_TRUE(funcWrapper(func, aNaN, qnan_0x42));
-    EXPECT_FALSE(funcWrapper(func, sNaN, snan_0x42));
-    EXPECT_FALSE(funcWrapper(func, qnan_0x42, aNaN));
-    EXPECT_TRUE(funcWrapper(func, snan_0x42, sNaN));
 
     EXPECT_TRUE(funcWrapper(func, neg_aNaN, neg_aNaN));
     EXPECT_TRUE(funcWrapper(func, neg_sNaN, neg_sNaN));
-    EXPECT_FALSE(funcWrapper(func, neg_aNaN, neg_qnan_0x42));
-    EXPECT_TRUE(funcWrapper(func, neg_sNaN, neg_snan_0x42));
-    EXPECT_TRUE(funcWrapper(func, neg_qnan_0x42, neg_aNaN));
-    EXPECT_FALSE(funcWrapper(func, neg_snan_0x42, neg_sNaN));
+
+    if constexpr (FPBits::FRACTION_LEN - 1 >= 5) {
+      T qnan_0x15 = FPBits::quiet_nan(Sign::POS, 0x15).get_val();
+      T neg_qnan_0x15 = FPBits::quiet_nan(Sign::NEG, 0x15).get_val();
+      T snan_0x15 = FPBits::signaling_nan(Sign::POS, 0x15).get_val();
+      T neg_snan_0x15 = FPBits::signaling_nan(Sign::NEG, 0x15).get_val();
+
+      EXPECT_TRUE(funcWrapper(func, aNaN, qnan_0x15));
+      EXPECT_FALSE(funcWrapper(func, sNaN, snan_0x15));
+      EXPECT_FALSE(funcWrapper(func, qnan_0x15, aNaN));
+      EXPECT_TRUE(funcWrapper(func, snan_0x15, sNaN));
+
+      EXPECT_FALSE(funcWrapper(func, neg_aNaN, neg_qnan_0x15));
+      EXPECT_TRUE(funcWrapper(func, neg_sNaN, neg_snan_0x15));
+      EXPECT_TRUE(funcWrapper(func, neg_qnan_0x15, neg_aNaN));
+      EXPECT_FALSE(funcWrapper(func, neg_snan_0x15, neg_sNaN));
+    }
+
+    if constexpr (FPBits::FRACTION_LEN - 1 >= 7) {
+      T qnan_0x42 = FPBits::quiet_nan(Sign::POS, 0x42).get_val();
+      T neg_qnan_0x42 = FPBits::quiet_nan(Sign::NEG, 0x42).get_val();
+      T snan_0x42 = FPBits::signaling_nan(Sign::POS, 0x42).get_val();
+      T neg_snan_0x42 = FPBits::signaling_nan(Sign::NEG, 0x42).get_val();
+
+      EXPECT_TRUE(funcWrapper(func, aNaN, qnan_0x42));
+      EXPECT_FALSE(funcWrapper(func, sNaN, snan_0x42));
+      EXPECT_FALSE(funcWrapper(func, qnan_0x42, aNaN));
+      EXPECT_TRUE(funcWrapper(func, snan_0x42, sNaN));
+
+      EXPECT_FALSE(funcWrapper(func, neg_aNaN, neg_qnan_0x42));
+      EXPECT_TRUE(funcWrapper(func, neg_sNaN, neg_snan_0x42));
+      EXPECT_TRUE(funcWrapper(func, neg_qnan_0x42, neg_aNaN));
+      EXPECT_FALSE(funcWrapper(func, neg_snan_0x42, neg_sNaN));
+    }
   }
 };
 

diff  --git a/libc/test/src/math/smoke/totalorderbf16_test.cpp b/libc/test/src/math/smoke/totalorderbf16_test.cpp
new file mode 100644
index 0000000000000..11aeeac0c52dd
--- /dev/null
+++ b/libc/test/src/math/smoke/totalorderbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for totalorderbf16 --------------------------------------===//
+//
+// 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 "TotalOrderTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/totalorderbf16.h"
+
+LIST_TOTALORDER_TESTS(bfloat16, LIBC_NAMESPACE::totalorderbf16)

diff  --git a/libc/test/src/math/smoke/totalordermagbf16_test.cpp b/libc/test/src/math/smoke/totalordermagbf16_test.cpp
new file mode 100644
index 0000000000000..b5a5a1b7d6c52
--- /dev/null
+++ b/libc/test/src/math/smoke/totalordermagbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for totalordermagbf16 -----------------------------------===//
+//
+// 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 "TotalOrderMagTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/totalordermagbf16.h"
+
+LIST_TOTALORDERMAG_TESTS(bfloat16, LIBC_NAMESPACE::totalordermagbf16)


        


More information about the libc-commits mailing list