码迷,mamicode.com
首页 > 其他好文 > 详细

gnu cc扩展

时间:2015-11-28 19:53:42      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

gnc cc扩展标准c的语法,详情请见:

http://uw714doc.sco.com/cgi-bin/info2html?%28gcc.info%29C%2520Extensions&lang=en

 

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 #define my_min(x, y)  ({const typeof(x) __x = x;  4     const typeof(y) __y = y;  5     (void) (&__x == &__y);  6     __x < __y ? __x : __y;})
 7 
 8 struct Str
 9 {
10     int a;
11     char b;
12     short c;
13 } __attribute__((packed));
14 
15 #define ERR_PRINTF(fmt, args...) {16     char msg[1024]; 17     snprintf(msg, sizeof(msg), "%s:%d "fmt"\n", __FUNCTION__, __LINE__, ##args);18     printf("%s", msg); 19 }
20 
21 #define likely(x)  __builtin_expect((x), 1)
22 #define unlikely(x)  __builtin_expect((x), 0)
23 
24 void f_unlikely()
25 {
26     printf("go unlikely\n"); 
27 }
28 
29 void f_likely()
30 {
31     printf("go likely\n"); 
32 }
33 
34 
35 void test_likely(int x)
36 {
37     if(unlikely(x))
38         f_unlikely();
39     else
40         f_likely();
41 }
42 
43 int main()
44 {
45     //typeof
46     int c = my_min(1, 2);
47     printf("%d\n", c);
48 
49     //packed
50     printf("sizeof(Str) = %lu\n", sizeof(Str));
51 
52     //##
53     ERR_PRINTF("%d test", 1);
54     ERR_PRINTF("test2");
55 
56     //__builtin_expect
57     test_likely(1);
58 
59     //__builtin_return_address
60     printf("return address %p\n", __builtin_return_address(0));
61     while(1)
62         sleep(10);
63     return 0;
64 }

执行结果:

[ ~/code/test_sighup]$ ./test_gcc_ex
1
sizeof(Str) = 7
main:60 1 test
main:61 test2
go unlikely
return address 0x7ff2beeaacdd

 

objdump -DC ./test_gcc_ex

00000000004006a0 <test_likely(int)>:
  4006a0:       85 ff                   test   %edi,%edi
  4006a2:       75 05                   jne    4006a9 <test_likely(int)+0x9>
  4006a4:       e9 d7 ff ff ff          jmpq   400680 <f_likely()>
  4006a9:       e9 e2 ff ff ff          jmpq   400690 <f_unlikely()>
  4006ae:       66 90                   xchg   %ax,%ax

 

参考代码:

http://blog.csdn.net/cugxueyu/article/details/1955372

gnu cc扩展

标签:

原文地址:http://www.cnblogs.com/Torstan/p/5003241.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!