On Sunday, 25 December 2011 at 04:44:57 UTC, Andrei Alexandrescu wrote:
I see every letter beyond this as a liability:

Yeah, though like Jacob said, the implementation doesn't
matter as much as the idea of giving it a name.

But, if you're curious about why I wrote it long form, it's
just a quirk I sometimes do; if I read something out loud
differently than how I wrote it, I'll sometimes rewrite it.
Here, I dictated it to myself as "if it's divisible by two,
return false (not odd), otherwise, return true (is odd)", and
decided to write it almost exactly like that.


Usually, these little things are meaningless once compiled,
but check this out:

===
bool test(int a) {
        if(a%2 == 0)
                return false;
        else
                return true;
}

bool test2(int a) {
        return a&1;
}
===

$ dmd -O -c test.d # 2.057 btw
$ objdump -d test.o
Disassembly of section .text._D5test24testFiZb:

00000000 <_D5test24testFiZb>:
  0:   55                      push   %ebp
  1:   8b ec                   mov    %esp,%ebp
  3:   50                      push   %eax
  4:   99                      cltd
  5:   33 c2                   xor    %edx,%eax
  7:   25 01 00 00 00          and    $0x1,%eax
  c:   03 d0                   add    %eax,%edx
  e:   83 fa 01                cmp    $0x1,%edx
 11:   19 c0                   sbb    %eax,%eax
 13:   8b e5                   mov    %ebp,%esp
 15:   40                      inc    %eax
 16:   5d                      pop    %ebp
 17:   c3                      ret

Disassembly of section .text._D5test25test2FiZb:

00000000 <_D5test25test2FiZb>:
  0:   55                      push   %ebp
  1:   8b ec                   mov    %esp,%ebp
  3:   50                      push   %eax
  4:   25 01 00 00 00          and    $0x1,%eax
  9:   f7 d8                   neg    %eax
  b:   19 c0                   sbb    %eax,%eax
  d:   8b e5                   mov    %ebp,%esp
  f:   f7 d8                   neg    %eax
 11:   5d                      pop    %ebp
 12:   c3                      ret


Almost the same, but not quite.... I think the two
functions should have compiled identically. With gcc
(compiling the code as C), it comes out:

00000000 <test>:
  0:   55                      push   %ebp
  1:   89 e5                   mov    %esp,%ebp
  3:   8b 45 08                mov    0x8(%ebp),%eax
  6:   83 e0 01                and    $0x1,%eax
  9:   5d                      pop    %ebp
  a:   c3                      ret

for both functions.

(btw "Akismet thinks your post looks like spam. " LOL)

Reply via email to