[comp.lang.c] sizes, bitfields, etc

chris@mimsy.UUCP (Chris Torek) (08/10/88)

>In article <1199@ficc.UUCP> peter@ficc.UUCP (Peter da Silva) writes:
>>Not trying to start a war or anything, but how far do you really have
>>to stretch the language to allow:
>> 	int	a:16;

In article <11794@steinmetz.ge.com> davidsen@steinmetz.ge.com
(William E. Davidsen Jr) answers:
>I proposed something like this to X3J11 [but size in bytes] ...
>The feeling was that it was (a) not really needed and (b) too much like
>fortran. I like your idea better, but the few cases where you want exact
>size rather than minimum size probably don't justify inclusion.

>It would be nice some times to be able to specify a bit array ....

>I would really like to see a "packed struct," also. This would be a
>struct packed on byte boundaries without fill, no matter *how bad* the
>code was to use them.

I am not sure that ANY of these belong in C (nor that that any or all
do not), but in some language(s) packed structures, bit arrays, and
exact-bit-sized variables would be useful.  If, however, you are going
to provide structure packing, I suggest NOT restricting it to byte
boundaries.  If you need tight packing, the expense for getting at
bit-fields that cross word boundaries is probably worth it.  If you
then can accept looser packing, all you need do is write (e.g.):

	... {
		bits	a:(ROUNDUP(3,BITS_PER_BYTE));
		bits	c:(ROUNDUP(11,BITS_PER_BYTE));
		...

where ROUNDUP is a compile-time expression that rounds its first
argument up to the nearest multiple of its second argument:

	/* nb. beware overflow in (x)+(y)-1 below */
	#define ROUNDUP(x, y)  (floor(((x) + (y) - 1) / (y)) * (y))
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

leo@philmds.UUCP (Leo de Wit) (08/11/88)

In article <12933@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes:
   [stuff deleted]...
|	... {
|		bits	a:(ROUNDUP(3,BITS_PER_BYTE));
|		bits	c:(ROUNDUP(11,BITS_PER_BYTE));
|		...
|
|where ROUNDUP is a compile-time expression that rounds its first
|argument up to the nearest multiple of its second argument:
|
|	/* nb. beware overflow in (x)+(y)-1 below */
|	#define ROUNDUP(x, y)  (floor(((x) + (y) - 1) / (y)) * (y))

Compile-time expression? This would imply that the compiler knows of floor()
- which I doubt, because math.h declares floor as
extern double floor();
furthermore the argument of floor should be double, and it looks like an
integer expression.
Any comments, Chris?

                        Leo.

chris@mimsy.UUCP (Chris Torek) (08/13/88)

>In article <12933@mimsy.UUCP> I wrote
>>where ROUNDUP is a compile-time expression ...
>>	#define ROUNDUP(x, y)  (floor(((x) + (y) - 1) / (y)) * (y))

In article <584@philmds.UUCP> leo@philmds.UUCP (Leo de Wit) writes:
>Compile-time expression? This would imply that the compiler knows of floor()

Oops.  I originally wrote

	ROUNDUP a b: floor( ... )

or something equally un-C-like (since this was intended not to be a
C-centric argument), and I put in the floor() to indicate that the
division should not be doing in rational or fixed- or floating-point
arithmetic.  I then decided that this would be overly confusing and
switched to the C syntax, without removing the floor() or adding any
explanation.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris