[rtems commit] Use uint32_t instead of long. Use unsigned defines ( Prevent overflows on 16bit targets)

Ralf Corsepius ralf.corsepius at rtems.org
Wed Apr 17 16:09:27 UTC 2013


On 04/17/2013 06:01 PM, Gedare Bloom wrote:
> On Wed, Apr 17, 2013 at 11:15 AM, Ralf Corsepius
> <ralf.corsepius at rtems.org> wrote:
>> On 04/17/2013 02:53 PM, Sebastian Huber wrote:
>>> On 04/17/2013 12:19 PM, Ralf Corsepius wrote:
>>> [...]
>>>> @@ -147,8 +147,8 @@ static uint32_t simple_random(uint32_t v)
>>>>
>>>>    static unsigned get_bucket_with_random(unsigned count, long random)
>>>>    {
>>>> -  long unsigned unit = (1U << 31) / count;
>>>> -  long unsigned bucket = (long unsigned) random / unit;
>>>> +  uint32_t unit = ((uint32_t) 1U << 31) / count;
>>>> +  uint32_t bucket = (uint32_t) random / unit;
>>>>
>>>>      if (bucket != count) {
>>>>        return bucket;
>>>
>>> A long must be able to store a 32-bit integer so there is no need to use
>>> uint32_t here.
>>
>> Such is theory - Realty is different.
>>
>> The assumptions of long >= 32 bit only applies to POSIX conformant targets.
>>
>> RTEMS however supports targets, which are not strictly POSIX compliant,
>> where longs are 16bit => The code above fails to compile the code above when
>> using longs.
>>
> I'm pretty sure the minimum width of long is 32 bits in standard C,
> nothing to do with POSIX. If you can cite a counter-example, fine.
I don't know, who was first to introduce this convention.

Reality is, all these implicit assumptions on sizes are irrelevant. 
Portable code is better off not relying upon these.

> Anyway, it doesn't make much difference here whether long or uint32 is
> used.
>
Well, is does make a difference. Sebastian's original code did not 
compile on certain targets.

Ralf



More information about the devel mailing list