Hi Alexey,

On 7/13/26 8:35 PM, Alexey Charkov wrote:
Current code calculates the fractional component in 32 bits before
assigning it to a 64-bit holding variable, causing overflow for real-world
values of k, given that OSC_HZ is 24000000U. It also does bitwise manual
massaging of an unsigned representation of what is actually a two's
complement signed value, which is confusing and makes the code harder to
read.

Read k into a properly signed type and promote operands to avoid overflow,

I'm not sure this is correct? Converting an unsigned integer (readl returns an u32 and con is a u32) to signed (k is s16) is implementation-defined as far as I understood (c.f. https://en.cppreference.com/c/language/conversion). I'm sure I misunderstood the spec but considering signed integer overflow is undefined (and I guess one could understand casting a u32 storing a number bigger than S16_MAX into an s16 to be some kind of overflow), I'm a bit concerned here. I probably forgot important stuff I learned a decade ago :) Can you point me where/what I misunderstood?

Cheers,
Quentin

Reply via email to