Hello Geert, On Tue, Mar 12, 2024 at 09:11:42AM +0100, Geert Uytterhoeven wrote: > 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. ack. > I guess NSEC_PER_MSEC would be too obfuscating? or USEC_PER_SEC? Not sure. Also I'm unsure if using KILO instead of 1000 is really an improvement. If you know that the clkrate is a multiple of 1000 there is no reason to not use clkrate / 1000 unconditionally. So maybe use rzg2l_gpt->rate_kHz (and error out in .probe if the rate isn't a multiple of 1000?) Best regards Uwe -- Pengutronix e.K. | Uwe Kleine-König | Industrial Linux Solutions | https://www.pengutronix.de/ |
Attachment:
signature.asc
Description: PGP signature