Why are CPU registers fast to access?

Jagan picture Jagan · Aug 19, 2010 · Viewed 14.2k times · Source

Register variables are a well-known way to get fast access (register int i). But why are registers on the top of hierarchy (registers, cache, main memory, secondary memory)? What are all the things that make accessing registers so fast?

Answer

Potatoswatter picture Potatoswatter · Aug 19, 2010

Registers are circuits which are literally wired directly to the ALU, which contains the circuits for arithmetic. Every clock cycle, the register unit of the CPU core can feed a half-dozen or so variables into the other circuits. Actually, the units within the datapath (ALU, etc.) can feed data to each other directly, via the bypass network, which in a way forms a hierarchy level above registers — but they still use register-numbers to address each other. (The control section of a fully pipelined CPU dynamically maps datapath units to register numbers.)

The register keyword in C does nothing useful and you shouldn't use it. The compiler decides what variables should be in registers and when.