Skip to content

Commit 739055b

Browse files
committed
Fix mulShift32 for s2f (handle shift >= 64)
1 parent 3a0740c commit 739055b

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

ryu/f2s_intrinsics.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,22 @@ static inline uint32_t mulShift32(const uint32_t m, const uint64_t factor, const
7474
const uint64_t bits0 = (uint64_t)m * factorLo;
7575
const uint64_t bits1 = (uint64_t)m * factorHi;
7676

77-
#ifdef RYU_32_BIT_PLATFORM
77+
#if defined(RYU_32_BIT_PLATFORM)
7878
// On 32-bit platforms we can avoid a 64-bit shift-right since we only
7979
// need the upper 32 bits of the result and the shift value is > 32.
8080
const uint32_t bits0Hi = (uint32_t)(bits0 >> 32);
8181
uint32_t bits1Lo = (uint32_t)(bits1);
8282
uint32_t bits1Hi = (uint32_t)(bits1 >> 32);
8383
bits1Lo += bits0Hi;
8484
bits1Hi += (bits1Lo < bits0Hi);
85-
const int32_t s = shift - 32;
86-
return (bits1Hi << (32 - s)) | (bits1Lo >> s);
85+
if (shift >= 64) {
86+
// s2f can call this with a shift value >= 64, which we have to handle.
87+
// This could now be slower than the !defined(RYU_32_BIT_PLATFORM) case.
88+
return (uint32_t)(bits1Hi >> (shift - 64));
89+
} else {
90+
const int32_t s = shift - 32;
91+
return (bits1Hi << (32 - s)) | (bits1Lo >> s);
92+
}
8793
#else // RYU_32_BIT_PLATFORM
8894
const uint64_t sum = (bits0 >> 32) + bits1;
8995
const uint64_t shiftedSum = sum >> (shift - 32);

0 commit comments

Comments
 (0)