GCC: 优化: xmm寄存器的使用,sprintf
文章目錄
- 參考
- 簡介
- 源碼
- 生成的匯編 -O2
- 不帶-O2
參考
gcc manual
http://gcc.gnu.org/pub/gcc/summit/2003/GENERIC%20and%20GIMPLE.pdf
簡介
在做sprintf的優(yōu)化時,有一個判斷標(biāo)準(zhǔn),具體的判斷標(biāo)準(zhǔn)是什么?需要看GCC的代碼。
ISO C90 函數(shù): abort, abs, acos, asin, atan2, atan, calloc, ceil, cosh, cos, exit, exp, fabs, floor, fmod, fprintf, fputs, frexp, fscanf, isalnum, isalpha, iscntrl, isdigit, isgraph, islower, isprint, ispunct, isspace, isupper, isxdigit, tolower, toupper, labs, ldexp, log10, log, malloc, memchr, memcmp, memcpy, memset, modf, pow, printf, putchar, puts, scanf, sinh, sin, snprintf, sprintf, sqrt, sscanf, strcat, strchr, strcmp, strcpy, strcspn, strlen, strncat, strncmp, strncpy, strpbrk,
strrchr, strspn, strstr, tanh, tan, vfprintf, vprintf and vsprintf,統(tǒng)統(tǒng)都會被認(rèn)為是內(nèi)建的函數(shù),即使沒有built-in前綴,除非指明編譯選項‘-fno-builtin’ is specified (or ‘-fno-builtin-function’ is specified for an individual function). 對應(yīng)的內(nèi)建函數(shù)有前綴“_builtin”。這樣gcc在實現(xiàn)的內(nèi)部就會根據(jù)一些標(biāo)準(zhǔn)做優(yōu)化。
源碼
[root@10 test]# cat discr.c #include <stdio.h> #include <stdlib.h> void fun1(int c, int b) {char abc[300];sprintf(abc,"abcde000000,123123123123123123123"); 、、、其實這里就當(dāng)前的語境來說,沒有必要做sprintf操作。system(abc);//printf("func1\n");//char *a;//*a=3; } void fun() {system("abcde");fun1(1,2); } int main() {fun(); }生成的匯編 -O2
[root@10 test]# gcc -O2 discr.c;使用了sse的寄存器xmm0.
(gdb) disass fun1 Dump of assembler code for function fun1:0x00000000004005d0 <+0>: sub $0x138,%rsp0x00000000004005d7 <+7>: movdqa 0x101(%rip),%xmm0 # 0x4006e00x00000000004005df <+15>: mov $0x33,%eax0x00000000004005e4 <+20>: mov %rsp,%rdi0x00000000004005e7 <+23>: mov %ax,0x20(%rsp)0x00000000004005ec <+28>: movaps %xmm0,(%rsp)0x00000000004005f0 <+32>: movdqa 0xf8(%rip),%xmm0 # 0x4006f00x00000000004005f8 <+40>: movaps %xmm0,0x10(%rsp)0x00000000004005fd <+45>: callq 0x4004a0 <system@plt>0x0000000000400602 <+50>: add $0x138,%rsp0x0000000000400609 <+57>: retq不帶-O2
(gdb) disass fun1 Dump of assembler code for function fun1:0x00000000004005d6 <+0>: push %rbp0x00000000004005d7 <+1>: mov %rsp,%rbp0x00000000004005da <+4>: sub $0x140,%rsp0x00000000004005e1 <+11>: mov %edi,-0x134(%rbp)0x00000000004005e7 <+17>: mov %esi,-0x138(%rbp)0x00000000004005ed <+23>: lea -0x130(%rbp),%rax0x00000000004005f4 <+30>: mov $0x4006e8,%esi0x00000000004005f9 <+35>: mov %rax,%rdi0x00000000004005fc <+38>: mov $0x0,%eax0x0000000000400601 <+43>: callq 0x4004e0 <sprintf@plt>0x0000000000400606 <+48>: lea -0x130(%rbp),%rax0x000000000040060d <+55>: mov %rax,%rdi0x0000000000400610 <+58>: callq 0x4004d0 <system@plt>0x0000000000400615 <+63>: nop0x0000000000400616 <+64>: leaveq0x0000000000400617 <+65>: retq End of assembler dump.總結(jié)
以上是生活随笔為你收集整理的GCC: 优化: xmm寄存器的使用,sprintf的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【C语言】do 关键字
- 下一篇: DolphinDB 助力 WILLIAM