[libc] Use custom utility functions within the libc project to eliminate some `-Wc++23-extensions` warnings
Cause
The issue arose when I was maintaining the AsmParser.cpp module.
I used the command ninja -C build llvm-mc to compile and debug the module to check for errors.
During the compilation, when the compiler reached libc/src/__support/math/erfcf16.h and libc/src/__support/math/erff16.h, the following warning messages appeared:
ninja: Entering directory `build'
[2/133] Building CXX object lib/Support/CMakeFiles/LLVMSupport.dir/APFloat.cpp.o
In file included from /home/the-essence-of-life/github-project/llvm-project/llvm/../libc/shared/math/erfcf16.h:15,
from /home/the-essence-of-life/github-project/llvm-project/llvm/../libc/shared/math.h:103,
from /home/the-essence-of-life/github-project/llvm-project/llvm/lib/Support/APFloat.cpp:43:
/home/the-essence-of-life/github-project/llvm-project/llvm/../libc/src/__support/math/erfcf16.h:96:5: warning: ‘f16’ or ‘F16’ suffix on floating constant only available with ‘-std=c++23’ or ‘-std=gnu++23’ [-Wc++23-extensions]
96 | return 1.0f16;
| ^~~~~~
/home/the-essence-of-life/github-project/llvm-project/llvm/../libc/src/__support/math/erfcf16.h:112:5: warning: ‘f16’ or ‘F16’ suffix on floating constant only available with ‘-std=c++23’ or ‘-std=gnu++23’ [-Wc++23-extensions]
112 | return 0.0f16;
| ^~~~~~
In file included from /home/the-essence-of-life/github-project/llvm-project/llvm/../libc/shared/math/erff16.h:15,
from /home/the-essence-of-life/github-project/llvm-project/llvm/../libc/shared/math.h:105:
/home/the-essence-of-life/github-project/llvm-project/llvm/../libc/src/__support/math/erff16.h:137:7: warning: ‘f16’ or ‘F16’ suffix on floating constant only available with ‘-std=c++23’ or ‘-std=gnu++23’ [-Wc++23-extensions]
137 | return is_neg ? -1.0f16 : 1.0f16;
| ^~~~~~
/home/the-essence-of-life/github-project/llvm-project/llvm/../libc/src/__support/math/erff16.h:137:7: warning: ‘f16’ or ‘F16’ suffix on floating constant only available with ‘-std=c++23’ or ‘-std=gnu++23’ [-Wc++23-extensions]
/home/the-essence-of-life/github-project/llvm-project/llvm/../libc/src/__support/math/erff16.h:147:3: warning: ‘f16’ or ‘F16’ suffix on floating constant only available with ‘-std=c++23’ or ‘-std=gnu++23’ [-Wc++23-extensions]
147 | int idx = static_cast<int>(xbits.abs().get_val() * 8.0f16);
| ^~~
Solution
I have already attempted to fix this bug by submitting a pull request, and I’ve also added unit tests to cover positive, negative, and zero values (#215613).
My initial approach to fixing this was to use constexpr to store this value as a variable, or to add conditional checks based on the version in the two header files, libc/src/math/erff16.h and libc/src/math/erfcf16.h, to work around the issue.
Later, I realized that if the original value were hard-coded directly into the files, they would need to be updated every time there was a major version update.
So, I modified the original logic to use the fputil::cast interface provided by libc to automatically derive the value.
// return <single-precision><float16> -> return fputil::cast<float16>(<single-precision>)
return 1.0f16; -> return fputil::cast<float16>(1.0f);
Additional Notes
In this module, I think there’s a piece of logic that warrants further discussion:
// file: libc/src/__support/math/erff16.h
// The original logic:
int idx = static_cast<int>(xbits.abs().get_val() * 8.0f16);
// Here's my fix:
/// I saved it to a variable using `constexpr float16`.
static constexpr float16 EIGHT_F16 = fputil::cast<float16>(8.0f);
int idx = static_cast<int>(xbits.abs().get_val() * EIGHT_F16);
The original error message was as follows:
/home/the-essence-of-life/github-project/llvm-project/llvm/../libc/src/__support/math/erff16.h:147:3: warning: ‘f16’ or ‘F16’ suffix on floating constant only available with ‘-std=c++23’ or ‘-std=gnu++23’ [-Wc++23-extensions]
147 | int idx = static_cast<int>(xbits.abs().get_val() * 8.0f16);
| ^~~
cc1plus: note: unrecognized command-line option ‘-Wno-c99-extensions’ may have been intended to silence earlier diagnostics
My thinking at the time was fairly straightforward: I added a variable to prevent changes within the function from affecting it,
but I didn't consider that the static keyword following constexpr static would cause the variable to be reinitialized at runtime.
And then there's this code snippet:
// file: libc/src/__support/math/erff16.h
return fputil::cast<float16>(is_neg ? -1.0f - xf * 0x1.0p-28f : 1.0f - xf * 0x1.0p-28f);
This question might require you to devote a little bit of your valuable time to answer it:
Why is it written in the format 0x1.0p-28f? I’m really looking forward to hearing your answers!
Thank you, everyone!
@llvm/issue-subscribers-libc
Author: original-cooling-space (zhangweize9-cyber)
The issue arose when I was maintaining the AsmParser.cpp module.
I used the command ninja -C build llvm-mc to compile and debug the module to check for errors.
During the compilation, when the compiler reached libc/src/__support/math/erfcf16.h and libc/src/__support/math/erff16.h, the following warning messages appeared:
ninja: Entering directory `build'
[2/133] Building CXX object lib/Support/CMakeFiles/LLVMSupport.dir/APFloat.cpp.o
In file included from /home/the-essence-of-life/github-project/llvm-project/llvm/../libc/shared/math/erfcf16.h:15,
from /home/the-essence-of-life/github-project/llvm-project/llvm/../libc/shared/math.h:103,
from /home/the-essence-of-life/github-project/llvm-project/llvm/lib/Support/APFloat.cpp:43:
/home/the-essence-of-life/github-project/llvm-project/llvm/../libc/src/__support/math/erfcf16.h:96:5: warning: ‘f16’ or ‘F16’ suffix on floating constant only available with ‘-std=c++23’ or ‘-std=gnu++23’ [-Wc++23-extensions]
96 | return 1.0f16;
| ^~~~~~
/home/the-essence-of-life/github-project/llvm-project/llvm/../libc/src/__support/math/erfcf16.h:112:5: warning: ‘f16’ or ‘F16’ suffix on floating constant only available with ‘-std=c++23’ or ‘-std=gnu++23’ [-Wc++23-extensions]
112 | return 0.0f16;
| ^~~~~~
In file included from /home/the-essence-of-life/github-project/llvm-project/llvm/../libc/shared/math/erff16.h:15,
from /home/the-essence-of-life/github-project/llvm-project/llvm/../libc/shared/math.h:105:
/home/the-essence-of-life/github-project/llvm-project/llvm/../libc/src/__support/math/erff16.h:137:7: warning: ‘f16’ or ‘F16’ suffix on floating constant only available with ‘-std=c++23’ or ‘-std=gnu++23’ [-Wc++23-extensions]
137 | return is_neg ? -1.0f16 : 1.0f16;
| ^~~~~~
/home/the-essence-of-life/github-project/llvm-project/llvm/../libc/src/__support/math/erff16.h:137:7: warning: ‘f16’ or ‘F16’ suffix on floating constant only available with ‘-std=c++23’ or ‘-std=gnu++23’ [-Wc++23-extensions]
/home/the-essence-of-life/github-project/llvm-project/llvm/../libc/src/__support/math/erff16.h:147:3: warning: ‘f16’ or ‘F16’ suffix on floating constant only available with ‘-std=c++23’ or ‘-std=gnu++23’ [-Wc++23-extensions]
147 | int idx = static_cast<int>(xbits.abs().get_val() * 8.0f16);
| ^~~
Solution
I have already attempted to fix this bug by submitting a pull request, and I’ve also added unit tests to cover positive, negative, and zero values (#215613).
My initial approach to fixing this was to use constexpr to store this value as a variable, or to add conditional checks based on the version in the two header files, libc/src/math/erff16.h and libc/src/math/erfcf16.h, to work around the issue.
Later, I realized that if the original value were hard-coded directly into the files, they would need to be updated every time there was a major version update.
So, I modified the original logic to use the fputil::cast interface provided by libc to automatically derive the value.
// return <single-precision><float16> -> return fputil::cast<float16>(<single-precision>)
return 1.0f16; -> return fputil::cast<float16>(1.0f);
Additional Notes
In this module, I think there’s a piece of logic that warrants further discussion:
// file: libc/src/__support/math/erff16.h
// The original logic:
int idx = static_cast<int>(xbits.abs().get_val() * 8.0f16);
// Here's my fix:
/// I saved it to a variable using `constexpr float16`.
static constexpr float16 EIGHT_F16 = fputil::cast<float16>(8.0f);
int idx = static_cast<int>(xbits.abs().get_val() * EIGHT_F16);
The original error message was as follows:
/home/the-essence-of-life/github-project/llvm-project/llvm/../libc/src/__support/math/erff16.h:147:3: warning: ‘f16’ or ‘F16’ suffix on floating constant only available with ‘-std=c++23’ or ‘-std=gnu++23’ [-Wc++23-extensions]
147 | int idx = static_cast<int>(xbits.abs().get_val() * 8.0f16);
| ^~~
cc1plus: note: unrecognized command-line option ‘-Wno-c99-extensions’ may have been intended to silence earlier diagnostics
My thinking at the time was fairly straightforward: I added a variable to prevent changes within the function from affecting it,
but I didn't consider that the static keyword following constexpr static would cause the variable to be reinitialized at runtime.
And then there's this code snippet:
// file: libc/src/__support/math/erff16.h
return fputil::cast<float16>(is_neg ? -1.0f - xf * 0x1.0p-28f : 1.0f - xf * 0x1.0p-28f);
This question might require you to devote a little bit of your valuable time to answer it:
Why is it written in the format 0x1.0p-28f? I’m really looking forward to hearing your answers!
Thank you, everyone!
Why is it written in the format 0x1.0p-28f? I’m really looking forward to hearing your answers!
For floating point constants that are a bit more complicated than 1.0 or 0.0, we prefer to write them in hexadecimal float format to be exact.
For floating point constants that are a bit more complicated than 1.0 or 0.0, we prefer to write them in hexadecimal float format to be exact.
Ah, I see! Thank you so much for taking the time to answer my question, I'm extremely grateful!