Hi Quentin, On Tue, Jul 21, 2026 at 4:56 PM Quentin Schulz <[email protected]> wrote: > > 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?
It is indeed implementation defined in the C standard, but given that U-Boot enforces the gnu11 convention, it's defined to be reduction modulo 2^16 [1] along with two's complement representation, which is exactly what we need here. [1] https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html Best regards, Alexey
