hippy wrote: ↑Mon Sep 23, 2019 8:11 pm
Whether 32-bit or 64-bit a simple looping "n = n + 1" compiles to something like -
Code: Select all
ld r0, #n
Loop: ld r1, @r0
add r1, #1
st r1, @r0
b Loop
So ignoring differences in clock speeds, that 32-bit overflows sooner than 64-bit, that pointers are bigger; where exactly is the gain in being "64-bit" over "32-bit" ?
This is the code generated by gcc-O0 on RPI1 when n is 32 bits (loop removed):
Code: Select all
ldr r3, [fp, #-8]
add r3, r3, #1
str r3, [fp,#-8]
And this is it when n is 64 bits:
Code: Select all
ldrd r2, [fp, #-12]
adds r2, r2, #1
adc r3, #0
strd r2, [fp, #-12]
(ldrd/strd load or store 2 registers.) It uses 2 registers rather than 1 above. It uses 4 instructions rather than 3. Optimisation will mitigate some differences, but it does put extra pressure on registers, code density, cache and execution time (and possibly power consumption). The 64-bit code for ARM64 (this from gcc on Gentoo on RPi4) is:
Code: Select all
ldr x0, [sp,8]
add x0, x0, 1
std x0, [sp, 8]
It again uses 1 register and 3 instructions just like the 32-bit version; you get the advantages of 64 bits - much bigger range and less likely overflows - with no extra cost! (Well, 'n' takes 8 bytes rather than 4, but in my example n was on the stack frame).
If you don't think you will ever need 64 bits for anything (other than arbitrary precision libraries, cryptography etc) then compare this when 'kwd' is an integer containing packed ASCII:
Code: Select all
if kwd = 'proc' # 32 bits: up to 4 characters in one int
if kwd = 'function' # 64 bits: up to 8 characters in one int
So where does the gain in being "64-bit" over "32-bit" actually come from ?
If you only ever use 32-bit operations, and none of your code includes 32-bit ops trying to do a 64-bit job so could be revised, then probably you will see no difference. Or it may even be slower because wider pointers take more bandwidth.
The advantage is the luxury of being able to use a wider numeric type, one that can count beyond 2**32 (large file sizes, large array/string sizes, bitarrays above 256MB which need 33+ bits to index, and a wider pointer type that can deal with total memory (including virtual memory) beyond 4GB.