On 04/25/2016 04:19 PM, Jonathan Lennox wrote:
Is there any portable way in GCC to detect whether the architecture we’re targeting has a fast native 64-bit type?
I suspect the closest you'll come to portability is by comparing the C11 macros INT_FAST32_MAX to INT_FAST64_MAX. If they have the same value it's certain that int_fast64_t is no slower than int_fast32_t. If they are not the same it's still possible that 64-bit ints are just as fast, but on the targets I've looked at GCC chooses the widest fast integer type for int_fast32_t (i.e., a 64-bit integer such as long on 64-bit targets and a 32-bit int on 32-bit targets). On these targets, multiplication is also typically at least as fast in the int_fast32_t type as in int_fast64_t. Martin
The motivation is that there are operations that it’s faster to do as a single 64-bit multiplication if the architecture really supports native 64-bit operations, but if instead 64-bit is emulated, it’s faster to write multiple 32-bit multiplications. My specific motivation is the Opus audio codec, which has code like: /* (a32 * (b32 >> 16)) >> 16 */ #if OPUS_FAST_INT64 #define silk_SMULWT(a32, b32) ((opus_int32)(((a32) * (opus_int64)((b32) >> 16)) >> 16)) #else #define silk_SMULWT(a32, b32) (((a32) >> 16) * ((b32) >> 16) + ((((a32) & 0x0000FFFF) * ((b32) >> 16)) >> 16)) #endif Right now, OPUS_FAST_INT64 is set like: /* Set this if opus_int64 is a native type of the CPU. */ /* Assume that all LP64 architectures have fast 64-bit types; also x86_64 (which can be ILP32 for x32) and Win64 (which is LLP64). */ #if defined(__LP64__) || defined(__x86_64__) || defined(_WIN64) #define OPUS_FAST_INT64 1 #endif But this isn’t complete; I know there are any number of 64-bit platforms that have x32-style ABIs. Rather than enumerate them all, is there some way I can get this information from GCC directly? (Extra points if it works on Clang as well.) Thanks!