9

If there a conditional check for whether processor is 32-bit or 64-bit? I'm looking for kind of configuration check like e.g. #cfg[x86] or cfg[x64].

Daniel Fath
  • 16,453
  • 7
  • 47
  • 82
  • 1
    Do you want to detect this at compile (to enable/disable different functions) or is a runtime detection sufficient? The latter can be done with `if mem::size_of::() == 8` (and will be optimized out). – Matthieu M. Jan 27 '17 at 14:54
  • At compile time, to disable portions of code. – Daniel Fath Jan 27 '17 at 17:23

2 Answers2

16

The #[cfg(target_pointer_width = "64")] from the cfg section in the Rust reference seems like a likely solution. It is based on the size of a pointer (as well as isize and usize), which should correspond to the architecture.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Daniel Fath
  • 16,453
  • 7
  • 47
  • 82
  • 3
    *which should correspond to the architecture* — not always. For example, AVR chips have 16-bit pointers but are 8-bit architecture. – Shepmaster Jan 27 '17 at 16:17
  • So neither is the answer? – Daniel Fath Jan 27 '17 at 17:23
  • 3
    It's more that the question you are asking is not specific enough. Why do you care about the "architecture"? If you care about the size of a pointer, that's one question. If you care about the other aspects, then `target_arch` might be right. If you care about the native CPU size of an integer, then I know of no answer. – Shepmaster Jan 27 '17 at 17:26
  • Block sort uses term 64-bit systems when defining FloorOfTwo function. I'm unsure what 64-bit would mean in this context ? https://en.wikipedia.org/wiki/Block_sort#Algorithm – Daniel Fath Jan 27 '17 at 17:29
  • 1
    That seems like a poor explanation of the algorithm then. It probably doesn't mean if the **system** is 64-bit, it means if a specific value has a certain number of bits. In the examples, it's always called with an array length. Since that's a `usize` in Rust, you care about the size of a usize. Although you can probably just use [`usize::next_power_of_two`](https://doc.rust-lang.org/std/primitive.usize.html#method.next_power_of_two). – Shepmaster Jan 27 '17 at 17:33
  • Sadly, no; `usize::next_power_of_two gives` different results. The algorithm wants lesser power of two e.g. `63u8.next_power_of_two()` should be `32` not `64`. Although some combination of `next_power_of_two` and shift could work. – Daniel Fath Jan 27 '17 at 18:20
7

You should check the Rust Reference chapter on conditional compilation:

target_arch = "..." - Target CPU architecture, such as "x86", "x86_64", "mips", "powerpc", "powerpc64", "arm", or "aarch64". This value is closely related to the first element of the platform target triple, though it is not identical.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
ljedrz
  • 20,316
  • 4
  • 69
  • 97
  • Is there a complete list of these `target_arch` values? One can find a list of `target triples` from [`platform support`](https://doc.rust-lang.org/nightly/rustc/platform-support.html), but this `target_arch` seems to be not that well documented. – Steve Lau Oct 12 '22 at 13:27