Loop optimization
bearophile
bearophileHUGS at lycos.com
Mon May 17 03:20:28 PDT 2010
Walter Bright:
> is not done because of roundoff error. Also,
> 0 * x => 0
> is also not done because it is not a correct replacement if x is a NaN.
I have done a little experiment, compiling this D1 code with LDC:
import tango.stdc.stdio: printf;
void main(char[][] args) {
double x = cast(double)args.length;
double y = 0 * x;
printf("%f\n", y);
}
I think the asm generated by ldc shows what you say:
ldc -O3 -release -inline -output-s test
_Dmain:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $32, %esp
movsd .LCPI1_0, %xmm0
movd 8(%ebp), %xmm1
orps %xmm0, %xmm1
subsd %xmm0, %xmm1
pxor %xmm0, %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 4(%esp)
movl $.str, (%esp)
call printf
xorl %eax, %eax
movl %ebp, %esp
popl %ebp
ret $8
So I have added an extra "unsafe floating point" optimization:
ldc -O3 -release -inline -enable-unsafe-fp-math -output-s test
_Dmain:
subl $12, %esp
movl $0, 8(%esp)
movl $0, 4(%esp)
movl $.str, (%esp)
call printf
xorl %eax, %eax
addl $12, %esp
ret $8
GCC has similar switches.
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list