logoalt Hacker News

jabltoday at 1:40 PM5 repliesview on HN

From your past posting history, I presume that you're implying this makes RISC-V better?

Do we have any data showing that having a dedicated zero register is better than a short and canonical instruction for zeroing an arbitrary register?


Replies

phiretoday at 2:17 PM

The zero register helps RISC-V (and MIPS before it) really cut down on the number of instructions, and hardware complexity.

You don't need a mov instruction, you just OR with $zero. You don't need a load immediate instruction you just ADDI/ORI with $zero. You don't need a Neg instruction, you just SUB with $zero. All your Compare-And-Branch instructions get a compare with $zero variant for free.

I refuse to say this "zero register" approach is better, it is part of a wide design with many interacting features. But once you have 31 registers, it's quite cheap to allocate one register to be zero, and may actually save encoding space elsewhere. (And encoding space is always an issue with fixed width instructions).

AArch64 takes the concept further, they have a register that is sometimes acts as the zero register (when used in ALU instructions) and other times is the stack pointer (when used in memory instructions and a few special stack instructions).

show 1 reply
wongarsutoday at 2:39 PM

MIPS for example also has one, along with a similar number of registers (~32). So it's not like RISC-V took a radical new position here, they were able to look back at what worked and what didn't, and decided that for their target a zero register was the right tradeoff. It's certainly the more "elegant" solution. A zero register is useful as input or output register for all kinds of operations, not just for zeroing

kevin_thibedeautoday at 1:47 PM

It's a definite liability on a machine with only 8 general purpose registers. Losing 12% of the register space for a constant would be a waste of hardware.

show 1 reply
grueztoday at 2:09 PM

It's basically the eternal debate of RISC vs CISC (x86). RISC proponents claim RISC is better because it's simpler to decode. CISC proponents retort that CISC means code can be more compact, which helps with cache hits.

show 1 reply
doogliustoday at 1:53 PM

I think one could just pick a convention where a particular GP register is zeroed at program startup and just make your own zero register that way, getting all the benefits at very small cost. The microarchitecture AIUI has a dedicated zero register so any processor-level optimizations would still apply.

show 1 reply