Search code examples
cachingcpu-cache

Purpose of index bits in CPU caching


The current Wikipedia article about CPU caches says the following:

The index describes which cache row (which cache line) that the data has been put in.

I doubt that this is true, because as far as I've understood the way how caches work, the index bits select the set in which an item can be stored. A set consists of multiple cache lines (aka. cache blocks). The tag bits then are used to identify cache lines.

Am I right?


Solution

  • You are confusing yourself with 2 different mapping schemes. The term set is used only when mapping used is set-associative. It has no meaning if mapping used is direct mapping.These different mappings are clearly explained in the link you provided in your question.

    Please Note that the statement

    The index describes which cache row (which cache line) that the data has been put in.

    is For Direct mapping scheme, where the number of bits in the index field describe the number of rows in cache memory. Meaning if index field is of x bits then there are 2 to the power x rows in cache memory. And the offset is used to locate which byte we want to read in a particular row, in case our memory is byte addressable.

    For set-associative scheme, our entire cache memory is divided into sets. The field index now is no longer there but it is divided into two fields namely set and offset, where set describes which set we are concerned with and offset is used to specify which byte in a set we want in case the memory is byte addressable.