Skip to main content

sra

Function sra 

Source
pub const fn sra(v: u64, sa: u32) -> u64
Expand description

SRA — 32-bit shift right arithmetic. Reproduces the VR4300 erratum.

The processor manual says the low 32 bits are filled with copies of bit 31 and bit 31 is then sign-extended into the upper half. Hardware does not do that. In practice the most significant bits are filled from the upper 32 bits of the register first, and the new bit 31 is then sign-extended — which leaks 64-bit state that should be inaccessible, in both 32- and 64-bit mode.

manual:   rd = (uint64_t)(int32_t)((int32_t)rt >> sa)
hardware: rd = (uint64_t)(int32_t)((int64_t)rt >> sa)

With rt = 0x0123456789ABCDEF, sa = 16, the manual predicts 0xFFFFFFFFFFFF89AB; hardware gives 0x00000000456789AB.

This is not a bug to fix. It is present on more consoles than the multiplication erratum and is not known to have ever been corrected, so software can depend on it. sra_reproduces_the_vr4300_erratum fails if it is “corrected”. Source: n64brew_wiki/markdown/VR4300.md § Known Bugs.