Skip to content

Architecture Dependencies

Eugene edited this page Jul 13, 2023 · 6 revisions

Arithmetic on the Raspberry Pi is compiler and processor dependent. As such, one has to be careful with writing arithmetic expressions to avoid run-time errors, such as overflows. For example, the size of long on a Raspberry Pi could be 32-bits or 64-bits long, depending on whether compiler and processor uses 32-bit or 64-bit architectures.

1. Check the compiler architecture

$ file /usr/bin/arm-linux-gnueabihf-gcc-10

/usr/bin/arm-linux-gnueabihf-gcc-10: ELF 32-bit LSB executable, ARM, EABI5 version 1 (GNU/Linux), 
dynamically linked, interpreter /lib/ld-linux-armhf.so.3, 
BuildID[sha1]=7330182b4f77faa5e39ef1a3788e88d36c372363, 
for GNU/Linux 3.2.0, stripped

2. Check the kernel architecture

$ uname -m

armv7l

3. Check the actual data-types

#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <limits.h>
#include <stdint.h>

#define typename(x) _Generic((x),                                                     \
            _Bool: "_Bool",                  unsigned char: "unsigned char",          \
             char: "char",                     signed char: "signed char",            \
        short int: "short int",         unsigned short int: "unsigned short int",     \
              int: "int",                     unsigned int: "unsigned int",           \
         long int: "long int",           unsigned long int: "unsigned long int",      \
    long long int: "long long int", unsigned long long int: "unsigned long long int", \
            float: "float",                         double: "double",                 \
      long double: "long double",                   char *: "pointer to char",        \
           void *: "pointer to void",                int *: "pointer to int",         \
          default: "other")

int main(int argc, char **argv) {
  #if INTPTR_MAX == INT64_MAX
    /* 64-bit */
    printf("64-bit \n");
  #elif INTPTR_MAX == INT32_MAX
    printf("32-bit \n");
  #else 
    printf("Unknown bit \n");
  #endif
	
  printf("INT_MAX: %d \n", INT_MAX);
  printf("LONG_MAX: %d \n", LONG_MAX);

  long x = 4419;
  x = x * 1000000;
  printf("%ld \n", x);
  printf("%ld \n", 4419 * 1000000);
  printf("%s \n", typename(x));

  return 0;
}