17

We can use the preprocessor to know if unsigned long long is defined:

#include <limits.h>

#ifndef ULLONG_MAX
typedef unsigned long t_mask; 
#else
typedef unsigned long long t_mask;
#endif

But how to know if __uint128_t is defined?

David Ranieri
  • 39,972
  • 7
  • 52
  • 94

4 Answers4

22

You can try the following. I do not know how reliable this is, but it might be the easiest way.

#ifdef __SIZEOF_INT128__
    // do some fancy stuff here
#else
    // do some fallback stuff here
#endif
firefexx
  • 666
  • 6
  • 16
4

I have not yet dealt with __uint128_t, but based on existing pattern usage, I would expect the following.

#include <stdint.h>

#ifndef UINT128MAX
    #error "__uint128_t not defined"
#endif

Hope this helps

Sparky
  • 13,505
  • 4
  • 26
  • 27
3

Since the __uint128_t type is a GCC extension, the proper thing to do is probably to check for some known-good version of GCC.

See this page for information about the macros used to version-check the GCC compiler.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 3
    Clang is aware of it to support GCC code, so I would lean towards Sparky's solution – user2913094 Mar 19 '16 at 21:58
  • 3
    The gcc extension is `__int128` -- `__int128_t`/`__uint128_t` is an intel ICC extension later picked up by most other compilers. – Chris Dodd Aug 22 '18 at 16:46
  • 2
    `unsigned __int128` is only supported by gcc on 64-bit targets, so a version check is insufficient unless you know you're only ever compiling for x86-64, AArch64, MIPS64, or whatever. – Peter Cordes Feb 21 '19 at 18:34
3

find your cc1 in the /usr/libexec/gcc tree, then interrogate it:

$ strings /usr/libexec/gcc/x86_64-redhat-linux/4.6.3/cc1 | grep uint128_t
__uint128_t            (or not)
cube
  • 3,867
  • 7
  • 32
  • 52