Skip to content

Commit 883be05

Browse files
committed
Fix undefined behaviour in Bungee
1 parent 143353e commit 883be05

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

  • thirdparty/bungee_library/upstream/src

thirdparty/bungee_library/upstream/src/Stretch.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,15 @@ struct Time
5151
inline int32_t delta(int32_t phase, int32_t previous, int m) const
5252
{
5353
constexpr auto logS = 32 + log2SynthesisHopRevolution;
54-
const int32_t da = (phase - previous) - m * a;
55-
return (m << logS) + (da >> 15) * multiplier;
54+
// const int32_t da = (phase - previous) - m * a;
55+
// return (m << logS) + (da >> 15) * multiplier;
56+
const int32_t phaseDiff = int32_t(int64_t(phase) - int64_t(previous));
57+
const int32_t ma = int32_t(int64_t(m) * int64_t(a));
58+
const int32_t da = int32_t(int64_t(phaseDiff) - int64_t(ma));
59+
const uint64_t shifted64 = uint64_t(uint32_t(m)) << logS;
60+
const int32_t mShifted = int32_t(uint32_t(shifted64));
61+
const int32_t scaled = int32_t((int64_t(da) >> 15) * int64_t(multiplier));
62+
return int32_t(int64_t(mShifted) + int64_t(scaled));
5663
}
5764

5865
inline int16_t delta(int16_t phase, int16_t previous, int m) const

0 commit comments

Comments
 (0)