2

On x86 systems, it's usually 32-bit words. On x86_64, it's 64 bits. How can this value be used to conditionally compile code?

The reference lists a number attributes that #[cfg()] exposes, but word size doesn't seem to be one of those exposed.

Doe
  • 585
  • 5
  • 19
  • 1
    I don't think the "word" size can be queried, because it is meaningless semantically. If you explain what you intend to check with this size, it would be easier: are you looking for pointer size? atomic size? something else? – Matthieu M. Mar 14 '17 at 10:08
  • @MatthieuM. That's poor reasoning for a systems language. As for the reason, it's to select optimal algorithms and unsigned integer types for a set of mathematical calculations for each arch. Moreover, I don't see how my specific goals are relevant to a general question as the one I asked here. – Doe Mar 15 '17 at 06:09
  • I am afraid that you do not understand how modern architectures work, it's a tad work complicated than just a "silver bullet" word size. 64-bits processors, for example, which can handle both u32 and u64 operations may be faster on 64-bits operations, in isolation, but can perform more 32-bits operations in a single cycle, and twice as many u32 can fit in a single cache line as u64. So whether to use u32 or u64 for better speed is NOT as simple as querying "a word size"... not any longer. Which is why I am saying that "a word size" is no longer meaningful in general. – Matthieu M. Mar 15 '17 at 07:40
  • @MatthieuM. Perhaps in general, but Rust claims to be a systems language, a language that by definition deals with specifics. Can this be said for all architecture targets supported by Rust? Not all software is written for x86. (In this instance, it's targeted towards ARM). From my testing (with --release, and holding system load, algorithm used, and cpu temperature constant), the 64-bit code path completes in about half the time (0.58) compared to its 32-bit counterpart on 64-bit systems (which is more or less aligned with my assumptions). – Doe Mar 15 '17 at 14:36
  • Rust **is** a systems language, this has nothing to do with this though. The problem is one of vocabulary; it's not clear what `word` means. As a simple example, [x86-64 is a 64-bits architecture with a 16-bits word size according to Wikipedia](https://en.wikipedia.org/wiki/Word_%28computer_architecture%29). Do you want your program to use 16-bits integrals on x86_64? – Matthieu M. Mar 15 '17 at 14:49
  • @MatthieuM. Fair enough. I should have used more precise wording. Should the title be changed to a less ambiguous title (i.e "How to conditionally compile based on largest data register size available")? It might hurt search engine discoverability though. – Doe Mar 16 '17 at 05:15
  • I don't think that's what you want either, due to vectorized instructions there are registers of up to 512 bits now (on x86_64 and at least 256 bits on ARM). I fully understand what you are aiming for, and I have no idea how to name it. It used to be that C would name the corresponding type `int`, but even that didn't survive the 64-bits transition as people worried than since `int` had been 32-bits for so long making it 64-bits would break stuff. I guess what you would want is "the size of the fastest integer", but as I mentioned early... it may not be the fastest for your algorithm. – Matthieu M. Mar 16 '17 at 07:36

1 Answers1

4

According to the conditional compilation docs, you can use one of the following to get close to what you're looking for.

target_pointer_width = "..."

target_has_atomic = "..." // one of "8", "16", "32", "64" and "ptr".
Błażej Michalik
  • 4,474
  • 40
  • 55
Mark Mucha
  • 1,560
  • 2
  • 12
  • 18
  • `target_has_atomic` is effectively equivalent to what I'm looking for. I'm using rust 1.9, which didn't list this attribute. – Doe Mar 15 '17 at 06:14