简体   繁体   English

乘以0优化

[英]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? 如果值可能是inf或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
        .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

Depends on whether the compiler implements IEEE754. 取决于编译器是否实现IEEE754。 Neither C nor C++ requires that a compiler supports NaN , but IEEE754 does. C和C ++都不要求编译器支持NaN ,但IEEE754也支持NaN

不仅infNaN阻止了那里的优化,它也是符号 - 0.0 *负的东西是-0.0 ,否则它是0.0 ,所以你实际上必须计算r[0]*r[1]的符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM