RTEMS | cpukit/dev/can/can-bittiming.c: Do not use abs() on unsigned values (!726)
Pavel Pisa (@ppisa)
gitlab at rtems.org
Fri Sep 19 22:13:18 UTC 2025
Pavel Pisa commented on a discussion on cpukit/dev/can/can-bittiming.c: https://gitlab.rtems.org/rtems/rtos/rtems/-/merge_requests/726#note_132797
> sample_point = 1000 * ( tseg + CAN_CALC_SYNC_SEG - tseg2 ) /
> ( tseg + CAN_CALC_SYNC_SEG );
>
> - sample_point_error = abs( sample_point_nominal - sample_point );
> + sample_point_error = sample_point_nominal - sample_point;
What is correct for values smaller than 32768 (16-minimal int width defined by standard or 2147483648 on the more common systems today) is
```
sample_point_error = abs( (int)sample_point_nominal - (int)sample_point );
```
The numbers should not exceed 32768 for all bitrates which make sense.
Another option is
```
sample_point_error = sample_point_nominal > sample_point ?;
sample_point_nominal - sample_point :
sample_point - sample_point_nominal;
```
Which would work to the range twice as big when compared to the first option.
--
View it on GitLab: https://gitlab.rtems.org/rtems/rtos/rtems/-/merge_requests/726#note_132797
You're receiving this email because of your account on gitlab.rtems.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/bugs/attachments/20250919/975c6e8d/attachment-0001.htm>
More information about the bugs
mailing list