Expand/Shrink

machine_bits

Definition: integer i = machine_bits()
Description: machine_bits() is a function built-in to the interpreter.
It indicates the architecture that the program is being executed on: 32 or 64.
pwa/p2js: Supported. The trivial routine in p2js.js simply always returns 32.
Notes: Use machine_bits() when you want to execute different code depending on which architecture the program is running on. Most applications do not need to use machine_bits(), except perhaps to optimise a critical inner loop, however the Phix interpreter, for example, has to know whether to generate 32 or 64 bit machine instructions.

The machine_word() function is very similar, yielding 4 or 8 for a 32 or 64 bit executable respectively.

The call to machine_bits() costs nothing. It is optimized at compile-time into the appropriate integer value: 32 or 64. Further, no code whatsoever will be emitted for the comparison, else, and one of the two branches in the following.

You may also use requires(32|64) to trigger a restart prompt or crash should the runtime requirements not be met.
Example 1:
if machine_bits()=32 then
    poke4(addr,{#55667788,#11223344})
else
    poke8(addr,#1122334455667788)
end if
Example 2:
    pokeN(addr,pszMsg,machine_word())   -- (poke a 4 or 8 byte pointer)
    poken(addr,pszMsg)                  --           ""
Example 3: This routine is Phix specific. For compatibility with Euphoria the following can be used (copied from builtins\cffi.e):

--/*
function machine_bits()
    ifdef BITS64 then  
        return 64
    elsedef  
        return 32
    end ifdef 
end function
--*/
Implementation: Optimised away by the compiler - search for T_machine_bits and T_machine_word in pmain.e for more details.
See Also: platform, format directive, poke, requires