-
Notifications
You must be signed in to change notification settings - Fork 61
Add "Bfloat16 in LLVM libc" blog post #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
krishna2803
wants to merge
6
commits into
llvm:main
Choose a base branch
from
krishna2803:bfloat16
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
89e3e15
add: "Bfloat16 in LLVM libc" blog post
krishna2803 2523915
nit: grammar
krishna2803 ad4b779
chore: add libc math.h status page link
krishna2803 b8641c6
Apply suggestions
krishna2803 39a134a
add: more details
krishna2803 e3a5b8a
Schedule a post
asl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
author: "Krishna Pandey" | ||
date: "2025-09-10" | ||
tags: ["GSoC", "libc", "math", "c++23"] | ||
title: "GSoC 2025: Bfloat16 in LLVM libc" | ||
--- | ||
|
||
|
||
## Introduction | ||
BFloat16 is a 16-bit floating-point format, introduced by Google and standardized in C++23 as `std::bfloat16_t`. It uses 1 sign bit, 8 exponent bits (the same as `float`), and 7 mantissa bits. This configuration allows BFloat16 to represent a much wider dynamic range than IEEE `binary16` (~ 3×10^38 compared to 65,504), though with lower precision. BFloat16 has become popular in AI and machine learning use-cases where it offers significant performance advantages. | ||
krishna2803 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
The goal of this project was to implement the BFloat16 type in LLVM libc along with the basic math functions like `fabsbf16`, `fmaxbf16`, etc. | ||
We also want all functions to be generic, platform independent and correctly rounded for all rounding modes. | ||
krishna2803 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
## What was done | ||
|
||
- BFloat16 type was added to the LLVM libc (`libc/src/__support/FPUtil/bfloat16.h`) [#144463](https://github.com/llvm/llvm-project/pull/144463). | ||
krishna2803 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
- All 70 expected basic math functions for `bfloat16` were implemented, using a generic approach that supports all libc supported architectures (ARM, RISC-V, GPUs, x86, Darwin) (see table below). | ||
- Implemented two additional basic math functions: `iscanonicalbf16` and `issignalingbf16`. | ||
- Implemented higher math functions: `sqrtbf16` [#156654](https://github.com/llvm/llvm-project/pull/156654) and `log_bf16` [#157811](https://github.com/llvm/llvm-project/pull/157811) (open). | ||
- Comparison operations for the `FPBits` class were added [#144983](https://github.com/llvm/llvm-project/pull/144983). | ||
|
||
| Basic Math Function | PR | | ||
|----------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------| | ||
| `fabsbf16` | [#148398](https://github.com/llvm/llvm-project/pull/148398) | | ||
| `ceilbf16`, `floorbf16`, `roundbf16`, `roundevenbf16`, `truncbf16` | [#152352](https://github.com/llvm/llvm-project/pull/152352) | | ||
| `bf16add`, `bf16addf`, `bf16addl`, `bf16addf128`, `bf16sub`, `bf16subf`, `bf16subl`, `bf16subf128` | [#152774](https://github.com/llvm/llvm-project/pull/152774) | | ||
| `fmaxbf16`, `fminbf16` | [#152782](https://github.com/llvm/llvm-project/pull/152782) | | ||
| `bf16mul`, `bf16mulf`, `bf16mull`, `bf16mulf128` | [#152847](https://github.com/llvm/llvm-project/pull/152847) | | ||
| `fmaximumbf16`, `fmaximum_magbf16`, `fmaximum_mag_numbf16`, `fmaximum_numbf16`, `fminimumbf16`, `fminimum_magbf16`, `fminimum_mag_numbf16`, `fminimum_numbf16` | [#152881](https://github.com/llvm/llvm-project/pull/152881) | | ||
| `bf16div`, `bf16divf`, `bf16divl`, `bf16divf128` | [#153191](https://github.com/llvm/llvm-project/pull/153191) | | ||
| `bf16fma`, `bf16fmaf`, `bf16fmal`, `bf16fmaf128` | [#153231](https://github.com/llvm/llvm-project/pull/153231) | | ||
| `llrintbf16`, `llroundbf16`, `lrintbf16`, `lroundbf16`, `nearbyintbf16`, `rintbf16` | [#153882](https://github.com/llvm/llvm-project/pull/153882) | | ||
| `fromfpbf16`, `fromfpxbf16`, `ufromfpbf16`, `ufromfpxbf16` | [#153992](https://github.com/llvm/llvm-project/pull/153992) | | ||
| `nextafterbf16`, `nextdownbf16`, `nexttowardbf16`, `nextupbf16` | [#153993](https://github.com/llvm/llvm-project/pull/153993) | | ||
| `getpayloadbf16`, `setpayloadbf16`, `setpayloadsigbf16` | [#153994](https://github.com/llvm/llvm-project/pull/153994) | | ||
| `nanbf16` | [#153995](https://github.com/llvm/llvm-project/pull/153995) | | ||
| `frexpbf16`, `ilogbbf16`, `ldexpbf16`, `llogbbf16`, `logbbf16` | [#154427](https://github.com/llvm/llvm-project/pull/154427) | | ||
| `modfbf16`, `remainderbf16`, `remquobf16` | [#154652](https://github.com/llvm/llvm-project/pull/154652) | | ||
| `canonicalizebf16`, `iscanonicalbf16`, `issignalingbf16`, `copysignbf16`, `fdimbf16` | [#155567](https://github.com/llvm/llvm-project/pull/155567) | | ||
| `totalorderbf16`, `totalordermagbf16` | [#155568](https://github.com/llvm/llvm-project/pull/155568) | | ||
| `scalbnbf16`, `scalblnbf16` | [#155569](https://github.com/llvm/llvm-project/pull/155569) | | ||
| `fmodbf16` | [#155575](https://github.com/llvm/llvm-project/pull/155575) | | ||
|
||
The implementation status can be viewed at the libc `math.h` header implementation [status page](https://libc.llvm.org/headers/math/index.html), which is updated regularly. | ||
|
||
## What was not done | ||
|
||
- The implementation relied on a generic approach, so the `__bf16` compiler intrinsic was not used. | ||
- Hardware optimizations provided by Intel's [AVX-512_BF16](https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#avx512techs=AVX512_BF16) were not utilized. These instructions only support round-to-nearest-even mode, always flush output denormals to zero, and treat input denormals as zero, which does not align with our goal. See [VCVTNE2PS2BF16 instruction description](https://www.felixcloutier.com/x86/vcvtne2ps2bf16#description). | ||
- Not all higher math functions were implemented due to time constraints. | ||
|
||
## Future Work | ||
- Implement the remaining higher math functions. | ||
- Perform performance comparisons with other libc implementations once their `bfloat16` support is available. | ||
|
||
- Update the test suite when the `mpfr_get_bfloat16` function becomes available. | ||
|
||
## Acknowledgements | ||
I would like to thank my mentors, Tue Ly and Nicolas Celik, for their invaluable guidance and support throughout this project. The project wouldn't have been possible without them. I am also grateful to the LLVM Foundation and the GSoC admins for giving me this opportunity. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.