【问题标题】:How to know if __uint128_t is defined [duplicate]如何知道是否定义了 __uint128_t [重复]
【发布时间】:2013-09-03 02:48:16
【问题描述】:

我们可以使用预处理器知道unsigned long long是否被定义:

#include <limits.h>

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

但是如何知道__uint128_t 是否被定义?

【问题讨论】:

    标签: c c-preprocessor 128-bit


    【解决方案1】:

    在 /usr/libexec/gcc 树中找到你的 cc1,然后询问它:

    $ 字符串 /usr/libexec/gcc/x86_64-redhat-linux/4.6.3/cc1 | grep uint128_t __uint128_t(或不)

    【讨论】:

      【解决方案2】:

      您可以尝试以下方法。我不知道这有多可靠,但这可能是最简单的方法。

      #ifdef __SIZEOF_INT128__
          // do some fancy stuff here
      #else
          // do some fallback stuff here
      #endif
      

      【讨论】:

      • 这是Linux内核所做的,参见include/linux/math64.h。
      【解决方案3】:

      我还没有处理 __uint128_t,但根据现有的模式用法,我预计会出现以下情况。

      #include <stdint.h>
      
      #ifndef UINT128MAX
          #error "__uint128_t not defined"
      #endif
      

      希望对你有帮助

      【讨论】:

      • 编译器(例如 x86-64 gcc8.2)不在 stdint.h 或 limits.h 中定义。
      【解决方案4】:

      由于the __uint128_t type is a GCC extension,正确的做法可能是检查一些已知良好的 GCC 版本。

      See this page 获取有关用于对 GCC 编译器进行版本检查的宏的信息。

      【讨论】:

      • Clang 知道它支持 GCC 代码,所以我倾向于 Sparky 的解决方案
      • gcc 扩展名是 __int128 -- __int128_t/__uint128_t 是后来被大多数其他编译器采用的英特尔 ICC 扩展名。
      • unsigned __int128 仅在 64 位目标上受 gcc 支持,因此除非您知道自己只为 x86-64、AArch64、MIPS64 或其他任何东西编译,否则版本检查是不够的。
      猜你喜欢
      • 1970-01-01
      • 2013-12-03
      • 2011-10-16
      • 1970-01-01
      • 2013-12-15
      • 2016-10-20
      • 2017-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多