Comment on time calculation and constant types

The free CAN Software API (Application Programming Interface) for Windows®
Post Reply
H.Mueller
Posts: 6
Joined: Wed 6. Aug 2014, 16:05

Comment on time calculation and constant types

Post by H.Mueller » Mon 15. Dec 2014, 10:23

Hi,

Please consider the following comments

A) Enhance PCAN-Basic Documentation on TPCANTimestamp
So far it is noted
"Calculation of total of microseconds : micros + 1000 * millis + 0xFFFFFFFF * 1000 * millis_overflow"

I suggest to explicit hint the reader to overflow issues. For usage in C/C++ the above equation should read e.g.
"micros + 1000ULL * millis + 0xFFFFFFFF * 1000ULL * millis_overflow"

Rationale:
0xFFFFFFFF * 1000 will immediately overflow to 0xFFFFFC18
0xFFFFFFFF * 1000ULL will result in 0x3E7FFFFFC18. This 64 bit value is still large enough to be multiplied with 16 bit unsigned value millis_overflow. The same argumentation holds for 1000ULL * millis if millis is near overflow.

Keep in mind order of operations and implicit conversions, e.g. (0xFFFFFFFF * 2ULL * 2) is different from (0xFFFFFFFF * 2 * 2ULL)


B) Enhancement proposal: match constant types to related variable types
Example: TPCANStatus is of type DWORD, which is unsigned. The related constants are defined as signed types, e.g.
#define PCAN_ERROR_BUSLIGHT 0x00004

Proposal is to change also to unsigned type, i.e.
#define PCAN_ERROR_BUSLIGHT 0x00004U
Rationale:
Operations, e.g. bit test operations with ~ (NOT), cause warnings for the following type of code

TPCANStatus status;
...
status &= ~PCAN_ERROR_BUSLIGHT;
gcc: negative integer implicitly converted to unsigned type [-Werror=sign-conversion]

Of course user can prevent the warning with an explicit cast.

Best Regards
H. Müller

K.Wagner
Software Development
Software Development
Posts: 1080
Joined: Wed 22. Sep 2010, 13:36

Re: Comment on time calculation and constant types

Post by K.Wagner » Tue 16. Dec 2014, 08:33

Hello H.Müller,

Thanks for your comments. We will consider them.
Best regards,
Keneth

Post Reply