当编译器未提供 uint8_t 时,什么是更好的替代方案?

发布于 2024-10-03 03:57:13 字数 620 浏览 0 评论 0原文

我正在使用 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

I'm using nvcc to compile a CUDA kernel. Unfortunately, nvcc doesn't seem to support uint8_t, although it does support int8_t (!). I'd just as soon not use unsigned char, for portability, readability, and sanity reasons. Is there another good alternative?


Just to forestall any possible misunderstanding, here are some details.

$ 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

Code containing

int8_t test = 0;

is fine, but code containing

uint8_t test = 0;

throws an error message like

test.cu(8): error: identifier "uint8_t" is undefined

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独木成林 2024-10-10 03:57:13

C99 整数类型不是“由编译器定义”——它们是在 中定义的。

尝试:

#include <stdint.h>

C99 integer types are not "defined by the compiler" - they are defined in <stdint.h>.

Try:

#include <stdint.h>
溺深海 2024-10-10 03:57:13
typedef unsigned char uint8_t;
typedef unsigned char uint8_t;
£冰雨忧蓝° 2024-10-10 03:57:13

这与 Mac OS X 使用的没有什么不同:

typedef unsigned char uint8_t;

您对 unsigned char 的可移植性有何担忧?如果担心 char 可能不代表 8 位存储,那么您可以包含一个静态断言,其内容如下:

typedef int Assert8BitChar[(CHAR_BIT == 8)? 0 : -1];

当违反假设时,这将导致编译出错。

This is no different from what Mac OS X uses:

typedef unsigned char uint8_t;

What is your concern about the portability of unsigned char? If the concern is that a char might not represent 8 bits of storage, then you can include a static assertion along the lines of:

typedef int Assert8BitChar[(CHAR_BIT == 8)? 0 : -1];

This will cause compilation to error out when the assumption is violated.

孤千羽 2024-10-10 03:57:13

这似乎可以用 nvcc 编译得很好:

#include <stdint.h>

int main() {
    uint8_t x = 0;
    return (int) x;
}

This seems to compile just fine with nvcc:

#include <stdint.h>

int main() {
    uint8_t x = 0;
    return (int) x;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文