我正在使用 nvcc 编译 CUDA 内核。不幸的是,nvcc 似乎不支持
uint8_t
,尽管它确实支持 int8_t
(!)。出于可移植性、可读性和理智的原因,我宁愿不使用 unsigned char
。还有其他好的选择吗?
为了防止任何可能的误解,这里有一些细节。
$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2010 NVIDIA Corporation
Built on Mon_Jun__7_18:56:31_PDT_2010
Cuda compilation tools, release 3.1, V0.2.1221
代码包含
int8_t test = 0;
很好,但是代码包含
uint8_t test = 0;
抛出类似错误消息
test.cu(8): error: identifier "uint8_t" is undefined
C99 整数类型不是“由编译器定义” - 它们是在
<stdint.h>
中定义的。
尝试:
#include <stdint.h>
typedef unsigned char uint8_t;
这与 Mac OS X 使用的没有什么不同:
typedef unsigned char uint8_t;
您对
unsigned char
的便携性有何担忧?如果担心 char
可能不代表 8 位存储,那么您可以包含如下静态断言:
typedef int Assert8BitChar[(CHAR_BIT == 8)? 0 : -1];
当违反假设时,这将导致编译出错。
这似乎与
nvcc
编译得很好:
#include <stdint.h>
int main() {
uint8_t x = 0;
return (int) x;
}