This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Patch for making USHRT_MAX of type unsigned int


Geoff Keating wrote:
> 
> Vladimir Makarov <vmakarov@toke.toronto.redhat.com> writes:
> 
> > Hello,
> >
> >   According to ANSI C standard, compiler defines type of decimal
> > constant without suffix searching appropriate type in the following
> > order:
> >        int
> >        long int
> >        long int int
> >
> >   What type will integer constant 65535 have on a 16-bit machine with
> > short int and int are represented by 2 byte integer and long int is
> > represented by 4 byte integer?  It will be long int.  In this case we
> > have
> >
> > sizeof (USHRT_MAX) == sizeof (long) == 4
> > sizeof (USHRT_MAX) > sizeof (UINT_MAX)
> >
> > which is not what the most users expect.  The following patch solves
> > the problem.  The unsigned suffix for the UCHAR_MAX, CHAR_MAX is added
> > for uniformity.
> >
> >   Actually, I did not find that sizeof (USHRT_MAX) > sizeof (UINT_MAX)
> > contradicts to the standard.  Therefore I am asking the approval to
> > commit the patch or waiting for any comment.
> 
> This patch is not quite right.
> 
> The C standard says that types such as USHRT_MAX should have type `as
> would an expression that is an object of the corresponding type
> converted according to the integer promotions'.
> 

Thanks, Geoff.  I missed that.

> So these values should be of type 'int' if 'int' can hold them, or
> 'unsigned int' if not (I believe one of the two is required to be able
> to hold the values).
> 
> I would suggest
> 
> #if INT_MAX <= 65535
> #define USHRT_MAX 65535
> #else
> #define USHRT_MAX 65535U
> #endif
> 
> For UCHAR_MAX, it is always correct to just write
> 
> #define UCHAR_MAX 255
> 
> as 'int' must be at least 16 bits.

Yes, I know it. Taking what you wrote above into account,  I'd propose
the following patch:

2001-06-25  Vladimir Makarov  <vmakarov@toke.toronto.redhat.com>

        * glimits.h (USHRT_MAX): Use unsigned suffix if int can not hold
        it.

Index: glimits.h
===================================================================
RCS file: /cvs/gcc/egcs/gcc/glimits.h,v
retrieving revision 1.10
diff -p -c -r1.10 glimits.h
*** glimits.h   2000/07/21 00:47:27     1.10
--- glimits.h   2001/06/25 22:04:24
***************
*** 44,53 ****
  #undef SHRT_MAX
  #define SHRT_MAX 32767
  
- /* Maximum value an `unsigned short int' can hold.  (Minimum is 0). 
*/
- #undef USHRT_MAX
- #define USHRT_MAX 65535
- 
  /* Minimum and maximum values a `signed int' can hold.  */
  #ifndef __INT_MAX__
  #define __INT_MAX__ 2147483647
--- 44,49 ----
***************
*** 56,61 ****
--- 52,65 ----
  #define INT_MIN (-INT_MAX-1)
  #undef INT_MAX
  #define INT_MAX __INT_MAX__
+ 
+ /* Maximum value an `unsigned short int' can hold.  (Minimum is 0). 
*/
+ #undef USHRT_MAX
+ #if INT_MAX < 65535
+ #define USHRT_MAX 65535U
+ else
+ #define USHRT_MAX 65535
+ #endif
  
  /* Maximum value an `unsigned int' can hold.  (Minimum is 0).  */
  #undef UINT_MAX


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]