From d4b8572f11869fd47a4606ea91ee59d2833ce23c Mon Sep 17 00:00:00 2001 From: Alexander Shaposhnikov Date: Sat, 28 Oct 2023 10:20:18 +0000 Subject: [PATCH] [compiler-rt] Fix src_rep_t_clz and clz_in_sig_frac This is a follow-up to 910a4bf5b. 1. __builtin_clz takes unsigned int, thus for uint16_t src_rep_t_clz can't use it directly but should subtract 16 (leading 16 bits of the promoted argument are zero). 2. Fix (and simplify) clz_in_sig_frac. Test plan: ninja check-compiler-rt (extendhfsf2_test.c and extenddftf2_test.c) --- compiler-rt/lib/builtins/fp_extend.h | 8 ++++++-- compiler-rt/test/builtins/Unit/extenddftf2_test.c | 6 +++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/compiler-rt/lib/builtins/fp_extend.h b/compiler-rt/lib/builtins/fp_extend.h index 961e521846a2..95ea2a7ac4b2 100644 --- a/compiler-rt/lib/builtins/fp_extend.h +++ b/compiler-rt/lib/builtins/fp_extend.h @@ -75,7 +75,11 @@ static const int srcSigFracBits = 10; // srcBits - srcSigFracBits - 1 static const int srcExpBits = 5; -#define src_rep_t_clz __builtin_clz +static inline int src_rep_t_clz_impl(src_rep_t a) { + return __builtin_clz(a) - 16; +} + +#define src_rep_t_clz src_rep_t_clz_impl #else #error Source should be half, single, or double precision! @@ -138,7 +142,7 @@ static inline src_rep_t extract_sig_frac_from_src(src_rep_t x) { #ifdef src_rep_t_clz static inline int clz_in_sig_frac(src_rep_t sigFrac) { - const int skip = (sizeof(dst_t) * CHAR_BIT - srcBits) + 1 + srcExpBits; + const int skip = 1 + srcExpBits; return src_rep_t_clz(sigFrac) - skip; } #endif diff --git a/compiler-rt/test/builtins/Unit/extenddftf2_test.c b/compiler-rt/test/builtins/Unit/extenddftf2_test.c index fcc030ca9220..b2fbdcf1b079 100644 --- a/compiler-rt/test/builtins/Unit/extenddftf2_test.c +++ b/compiler-rt/test/builtins/Unit/extenddftf2_test.c @@ -64,7 +64,11 @@ int main() UINT64_C(0x3fd2edcba9876543), UINT64_C(0x2000000000000000))) return 1; - + // denormal + if (test__extenddftf2(1.8194069811494184E-308, + UINT64_C(0x3c00a2a7757954b9), + UINT64_C(0x6000000000000000))) + return 1; #else printf("skipped\n");