Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

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.

use Config qw(%Config);
if ($Config{archname} =~ '64') {
    f64;
} else {
    f32;
}
if constexpr(sizeof(nullptr) == 8) {
  f64();
} else {
  f32();
}
#ifdef __x86_64
f64();
#else
#ifdef _M_AMD64
f64();
#else
f32();
#endif
#endif
if(Environment.Is64BitOperatingSystem)
{
    f64();
}
else
{
    f32();
}
version(X86)
    f32();
version(X86_64)
    f64();
static if (size_t.sizeof == 4)
    f32();
static if (size_t.sizeof == 8)
    f64();
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
import "strconv"
if strconv.IntSize==32 {
	f32()
}
if strconv.IntSize==64 {
	f64()
}
import System.Info
detectArch :: IO ()
detectArch = do
    case arch == "x86_64" of
        True -> do f64
        False -> case arch == "x86" of
                    True -> do f32
os
const is64Bit = os.arch() === 'x64' || process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432');
is64Bit ? _f64() : _f32();
switch(System.getProperty("sun.arch.data.model")) {
  case "64": 
    f64(); 
    break;
  case "32":
    f32();
    break;
}
(defun detect-architecture ()
  (let ((arch (machine-type)))
    (cond ((equalp arch "X86-64") (f64))
          ((equalp arch "X86") (f32))
          (t "unknown"))))
<?php

if (PHP_INT_SIZE === 4)
    f32();
else if (PHP_INT_SIZE === 8)
    f64();
begin
  {$ifdef cpu64}
  f64;
  {$endif}
  {$ifdef cpu32}
  f32;
  {$endif}
end.
import sys
if sys.maxsize > 2**32:
    f64()
else:
    f32()

case 1.size
  when 8 then f64
  when 4 then f32
end
match std::mem::size_of::<&char>() {
    4 => f32(),
    8 => f64(),
    _ => {}
}
#[cfg(target_pointer_width = "64")]
f64();

#[cfg(target_pointer_width = "32")]
f32();	

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