[libc-commits] [libc] 6074335 - [libc][math][c++23] Add {nearbyint, rint, lrint, llrint, lround, llround}bf16 math functions (#153882)

via libc-commits libc-commits at lists.llvm.org
Sun Aug 24 08:21:06 PDT 2025


Author: Krishna Pandey
Date: 2025-08-24T20:51:02+05:30
New Revision: 60743358d15f19b89e3ef5daa1b28e7b1bef453e

URL: https://github.com/llvm/llvm-project/commit/60743358d15f19b89e3ef5daa1b28e7b1bef453e
DIFF: https://github.com/llvm/llvm-project/commit/60743358d15f19b89e3ef5daa1b28e7b1bef453e.diff

LOG: [libc][math][c++23] Add {nearbyint,rint,lrint,llrint,lround,llround}bf16 math functions (#153882)

This PR adds the following basic math functions for BFloat16 type along
with the tests:
- nearbyintbf16
- rintbf16
- lrintbf16
- llrintbf16
- lroundbf16
- llroundbf16

---------

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

Added: 
    libc/src/math/generic/llrintbf16.cpp
    libc/src/math/generic/llroundbf16.cpp
    libc/src/math/generic/lrintbf16.cpp
    libc/src/math/generic/lroundbf16.cpp
    libc/src/math/generic/nearbyintbf16.cpp
    libc/src/math/generic/rintbf16.cpp
    libc/src/math/llrintbf16.h
    libc/src/math/llroundbf16.h
    libc/src/math/lrintbf16.h
    libc/src/math/lroundbf16.h
    libc/src/math/nearbyintbf16.h
    libc/src/math/rintbf16.h
    libc/test/src/math/llrintbf16_test.cpp
    libc/test/src/math/llroundbf16_test.cpp
    libc/test/src/math/lrintbf16_test.cpp
    libc/test/src/math/lroundbf16_test.cpp
    libc/test/src/math/nearbyintbf16_test.cpp
    libc/test/src/math/rintbf16_test.cpp
    libc/test/src/math/smoke/llrintbf16_test.cpp
    libc/test/src/math/smoke/llroundbf16_test.cpp
    libc/test/src/math/smoke/lrintbf16_test.cpp
    libc/test/src/math/smoke/lroundbf16_test.cpp
    libc/test/src/math/smoke/nearbyintbf16_test.cpp
    libc/test/src/math/smoke/rintbf16_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/__support/FPUtil/bfloat16.h
    libc/src/math/CMakeLists.txt
    libc/src/math/generic/CMakeLists.txt
    libc/test/src/math/CMakeLists.txt
    libc/test/src/math/RoundToIntegerTest.h
    libc/test/src/math/smoke/CMakeLists.txt
    libc/test/src/math/smoke/RoundToIntegerTest.h
    libc/utils/MPFRWrapper/MPFRUtils.cpp

Removed: 
    


################################################################################
diff  --git a/libc/config/baremetal/aarch64/entrypoints.txt b/libc/config/baremetal/aarch64/entrypoints.txt
index c4ec03fc146e8..1784e108d6acd 100644
--- a/libc/config/baremetal/aarch64/entrypoints.txt
+++ b/libc/config/baremetal/aarch64/entrypoints.txt
@@ -785,16 +785,22 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.llrintbf16
+  libc.src.math.llroundbf16
+  libc.src.math.lrintbf16
+  libc.src.math.lroundbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
   libc.src.math.nanbf16
+  libc.src.math.nearbyintbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.rintbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16

diff  --git a/libc/config/baremetal/arm/entrypoints.txt b/libc/config/baremetal/arm/entrypoints.txt
index d9e173a71e47a..9a58ebd28ea64 100644
--- a/libc/config/baremetal/arm/entrypoints.txt
+++ b/libc/config/baremetal/arm/entrypoints.txt
@@ -788,16 +788,22 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.llrintbf16
+  libc.src.math.llroundbf16
+  libc.src.math.lrintbf16
+  libc.src.math.lroundbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
   libc.src.math.nanbf16
+  libc.src.math.nearbyintbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.rintbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16

diff  --git a/libc/config/baremetal/riscv/entrypoints.txt b/libc/config/baremetal/riscv/entrypoints.txt
index 9b0cf14037fae..2422551137052 100644
--- a/libc/config/baremetal/riscv/entrypoints.txt
+++ b/libc/config/baremetal/riscv/entrypoints.txt
@@ -788,16 +788,22 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.llrintbf16
+  libc.src.math.llroundbf16
+  libc.src.math.lrintbf16
+  libc.src.math.lroundbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
   libc.src.math.nanbf16
+  libc.src.math.nearbyintbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.rintbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16

diff  --git a/libc/config/darwin/aarch64/entrypoints.txt b/libc/config/darwin/aarch64/entrypoints.txt
index 2a634852a2ed0..a3e7aad86235d 100644
--- a/libc/config/darwin/aarch64/entrypoints.txt
+++ b/libc/config/darwin/aarch64/entrypoints.txt
@@ -618,16 +618,22 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.llrintbf16
+  libc.src.math.llroundbf16
+  libc.src.math.lrintbf16
+  libc.src.math.lroundbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
   libc.src.math.nanbf16
+  libc.src.math.nearbyintbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.rintbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16

diff  --git a/libc/config/darwin/x86_64/entrypoints.txt b/libc/config/darwin/x86_64/entrypoints.txt
index 6a18d557f86fb..2343a3d6185f9 100644
--- a/libc/config/darwin/x86_64/entrypoints.txt
+++ b/libc/config/darwin/x86_64/entrypoints.txt
@@ -261,16 +261,22 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.llrintbf16
+  libc.src.math.llroundbf16
+  libc.src.math.lrintbf16
+  libc.src.math.lroundbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
   libc.src.math.nanbf16
+  libc.src.math.nearbyintbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.rintbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16

diff  --git a/libc/config/gpu/amdgpu/entrypoints.txt b/libc/config/gpu/amdgpu/entrypoints.txt
index 008f7c7bd642c..7b9ee44f5013c 100644
--- a/libc/config/gpu/amdgpu/entrypoints.txt
+++ b/libc/config/gpu/amdgpu/entrypoints.txt
@@ -644,16 +644,22 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.llrintbf16
+  libc.src.math.llroundbf16
+  libc.src.math.lrintbf16
+  libc.src.math.lroundbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
   libc.src.math.nanbf16
+  libc.src.math.nearbyintbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.rintbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16

diff  --git a/libc/config/gpu/nvptx/entrypoints.txt b/libc/config/gpu/nvptx/entrypoints.txt
index 7d36aa4eb40f9..5b724a3bdeae3 100644
--- a/libc/config/gpu/nvptx/entrypoints.txt
+++ b/libc/config/gpu/nvptx/entrypoints.txt
@@ -646,16 +646,22 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.llrintbf16
+  libc.src.math.llroundbf16
+  libc.src.math.lrintbf16
+  libc.src.math.lroundbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
   libc.src.math.nanbf16
+  libc.src.math.nearbyintbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.rintbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16

diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index d04a382de494d..1f2c58cf11dd0 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -873,16 +873,22 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.llrintbf16
+  libc.src.math.llroundbf16
+  libc.src.math.lrintbf16
+  libc.src.math.lroundbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
   libc.src.math.nanbf16
+  libc.src.math.nearbyintbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.rintbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16

diff  --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 865392a918624..daeeee5cc96c8 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -488,16 +488,22 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.llrintbf16
+  libc.src.math.llroundbf16
+  libc.src.math.lrintbf16
+  libc.src.math.lroundbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
   libc.src.math.nanbf16
+  libc.src.math.nearbyintbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.rintbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16

diff  --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 207608c431a9b..3a42342fd0ded 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -891,16 +891,22 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.llrintbf16
+  libc.src.math.llroundbf16
+  libc.src.math.lrintbf16
+  libc.src.math.lroundbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
   libc.src.math.nanbf16
+  libc.src.math.nearbyintbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.rintbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 2ba2b6874e2e5..fc52bf6f60c25 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -924,16 +924,22 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.llrintbf16
+  libc.src.math.llroundbf16
+  libc.src.math.lrintbf16
+  libc.src.math.lroundbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
   libc.src.math.nanbf16
+  libc.src.math.nearbyintbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.rintbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16

diff  --git a/libc/config/windows/entrypoints.txt b/libc/config/windows/entrypoints.txt
index 32a6a578aae13..413a7a3c48f70 100644
--- a/libc/config/windows/entrypoints.txt
+++ b/libc/config/windows/entrypoints.txt
@@ -334,16 +334,22 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
   libc.src.math.fminimum_magbf16
   libc.src.math.fminimum_mag_numbf16
   libc.src.math.fminimum_numbf16
+  libc.src.math.llrintbf16
+  libc.src.math.llroundbf16
+  libc.src.math.lrintbf16
+  libc.src.math.lroundbf16
   libc.src.math.fromfpbf16
   libc.src.math.fromfpxbf16
   libc.src.math.getpayloadbf16
   libc.src.math.nanbf16
+  libc.src.math.nearbyintbf16
   libc.src.math.nextafterbf16
   libc.src.math.nextdownbf16
   libc.src.math.nexttowardbf16
   libc.src.math.nextupbf16
   libc.src.math.roundbf16
   libc.src.math.roundevenbf16
+  libc.src.math.rintbf16
   libc.src.math.setpayloadbf16
   libc.src.math.setpayloadsigbf16
   libc.src.math.truncbf16

diff  --git a/libc/docs/headers/math/index.rst b/libc/docs/headers/math/index.rst
index 1481992815857..b0fd807845b99 100644
--- a/libc/docs/headers/math/index.rst
+++ b/libc/docs/headers/math/index.rst
@@ -199,21 +199,21 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | llogb            | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.10              | F.10.3.10                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| llrint           | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.5               | F.10.6.5                   |
+| llrint           | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.9.5               | F.10.6.5                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| llround          | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.7               | F.10.6.7                   |
+| llround          | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.9.7               | F.10.6.7                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | logb             | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.17              | F.10.3.17                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| lrint            | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.5               | F.10.6.5                   |
+| lrint            | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.9.5               | F.10.6.5                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| lround           | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.7               | F.10.6.7                   |
+| lround           | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.9.7               | F.10.6.7                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | modf             | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.6.18              | F.10.3.18                  |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | nan              | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.11.2              | F.10.8.2                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| nearbyint        | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.3               | F.10.6.3                   |
+| nearbyint        | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.9.3               | F.10.6.3                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | nextafter        | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.11.3              | F.10.8.3                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
@@ -227,7 +227,7 @@ Basic Operations
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | remquo           | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.10.3              | F.10.7.3                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
-| rint             | |check|          | |check|         | |check|                | |check|              | |check|                |                        | 7.12.9.4               | F.10.6.4                   |
+| rint             | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.9.4               | F.10.6.4                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
 | round            | |check|          | |check|         | |check|                | |check|              | |check|                | |check|                | 7.12.9.6               | F.10.6.6                   |
 +------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+

diff  --git a/libc/src/__support/FPUtil/bfloat16.h b/libc/src/__support/FPUtil/bfloat16.h
index f74fa0c2bef5a..ba97b42e23218 100644
--- a/libc/src/__support/FPUtil/bfloat16.h
+++ b/libc/src/__support/FPUtil/bfloat16.h
@@ -59,6 +59,11 @@ struct BFloat16 {
     return cpp::bit_cast<float>(x_bits);
   }
 
+  template <typename T, cpp::enable_if_t<cpp::is_integral_v<T>, int> = 0>
+  LIBC_INLINE constexpr explicit operator T() const {
+    return static_cast<T>(static_cast<float>(*this));
+  }
+
   LIBC_INLINE bool operator==(BFloat16 other) const {
     return fputil::equals(*this, other);
   }

diff  --git a/libc/src/math/CMakeLists.txt b/libc/src/math/CMakeLists.txt
index a90c5ebcee244..2fe3d194e1344 100644
--- a/libc/src/math/CMakeLists.txt
+++ b/libc/src/math/CMakeLists.txt
@@ -392,24 +392,28 @@ add_math_entrypoint_object(llrintf)
 add_math_entrypoint_object(llrintl)
 add_math_entrypoint_object(llrintf16)
 add_math_entrypoint_object(llrintf128)
+add_math_entrypoint_object(llrintbf16)
 
 add_math_entrypoint_object(llround)
 add_math_entrypoint_object(llroundf)
 add_math_entrypoint_object(llroundl)
 add_math_entrypoint_object(llroundf16)
 add_math_entrypoint_object(llroundf128)
+add_math_entrypoint_object(llroundbf16)
 
 add_math_entrypoint_object(lrint)
 add_math_entrypoint_object(lrintf)
 add_math_entrypoint_object(lrintl)
 add_math_entrypoint_object(lrintf16)
 add_math_entrypoint_object(lrintf128)
+add_math_entrypoint_object(lrintbf16)
 
 add_math_entrypoint_object(lround)
 add_math_entrypoint_object(lroundf)
 add_math_entrypoint_object(lroundl)
 add_math_entrypoint_object(lroundf16)
 add_math_entrypoint_object(lroundf128)
+add_math_entrypoint_object(lroundbf16)
 
 add_math_entrypoint_object(modf)
 add_math_entrypoint_object(modff)
@@ -429,6 +433,7 @@ add_math_entrypoint_object(nearbyintf)
 add_math_entrypoint_object(nearbyintl)
 add_math_entrypoint_object(nearbyintf16)
 add_math_entrypoint_object(nearbyintf128)
+add_math_entrypoint_object(nearbyintbf16)
 
 add_math_entrypoint_object(nextafter)
 add_math_entrypoint_object(nextafterf)
@@ -479,6 +484,7 @@ add_math_entrypoint_object(rintf)
 add_math_entrypoint_object(rintl)
 add_math_entrypoint_object(rintf16)
 add_math_entrypoint_object(rintf128)
+add_math_entrypoint_object(rintbf16)
 
 add_math_entrypoint_object(round)
 add_math_entrypoint_object(roundf)

diff  --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index 3f13066c19d3a..1a61ccb107bcf 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1012,6 +1012,19 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.nearest_integer_operations
 )
 
+add_entrypoint_object(
+  lroundbf16
+  SRCS
+    lroundbf16.cpp
+  HDRS
+    ../lroundf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.nearest_integer_operations
+    libc.src.__support.macros.config
+)
+
 add_entrypoint_object(
   llround
   SRCS
@@ -1064,6 +1077,19 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.nearest_integer_operations
 )
 
+add_entrypoint_object(
+  llroundbf16
+  SRCS
+    llroundbf16.cpp
+  HDRS
+    ../llroundf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.nearest_integer_operations
+    libc.src.__support.macros.config
+)
+
 add_entrypoint_object(
   rint
   SRCS
@@ -1124,6 +1150,21 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.nearest_integer_operations
 )
 
+add_entrypoint_object(
+  rintbf16
+  SRCS
+    rintbf16.cpp
+  HDRS
+    ../rintbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.nearest_integer_operations
+    libc.src.__support.macros.config
+  FLAGS
+    ROUND_OPT
+)
+
 add_entrypoint_object(
   lrint
   SRCS
@@ -1176,6 +1217,19 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.nearest_integer_operations
 )
 
+add_entrypoint_object(
+  lrintbf16
+  SRCS
+    lrintbf16.cpp
+  HDRS
+    ../lrintbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.nearest_integer_operations
+    libc.src.__support.macros.config
+)
+
 add_entrypoint_object(
   llrint
   SRCS
@@ -1228,6 +1282,17 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.nearest_integer_operations
 )
 
+add_entrypoint_object(
+  llrintbf16
+  SRCS
+    llrintbf16.cpp
+  HDRS
+    ../llrintbf16.h
+  DEPENDS
+    libc.src.__support.macros.properties.types
+    libc.src.__support.FPUtil.nearest_integer_operations
+)
+
 add_entrypoint_object(
   nearbyint
   SRCS
@@ -1280,6 +1345,19 @@ add_entrypoint_object(
     libc.src.__support.FPUtil.nearest_integer_operations
 )
 
+add_entrypoint_object(
+  nearbyintbf16
+  SRCS
+    nearbyintbf16.cpp
+  HDRS
+    ../nearbyintbf16.h
+  DEPENDS
+    libc.src.__support.common
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.nearest_integer_operations
+    libc.src.__support.macros.config
+)
+
 add_entrypoint_object(
   erff
   SRCS

diff  --git a/libc/src/math/generic/llrintbf16.cpp b/libc/src/math/generic/llrintbf16.cpp
new file mode 100644
index 0000000000000..ec85454d788ff
--- /dev/null
+++ b/libc/src/math/generic/llrintbf16.cpp
@@ -0,0 +1,23 @@
+//===-- Implementation of llrintbf16 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/llrintbf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.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(long long, llrintbf16, (bfloat16 x)) {
+  return fputil::round_to_signed_integer_using_current_rounding_mode<bfloat16,
+                                                                     long long>(
+      x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/math/generic/llroundbf16.cpp b/libc/src/math/generic/llroundbf16.cpp
new file mode 100644
index 0000000000000..2497b6b4a7df3
--- /dev/null
+++ b/libc/src/math/generic/llroundbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of llroundbf16 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/llroundbf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.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(long long, llroundbf16, (bfloat16 x)) {
+  return fputil::round_to_signed_integer<bfloat16, long long>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/math/generic/lrintbf16.cpp b/libc/src/math/generic/lrintbf16.cpp
new file mode 100644
index 0000000000000..4b378909413d3
--- /dev/null
+++ b/libc/src/math/generic/lrintbf16.cpp
@@ -0,0 +1,22 @@
+//===-- Implementation of lrintbf16 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/lrintbf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.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(long, lrintbf16, (bfloat16 x)) {
+  return fputil::round_to_signed_integer_using_current_rounding_mode<bfloat16,
+                                                                     long>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/math/generic/lroundbf16.cpp b/libc/src/math/generic/lroundbf16.cpp
new file mode 100644
index 0000000000000..89095d1786f63
--- /dev/null
+++ b/libc/src/math/generic/lroundbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of lroundbf16 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/lroundbf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.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(long, lroundbf16, (bfloat16 x)) {
+  return fputil::round_to_signed_integer<bfloat16, long>(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/math/generic/nearbyintbf16.cpp b/libc/src/math/generic/nearbyintbf16.cpp
new file mode 100644
index 0000000000000..a6391995963ff
--- /dev/null
+++ b/libc/src/math/generic/nearbyintbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of nearbyintbf16 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/nearbyintbf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.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(bfloat16, nearbyintbf16, (bfloat16 x)) {
+  return fputil::round_using_current_rounding_mode(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/math/generic/rintbf16.cpp b/libc/src/math/generic/rintbf16.cpp
new file mode 100644
index 0000000000000..2ffe16a716205
--- /dev/null
+++ b/libc/src/math/generic/rintbf16.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of rintbf16 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/rintbf16.h"
+#include "src/__support/FPUtil/NearestIntegerOperations.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(bfloat16, rintbf16, (bfloat16 x)) {
+  return fputil::round_using_current_rounding_mode(x);
+}
+
+} // namespace LIBC_NAMESPACE_DECL

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

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

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

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

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

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

diff  --git a/libc/test/src/math/CMakeLists.txt b/libc/test/src/math/CMakeLists.txt
index e15df147c3c35..8e24c5d5c2afd 100644
--- a/libc/test/src/math/CMakeLists.txt
+++ b/libc/test/src/math/CMakeLists.txt
@@ -668,6 +668,25 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  lroundbf16_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    lroundbf16_test.cpp
+  HDRS
+    RoundToIntegerTest.h
+  DEPENDS
+    libc.hdr.fenv_macros
+    libc.src.errno.errno
+    libc.src.math.lroundbf16
+    libc.src.__support.CPP.algorithm
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+)
+
 add_fp_unittest(
   llround_test
   NEED_MPFR
@@ -740,6 +759,25 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  llroundbf16_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    llroundbf16_test.cpp
+  HDRS
+    RoundToIntegerTest.h
+  DEPENDS
+    libc.hdr.fenv_macros
+    libc.src.errno.errno
+    libc.src.math.llroundbf16
+    libc.src.__support.CPP.algorithm
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+)
+
 add_fp_unittest(
   nearbyint_test
   NEED_MPFR
@@ -800,6 +838,22 @@ add_fp_unittest(
     libc.src.__support.CPP.array
 )
 
+add_fp_unittest(
+  nearbyintbf16_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    nearbyintbf16_test.cpp
+  HDRS
+    NearbyIntTest.h
+  DEPENDS
+    libc.src.math.nearbyintbf16
+    libc.src.__support.CPP.algorithm
+    libc.src.__support.CPP.array
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   rint_test
   NEED_MPFR
@@ -868,6 +922,24 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  rintbf16_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    rintbf16_test.cpp
+  HDRS
+    RIntTest.h
+  DEPENDS
+    libc.hdr.fenv_macros
+    libc.src.math.rintbf16
+    libc.src.__support.CPP.algorithm
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+)
+
 add_fp_unittest(
   lrint_test
   NEED_MPFR
@@ -932,6 +1004,23 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  lrintbf16_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    lrintbf16_test.cpp
+  HDRS
+    RoundToIntegerTest.h
+  DEPENDS
+    libc.src.math.lrintbf16
+    libc.src.__support.CPP.algorithm
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+)
+
 add_fp_unittest(
   llrint_test
   NEED_MPFR
@@ -996,6 +1085,23 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  llrintbf16_test
+  NEED_MPFR
+  SUITE
+    libc-math-unittests
+  SRCS
+    llrintbf16_test.cpp
+  HDRS
+    RoundToIntegerTest.h
+  DEPENDS
+    libc.src.math.llrintbf16
+    libc.src.__support.CPP.algorithm
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+)
+
 add_fp_unittest(
   exp_test
   NEED_MPFR

diff  --git a/libc/test/src/math/RoundToIntegerTest.h b/libc/test/src/math/RoundToIntegerTest.h
index 6af9cfea0e0a5..e5e93869a3235 100644
--- a/libc/test/src/math/RoundToIntegerTest.h
+++ b/libc/test/src/math/RoundToIntegerTest.h
@@ -127,8 +127,8 @@ class RoundToIntegerTestTemplate
     test_one_input(func, FloatType(-1.0), IntType(-1), false);
     test_one_input(func, FloatType(10.0), IntType(10), false);
     test_one_input(func, FloatType(-10.0), IntType(-10), false);
-    test_one_input(func, FloatType(1234.0), IntType(1234), false);
-    test_one_input(func, FloatType(-1234.0), IntType(-1234), false);
+    test_one_input(func, FloatType(1232.0), IntType(1232), false);
+    test_one_input(func, FloatType(-1232.0), IntType(-1232), false);
 
     // The rest of this function compares with an equivalent MPFR function
     // which rounds floating point numbers to long values. There is no MPFR

diff  --git a/libc/test/src/math/llrintbf16_test.cpp b/libc/test/src/math/llrintbf16_test.cpp
new file mode 100644
index 0000000000000..e841e627e2634
--- /dev/null
+++ b/libc/test/src/math/llrintbf16_test.cpp
@@ -0,0 +1,15 @@
+//===-- Unittests for llrintbf16 ------------------------------------------===//
+//
+// 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 "RoundToIntegerTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/llrintbf16.h"
+
+LIST_ROUND_TO_INTEGER_TESTS_WITH_MODES(bfloat16, long long,
+                                       LIBC_NAMESPACE::llrintbf16)

diff  --git a/libc/test/src/math/llroundbf16_test.cpp b/libc/test/src/math/llroundbf16_test.cpp
new file mode 100644
index 0000000000000..c3b7ea43c3e8a
--- /dev/null
+++ b/libc/test/src/math/llroundbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for llroundbf16 -----------------------------------------===//
+//
+// 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 "RoundToIntegerTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/llroundbf16.h"
+
+LIST_ROUND_TO_INTEGER_TESTS(bfloat16, long long, LIBC_NAMESPACE::llroundbf16)

diff  --git a/libc/test/src/math/lrintbf16_test.cpp b/libc/test/src/math/lrintbf16_test.cpp
new file mode 100644
index 0000000000000..65a5633b81723
--- /dev/null
+++ b/libc/test/src/math/lrintbf16_test.cpp
@@ -0,0 +1,15 @@
+//===-- Unittests for lrintbf16 -------------------------------------------===//
+//
+// 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 "RoundToIntegerTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/lrintbf16.h"
+
+LIST_ROUND_TO_INTEGER_TESTS_WITH_MODES(bfloat16, long,
+                                       LIBC_NAMESPACE::lrintbf16)

diff  --git a/libc/test/src/math/lroundbf16_test.cpp b/libc/test/src/math/lroundbf16_test.cpp
new file mode 100644
index 0000000000000..2f2b7b1a71b36
--- /dev/null
+++ b/libc/test/src/math/lroundbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for lroundbf16 ------------------------------------------===//
+//
+// 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 "RoundToIntegerTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/lroundbf16.h"
+
+LIST_ROUND_TO_INTEGER_TESTS(bfloat16, long, LIBC_NAMESPACE::lroundbf16)

diff  --git a/libc/test/src/math/nearbyintbf16_test.cpp b/libc/test/src/math/nearbyintbf16_test.cpp
new file mode 100644
index 0000000000000..2d64fc5a154ee
--- /dev/null
+++ b/libc/test/src/math/nearbyintbf16_test.cpp
@@ -0,0 +1,14 @@
+//===-- Unittests for nearbyintbf16 ---------------------------------------===//
+//
+// 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 "NearbyIntTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/nearbyintbf16.h"
+
+LIST_NEARBYINT_TESTS(bfloat16, LIBC_NAMESPACE::nearbyintbf16)

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

diff  --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 65da1198a4044..929d7b02fa962 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -936,6 +936,23 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  lroundbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    lroundbf16_test.cpp
+  HDRS
+    RoundToIntegerTest.h
+  DEPENDS
+    libc.src.errno.errno
+    libc.src.math.lroundbf16
+    libc.src.__support.CPP.algorithm
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+)
+
 add_fp_unittest(
   llround_test
   SUITE
@@ -1016,6 +1033,23 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  llroundbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    llroundbf16_test.cpp
+  HDRS
+    RoundToIntegerTest.h
+  DEPENDS
+    libc.src.errno.errno
+    libc.src.math.llroundbf16
+    libc.src.__support.CPP.algorithm
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+)
+
 add_fp_unittest(
   rint_test
   SUITE
@@ -1086,6 +1120,21 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  rintbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    rintbf16_test.cpp
+  HDRS
+    RIntTest.h
+  DEPENDS
+    libc.src.math.rintbf16
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+)
+
 add_fp_unittest(
   lrint_test
   SUITE
@@ -1166,6 +1215,23 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  lrintbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    lrintbf16_test.cpp
+  HDRS
+    RoundToIntegerTest.h
+  DEPENDS
+    libc.src.errno.errno
+    libc.src.math.lrintbf16
+    libc.src.__support.CPP.algorithm
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+)
+
 add_fp_unittest(
   llrint_test
   SUITE
@@ -1246,6 +1312,23 @@ add_fp_unittest(
     libc.src.__support.FPUtil.fp_bits
 )
 
+add_fp_unittest(
+  llrintbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    llrintbf16_test.cpp
+  HDRS
+    RoundToIntegerTest.h
+  DEPENDS
+    libc.src.errno.errno
+    libc.src.math.llrintbf16
+    libc.src.__support.CPP.algorithm
+    libc.src.__support.FPUtil.bfloat16
+    libc.src.__support.FPUtil.fenv_impl
+    libc.src.__support.FPUtil.fp_bits
+)
+
 add_fp_unittest(
   exp_test
   SUITE
@@ -3596,6 +3679,20 @@ add_fp_unittest(
     libc.src.math.nearbyintf128
 )
 
+add_fp_unittest(
+  nearbyintbf16_test
+  SUITE
+    libc-math-smoke-tests
+  SRCS
+    nearbyintbf16_test.cpp
+  HDRS
+    NearbyIntTest.h
+  DEPENDS
+    libc.hdr.fenv_macros
+    libc.src.math.nearbyintbf16
+    libc.src.__support.FPUtil.bfloat16
+)
+
 add_fp_unittest(
   nextafter_test
   SUITE

diff  --git a/libc/test/src/math/smoke/RoundToIntegerTest.h b/libc/test/src/math/smoke/RoundToIntegerTest.h
index 2b460aef6ef32..6866e9109ca0a 100644
--- a/libc/test/src/math/smoke/RoundToIntegerTest.h
+++ b/libc/test/src/math/smoke/RoundToIntegerTest.h
@@ -97,8 +97,8 @@ class RoundToIntegerTestTemplate
     test_one_input(func, F(-1.0), I(-1), false);
     test_one_input(func, F(10.0), I(10), false);
     test_one_input(func, F(-10.0), I(-10), false);
-    test_one_input(func, F(1234.0), I(1234), false);
-    test_one_input(func, F(-1234.0), I(-1234), false);
+    test_one_input(func, F(1232.0), I(1232), false);
+    test_one_input(func, F(-1232.0), I(-1232), false);
   }
 
   void testRoundNumbers(RoundToIntegerFunc func) {
@@ -124,7 +124,7 @@ class RoundToIntegerTestTemplate
         continue;
       // All subnormal numbers should round to zero.
       if (TestModes) {
-        if (x > 0) {
+        if (x > zero) {
           LIBC_NAMESPACE::fputil::set_round(FE_UPWARD);
           test_one_input(func, x, I(1), false);
           LIBC_NAMESPACE::fputil::set_round(FE_DOWNWARD);

diff  --git a/libc/test/src/math/smoke/llrintbf16_test.cpp b/libc/test/src/math/smoke/llrintbf16_test.cpp
new file mode 100644
index 0000000000000..e841e627e2634
--- /dev/null
+++ b/libc/test/src/math/smoke/llrintbf16_test.cpp
@@ -0,0 +1,15 @@
+//===-- Unittests for llrintbf16 ------------------------------------------===//
+//
+// 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 "RoundToIntegerTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/llrintbf16.h"
+
+LIST_ROUND_TO_INTEGER_TESTS_WITH_MODES(bfloat16, long long,
+                                       LIBC_NAMESPACE::llrintbf16)

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

diff  --git a/libc/test/src/math/smoke/lrintbf16_test.cpp b/libc/test/src/math/smoke/lrintbf16_test.cpp
new file mode 100644
index 0000000000000..65a5633b81723
--- /dev/null
+++ b/libc/test/src/math/smoke/lrintbf16_test.cpp
@@ -0,0 +1,15 @@
+//===-- Unittests for lrintbf16 -------------------------------------------===//
+//
+// 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 "RoundToIntegerTest.h"
+
+#include "src/__support/FPUtil/bfloat16.h"
+#include "src/math/lrintbf16.h"
+
+LIST_ROUND_TO_INTEGER_TESTS_WITH_MODES(bfloat16, long,
+                                       LIBC_NAMESPACE::lrintbf16)

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

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

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

diff  --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index ff3bebb1c500b..2155f324aa8a5 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -791,6 +791,8 @@ template bool round_to_long<float16>(float16, long &);
 template bool round_to_long<float128>(float128, long &);
 #endif // LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
 
+template bool round_to_long<bfloat16>(bfloat16, long &);
+
 template <typename T> bool round_to_long(T x, RoundingMode mode, long &result) {
   MPFRNumber mpfr(x);
   return mpfr.round_to_long(get_mpfr_rounding_mode(mode), result);
@@ -808,6 +810,8 @@ template bool round_to_long<float16>(float16, RoundingMode, long &);
 template bool round_to_long<float128>(float128, RoundingMode, long &);
 #endif // LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
 
+template bool round_to_long<bfloat16>(bfloat16, RoundingMode, long &);
+
 template <typename T> T round(T x, RoundingMode mode) {
   MPFRNumber mpfr(x);
   MPFRNumber result = mpfr.rint(get_mpfr_rounding_mode(mode));
@@ -826,6 +830,8 @@ template float16 round<float16>(float16, RoundingMode);
 template float128 round<float128>(float128, RoundingMode);
 #endif // LIBC_TYPES_FLOAT128_IS_NOT_LONG_DOUBLE
 
+template bfloat16 round<bfloat16>(bfloat16, RoundingMode);
+
 } // namespace mpfr
 } // namespace testing
 } // namespace LIBC_NAMESPACE_DECL


        


More information about the libc-commits mailing list