GCC提供的几个內建函数

参考

https://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Other-Builtins.html#Other-Builtins

https://en.wikipedia.org/wiki/Find_first_set#CTZ

 

Tool/libraryNameTypeInput type(s)NotesResult for zero input
POSIX.1 compliant libc
4.3BSD libc
OS X 10.3 libc[2][21]
ffsLibrary functionintIncludes glibc.
POSIX does not supply the complementary log base 2 / clz.
0
FreeBSD 5.3 libc
OS X 10.4 libc[22]
ffsl
fls
flsl
Library functionint,
long
fls ("find last set") computes (log base 2) + 1.0
FreeBSD 7.1 libc[23]ffsll
flsll
Library functionlong long 0
GCC__builtin_ffs[l,ll,imax]Built-in functionsunsigned int,
unsigned long,
unsigned long long,
uintmax_t
 0
GCC 3.4.0[24][25]

Clang 5.x [26][27]

__builtin_clz[l,ll,imax]
__builtin_ctz[l,ll,imax]
undefined
Visual Studio 2005_BitScanForward[28]
_BitScanReverse[29]
Compiler intrinsicsunsigned long,
unsigned __int64
Separate return value to indicate zero input0
Visual Studio 2008__lzcnt[30]Compiler intrinsicunsigned short,
unsigned int,
unsigned __int64
Relies on x64-only lzcnt instructionInput size in bits
Intel C++ Compiler_bit_scan_forward
_bit_scan_reverse[31]
Compiler intrinsicsint undefined
NVIDIA CUDA[32]__clzFunctions32-bit, 64-bitCompiles to fewer instructions on the GeForce 400 Series32
__ffs0
LLVMllvm.ctlz.*
llvm.cttz.*[33]
Intrinsic8, 16, 32, 64, 256LLVM assembly languageInput size if arg 2
is 0, else undefined
GHC 7.10 (base 4.8), in Data.BitscountLeadingZeros
countTrailingZeros
Library functionFiniteBits b => bHaskell programming languageInput size in bits

 

 

— Built-in Function: int __builtin_ffs (unsigned int x)

  Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero.

  右起第一个1的位置

— Built-in Function: int __builtin_clz (unsigned int x)

  Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.

  左起0的个数

— Built-in Function: int __builtin_ctz (unsigned int x)

  Returns the number of trailing 0-bits in x, starting at the least significant bit position. If x is 0, the result is undefined。

  右起0的个数

— Built-in Function: int __builtin_popcount (unsigned int x)

  Returns the number of 1-bits in x.

  x中1的个数

— Built-in Function: int __builtin_parity (unsigned int x)

  Returns the parity of x, i.e. the number of 1-bits in x modulo 2.

  x中1的个数的奇偶

上面函数都有long,long long版本[-l, -ll]。按上面表格看,也有-imax(eg, -i32 for uint32_t)

 

— Built-in Function: int32_t __builtin_bswap32 (int32_t x)

翻转32位数各字节

Returns x with the order of the bytes reversed; for example, 0xaabbccdd becomes 0xddccbbaa. Byte here always means exactly 8 bits.

— Built-in Function: int64_t __builtin_bswap64 (int64_t x)

翻转64位数各字节

Similar to __builtin_bswap32, except the argument and return types are 64-bit.

 

— Built-in Function: long __builtin_expect (long exp, long c)

分支预测

You may use __builtin_expect to provide the compiler with branch prediction information. In general, you should prefer to use actual profile feedback for this (-fprofile-arcs), as programmers are notoriously bad at predicting how their programs actually perform. However, there are applications in which this data is hard to collect.

The return value is the value of exp, which should be an integral expression. The semantics of the built-in are that it is expected that exp == c. For example:

          if (__builtin_expect (x, 0))
            foo ();
     

would indicate that we do not expect to call foo, since we expect x to be zero. Since you are limited to integral expressions for exp, you should use constructions such as

          if (__builtin_expect (ptr != NULL, 1))
            error ();
     

when testing pointer or floating-point values.

— Built-in Function: void __builtin_prefetch (const void *addr, ...)

预取

This function is used to minimize cache-miss latency by moving data into a cache before it is accessed. You can insert calls to __builtin_prefetch into code for which you know addresses of data in memory that is likely to be accessed soon. If the target supports them, data prefetch instructions will be generated. If the prefetch is done early enough before the access then the data will be in the cache by the time it is accessed.

The value of addr is the address of the memory to prefetch. There are two optional arguments, rw and locality. The value of rw is a compile-time constant one or zero; one means that the prefetch is preparing for a write to the memory address and zero, the default, means that the prefetch is preparing for a read. The value locality must be a compile-time constant integer between zero and three. A value of zero means that the data has no temporal locality, so it need not be left in the cache after the access. A value of three means that the data has a high degree of temporal locality and should be left in all levels of cache possible. Values of one and two mean, respectively, a low or moderate degree of temporal locality. The default is three.

          for (i = 0; i < n; i++)
            {
              a[i] = a[i] + b[i];
              __builtin_prefetch (&a[i+j], 1, 1);
              __builtin_prefetch (&b[i+j], 0, 1);
              /* ... */
            }
     

Data prefetch does not generate faults if addr is invalid, but the address expression itself must be valid. For example, a prefetch of p->next will not fault if p->next is not a valid address, but evaluation will fault if p is not a valid address.

If the target does not support data prefetch, the address expression is evaluated if it includes side effects but no other code is generated and GCC does not issue a warning.

__builtin_return_address(LEVEL)

—This function returns the return address of the current function,or of one of its callers. The LEVEL argument is number of frames to scan up the call stack. A value of ‘0’ yields the return address of the current function,a value of ‘1’ yields the return address of the caller of the current function,and so forth.

 

__builtin_alloca (https://linux.die.net/man/3/alloca)

alloca - allocate memory that is automatically freed

Synopsis

#include <alloca.h>

void *alloca(size_t size);

Description

The  alloca() function allocates  size bytes of space in the stack frame of the caller. This temporary space is automatically freed when the function that called  alloca() returns to its caller.

Return Value

 

The  alloca() function returns a pointer to the beginning of the allocated space. If the allocation causes stack overflow, program behavior is undefined.

Conforming to

This function is not in POSIX.1-2001.

There is evidence that the alloca() function appeared in 32V, PWB, PWB.2, 3BSD, and 4BSD. There is a man page for it in 4.3BSD. Linux uses the GNU version.

Notes

The  alloca() function is machine- and compiler-dependent. For certain applications, its use can improve efficiency compared to the use of  malloc(3) plus  free(3). In certain cases, it can also simplify memory deallocation in applications that use  longjmp(3) or  siglongjmp(3). Otherwise, its use is discouraged.

Because the space allocated by alloca() is allocated within the stack frame, that space is automatically freed if the function return is jumped over by a call to longjmp(3) or siglongjmp(3).

Do not attempt to free(3) space allocated by alloca()!

Notes on the GNU version

Normally,  gcc(1) translates calls to  alloca() with inlined code. This is not done when either the  -ansi-std=c89-std=c99, or the  -fno-builtin option is given (and the header  <alloca.h> is not included). But beware! By default the glibc version of  <stdlib.h> includes  <alloca.h> and that contains the line:
#define alloca(size)   __builtin_alloca (size)
with messy consequences if one has a private version of this function.

The fact that the code is inlined means that it is impossible to take the address of this function, or to change its behavior by linking with a different library.

The inlined code often consists of a single instruction adjusting the stack pointer, and does not check for stack overflow. Thus, there is no NULL error return.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误提示说明系统无法识别"gcc"命令。这个问题可能有几个可能的原因。 首先,可能是因为环境变量设置中路径书写错误或者粘贴错误。请确认环境变量中的路径是否正确,并且没有拼写错误。 其次,可能是因为你在修改环境变量后没有重新启动VScode。在修改完环境变量后,请重新打开VScode,以使更改生效。 最后,可能是因为你没有安装MinGW。MinGW是一个用于开发C/C++应用程序的工具集,其中包含了gcc编译器。请确保你已经正确安装了MinGW。 综上所述,解决这个问题的方法包括:检查环境变量路径是否正确,重新启动VScode以使更改生效,以及确保你安装了MinGW。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [已为此翻译单元 禁用波形曲线//gcc : 无法将“gcc”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。](https://blog.csdn.net/weixin_52361405/article/details/127061757)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [gcc : 无法将“gcc”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径...](https://blog.csdn.net/littlelittleR/article/details/121438006)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值