开发者

What's a good alternative to uint8_t when it is not provided by the compiler?

开发者 https://www.devze.com 2023-01-25 19:54 出处:网络
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, readabili

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 compilat开发者_StackOverflowion 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


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

Try:

#include <stdint.h>


typedef unsigned char uint8_t;


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.


This seems to compile just fine with nvcc:

#include <stdint.h>

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号