hdf5 icon indicating copy to clipboard operation
hdf5 copied to clipboard

H5T_NATIVE_CHAR is unsigned on linux arm64

Open krisfed opened this issue 3 weeks ago • 1 comments

I think this is expected as it looks like chars are unsigned on linux arm64 by default [1], but wanted to confirm.

We are using HDF5 1.14.4.3 , and on "normal" linux (x86_64) we get

hid_t  myType = H5Tcopy(H5T_NATIVE_CHAR);
H5T_sign_t sign = H5Tget_sign(myType);  // 1, meaning it is signed

but on linux ARM64 (aarch64) we get:

hid_t  myType = H5Tcopy(H5T_NATIVE_CHAR);
H5T_sign_t sign = H5Tget_sign(myType);  // 0, meaning it is unsigned

We have some code roughly along these lines to map the in-file data type to in-memory data type based on class, size, and sign-ness, and that's where we noticed the difference on ARM64:

    hid_t  file_type_id = H5Aget_type(<...>);
    size_t datatype_size = H5Tget_size(file_type_id);
    H5T_class_t datatype_class = H5Tget_class(file_type_id);

   hid_t mem_type_id = -1;
    switch (datatype_class) {
      case H5T_INTEGER: {
          H5T_sign_t sign = H5Tget_sign(file_type_id);

          if(sign==H5T_SGN_NONE) {  // for unsigned integers 

              if(datatype_size==1) {
                  mem_type_id = H5Tcopy(H5T_NATIVE_UCHAR); // for unsigned char
              }
              else if(datatype_size==2) {
                  <...>
          }
          else {  // for signed integers 
              if(datatype_size==1) {
                  mem_type_id = H5Tcopy(H5T_NATIVE_CHAR); // for signed char
              }
              else if(datatype_size==2) {
                 <...>
          }
          break;
      }
      case <...>

Am I correct in thinking we should replace H5T_NATIVE_CHAR with H5T_NATIVE_SCHAR here to guarantee that signed integers that are 1 byte in size are correctly represented independently of the platform?

[1] "The signedness of char depends on the compiler and the target platform: the defaults for ARM and PowerPC are typically unsigned, the defaults for x86 and x64 are typically signed." https://en.cppreference.com/w/cpp/language/types.html

krisfed avatar Dec 22 '25 13:12 krisfed