Low-level integer routines with gcc: Difference between revisions
No edit summary |
mNo edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Gcc supports low-level integer routines similar to the FORTRAN routines POPCOUNT, ISHIFT... | Gcc supports low-level integer routines similar to the FORTRAN routines POPCOUNT, ISHIFT... | ||
The examples below are given for ''long long'', the routines name changes for ''long'' and ''int'' | |||
<source lang="cpp"> | <source lang="cpp"> | ||
long long __ashlti3 (long long a, int b ) //These functions return the result of shifting a left by b bits. | long long __ashlti3 (long long a, int b ) //These functions return the result of shifting a left by b bits. | ||
Line 14: | Line 16: | ||
int __popcountti2 (long long a ) //These functions return the number of bits set in a. | int __popcountti2 (long long a ) //These functions return the number of bits set in a. | ||
int64_t __bswapdi2 (int64 t a ) //These functions return the a byteswapped. | int64_t __bswapdi2 (int64 t a ) //These functions return the a byteswapped. | ||
</source> | |||
Ref : [http://gcc.gnu.org/onlinedocs/gccint.pdf From Gnu Compiler Collection Internals] | |||
An alternative is to dig directly into the /include/ directory of gcc and find some useful routines: for example in smmintrin.h, one finds a popcount for longs: | |||
<source lang="cpp"> | |||
long word = 1028464683; | |||
cout << __builtin_popcountll(word) << endl; | |||
</source> | </source> |
Latest revision as of 14:45, 28 June 2012
Gcc supports low-level integer routines similar to the FORTRAN routines POPCOUNT, ISHIFT...
The examples below are given for long long, the routines name changes for long and int <source lang="cpp"> long long __ashlti3 (long long a, int b ) //These functions return the result of shifting a left by b bits. long long __ashrti3 (long long a, int b ) //These functions return the result of arithmetically shifting a right by b bits. long long __divti3 (long long a, long long b ) //These functions return the quotient of the signed division of a and b. long long __lshrti3 (long long a, int b ) //These functions return the result of logically shifting a right by b bits. int __clzti2 (long long a ) //These functions return the number of leading 0-bits in a, starting at the most significant bit position.
//If a is zero, the result is undefined.
int __ctzti2 (long long a ) //These functions return the number of trailing 0-bits in a, starting at the least significant bit position.
//If a is zero, the result is undefined.
int __ffsti2 (long long a ) //These functions return the index of the least significant 1-bit in a, or the value zero if a is zero.
// The least significant bit is index one.
int __parityti2 (long long a ) //These functions return the value zero if the number of bits set in a is even, and the value one otherwise. int __popcountti2 (long long a ) //These functions return the number of bits set in a. int64_t __bswapdi2 (int64 t a ) //These functions return the a byteswapped. </source>
Ref : From Gnu Compiler Collection Internals
An alternative is to dig directly into the /include/ directory of gcc and find some useful routines: for example in smmintrin.h, one finds a popcount for longs: <source lang="cpp"> long word = 1028464683; cout << __builtin_popcountll(word) << endl; </source>