long
我试图了解使用或选择内置 64 位数据类型的要求long long
。我无法理解long
vs的类型等效性和对齐要求long long
。
long
在 LP64/ILP64/ LLP64long long
数据模型下选择 64 位类型时,最佳实践或标准是什么?
这里有一些相关的问题。它们完全涵盖了其他主题,例如sizeof(long) <= sizeof(long long)
,但它们并没有完全处理等价或对齐期望。它们还严重依赖非内置类型,例如uint64_t
.
- UNIX 系统 - 64 位和数据大小中立
- c/c++ 中的 unsigned long/long/int 有什么区别?
- 最好在 64 位中使用 long 或 long long
- 在 64 位数据模型上指定 64 位无符号整数文字
背景信息是类似于下面的代码来选择一个内置的 64 位类型来使用:
#if _LP64 || __LP64__ || _ILP64 || __ILP64__
typedef my_u64 unsigned long;
#else
typedef my_u64 unsigned long long;
#endif
i686 和 ARMv7 及以下版本可以使用typedef my_u64 unsigned long long
.
第一个问题案例
在 x86_64 (AMD64) 机器上,如果 typedef my_u64 unsigned long
有效(由于LP64
),则会导致编译错误:
$ gcc -mrdrnd test.cc -o test.exe
test.cc: In function ‘int main(int, char**)’:
test.cc:18:22: error: invalid conversion from ‘my_u64* {aka long unsigned int*}’ to ‘long long unsigned int*’ [-fpermissive]
_rdrand64_step(&val);
^
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.9/include/x86intrin.h:46:0,
from test.cc:2:
/usr/lib/gcc/x86_64-linux-gnu/4.9/include/immintrin.h:166:1: note: initializing argument 1 of ‘int _rdrand64_step(long long unsigned int*)’
_rdrand64_step (unsigned long long *__P)
^
第二个问题案例
在 Aarch64 (ARM64) 机器上,如果 typedef my_u64 unsigned long long
有效(修复 x86_64 错误),则会导致编译错误:
$ gcc test.cc -o test.exe
test.cc: In function ‘int main(int, char**)’:
test.cc:21:16: error: invalid conversion from ‘my_u64* {aka long long unsigned int*}’ to ‘const uint64_t* {aka const long unsigned int*}’ [-fpermissive]
vld1q_u64(val);
^
In file included from test.cc:4:0:
/usr/lib/gcc/aarch64-linux-gnu/4.9/include/arm_neon.h:17003:1: note: initializing argument 1 of ‘uint64x2_t vld1q_u64(const uint64_t*)’
vld1q_u64 (const uint64_t *a)
^
$ cat test.cc
#if __x86_64__
#include <x86intrin.h>
#elif __aarch64__
#include <arm_neon.h>
#include <arm_acle.h>
#endif
#if _LP64 || __LP64__
typedef unsigned long my_u64;
#else
typedef unsigned long long my_u64;
#endif
int main(int argc, char* argv[])
{
#if __x86_64__
my_u64 val;
_rdrand64_step(&val);
#elif __aarch64__
my_u64 val[2];
vld1q_u64(val);
#endif
return 0;
}