Macro likely and unlikely

Source: Internet
Author: User

In the source code, macros likely and unlikely are defined as follows (in include/Linux/compiler. h ):

# Define likely (x) _ builtin_exact CT (!! (X), 1)
# Define unlikely (x) _ builtin_exact CT (!! (X), 0)

To understand macros likely and unlikely, it is obvious that _ builtin_expect must be understood. _ Builtin_ct CT is a macro introduced by GCC (version> = 2.9). It helps the compiler determine the expected value of the conditional jump to avoid time waste caused by the jump. ScoredCodeFor example:

If (likely (ACAT = 1) // indicates that in most cases, if is true,ProgramMost directly execute the programs in if
While
If (unlikely (thread_memory_magazine1_is_empty (tmem, IX) // indicates that if is false in most cases, most programs directly execute the programs in else.

The jump commands used by the compilation statements generated by the compilation of two sections of code are different. __builtin_ct is actually used to satisfy the fact that no jump command is executed in most cases, SO _ builtin_skip CT only tells the compiler optimization, it does not change its true value judgment.

Example:

 //  Test_builtin_expect.c # Define Likely (x) _ builtin_exact CT (!! (X), 1) # Define Unlikely (x) _ builtin_exact CT (!! (X), 0) Int Test_likely ( Int  X ){  If  (Likely (x) {x = 5  ;}  Else  {X = 6  ;}  Return X ;}  Int Test_unlikely ( Int  X ){  If  (Unlikely (x) {x = 5  ;}  Else  {X = 6  ;}  Return  X ;} 
[Lammy @ localhost test_builtin_regular CT] $ gcc-fprofile-arcs-O2-C test_builtin_expect.c [lammy @ localhost test_builtin_regular CT] $ objdump-D placement: File Format elf32-i386Disassembly of section. text: 00000000 <test_likely>: 0: 55 push % EBP 1: 89 E5 mov % ESP, % EBP 3: 8B 45 08 mov 0x8 (% EBP ), % eax 6: 83 05 38 00 00 01 addl $0x1, 0x38 D: 83 15 3C 00 00 00 00 adcl $0x0, 0x3c 14: 85 C0 test % eax, % eax 16: 74 15 je 2D <test_likely + 0x2d> // here 18: 83 05 40 00 00 00 01 addl $0x1,0x40 1f: B8 05 00 00 00 mov $0x5, % eax 24: 83 15 44 00 00 00 00 adcl $0x0 0x44 2b: 5D pop % EBP 2C: C3 RET 2D: 83 05 48 00 00 00 01 addl $0x1,0x48 34: B8 06 00 00 00 mov $0x6, % eax 39: 83 15 4C 00 00 00 00 adcl $0x0, 0x4c 40: 5D pop % EBP 41: C3 RET 42: 8d B4 26 00 00 00 00 Lea 0x0 (% ESI, % eiz, 1), % ESI 49: 8d BC 27 00 00 00 00 Lea 0x0 (% EDI, % eiz, 1), % edi00000050 <test_unlikely >:50: 55 push % EBP 51: 89 E5 mov % ESP, % EBP 53: 8b 55 08 mov 0x8 (% EBP), % edX 56: 83 05 20 00 00 00 01 addl $0x1, 0x20 5D: 83 15 24 00 00 00 00 adcl $0x0 0x24 64: 85 D2 test % edX, % edX 66: 75 15 JNE 7d <test_unlikely + 0x2d> // see here 68: 83 05 30 00 00 00 01 addl $0x30 6f: b8 06 00 00 00 mov $0x6, % eax 74: 83 15 34 00 00 00 00 adcl $0x0 0x34 7b: 5D pop % EBP 7c: c3 RET 7d: 83 05 28 00 00 01 addl $0x1,0x28 84: B8 05 00 00 00 mov $0x5, % eax 89: 83 15 2C 00 00 00 00 adcl $0x0, 0x2c 90: 5D pop % EBP 91: C3 RET 92: 8d B4 26 00 00 00 00 Lea 0x0 (% ESI, % eiz, 1), % ESI 99: 8d BC 27 00 00 00 00 Lea 0x0 (% EDI, % eiz, 1), % edi000000a0 <_ global0000i_65535_0 _ test_likely>: A0: 55 push % EBP A1: 89 E5 mov % ESP, % EBP A3: 83 EC 08 Sub $0x8, % ESP A6: C7 04 24 00 00 00 00 movl $0x0, (% ESP) AD: e8 fc ff call AE <_ global1_ I _65535_0 _ test_likely + 0xe> B2: C9 leave B3: C3 RET [lammy @ localhost test_builtin_effecct] $

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.