[comp.lang.c] bitmask lookup table creation

kaleb@mars.jpl.nasa.gov (Kaleb Keithley) (06/06/90)

I want to create the following bitmask lookup table:
    unsigned short masks[] = {
        0x0000, 0x0001, 0x0003, 0x0007,
        0x000F, 0x001F, 0x003F, 0x007F,
        0x00FF, 0x01FF, 0x03FF, 0x07FF,
        0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF 
    };

in a machine independent way.

In /usr/include/values.h, the following are #defined:
#define BITS(type)  (BITSPERBYTE * (int)sizeof(type)
#define HIBITS  ((short)(1 << BITS(short) - 1))
#define MAXSHORT    ((short)~HIBITS)

which are 0x8000 and 0x7FFF respectively on my machine, which happens to
be a Sun Sparc.

It is nearly trivial to do it at run-time, and the above definitely works
on my Sun.  Does anyone have any neat trick to generate this table at 
compile time in such a way as to be completely machine independent?

adva-thanks-nce
kaleb@thyme.jpl.nasa.gov            Jet Propeller Labs
Kaleb Keithley

"So that's what an invisible barrier looks like"

kaleb@mars.jpl.nasa.gov (Kaleb Keithley) (06/06/90)

Don't like to follow up my own article, but:

In article I wrote:
>I want to create the following bitmask lookup table:
>    unsigned short masks[] = {
>        0x0000, 0x0001, 0x0003, 0x0007,
>        0x000F, 0x001F, 0x003F, 0x007F,
>        0x00FF, 0x01FF, 0x03FF, 0x07FF,
>        0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF 
>    };
>
>in a machine independent way.
>
>In /usr/include/values.h, the following are #defined:
>#define BITS(type)  (BITSPERBYTE * (int)sizeof(type)
>#define HIBITS  ((short)(1 << BITS(short) - 1))
>#define MAXSHORT    ((short)~HIBITS)
>
>which are 0x8000 and 0x7FFF respectively on my machine, which happens to
>be a Sun Sparc.
>
>It is nearly trivial to do it at run-time, and the above definitely works
>on my Sun.  Does anyone have any neat trick to generate this table at 
>compile time in such a way as to be completely machine independent?

Well, so far I got two answers, one said the above was correct, the other
told me to look at X Window Server code.

I don't want to belittle the person who told me the above was correct, but...
I looked at the MIT server code, and found that it makes two distinctions: 
If you have a VAX or a 386 then BITMAP_BIT_ORDER is defined as LSBFirst, 
otherwise it's MSBFirst.  Then there are two versions of the table, much 
like the one I have above, one for MSBFirst, one for LSBFirst.  I could do 
this, and in fact, probably will, but, the question still remains; Is there 
a way, using the three defined macros, to generate the correct table at 
compile time?


kaleb@thyme.jpl.nasa.gov            Jet Propeller Labs
Kaleb Keithley

"So that's what an invisible barrier looks like"

andre@targon.UUCP (andre) (06/13/90)

 In article <3948@jato.Jpl.Nasa.Gov> kaleb@mars.UUCP (Kaleb Keithley) writes:
 }Don't like to follow up my own article, but:

 }In article I wrote:
 }>I want to create the following bitmask lookup table:
 }>    unsigned short masks[] = {
 }>        0x0000, 0x0001, 0x0003, 0x0007,
 }>        0x000F, 0x001F, 0x003F, 0x007F,
 }>        0x00FF, 0x01FF, 0x03FF, 0x07FF,
 }>        0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF
 }>    };
 }>
 }>in a machine independent way.
 }>
 }>In /usr/include/values.h, the following are #defined:
 }>#define BITS(type)  (BITSPERBYTE * (int)sizeof(type)
 }>#define HIBITS  ((short)(1 << BITS(short) - 1))
 }>#define MAXSHORT    ((short)~HIBITS)

 }>which are 0x8000 and 0x7FFF respectively on my machine, which happens to
 }>be a Sun Sparc.

 }>It is nearly trivial to do it at run-time, and the above definitely works
		^^^^^^^             ^^^^^^^^
 }>on my Sun.  Does anyone have any neat trick to generate this table at
 }>compile time in such a way as to be completely machine independent?

If you re-define run-time to be make-time, you can do this easily.
Add to your project a small program that will generate the c-code
of your correct table and use that to compile the 'big' program.

if you decide to put the table in object table.o,

executable: $(OBJS) table.o
	$(CC) -o $@ $(OBJS) table.o

table.c: make_table
	make_table > $@

	hope this helps,

-- 
The mail|    AAA         DDDD  It's not the kill, but the thrill of the chase.
demon...|   AA AAvv   vvDD  DD        Ketchup is a vegetable.
hits!.@&|  AAAAAAAvv vvDD  DD                    {nixbur|nixtor}!adalen.via
--more--| AAA   AAAvvvDDDDDD    Andre van Dalen, uunet!hp4nl!targon!andre