fast-math
まぁ、実は関数呼び出しが omit されているのは gcc 4.0.1 on Mac OS X 10.4 でしか確認できていなかったりして、そのあたりの原因が何処にあるのかは良く分からないけど、手元で再現できないなら疑われるのは仕方がないといえば仕方が無い(アセンブリリスト全部載せておくという方法もあるけど)。単純に gcc のバージョンの差か確認しようと思ったら、Ubuntu 7.10 に gcc 4.0 のパッケージがなくて面倒っぽいのでやらない。
<追記>Darwin では -fno-math-errno がデフォルトで ON、=> 数学関数は副作用無し => 戻り値を使っていない pow は呼び出す必要なしということっぽい(コメント参照。 Thanks shinichiro_h さん)<追記>
で、このへんみて知ったのだけど、-ffast-math オプションなんてあるんだなぁ。
どれどれ。
#include <math.h> int main() { int i; double x; for (i = 0; i < 100; i++) { x = pow(3, i); } return 0; }
以下、gcc 3.4.4 on Cygwinで。
gcc -O2 でコンパイル
.file "side_effect_free.c" .def ___main; .scl 2; .type 32; .endef .text .p2align 4,,15 .globl _main .def _main; .scl 2; .type 32; .endef _main: pushl %ebp movl $16, %eax movl %esp, %ebp pushl %ebx subl $20, %esp andl $-16, %esp call __alloca call ___main xorl %ebx, %ebx .p2align 4,,15 L5: pushl %ebx movl $1074266112, %eax incl %ebx fildl (%esp) addl $4, %esp movl $0, (%esp) movl %eax, 4(%esp) fstpl 8(%esp) call _pow fstp %st(0) cmpl $99, %ebx jle L5 movl -4(%ebp), %ebx xorl %eax, %eax leave ret .def _pow; .scl 3; .type 32; .endef
.file "side_effect_free.c" .def ___main; .scl 2; .type 32; .endef .text .p2align 4,,15 .globl _main .def _main; .scl 2; .type 32; .endef _main: pushl %ebp movl $16, %eax movl %esp, %ebp subl $24, %esp andl $-16, %esp call __alloca call ___main xorl %eax, %eax .p2align 4,,15 L5: incl %eax cmpl $99, %eax jle L5 leave xorl %eax, %eax ret
まぁ、そゆこと。
で、そもそもこのオプションはなんなんだろうと思ったのだけど。
% gcc -v --help 2>&1 2> /dev/null | grep fast-math -ffast-math This switch lacks documentation
おぃ。じゃ、info の方はというとこちらはちゃんとあった。
`-ffast-math' Sets `-fno-math-errno', `-funsafe-math-optimizations', `-fno-trapping-math', `-ffinite-math-only', `-fno-rounding-math', `-fno-signaling-nans' and `fcx-limited-range'. This option causes the preprocessor macro `__FAST_MATH__' to be defined. This option should never be turned on by any `-O' option since it can result in incorrect output for programs which depend on an exact implementation of IEEE or ISO rules/specifications for math functions.
うむむ。とりあえず、Mac ではこいつがデフォルトで ON という話ではなさそう。やっぱバージョンか。