Low-level integer routines with gcc

From LPTMS Wiki
Revision as of 14:45, 28 June 2012 by Roux (talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

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.

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:

long word = 1028464683;
cout << __builtin_popcountll(word) << endl;