[asterisk-dev] Re: file: trunk r41694 - /trunk/main/file.c

Paul Cadach paul at odt.east.telecom.kz
Fri Sep 1 21:38:10 MST 2006


Hello,

Tony Mountifield wrote:
> > - if (!cmdfd) {
> > + if (cmdfd > -1) {
>
> Just a trivial stylistic issue, but it always looks odd to me doing
> "if (x > -1)" instead of "if (x >= 0)". And I think most instruction
> sets allow a slightly more efficient test against zero than against
> a specific non-zero value.

Any compiler can optimize this comparsion well. (cmdfd > -1) is better to read and understand rather than (cmdfd >= 0)
because invalid/missing cmdfd specified as -1 in the code.

Assembler code generated by gcc WITHOUT -O optimization for (cmdfd > -1):
        cmpl    $-1, 8(%ebp)
        jle     .L1
Assembler code generated by gcc with -O optimization for (cmdfd > -1):
        cmpl    $0, 8(%ebp)
        js      .L1

Assembler code generated by gcc with -O AND WITHOUT optimization for (cmdfd >= 0):
        cmpl    $0, 8(%ebp)
        js      .L1

As you can see, it is the same when compiler optimizations enabled.


So, write your code as much readable as possible, small and intuitive optimizations will be done for you by compiler.


WBR,
Paul.




More information about the asterisk-dev mailing list