bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: stdint_.h vs intmax_t & uintmax_t


From: Ben Pfaff
Subject: Re: stdint_.h vs intmax_t & uintmax_t
Date: Mon, 09 Jan 2006 16:02:54 -0800
User-agent: Gnus/5.110004 (No Gnus v0.4) Emacs/21.4 (gnu/linux)

"Mark D. Baushke" <address@hidden> writes:

> Bruno Haible <address@hidden> writes:
>
>> Mark D. Baushke wrote on 2005-11-15:
>> > it would be more portable to use this:
>> > 
>> >     #ifndef SIZE_MAX
>> >     # define SIZE_MAX ((size_t)-1)
>> >     #endif
>> > 
>> > because ((size_t)-1) will work even if size_t is narrower than int.
>> 
>> Yes. As explained on 2005-07-11, I prefer to write this as
>>   ((size_t)~(size_t)0)
>> because this way we don't need to refer to ISO C 99 6.3.1.3.(2).
>
> Hmmm... let me see.
>
> ISO C 99 6.3.1.3.(2) says:
> | 2 Otherwise, if the new type is unsigned, the
> |   value is converted by repeatedly adding or
> |   subtracting one more than the maximum value that
> |   can be represented in the new type until the
> |   value is in the range of the new type. 49)
> | 
> | 49) The rules describe arithmetic on the
> |     mathematical value, not the value of a given
> |     type of expression.
>
> I guess I didn't see the ISO C 99 reference in the
> 2005-07-11 posting. Perhaps, I didn't find the one
> you referenced...

I don't understand why ((size_t)-1) is considered C99 specific.
Here is the equivalent paragraph from C90, numbered 3.2.1.2.(2)
and .(3):

       When a signed integer is converted to an unsigned integer
    with equal or greater size, if the value of the signed
    integer is nonnegative, its value is unchanged.  Otherwise:
    if the unsigned integer has greater size, the signed integer
    is first promoted to the signed integer corresponding to the
    unsigned integer; the value is converted to unsigned by
    adding to it one greater than the largest number that can be
    represented in the unsigned integer type. /22/

       When an integer is demoted to an unsigned integer with
    smaller size, the result is the nonnegative remainder on
    division by the number one greater than the largest unsigned
    number that can be represented in the type with smaller size.

...

    22. In a two's-complement representation, there is no actual
    change in the bit pattern except filling the high-order bits
    with copies of the sign bit if the unsigned integer has
    greater size.

Thus, ((size_t)-1) is also the maximum value of size_t in C90.

Am I misunderstanding some argument here?
-- 
Ben Pfaff 
email: address@hidden
web: http://benpfaff.org





reply via email to

[Prev in Thread] Current Thread [Next in Thread]