Hi Uwe, On Tue, Mar 12, 2024 at 8:20 AM Uwe Kleine-König <u.kleine-koenig@xxxxxxxxxxxxxx> wrote: > On Tue, Feb 20, 2024 at 07:43:17PM +0000, Biju Das wrote: > > --- /dev/null > > +++ b/drivers/pwm/pwm-rzg2l-gpt.c > > +static u64 calculate_period_or_duty(struct rzg2l_gpt_chip *rzg2l_gpt, u32 val, u8 prescale) > > +{ > > + u64 tmp, d; > > + > > + /* > > + * Rate is in MHz and is always integer for peripheral clk > > + * 2^32 * 2^10 (prescalar) * 10^9 > 2^64 > > + * 2^32 * 2^10 (prescalar) * 10^6 < 2^64 > > + * Multiply val with prescalar first, if the result is less than > > + * 2^34, then multiply by 10^9. Otherwise divide nr and dr by 10^3 > > + * so that it will never overflow. > > + */ > > + > > + tmp = (u64)val << (2 * prescale); > > + if (tmp <= (1ULL << 34)) { > > I would have written that as: > > if (tmp >> 34 == 0) > > (which implements tmp < (1ULL << 34), which doesn't matter much). > > > + tmp *= NSEC_PER_SEC; > > + d = rzg2l_gpt->rate; > > + } else { > > + tmp *= div64_u64(NSEC_PER_SEC, KILO); > > I don't know if the compiler is clever enough to not calculate that > every time? Not on 32-bit when written that way. > Also using div64_u64 is too heavy given that both values fit > into an u32. Indeed, so "NSEC_PER_SEC / KILO" should be fine. I guess NSEC_PER_MSEC would be too obfuscating? > > > + d = div64_u64(rzg2l_gpt->rate, KILO); > > At first I thought you could better use 1024 as the common divisor here > as it could be implemented using a shift operation. But I understood > with the comment above that we're not losing precision here as both > NSEC_PER_SEC and rate are a multiple of 1000. > > Maybe s/Rate is in MHz and is always integer for peripheral clk/Rate is > a multiple of 1000000, and so dividing by 1000 is an exact operation./ ? > > > + } > > + > > + return DIV64_U64_ROUND_UP(tmp, d); > > +} Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@xxxxxxxxxxxxxx In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds