integer overflow with PWM_FRACTION_TO_WIDTH and 32 bits timers

Use this forum for requesting small changes in ChibiOS. Large changes should be discussed in the development forum. This forum is NOT for support.
Post Reply
User avatar
alex31
Posts: 445
Joined: Fri May 25, 2012 10:23 am
Has thanked: 57 times
Been thanked: 88 times

integer overflow with PWM_FRACTION_TO_WIDTH and 32 bits timers

Post 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
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: integer overflow with PWM_FRACTION_TO_WIDTH and 32 bits timers

Post 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
Post Reply