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

I use nvcc to compile the CUDA kernel. Unfortunately, nvcc does not seem to support uint8_t, although it does support int8_t(!). I would also not use it unsigned char, for the convenience of portability, readability and sanity. Is there another good alternative?


To prevent 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;

excellent, but the code containing

uint8_t test = 0;

gives an error message, for example

test.cu(8): error: identifier "uint8_t" is undefined
+3
source share
4 answers

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

Try:

#include <stdint.h>
+19
typedef unsigned char uint8_t;
+1

, Mac OS X:

typedef unsigned char uint8_t;

What is your concern about portability unsigned char? If the problem is that it charmay not represent 8 bits of storage, then you can enable static statement line by line:

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

This will cause compilation to fail if an assumption is violated.

+1
source

It seems that compiling is just fine with nvcc:

#include <stdint.h>

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

Source: https://habr.com/ru/post/1775050/


All Articles