Article View: comp.sys.powerpc
Article #3146Re: Debate: PowerMac "trounces" Pentium....???
From: Dan Pop
Date: Tue, 20 Feb 1996 20:41
Date: Tue, 20 Feb 1996 20:41
84 lines
4346 bytes
4346 bytes
In <nevin-1902962104170001@150.135.1.79> nevin@CS.Arizona.EDU (Nevin ":-]" Liber) writes:
>In article <danpop.824475697@rscernix>, danpop@mail.cern.ch (Dan Pop) wrote:
>
>> >Like I said, this stuff isn't that easy to get right and 100% portable.
>
>> It's definitely portable, because buff doesn't have to be statically
>> allocated. It's trivial to compute the maximum size of buff, based on
>> sizeof(int) and allocate the right number of bytes for buff (ever heard
>> of the malloc thingie?).
>
>Of course, you didn't even know this was an issue until I pointed it out.
>I notice that you don't post the code to do that calculation here. Also,
>it is lousy programming to incur a run-time cost for something that is a
>known quantity at compile time.
Even if the expression can be evaluated at compile time, it is _not_ an
integer constant expression (as defined by the ANSI standard), hence it
cannot be used as the size of a statically allocated array. I could
try to approximate log(10)/log(2) by the ratio of two integers, but
this would be only an approximation, not the real thing.
>> It's easy to get it right and 100% portable for any C programmer with half
>> a clue.
>
>Well, I guess you don't have half a clue, since you *didn't* post the code
>that gets this 100% correct and portable. A bunch of handwaving with your
>malloc comment just doesn't cut it. If it's so darn trivial to compute
>the maximum size of buff, why don't you provide a compile-time expression
>that evaluates to it?
Here it is: CHAR_BIT * sizeof(int) * log(2) / log(10) + 3, converted to
an integer type, of course. Replace 2 and 10 by 2.0 and 10.0 if you
don't have an ANSI C compiler.
The best attempt to get a constant expression usable to size an array
would be:
(CHAR_BIT * sizeof(int) * 6931471) / 23025850 + 3
Unfortunatelly, it's considerably more obfuscated than the original version
(three magical constants are a bit too much).
>Another thing you failed to mention (I know, you're
>gonna claim that it is so trivial that it doesn't need mentioning to cover
>up the fact that you didn't even consider it)
Wow, since when are you a mind reader?
>is that the maximum size of
>buff is also dependent on the number of bits per byte.
Don't be idiot. The number of bits per byte has to be used in order to
compute the number of bits of the int type and this computation is
unavoidable when computing the size of the buffer. It's obvious that
the final result of an expression depends on all the factors involved in
that expression. You don't need a PhD in math to realize this.
>Personally, I'm
>not sure C is rich enough to support this, since it requires being able to
>take the log of a number at compile time.
You can't do this in a constant expression. You have to either use the
workaround above (which is overflow-proof, BTW) or use the exact expression
and malloc().
>But, since you claim to know
>better, why don't you post this compile-time expression for us?
Done.
>The hard part about writing portable code isn't the complexity of the
>final result; it's making sure that you've taken all the issues into
>account, because your compiler won't tell you when you haven't.
This is precisely what makes portable programming interesting.
Dan (whose name can be seen in the "Acknowledgements" section of the
comp.lang.c FAQ :-)
--
Dan Pop
CERN, CN Division
Email: danpop@mail.cern.ch
Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
Technical Details
Message-ID:
<danpop.824848868@rscernix>Path:
arc1-ca.pugleaf.net!rcp6.elan.af.mil!rscernix!danpopReferences:
<nevin-0602962253080001@150.135.1.79> <4fdi45$gu7@eken.hv.se> <4fea7g$eio@nyx.cs.du.edu> <9602092127.AA24456@ues5.cern.ch> <nevin-1202960529130001@150.135.1.79> <9602121654.AA02634@ues5.cern.ch> <4fqdta$rhh@meaddata.lexis-nexis.com> <danpop.824303108@rsce <nevin-1902962104170001@150.135.1.79>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!rcp6.elan.af.mil!rscernix!danpop From: danpop@mail.cern.ch (Dan Pop) Newsgroups: comp.sys.powerpc,comp.os.linux.advocacy,comp.sys.mac.advocacy,comp.os.os2.advocacy,comp.sys.amiga.advocacy,comp.sys.intel Subject: Re: Debate: PowerMac "trounces" Pentium....??? Date: 20 Feb 96 20:41:08 GMT Organization: CERN European Lab for Particle Physics Message-ID: <danpop.824848868@rscernix> References: <nevin-0602962253080001@150.135.1.79> <4fdi45$gu7@eken.hv.se> <4fea7g$eio@nyx.cs.du.edu> <9602092127.AA24456@ues5.cern.ch> <nevin-1202960529130001@150.135.1.79> <9602121654.AA02634@ues5.cern.ch> <4fqdta$rhh@meaddata.lexis-nexis.com> <danpop.824303108@rsce <nevin-1902962104170001@150.135.1.79> NNTP-Posting-Host: ues5.cern.ch X-Newsreader: NN version 6.5.0 #7 (NOV)