Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are CPU registers fast to access?

Tags:

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?

like image 888
Jagan Avatar asked Aug 19 '10 03:08

Jagan


2 Answers

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.

like image 76
Potatoswatter Avatar answered Dec 20 '22 09:12

Potatoswatter


Registers are a core part of the CPU, and much of the instruction set of a CPU will be tailored for working against registers rather than memory locations. Accessing a register's value will typically require very few clock cycles (likely just 1), as soon as memory is accessed, things get more complex and cache controllers / memory buses get involved and the operation is going to take considerably more time.

like image 29
Will A Avatar answered Dec 20 '22 09:12

Will A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!