在平台上获取,可移植,更大和更快(无符号)的整数类型

时间:2021-08-04 19:58:14

Imagine the hypothetical case of a custom arbitrary precision integer type that must run as fast as possible, being fully portable at the same time. To achieve this goal, we need to use the larger and faster (unsigned) integer type available for a given platform.

想象一下自定义任意精度整数类型的假设情况,它必须尽可能快地运行,同时完全可移植。为了实现这一目标,我们需要使用可用于给定平台的更大和更快(无符号)整数类型。

It must be the larger because this way we will process maximum number of bits per cycle.

它必须更大,因为这样我们将处理每个周期的最大位数。

It must be the faster because on some platforms there are available larger integer types but with only a subset of integer operations over them (for example, in x86, 128 bit integers doesn't allow to do a one-instruction add-with-carry operation).

它必须更快,因为在某些平台上有可用的更大的整数类型但只有整数运算的子集(例如,在x86中,128位整数不允许执行单指令加载 - 携带操作)。

What is the best and most portable way to obtain the larger and faster integer type on every platform that has a C99 standard compiler (if it is possible at all)?

在具有C99标准编译器的每个平台上获得更大更快的整数类型的最佳和最便携的方法是什么(如果可能的话)?

1 个解决方案

#1


2  

What is the best and most portable way to obtain the larger and faster integer type on every platform that has a C99 standard compiler?

在具有C99标准编译器的每个平台上获得更大更快的整数类型的最佳和最便携的方法是什么?

Compile a test program with all candidate types, run it, time it, select the fastest and then compile your application.

使用所有候选类型编译测试程序,运行它,计时,选择最快,然后编译您的应用程序。

In other words, finding the fastest type is done at configure-time, not at compile-time. This has the additional benefit of using the fastest type even if the implementation lies about it in stdint.h.

换句话说,找到最快的类型是在配置时完成的,而不是在编译时完成的。这具有使用最快类型的额外好处,即使在stdint.h中实现它也是如此。

#1


2  

What is the best and most portable way to obtain the larger and faster integer type on every platform that has a C99 standard compiler?

在具有C99标准编译器的每个平台上获得更大更快的整数类型的最佳和最便携的方法是什么?

Compile a test program with all candidate types, run it, time it, select the fastest and then compile your application.

使用所有候选类型编译测试程序,运行它,计时,选择最快,然后编译您的应用程序。

In other words, finding the fastest type is done at configure-time, not at compile-time. This has the additional benefit of using the fastest type even if the implementation lies about it in stdint.h.

换句话说,找到最快的类型是在配置时完成的,而不是在编译时完成的。这具有使用最快类型的额外好处,即使在stdint.h中实现它也是如此。