dmd 2.029 release
bearophile
bearophileHUGS at lycos.com
Wed Apr 22 15:27:54 PDT 2009
Don:
> Actually it's not so difficult. I've created a patch for bug 2807 --
> it's only 5 lines long! It gives an error message if a nested pure
> function accesses a mutable variable from an outer scope.
Thank you very much Don, your work helps a lot.
Every time I try a tiny program I find something I don't understand:
import std.stdio: writeln;
import std.math: sqrt;
import std.conv: to;
void main(string[] args) {
double x = args.length == 2 ? to!(double)(args[1]) : 4.0;
writeln(sqrt(x) + sqrt(x));
}
I have also tried with std.math.sin with similar results:
L0: enter 8,0
mov EAX,8[EBP]
cmp EAX,2
jne L30
cmp EAX,1
ja L1B
mov EAX,6
call near ptr _D6test7__arrayZ
L1B: mov EDX,0Ch[EBP]
mov EAX,8[EBP]
mov EAX,8[EDX]
mov EDX,0Ch[EDX]
push EDX
push EAX
call near ptr _D3std4conv13__T2toTdTAyaZ2toFAyaZd
jmp short L36
L30: fld qword ptr FLAT:_DATA[00h]
L36: fstp qword ptr -8[EBP]
fld qword ptr -8[EBP]
fsin
fld qword ptr -8[EBP]
fsin
faddp ST(1),ST
sub ESP,0Ch
fstp tbyte ptr [ESP]
call near ptr _D3std5stdio14__T7writelnTeZ7writelnFeZv
xor EAX,EAX
leave
ret
Isn't sin(x)+sin(x) pure? Even if the compiler doesn't want to replace x+x with x*2 because x is a floating point, it can do:
y = sin(x)
y+y
And that gives the same result even with FPs.
Note: with SSE2 it's even possible to perform two sqrt(double) at the same time, so a compiler can implement sin(x)+sin(y) with a single instruction (SQRTSD) (plus eventually some register loading/unloading).
Bye,
bearophile
More information about the Digitalmars-d-announce
mailing list