njk@freja.diku.dk (Niels J|rgen Kruse) (04/28/89)
cik@l.cc.purdue.edu (Herman Rubin) writes: >Now suppose I am doing some serious array operations, and I have to know ????????????? >whether one array buffer is longer than another. The elements are of type ???????????? Sounds like you are doing fortran style memory management with one big monster array parceled out. You don't *have* to do that in C, you know. >long. Do I have to do this multiplying and dividing by 4 all the time? No. If you have pointers long *p1,*p2,*p3,*p4; with p1 <= p2 and p3 <= p4 and want to test (p2-p1) < (p4-p3), you can write if ((char *)p2 - (char *)p1 < (char *)p4 - (char *)p3) which won't generate any shifting. If the architecture in question allows longs that are not aligned on a natural byte boundary, this is not equivalent to the test without the casts, so the compiler can not be blamed for not doing this optimization itself. Another case for RISC :-) This hand optimization doesn't hurt you on word addressed machines, as they generaly require longs aligned on a word boundary. Hence if ((char *)p2 - (char *)p1 < (char *)p4 - (char *)p3) is equivalent to if ((int *)p2 - (int *)p1 < (int *)p4 - (int *)p3) and that optimization can be expected from the compiler. >Another example of "user-friendly" which turns out to be "user-inimical." >-- >Herman Rubin, Dept. of Statistics, Purdue Univ., West Lafayette IN47907 >Phone: (317)494-6054 >hrubin@l.cc.purdue.edu (Internet, bitnet, UUCP) -- Niels J|rgen Kruse Email njk@diku.dk Mail Tustrupvej 7, 2 tv, 2720 Vanlose, Denmark
gwyn@smoke.BRL.MIL (Doug Gwyn) (04/29/89)
In article <4625@freja.diku.dk> njk@freja.diku.dk (Niels J|rgen Kruse) writes:
-No. If you have pointers
-long *p1,*p2,*p3,*p4;
-with p1 <= p2 and p3 <= p4 and want to test (p2-p1) < (p4-p3),
-you can write
-if ((char *)p2 - (char *)p1 < (char *)p4 - (char *)p3)
-which won't generate any shifting.
That might generate shifting, depending on the implementation.
What you should write is simply
if ( p2 - p1 < p4 - p3 )
gwyn@smoke.BRL.MIL (Doug Gwyn) (04/29/89)
In article <10160@smoke.BRL.MIL> gwyn@brl.arpa (Doug Gwyn) writes: >What you should write is simply > if ( p2 - p1 < p4 - p3 ) I forgot to say why. Although this might result in generated code that does unnecessary work, on the other hand it might not. Concern for such low-level "microefficiency" is nearly always misplaced. If this particular test occurred in bottleneck code, then it might be worthwhile to tweak it, but in the vast majority of cases the visually simpler code is preferable.
maart@cs.vu.nl (Maarten Litmaath) (05/19/89)
mesmo@Portia.Stanford.EDU (Chris Johnson) writes:
\... The statement that *(a+i) == *(i+a) is [...] invalid.
Guess again. Better: read K&R.
--
"`Goto considered harmful' considered |Maarten Litmaath @ VU Amsterdam:
harmful" considered harmful! |maart@cs.vu.nl, mcvax!botter!maart