[comp.lang.c] Common ansi violation, exemplified.

am@jenny (Alan Mycroft) (04/18/88)

In article <1988Apr13.163235.420@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
>> 	#elif (1<<63) < 0
>> 	#define int_size_in_bits	64
>Furthermore, even
>ignoring that, there is another problem:  the result of shifting ***beyond***
>the available number of bits is implementation-defined (or undefined).
There is a related problem in lesser shifts in that the ANSI draft forbids
        1<<31   even when ints have 32 bits (overflow)
Note that
	1U<<31  has to be used instead.
Since overflow is undefined, the test
        if ((1<<31) < 0) may do exactly as it wishes.
When ints have 32 bits, I believe the public review 2nd draft *requires*
constant reducers to report a diagnostic for 1<<31 if they reduce it
(overflow during constant reduction to be reported).
They probably do not need to do so if they do not reduce it.
Hmm, wonder about the 'as if' rule here!!!

duane@cg-atla.UUCP (Andrew Duane X5993) (04/20/88)

In article <122@gannet.cl.cam.ac.uk>, am@jenny (Alan Mycroft) writes:
> In article <1988Apr13.163235.420@utzoo.uucp> henry@utzoo.uucp (Henry Spencer) writes:
> >> 	#elif (1<<63) < 0
> >> 	#define int_size_in_bits	64
> >Furthermore, even
> >ignoring that, there is another problem:  the result of shifting ***beyond***
> >the available number of bits is implementation-defined (or undefined).
> There is a related problem in lesser shifts in that the ANSI draft forbids
>         1<<31   even when ints have 32 bits (overflow)
> Note that
> 	1U<<31  has to be used instead.

Pardon me for coming in late, but hasn't anyone looked through
the system include files? In /usr/include/sys/param.h, I see
the defines (on a SUN3):

/*
 * Fundamental constants of the implementation.
 */
#define NBBY    8               /* number of bits in a byte */
#define NBPW    sizeof(int)     /* number of bytes in an integer */

If what you are after is a define for the number of bytes in an
integer, why not try:

#define int_size_in_bits (NBBY*NBPW)

Andrew L. Duane (JOT-7)  w:(617)-658-5600 X5993  h:(603)-434-7934
Compugraphic Corp.			 decvax!cg-atla!duane
200 Ballardvale St.		       ulowell/ \laidback
Wilmington, Mass. 01887		   cbosgd!ima/   \cgeuro
Mail Stop 200II-3-5S		     ism780c/     \wizvax

Only my cat shares my opinions, and she doesn't care how many
bits are in a byte, just how many bits are in her food dish.

chris@mimsy.UUCP (Chris Torek) (04/21/88)

[various quotations of various people defining int_size_in_bits deleted,
but note that the original idea was to test the result in the preprocessor
proper]

In article <3323@cg-atla.UUCP> duane@cg-atla.UUCP (Andrew Duane X5993) writes:
>Pardon me for coming in late, but hasn't anyone looked through
>the system include files? In /usr/include/sys/param.h, I see
>the defines (on a SUN3):

>#define NBBY    8               /* number of bits in a byte */
>#define NBPW    sizeof(int)     /* number of bytes in an integer */

[so]
>#define int_size_in_bits (NBBY*NBPW)

sizeof() is prohibited in preprocessor arithmetic; hence, with
this definition,

	#if int_size_in_bits <= 32

will not do anything useful.  The macro in <sys/param.h> is not a
violation of anything at all; use of the value produced by NBPW is
merely restricted to the compiler proper.  The Sun kernel has no
occurrences of the form `#if <expression involving NBPW>', and neither
can anyone else write this.  If you are content with doing this
arithemetic outside the preprocessor, there is no problem, but
somewhere back in the reference history, someone wanted to write

	#if int_size_in_bits <= 16
	typedef long foo_t;
	#else
	typedef int foo_t;
	#endif
	/* or something like that */
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris