bright@nazgul.UUCP (Walter Bright) (01/19/91)
Many times I've seen code that says a type needs to be 32 bits wide when what is really meant is >=32 bits is needed. This is typical when one sees a typedef like: typedef long int32; This typedef implies that exactly 32 bits is required, rather than >=32. Note that on a PDP-10, longs are 36 bits! A more portable approach would be to have a typedef indicating what type you need, like: typedef long fileoffset_type; /* need at least 32 bits */ If you have a need for *exactly* 32 bits, you can do things like: x &= 0xFFFFFFFF; /* truncate result to 32 bits */ Any decent compiler will not generate any code for that if x is 32 bits wide (and isn't volatile!). Alternatively, you can do things like: if (sizeof(x) == 4) /* code here depends on 32 bits in x */ ... else /* Portability alert! */ assert(0); /* rewrite this algorithm! */ P.S. I prefer stuff like: #if sizeof(long) == 4 ... #else #error This algorithm needs to be ported #endif but ANSI C seems to have torpedoed that.
gwyn@smoke.brl.mil (Doug Gwyn) (01/22/91)
In article <231@nazgul.UUCP> bright@nazgul.UUCP (Walter Bright) writes: >P.S. I prefer stuff like: > #if sizeof(long) == 4 > ... > #else > #error This algorithm needs to be ported > #endif >but ANSI C seems to have torpedoed that. I don't know why you say "torpedoed" -- that usage was never proper C, was not supported by the UNIX C implementations, and would require significantly more complex preprocessing, assuming that one could even specify precisely how to fit it into the phases of translation.
diamond@jit345.swstokyo.dec.com (Norman Diamond) (01/22/91)
In article <231@nazgul.UUCP> bright@nazgul.UUCP (Walter Bright) writes: ------------- !!!!!!!!!!!!! ????????????? >If you have a need for *exactly* 32 bits, you can do things like: > x &= 0xFFFFFFFF; /* truncate result to 32 bits */ >... Alternatively, you can do things like: > if (sizeof(x) == 4) > /* code here depends on 32 bits in x */ On a machine with 36-bit words and C support hacked with 9-bit chars, this test will give an undesired result. On a machine with 16-bit bytes, this test will give an undesired result either way, if x really has 32 bits or if it doesn't. It is hard to believe that Mr. Bright would make this kind of mistake. -- Norman Diamond diamond@tkov50.enet.dec.com If this were the company's opinion, I wouldn't be allowed to post it.
pt@geovision.uucp (Paul Tomblin) (01/24/91)
bright@nazgul.UUCP (Walter Bright) writes: > if (sizeof(x) == 4) > /* code here depends on 32 bits in x */ > ... > else > /* Portability alert! */ > assert(0); /* rewrite this algorithm! */ What could possibly be the advantage of that code over the following?: /* If this assertion fails, port to another machine or rewrite! */ assert(sizeof(x) == 4); -- Paul Tomblin, Department of Redundancy Department. ! My employer does The Romanian Orphans Support Group needs your help, ! not stand by my Ask me for details. ! opinions.... pt@geovision.gvc.com or {cognos,uunet}!geovision!pt ! Me neither.
mcdonald@aries.scs.uiuc.edu (Doug McDonald) (01/24/91)
In article <1348@geovision.UUCP> pt@geovision.uucp (Paul Tomblin) writes: >bright@nazgul.UUCP (Walter Bright) writes: >> if (sizeof(x) == 4) >> /* code here depends on 32 bits in x */ Let us assume that it depends on having EXACTLY 32 bits in x. >> ... >> else >> /* Portability alert! */ >> assert(0); /* rewrite this algorithm! */ > >What could possibly be the advantage of that code over the following?: > > /* If this assertion fails, port to another machine or rewrite! */ > assert(sizeof(x) == 4); > Because the original will alert the reader when sizeof(x) is 4 but x has 64 bits. OR where sizeof(x) is 4 but x has 36 bits. Doug McDonald
pfalstad@phoenix.Princeton.EDU (Paul Falstad) (01/25/91)
mcdonald@aries.scs.uiuc.edu (Doug McDonald) wrote: > >In article <1348@geovision.UUCP> pt@geovision.uucp (Paul Tomblin) writes: >>bright@nazgul.UUCP (Walter Bright) writes: >>> if (sizeof(x) == 4) >>> /* code here depends on 32 bits in x */ >>> ... >>> else >>> /* Portability alert! */ >>> assert(0); /* rewrite this algorithm! */ >>What could possibly be the advantage of that code over the following?: >> /* If this assertion fails, port to another machine or rewrite! */ >> assert(sizeof(x) == 4); >Because the original will alert the reader when sizeof(x) is 4 but >x has 64 bits. OR where sizeof(x) is 4 but x has 36 bits. What? I don't get it. The original code: if (sizeof(x) == 4) ... else assert(0); is the same as if (!(sizeof(x) == 4)) assert(0); ... which is the same as assert(sizeof(x) == 4); -- Paul Falstad, pfalstad@phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD "And she's always on about men following her. I don't know what she thinks they're going to do to her. Vomit on her, Basil, says."-Flowery Twats
wirzenius@cc.helsinki.fi (Lars Wirzenius) (01/25/91)
In article <1991Jan22.022206.24691@tkou02.enet.dec.com>, diamond@jit345.swstokyo.dec.com (Norman Diamond) writes: > In article <231@nazgul.UUCP> bright@nazgul.UUCP (Walter Bright) writes: >> >>If you have a need for *exactly* 32 bits, you can do things like: >> if (sizeof(x) == 4) >> /* code here depends on 32 bits in x */ > > On a machine with 36-bit words and C support hacked with 9-bit chars, > this test will give an undesired result. Wouldn't the following work (assuming CHAR_BIT is defined properly), if the aim is to verify that x has a size of 32 bits: if (sizeof(x) * CHAR_BIT == 32) By the way, is there any guarantee that all bits are used to represent a value? That is, does a 16-bit unsiged always have a value range of 0..65535? Lars Wirzenius wirzenius@cc.helsinki.fi