| |
 | Posted by Cecil Ward in reply to WhatMeWorry | Permalink Reply |
|
Cecil Ward 
Posted in reply to WhatMeWorry
| On Wednesday, 26 April 2023 at 23:07:39 UTC, WhatMeWorry wrote:
> On Wednesday, 26 April 2023 at 23:02:07 UTC, Richard (Rikki) Andrew Cattermole wrote:
>> Don't forget ``num % 2 == 0``.
>>
>> None should matter, pretty much all production compilers within the last 30 years should recognize all forms of this and do the right thing.
>
> Thanks. Fastest reply ever! And I believe across the world? I suppose my examples required overhead of a function call. So maybe num % 2 == 0 is fastest?
I made a small change, making the retval a bool rather than an int. I got slightly better code generation with the int, as it seems that some of the compilers have not yet got all the good tricks they should be using when manipulating bool-typed expressions and also it can be one extra instruction converting values to bool strictly zero or one, not zero or any non-zero value. Here’s the D, enlarged a little so that we can see your routine in action, inlined. Your isEven boils down to two instructions with a seriously optimising compiler. I’ve included the x86-64 machine code generated by the GDC and LDC compilers so you can see how fast it is. GDC made a bit of a dog’s breakfast of my longer routine whereas LDC performed superbly. GDC generated twice as much code, but its excellent instruction scheduler and what looks like an awareness of ILP mean that the two streams of instructions will be carried out in parallel so the two streams will only take three instruction times - ie whatever the total time is for those three instruction in the one stream - not six.
bool isEven( int num )
{
return ! ( num & 1 );
}
bool AreBothEven( int a, int b ) // returns true if both arguments are even
{
return isEven( a ) && isEven( b );
}
===
Compiler output:: GDC:: x86-64: -O3 -mcpu=native -frelease
bool isEven( int ):
mov eax, edi
not eax
and eax, 1
ret
bool AreBothEven( int, int ):
mov eax, edi
not esi
not eax
and esi, 1
and eax, 1
cmovne eax, esi
ret
===
Compiler LDC: x86-64: -O3 -mcpu=native -release
bool isEven( int ):
test dil, 1
sete al
ret
bool AreBothEven( int, int ):
or edi, esi
test dil, 1
sete al
ret
|