Efficient coding considered harmful?

T. William Wells bill at twwells.uucp
Sun Nov 6 14:57:13 AEST 1988


Grrrrrrr........

In article <2730 at hound.UUCP> rkl1 at hound.UUCP (K.LAUX) writes:
: | : >... dividing positive numbers by powers of two should be done with a shift.
:
:       I'm suprized that noone has questioned the validity of shifting
: instead of dividing by (powers of) 2.

No one has questioned it because, given the rider that *everyone* has
attached (the thing being divided being positive), it *is* valid.

:       What about the difference between Logical and Arithmetic shifts?

There is none, for positive numbers.

: If you want to divide, then divide!  A lot of compilers are smart enough
: to implement the divide as a shift, but only where appropriate.

And many are not. Worse, most frequently, the compiler can't
determine whether the number really is positive. In fact, only the
most advanced compilers are capable of this.

The exception to this is when the number being divided is declared
unsigned.  So, the options are a cast which may or may not cause the
optimizer to do what one wants, or a shift which will.

:       I *did* notice the condition of dividend being positive,

But you obviously decided to ignore the condition. Or you failed to
consider that it is a critical condition.

:                                                                but now
: you have to *guarantee* that it will always be positive.

So? If I can't determine correctly that such is true, I have much
worse problems than code which doesn't meet your approval.

:       As for the 'compiler being broken' if it doesn't do the same thing
: for a shift and dividing, this is not true.  In one case, you only told
: it to shift, the other to do a divide:  they made be *equivalent*, but
: they certainly are not *equal*.

Go back to school. If they aren't equal, then they aren't equivalent.
If they are equivalent then the must be equal.  Under appropriate
restrictions, a>>b and a/(2**b) are equal, thus equivalent.

:       So, please, if you *functionally* require a divide by (powers of) 2,
: code it as a divide and let the compiler *implement* it (maybe optimization
: needs to be turned on).

So please, get your head straight, and learn to think, before
intruding your ignorance in the debates of your betters.

---
Bill
{uunet|novavax}!proxftl!twwells!bill



More information about the Comp.lang.c mailing list