Is there a performance hit for using a u32 on an x64 chip?

Ah, makes sense. Sounds like the cat is out of the bag. Up to you whether to keep the code online, but if you do you may get other Euler types messaging you in the future. :slightly_smiling_face:

I'm thinking on it.

I have a bit of a philosophical problem with the idea of hiding the solutions.

Surely the reward for such a challenge, like all kind of problems in mathematics and elsewhere, is in the thinking about it, coming up with a solution oneself, or at least reaching an understanding of how it works.

We are not about to hide all of mathematics because it's better people work it out for themselves!

In a similar vein I first encountered the Fast Fourier Transform as BASIC source code published in some magazine. Despite years of exposure to maths and Fourier Analysis at uni this code made no sense to me at all. It was insane. How could all that bit-reversal stuff possibly produce a useful result? I could not related it to any maths we had done.

It was two decades later when I woke up with a start in the night and realized how the FFT works. I had to jump out of bed and write my own FFT from scratch before I forgot!


Just to spell it out, here's some of the most important tradeoffs for choosing between 32 and 64-bit arithmetic, if you're running on a 64-bit machine:

Reasons to use 64-bit numbers:

  • you can store bigger numbers
  • there's no need to emulate integer overflow in software (by AND-ing with 0x00000000FFFFFFFF, or by being forced to use special instructions)
    • This isn't as big of a deal in Rust as it is in C, because Rust iterators don't normally use integer types. Slice and Vector iterators use pointers, and they use an intrinsic called ptr::offset that will trigger Undefined Behaviour if you run outside of the allocation (which includes overflow).

Reasons to use 32-bit numbers:

  • uses less memory
    • bandwidth and cache
    • space
  • more items processed in a single SIMD operation
  • x64 can do 32-bit operations pretty easily, while ia32 does a poorer job on 64-bit operations

And let's not discuss Project Euler's policies. It's super off-topic.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.