Page 1 of 1

integer overflow with PWM_FRACTION_TO_WIDTH and 32 bits timers

Posted: Mon Jun 21, 2021 3:03 pm
by alex31
Hello,

The PWM_FRACTION_TO_WIDTH seems tailored for 16 bits timer :

Code: Select all

#define PWM_FRACTION_TO_WIDTH(pwmp, denominator, numerator)                 \
  ((pwmcnt_t)((((pwmcnt_t)(pwmp)->period) *                                 \
               (pwmcnt_t)(numerator)) / (pwmcnt_t)(denominator)))


In case of 32 bits timers, period can be large, and period * numerator can overflow.

Is it possible to replace the three last pwmcnt_t casts by uint64_t casts ?

There will be some runtime overhead, but PWM_FRACTION_TO_WIDTH should not be called from a hot path code.

Alexandre

Re: integer overflow with PWM_FRACTION_TO_WIDTH and 32 bits timers

Posted: Mon Jun 21, 2021 5:31 pm
by Giovanni
Hi,

HAL runs also on compilers that support no C99 and have no uint64_t types, this solution is problematic. Note that it is what we do in RT for time conversions but we can use C99 on that module.

Giovanni