Nesting in pure functions
bearophile
bearophileHUGS at lycos.com
Mon Apr 6 02:48:53 PDT 2009
bearophile:
> I don't know why sqr is written like this:
Sorry, I meant:
>I don't know why sqr isn't written like this:<
--------------------
This is the same compiled with -inline too, as you can see double_sqr() isn't being inlined, despite containing just an imul and an add:
sqr:
mov EAX,4[ESP]
imul EAX,EAX
ret 4
double_sqr:
imul EAX,EAX
add EAX,EAX
ret
main:
L0: push EAX
cmp dword ptr 8[ESP],2
jne L1D
mov EDX,0Ch[ESP]
mov EAX,8[ESP]
push dword ptr 0Ch[EDX]
push dword ptr 8[EDX]
call near ptr toInt
jmp short L22
L1D: mov EAX,0Ah
L22: call near ptr double_sqr
add EAX,EAX
mov ECX,offset FLAT:_DATA
push EAX
push ECX
call near ptr printf
-------------
Regarding the inlining in D, it's supposed to be automatic, but often the programmer knows better.
For a recent example of mine take a look at the "mastermind_breaking.d" program inside this zip:
http://www.fantascienza.net/leonardo/js/mastermind_break.zip
I have had to put the function code inside a string mixin to force a manual inlining and improve the performance of the program.
Bye,
bearophile
More information about the Digitalmars-d
mailing list