歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

uclinux-2008R1-RC8(bf561)到VDSP5的移植(28):likely

在include/linux/compiler.h中定義了兩個宏:

/*
 * Generic compiler-dependent macros required for kernel
 * build go below this comment. Actual compiler/compiler version
 * specific implementations come from the above header files
 */

#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x)    __builtin_expect(!!(x), 0)

__builtin_expect(exp, val)是GCC的一個內建函數,而VDSP無此函數,它用於為編譯器提供分支預測信息,其返回值是整數表達式exp的值。這個內建函數的語義是 exp 的預期值是 val,編譯器可以根據這個信息適當地重排語句塊的順序,使程序在預期的情況下有更高的執行效率。

在VDSP中,提供了兩個相似的函數:

You can use the expected_true and expected_false built-in functions to control the compiler’s behavior for specific cases. By using these functions, you can tell the compiler which way a condition is most likely to evaluate. This influences the default flow of execution.
For example,
   if (buffer_valid(data_buffer))
      if (send_msg(data_buffer))
         system_failure();
shows two nested conditional statements. If it was known that, for this example, buffer_valid() would usually return true, but that send_msg() would rarely do so, the code could be written as
   if (expected_true(buffer_valid(data_buffer)))
      if (expected_false(send_msg(data_buffer)))
         system_failure();

因此,我們將這兩個宏定義改為:

/*
 * Generic compiler-dependent macros required for kernel
 * build go below this comment. Actual compiler/compiler version
 * specific implementations come from the above header files
 */

#define likely(x) expected_true(!!(x))
#define unlikely(x)    expected_false(!!(x))

Copyright © Linux教程網 All Rights Reserved