What happens if I compile a program that uses uint64 on old 32 bit hardware?

I used to build some of my little hobby projects for the old Raspberry Pi 32 bit. In those cases, I never used uint64, int64, float64, etc.

Today, I was working on a new project and had the thought cross my mind; if I were to compile this on 32 bit hardware, what would happen under the hood? If I have a uint64 and compile for 32 bit, is it some kind of emulated 64 bit? Do I just lose precision? Is it undefined?

Sounds no way to do this in Go
but if you want, maybe you can use this logic :
//
using unsafe.Sizeof to determine the architecture at runtime and execute the appropriate code block.
//

  • If you need to work with large numbers, consider using libraries that provide alternative implementations for 64-bit math on 32-bit systems (e.g., GMP library). This could a possible way to go!

You have nothing to do, it just works :magic_wand:, with perfect precision.

The compiler internally model uint64 on 32 bits platforms as roughly this:

type uint64 struct {
 lo, hi uint32
}

Then each operation is emulated in multiple steps, for example:

a = b + c

gives:

a.lo, carry := bits.Add32(b.lo, c.lo, 0)
a.hi, _ = bits.Add32(b.hi, c.hi, carry)

This emulation has a slight performance impact, for example you usually need 3 32 bits multiply to simulate one 64 bits one. Divisions are hit the worst.

3 Likes
  • This is the most likely scenario. The compiler recognizes that the target architecture is 32-bit and doesn’t support native uint64 data type.