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.
User avatar
alex31
Posts: 374
Joined: Fri May 25, 2012 10:23 am
Location: toulouse, france
Has thanked: 38 times
Been thanked: 61 times
Contact:

integer overflow with PWM_FRACTION_TO_WIDTH and 32 bits timers

Postby alex31 » Mon Jun 21, 2021 3:03 pm

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: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: integer overflow with PWM_FRACTION_TO_WIDTH and 32 bits timers

Postby Giovanni » Mon Jun 21, 2021 5:31 pm

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


Return to “Small Change Requests”

Who is online

Users browsing this forum: No registered users and 13 guests