Cache Organization: Tags, Indexes and Offsets
A cache finds a block in one step by cutting its address into three fields: the low bits (offset) pick the byte inside the block, the middle bits (index) pick the set to look in, and the high bits (tag) are stored to check it's the right block. The field sizes come straight from powers of two.
What the symbols mean
- Offset bits
Bits that pick a byte inside a block.
- Block size
Bytes per block, a power of two.
- Index bits
Bits that pick which set to look in.
- Number of sets
How many sets the cache has, a power of two.
- Tag bits
The rest of the address, stored with each block to identify it.
- Address bits
Bits in a whole address, like 32 or 48.
What the symbols mean
- Capacity
Bytes of data the cache holds.
- Associativity
Blocks per set (ways). k = 1 is direct-mapped; one set holding every block is fully associative.
What the symbols mean
- Total storage
All the bits the cache really needs.
- Status bits
Per block: a valid bit, plus a dirty bit if the cache uses one.
Explanation
Cache addressing is simply the rule a cache uses to decide where a block of memory lives inside it, using nothing but the bits of the address. The trick is to cut the address into three fields, read from left to right: tag | index | offset. The offset says which byte inside the block you want, the index says which slot of the cache (called a set) to look in, and the tag is everything left over, stored next to the block so the cache can tell whether the block sitting in that slot is really the one you asked for. Three fields, three questions: which byte, which set, which block. It's bookkeeping, but it's the bookkeeping that lets a cache find your data in one step instead of searching all of it.
The offset: which byte in the block. A block is bytes, and is a power of two, say . To name one of bytes you need bits, so there are offset bits, and they're the lowest bits of the address. In arithmetic terms that's just the remainder, offset address . Taking a number mod a power of two keeps its low bits, which is a mask with AND (see Binary and Hexadecimal and Bitwise Operations).
The index: which set. Shift the offset bits away (divide by ) and you get the block number, which block of memory the byte lives in. If the cache has sets, block number picks the set. With that's simply the next bits up, so . Neighboring blocks land in neighboring sets, which is exactly what spatial locality wants: an array walks through the sets one after another instead of piling into one.
The tag: is it really my block? Many memory blocks share each set, all the block numbers with the same remainder mod . So the cache stores the remaining high bits, the tag, with each block. A lookup goes straight to the set the index names and compares tags: a match (with the valid bit on) is a hit. With -bit addresses that leaves tag bits.
Think about why this split feels natural. It's the same as writing a number in a mixed base:
That's exactly like writing seconds as hour, minutes and seconds. Because and are powers of two, the "digits" are just groups of bits, and the hardware gets them for free by wiring, with no division at all.
How many sets? Associativity. A cache with bytes of data holds blocks. They're grouped into sets of blocks each ( ways), so .
- Direct-mapped (): each block has exactly one place it can go. Fast and simple, but two blocks that share an index keep kicking each other out (conflict misses), even if the rest of the cache is empty.
- -way set associative: a block can go in any of the places in its set, and the cache compares tags at once.
- Fully associative: a single set holding everything, so , , and the whole block number is the tag. No conflicts, but every lookup compares against every tag, so only small structures (like the TLB in the next lesson) work this way.
Every doubling of halves , so one bit moves from the index into the tag. The offset never changes; it depends only on the block size.
What it costs: tag storage. A cache stores more than data. Each block carries its tag and a valid bit (often a dirty bit too, meaning the block was written to and must be copied back to memory before it's thrown out). So the real storage is bits. A "32 KiB cache" means 32 KiB of data; the actual memory on the chip is a few percent bigger.
A common mistake. It's tempting to compute the number of sets as and stop. That's the number of blocks. Divide by the associativity too: a 32 KiB, 4-way cache with 64-byte blocks has blocks but only sets, so index bits, not . Another one is counting the offset in words instead of bytes. Addresses count bytes, so a 64-byte block needs offset bits even if it holds sixteen 4-byte words.
There's a picture further down where you can slide from direct-mapped to fully associative and watch the bits move.
See it
Worked examples
A KiB direct-mapped cache has -byte blocks, and addresses are bits. How many bits go to the tag, the index and the offset? Where does the address go?
- 1
Start with the offset, since it depends only on the block size. A block is bytes, so naming one byte inside it takes bits.
A KiB, -way set-associative cache has -byte blocks and -bit addresses. Each block also has a valid bit. Find the tag, index and offset bits and the total storage in bits, tags included.
- 1
Offset: , so .
Practice
Type your answer and check it. Solve them all to complete the lesson.
A KiB direct-mapped cache has -byte blocks and -bit addresses. How many tag, index and offset bits are there? Answer in that order.
A KiB, -way set-associative cache has -byte blocks, and addresses are bits. How many tag bits does each block need?
A fully associative cache has -byte blocks and -bit addresses. How many tag bits does each block need?
A direct-mapped cache has -byte blocks and sets. Which set (in decimal) does the address map to?
A program alternates between reading and , and the two arrays start exactly KiB apart in memory. The cache is KiB and direct-mapped. What happens?
A KiB direct-mapped cache has -byte blocks, -bit addresses and one valid bit per block. How many bits of storage does it need in total, tags included?
A small microcontroller uses -bit addresses and has a KiB, -way set-associative cache with -byte blocks. How many different blocks of memory compete for each set?
A KiB direct-mapped cache has -byte blocks. A loop sums an array of four-byte ints ( KiB) twice, in order, starting from an empty cache. What's the hit rate over both passes? (Uses Locality.)
The hardest one here. Get it, and you've got this lesson.
A cache on -bit addresses uses -bit tags, is -way set associative and has -byte blocks. Each block also stores a valid bit and a dirty bit. (a) How many KiB of data does it hold? (b) How many bits does it need in total? Answer in that order.
Where it's used
These bit fields are wired into every processor: the index bits literally select a row of the cache's memory array, and the tag comparators sit beside it. Knowing the split explains real performance surprises. Arrays whose sizes are large powers of two (say, rows of floats) can map to the same few sets and thrash even a big cache, which is why numerical libraries sometimes pad rows by a few elements. Operating systems use the same field arithmetic for "page coloring," placing pages so they don't fight over the same cache sets, and hardware designers trade associativity (fewer conflicts) against the power cost of comparing more tags on every access.