Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!rbutterworth From: rbutterworth@watmath.UUCP Newsgroups: comp.lang.c Subject: Re: standardizing integral type sizes Message-ID: <6965@watmath.UUCP> Date: Wed, 15-Apr-87 08:56:23 EST Article-I.D.: watmath.6965 Posted: Wed Apr 15 08:56:23 1987 Date-Received: Thu, 16-Apr-87 00:48:51 EST References: <101@umich.UUCP> <791@xanth.UUCP> <1987Apr9.155110.28398@sq.uucp> <3162@jade.BERKELEY.EDU> Organization: U of Waterloo, Ontario Lines: 42 In article <3162@jade.BERKELEY.EDU>, mwm@eris.BERKELEY.EDU (Mike (My watch has windows) Meyer) writes: > Something that did what you said. For instance, consider a > hypothetical Queer Machine for C (QM/C), which has 18 bit words (word > addressed), and instructions for dealing with double words. The > obvious implementation has int = short = 18 bits, and long = 36 bits. > Now, supposed I need 16 bits of magnitude on a signed value. For this > machine, declaring things as int works just fine. But the program will > not work on "standard" machines, because it really wants longs for > that value. But if I declare things as "long," I chew up twice the > space for storage, and presumably more time. I've never actually done this myself, but if you are really worried about specifying minimum integer size and having the source still portable, you could set up a header file for each different architecture/compiler something like this: typedef int int1; typedef int int2; ... typedef int int18; typedef long int19; typedef long int20; ... typedef long int36; Then when you have a variable that needs at least 12 bits, you can declare it as "int12 var;". The typedef in the header file will give you the most efficient type for the variable. "int5" would be typedefed to "int" on most machines, to "short" on those machines whose short instructions are as fast and as small as for ints, and as "signed char" on those machines that have fast char arithmetic. In those applications where you ask for "int35", it would not compile on machines that can't handle 35 bit integers. This is of course exactly what you want. Note that "extern char x,y,z; x=y+z;" can generate several times the amount of code as "extern int x,y,z; x=y+z;" on some machines. Thus if you want 5 bit integers, "int5" would give you "int" on those machines. Note that a parallel set of typedefs (e.g. pack8) would be needed when the concern is for saving space (e.g. large arrays) and not code efficiency.