This is an automated email from the ASF dual-hosted git repository.
acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 3c9b6318a00 libs/libm: support 128-bit quad-precision and universal
fallback in truncl
3c9b6318a00 is described below
commit 3c9b6318a00bef0a7dac4577e0125e9bc3ab8404
Author: Swatantra Yadav <[email protected]>
AuthorDate: Thu Oct 15 17:49:55 2026 +0530
libs/libm: support 128-bit quad-precision and universal fallback in truncl
Previously, truncl() aborted the kernel with PANIC() on architectures where
long double is not equal to double and LDBL_MANT_DIG != 64 (such as IEEE-754
binary128 quad-precision on RISC-V 64, AArch64, or SPARC with LDBL_MANT_DIG
== 113).
This commit:
1. Extends the musl bit-level branch to support LDBL_MANT_DIG == 113
(IEEE-754
quad-precision binary128) with proper big- and little-endian ldshape
layouts.
2. Replaces PANIC() in the #else block with an IEEE-754 compliant
mathematical
fallback using copysignl((x >= 0.0) ? floorl(x) : ceill(x), x),
guaranteeing
that truncl() never crashes on any architecture.
3. Removes the unused assert.h include and adds endian.h in alphabetical
order.
Fixes: #18351
Signed-off-by: Swatantra Yadav <[email protected]>
---
libs/libm/libm/lib_truncl.c | 28 ++++++++++++++++++++++++----
1 file changed, 24 insertions(+), 4 deletions(-)
diff --git a/libs/libm/libm/lib_truncl.c b/libs/libm/libm/lib_truncl.c
index 48aabbba872..91c8c80ac2d 100644
--- a/libs/libm/libm/lib_truncl.c
+++ b/libs/libm/libm/lib_truncl.c
@@ -35,10 +35,10 @@
#include <nuttx/config.h>
#include <nuttx/compiler.h>
-#include <assert.h>
-#include <stdint.h>
+#include <endian.h>
#include <float.h>
#include <math.h>
+#include <stdint.h>
/****************************************************************************
* Public Functions
@@ -54,7 +54,7 @@ long double truncl(long double x)
return trunc(x);
}
-#elif LDBL_MANT_DIG == 64
+#elif LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113
static const long double toint = 1 / LDBL_EPSILON;
@@ -63,8 +63,21 @@ union ldshape
long double f;
struct
{
+#if LDBL_MANT_DIG == 64
uint64_t m;
uint16_t se;
+#elif defined(CONFIG_ENDIAN_BIG) || \
+ (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
+ uint16_t se;
+ uint16_t top;
+ uint32_t mid;
+ uint64_t lo;
+#else
+ uint64_t lo;
+ uint32_t mid;
+ uint16_t top;
+ uint16_t se;
+#endif
} i;
};
@@ -113,9 +126,16 @@ long double truncl(long double x)
#else
+/* Universal fallback for any other long double representation */
+
long double truncl(long double x)
{
- PANIC(); /* FIX ME */
+ if (isnan(x))
+ {
+ return x;
+ }
+
+ return copysignl((x >= 0.0) ? floorl(x) : ceill(x), x);
}
#endif