DMD 1.019 and 2.003 releases

0ffh spam at frankhirsch.net
Tue Jul 24 15:01:06 PDT 2007


BCS wrote:
> Reply to 0ffh,
> 
>> If I know that testing a condition is nothing but comparing a number
>> to zero, writing "if (number!=0)" just becomes redundant.
> 
> actually ASM usually has built in >, < and = comparisons, some times 
> these implicitly use 0 for the other side, but it still it is not /just/ 
> a zero test.

Forgive me if I am being thick, but given C (or even D), when does
"if (x!=0)" (or "if (x!=null)") ever give a different result from "if (x)"?
I am naturally speaking integral types and pointers, for floating point
numbers the resulting code will obviously depend on the specific format
used (though compilers will usually make it to act as expected, but I know
I have to /know/, not assume).

As to why "if (x)" is (at least historically) a Good Thing (tm):
If "x" is any kind of calculation, that calculation will usually implicitly
set (or clear) the zero flag of the CPU, doing away with the need for any
kind of comparison at all. On some machines just loading the value of a
memory location into a register will work the zero flag. On many, doing
something like OR or AND of the register against itself will do the trick
a bit faster than actually comparing against any number (including zero).
On the x86, at some point of time, TESTing a memory location was faster
than CMPing it (with a very pinch-of-salty IIRC).
Especially "while" does usually gain some benefit from those cases.
(And of cause a good back end optimisation will still exploit those because
it knows that the "(x!=0)" is the same special case as the "(x)", granted.)

So my defense is reduced to it being a good and useful convention, as I 
cherish every keystroke I don't have to execute, and it's really easy to
learn and get used to... and, above all else, it /looks/ better! ;-)

Regards, Frank



More information about the Digitalmars-d-announce mailing list