开发者

Multiply by 0 optimization

Suppose i have:

double f(const double *r) {
    return 0*(r[0]*r[1]);
}

should compiler be able to optimize out the segment, or does it still have to perform operation, in case the values might be inf or nan?

gcc -O3 -S test.c:

        .file   "test.c"
        .text
        .p2align 4,,15
.globl f
        .type   f, @function
f:
.LFB0:
        .cfi_startproc
        movsd   (%rdi), %xmm0
        mulsd   8(%rdi), %xmm0
        mulsd   .LC0(%rip), %xmm0
        ret
        .cfi_endproc
.LFE0:
        .size   f, .-f
        开发者_Python百科.section        .rodata.cst8,"aM",@progbits,8
        .align 8
.LC0:
        .long   0
        .long   0
        .ident  "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
        .section        .note.GNU-stack,"",@progbits

seems no elimination?

aha:

gcc -O3  -ffast-math  -S test.c

        .file   "test.c"
        .text
        .p2align 4,,15
.globl f
        .type   f, @function
f:
.LFB0:
        .cfi_startproc
        xorpd   %xmm0, %xmm0
        ret
        .cfi_endproc
.LFE0:
        .size   f, .-f
        .ident  "GCC: (Ubuntu 4.4.3-4ubuntu5) 4.4.3"
        .section        .note.GNU-stack,"",@progbits


It isn't only inf and NaN that prevent the optimization there, it's also the sign - 0.0 * something negative is -0.0, otherwise it's 0.0, so you actually have to compute the sign of r[0]*r[1].


Depends on whether the compiler implements IEEE754. Neither C nor C++ requires that a compiler supports NaN, but IEEE754 does.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜