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].
Asked
Active
Viewed 5,975 times
8
Daniel Fath
- 13,643
- 6
- 46
- 76
-
1Do 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 Answers
15
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
- 326,504
- 69
- 892
- 1,159
Daniel Fath
- 13,643
- 6
- 46
- 76
-
2*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
-
-
3It'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
-
1That 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
- 326,504
- 69
- 892
- 1,159
ljedrz
- 17,806
- 3
- 63
- 86