Introduction
A cache holds far fewer blocks than main memory, so many memory blocks must share each cache location. The mapping function decides where a memory block may be placed, and therefore how the cache finds it again. Direct mapping allows exactly one line per block; fully associative mapping allows any line; -way set-associative mapping allows any of the lines of one set. The choice fixes how the address is split into tag, index and offset, how many tag comparators the hardware needs, how much tag storage the cache needs, and how many conflict misses a program suffers.
The calculations on this topic are address splits, tag-directory sizes, comparator counts, the line or set of a given address, and hit/miss traces. Policies (write, replacement) and the three Cs are in Cache Memory.
Prerequisites
- Cache Memory: Organization, Write and Replacement Policies: lines, tags, valid and dirty bits, LRU and FIFO, compulsory, capacity and conflict misses.
Core concepts
Notation and addressing convention used in all Gate CS Mastery notes. Memory is byte-addressable unless a question says word-addressable. is the number of physical address bits, the cache data capacity in bytes, the block size in bytes, the number of lines, the associativity and the number of sets. All of these are powers of 2. A memory block number is .
| Mapping | Where block may go | Address split (high to low) | Tag comparators |
|---|---|---|---|
| Direct mapped () | Line | tag line index offset | 1 |
| -way set-associative | Any line of set | tag set index offset | |
| Fully associative () | Any line | tag offset |
Field widths:
Equivalently, the tag width is of the number of memory blocks that share one set: . Doubling the associativity halves the number of sets, removes one index bit and adds one tag bit.
Word-addressable memory. If each address names a word of bytes, measure and in words: offset when is given in bytes.
Hardware for each mapping
- Direct mapped: the index drives a decoder that selects one line; one comparator checks the stored tag against the address tag; hit valid AND equal. Fast and cheap, but two blocks with the same index evict each other even when the rest of the cache is empty.
- Set-associative: the index selects a set; comparators check the tags in parallel; a -to-1 multiplexer selects the data of the matching way. A replacement policy chooses the victim within the set.
- Fully associative: every line's tag is compared at once (content-addressable memory), one comparator per line, each as wide as the tag. No conflict misses, but the most hardware and usually the slowest hit.
Storage overhead
The tag directory (tag memory) holds one tag per line:
"Cache size" in a question normally means the data capacity ; the metadata is extra.
How it works
Figure 1 shows the three splits for one cache. Figure 2 shows a 2-way set-associative lookup.


Algorithms and pseudocode
SPLIT(addr, B, S) // B = block bytes, S = number of sets
offset <- addr mod B
block <- floor(addr / B)
index <- block mod S // S = L for direct mapped, S = 1 for fully associative
tag <- floor(block / S)
return (tag, index, offset)
LOOKUP(addr)
(tag, index, offset) <- SPLIT(addr, B, S)
for way <- 1 to k // k comparators work in parallel
if V[index][way] = 1 and TAG[index][way] = tag then return HIT(way)
return MISSWith and powers of two, mod and floor are bit-field extractions: the offset is the low bits, the index the next bits, the tag the rest. Lookup time is one parallel comparison; hardware cost grows with .
Mathematical explanation
Why the tag has that width. All addresses with the same index and offset differ only in the tag. There are memory blocks that map to one set, and the tag must tell them apart, so it needs bits.
Blocks per line or set. Each set is shared by memory blocks; a direct-mapped line by .
Inverse problems. Given the tag width , the block size and : , so .
Comparator and multiplexer sizes. A -way cache needs comparators of tag width and a -to-1 multiplexer (as wide as the data returned). A direct-mapped cache needs one comparator and no way multiplexer.
Hit time. In a -way cache the data of the hit way can leave only after the parallel tag comparisons and the -to-1 multiplexer, so the hit latency is roughly one comparator delay plus one multiplexer delay. A direct-mapped cache can send its single line's data out while the tag is still being compared, which is why it hits fastest.
Replacement bits. A set-associative or fully associative cache adds replacement state per set: at least bits for exact LRU, or bits with an age counter per line (see Cache Memory). A direct-mapped cache needs none.
Worked examples
Example 1: address split (Figure 1)
32-bit byte addresses, KB bytes, bytes, so lines.
| Mapping | Sets | Offset | Index | Tag | Comparators | Tag directory |
|---|---|---|---|---|---|---|
| Direct mapped | 512 | 6 | 9 | 17 | 1 of 17 bits | bits |
| 4-way | 128 | 6 | 7 | 19 | 4 of 19 bits | bits |
| Fully associative | 1 | 6 | 0 | 26 | 512 of 26 bits | bits |
Adding a valid bit and a dirty bit per line to the direct-mapped cache gives metadata bits.
Example 2: which line, which tag
For the direct-mapped cache of Example 1, address 0x00001234: offset 0x34 , block 0x1234 (0x48), index , tag . Address 0x00009234 has block 584, index and tag 1. The two addresses compete for line 72: a program alternating between them misses every time in the direct-mapped cache, and hits after the first two misses in the 4-way cache, where both blocks fit in set .
Example 3: one trace, three mappings
A cache with 4 one-block lines; block trace 0, 4, 1, 0, 5, 4, 1, 0; LRU where there is a choice.
| Mapping | Placement | Hit/miss | Misses |
|---|---|---|---|
| Direct mapped | line : 0, 4 share line 0; 1, 5 share line 1 | M M M M M M M M | 8 |
| 2-way, 2 sets | set : 0, 4 in set 0; 1, 5 in set 1 | M M M H M H H H | 4 |
| Fully associative | any line | M M M H M H H H | 4 |
Only four distinct blocks are used, so 4 misses are compulsory; the 4 extra direct-mapped misses are conflict misses. Two ways per set are already enough here.
Example 4: array traversal
An int array A[64][64] (4-byte elements, row-major, starting at address 0) is read once in full by a cache of 4 KB with 64-byte blocks (64 lines, each block holds 16 elements).
- Row by row (
for i, for j): consecutive accesses fall in the same block, so there is 1 miss per 16 elements: misses in any of the mappings. - Column by column (
for j, for i) in the direct-mapped cache: is in block , so rows and map to the same line. A column touches 64 blocks but only 16 distinct lines, and each access evicts a block needed by the next column: all 4096 accesses miss. - The same column order in a fully associative 64-line LRU cache misses only 256 times: one column's 64 blocks fit exactly and are reused by the next 15 columns. So 3840 of the 4096 direct-mapped misses are conflict misses. Every associativity below fully associative (up to 32 ways) still misses on all 4096 accesses; only the fully associative 64-line cache gets 256. Block lands in set . For every from 1 to 16 the sets a column uses hold only 16 lines in total, and at 32 ways the column occupies a single set of 32 lines, so in every case the 64 blocks of a column still evict each other.
Example 5: inverse problem
A 4-way set-associative cache with 32-byte blocks on 32-bit addresses has 20-bit tags. Then , , and KB. If the same cache were direct mapped, the index would be 9 bits and the tag 18 bits.
Example 6: the complete tag directory
Take the 4-way cache of Example 1 (512 lines, 128 sets, 19-bit tags), write-back, with a 2-bit LRU age counter per line. Each line stores bits of metadata (tag, valid, dirty, age), so the directory holds bits. With the minimum exact LRU state of bits per set instead of counters, it holds bits. Either way the data array itself is bits.
Example 7: word-addressable memory
A machine has 16-bit addresses, each naming a 32-bit (4-byte) word. A direct-mapped cache holds 2 KB of data in 16-byte blocks.
- Block size in words: , so the offset is bits.
- Lines: , so the index is 7 bits.
- Tag: bits.
Using the byte offset would give a 5-bit tag: the wrong answer that a byte-addressing habit produces. Memory here is words KB.
Example 8: LRU against FIFO in a set-associative cache
A 2-way set-associative cache with 2 sets (set ), block trace 0, 4, 1, 0, 8, 5, 0, 4, 1, 8.
| Policy | Hit/miss | Misses |
|---|---|---|
| LRU | M M M H M M H M H M | 7 |
| FIFO | M M M H M M M M H M | 8 |
Set 1 sees only 1, 5, 1 and behaves the same under both policies. The difference is in set 0 when block 8 arrives, holding 0 and 4. Block 0 has just hit, so LRU evicts 4 and the next 0 hits; FIFO ignores that hit, evicts 0 (loaded first) and the next 0 misses.
Important properties and observations
- Index bits come from the number of sets, not lines. For a -way cache use .
- Associativity moves bits from index to tag: each doubling of adds one tag bit and doubles the comparators.
- Offset width depends only on the block size, not on the mapping.
- A fully associative cache has no index field and no conflict misses.
- Direct mapping hits fastest (one comparison, no way selection), which is why L1 caches are often direct mapped or low-associativity.
- Same index, different tag means a conflict: in a direct-mapped cache, two such blocks can never be resident together.
- Common misconceptions.
- "Tag bits for every mapping." That is direct mapping only; set-associative uses .
- "A fully associative cache needs one comparator." It needs one per line.
- "Higher associativity always lowers misses." It removes conflict misses only when the competing blocks fit in one set; Example 4 shows every associativity below fully associative (up to 32 ways) giving no gain on one access pattern.
GATE examination perspective
- UNITS FIRST. Byte or word addressable; KB means bytes; convert block and cache sizes to the addressing unit.
- SPLIT. Offset , index , tag the rest. Check that the three add up to .
- TAG DIRECTORY SIZE. Lines times tag bits; add valid, dirty and replacement bits only if asked.
- COMPARATORS AND MUX. comparators of tag width and a -to-1 multiplexer for -way; one comparator for direct mapped; for fully associative.
- LINE OF AN ADDRESS. Block number mod lines (or sets); in hexadecimal, read the bit fields directly.
- TRACES. Mark set by set; apply LRU within a set only.
- ARRAY LOOPS. Count blocks per row, and check which rows collide in the same line or set.
Summary
Sources
- C. Hamacher, Z. Vranesic, S. Zaky, Computer Organization, 5th ed., McGraw-Hill, 2002: Chapter 5, Sections 5.5.1 (Mapping Functions) and 5.5.3 (Example of Mapping Techniques).
- D. A. Patterson, J. L. Hennessy, Computer Organization and Design: The Hardware/Software Interface, 5th ed. (MIPS edition), Morgan Kaufmann, 2013: Sections 5.3 (The Basics of Caches) and 5.4 (Measuring and Improving Cache Performance).
- W. Stallings, Computer Organization and Architecture, 10th ed., Pearson: Section 4.3 (Elements of Cache Design).