[libc] [llvm] [APFloat] Add exp function for APFloat::IEEESsingle using expf implementation from LLVM libc. (PR #143959)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 10 09:35:32 PST 2025
================
@@ -5601,6 +5604,29 @@ float APFloat::convertToFloat() const {
return Temp.getIEEE().convertToFloat();
}
+static constexpr int getFEnvRoundingMode(llvm::RoundingMode rm) {
+ switch (rm) {
+ case APFloat::rmTowardPositive:
+ return FE_UPWARD;
+ case APFloat::rmTowardNegative:
+ return FE_DOWNWARD;
+ case APFloat::rmTowardZero:
+ return FE_TOWARDZERO;
+ default:
+ // TODO: fix rmNearestTiesToAway for platform without FE_TONEARESTFROMZERO.
+ return FE_TONEAREST;
+ };
+}
+
+APFloat exp(const APFloat &X, RoundingMode rounding_mode) {
+ if (&X.getSemantics() == &semIEEEsingle) {
+ float result = LIBC_NAMESPACE::shared::expf(
+ X.convertToFloat(), getFEnvRoundingMode(rounding_mode));
+ return APFloat(result);
----------------
lntue wrote:
@majnemer @jcranmer-intel We would love to reuse APFloat parts in LLVM libc if possible. Unfortunately, APFloat uses many pieces from C++ STL, for which a lot of them come back to libc. We tried to get some sort of carveout of libc++ to be able to reuse directly in LLVM libc, but that turned out to be a lot more involved and messy on both libc++ and libc. In the end, we have to reimplement pieces of libc++ that we use in LLVM libc, which might not be as robust as in libc++, but it was actually not that many.
About floating point facility, currently we do have all the needed parts for a complete soft-float library for all* the floating point types (for ppc_fp128, I'll need some public documentation on all the edge case behaviors to complete it). It was used in our recent GSoC '25 project to enable `bfloat16` on all platforms. And we plan to extend that next year (2026) to provide `expf128`, `expf80`, `expdd` that work on all platforms.
TLDR:
- From the project restrictions POV, it makes sense for APFloat to depend on LLVM libc, and hopefully with direct code dependency without build dependency, we will make this easier.
- LLVM libc math implementations will provide all the soft-float implementations to make sure all the types and functions are working consistently on all platforms. We will slowly but surely be able to remove restrictions such as modifying or depending on floating point environments.
https://github.com/llvm/llvm-project/pull/143959
More information about the llvm-commits
mailing list