Hi Uwe, On Thu, Apr 14, 2022 at 12:27 PM Geert Uytterhoeven <geert@xxxxxxxxxxxxxx> wrote: > On Wed, Apr 13, 2022 at 10:51 AM Uwe Kleine-König > <u.kleine-koenig@xxxxxxxxxxxxxx> wrote: > > Dividing by the result of a division looses precision. Consider for example > > clk_rate = 33000000 and period_ns = 500001. Then > > > > clk_rate / (NSEC_PER_SEC / period_ns) > > > > has the exact value 16500.033, but in C this evaluates to 16508. It gets > > worse for even bigger values of period_ns, so with period_ns = 500000001, > > the exact result is 16500000.033 while in C we get 33000000. > > > > For that reason use > > > > clk_rate * period_ns / NSEC_PER_SEC > > > > instead which doesn't suffer from this problem. To ensure this doesn't > > overflow add a safeguard check for clk_rate. > > > > Incidentally this fixes a division by zero if period_ns > NSEC_PER_SEC. > > Another side effect is that values bigger than INT_MAX for period and > > duty_cyle are not wrongly discarded any more. > > You forgot to mention that pwm_state.period is no longer truncated to u32. Please ignore this bogus comment. Sorry for the fuzz. 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