Logo

Programming-Idioms

  • Fortran
  • C++

Idiom #160 Detect if 32-bit or 64-bit architecture

Execute f32() if platform is 32-bit, or f64() if platform is 64-bit.
This can be either a compile-time condition (depending on target) or a runtime detection.

#ifdef __x86_64
f64();
#else
#ifdef _M_AMD64
f64();
#else
f32();
#endif
#endif

At compile time, compiler dependent.
if constexpr(sizeof(nullptr) == 8) {
  f64();
} else {
  f32();
}

This tests the size of a pointer.
program main
  use iso_c_binding
  implicit none
  type(c_ptr) :: x
  logical, parameter :: is_64 = c_sizeof(x) == 8
  if (is_64) then
     call f64
  else
     call f32
  end if
end program main

This checks the size of the standard C interop pointer and deduces the system type from that.
if(Environment.Is64BitOperatingSystem)
{
    f64();
}
else
{
    f32();
}

New implementation...
< >
programming-idioms.org