Sub topic 6
Splitting an address into tag, index and offset
This chapter assumes you can convert between binary and hexadecimal, work with powers of two and base-2 logarithms, and know that memory is byte-addressable unless a question says otherwise. It also assumes you know what a cache block and a hit are from the earlier chapters.
Every cache lookup starts by cutting the memory address into three fields. The hardware does this with wires, not arithmetic. Getting the cut right is the first step of almost every GATE cache question.
What each field does
A cache stores copies of memory blocks. To find an address, the cache has to answer three questions:
- Which byte inside the block do we want? The offset answers this.
- Which cache line, or which set of lines, could hold the block? The index answers this.
- Of the many memory blocks that share that place, is this the one stored there? The tag answers this.
The fields sit in the address in this order, from the most significant bit to the least significant bit:
| Field | Position | Width | Purpose |
|---|---|---|---|
| Tag | High bits | address bits − index bits − offset bits | Identifies which block is stored |
| Index | Middle bits | log2(number of sets) | Selects the set (or line) |
| Offset | Low bits | log2(block size in bytes) | Selects the byte inside the block |
Computing the field widths
Only three numbers are needed: the address width, the block size, and the number of sets.
- Offset bits = log2(block size in bytes).
- Index bits = log2(number of sets).
- Tag bits = address bits − index bits − offset bits.
The number of sets comes from the cache geometry:
- Number of lines = cache size / block size.
- Number of sets = number of lines / associativity (the number of ways).
Associativity changes only the index width. The offset width depends on the block size alone. The tag takes whatever bits remain, so it grows by one bit each time the index shrinks by one bit.
A function that splits an address
This function performs the bit operations that the hardware does with wires.
def split_address(addr, block_size, num_sets):
# block_size and num_sets must be powers of two
offset_bits = block_size.bit_length() - 1
index_bits = num_sets.bit_length() - 1
offset = addr & (block_size - 1)
index = (addr >> offset_bits) & (num_sets - 1)
tag = addr >> (offset_bits + index_bits)
return tag, index, offset
# 16-byte blocks, 16 sets
print(split_address(0x1234, 16, 16)) # (18, 3, 4), i.e. tag=0x12, index=3, offset=4For a power of two n, n.bit_length() - 1 equals log2(n). The mask block_size - 1 is a run of ones as long as the offset field, so addr & mask keeps only the low bits. Shifting right drops the fields you have already used.
Here is the same split by hand. The address 0x1234 is 0001 0010 0011 0100 in binary. The block is 16 bytes, so the offset is the lowest 4 bits, 0100, which is 4. With 16 sets, the next 4 bits are the index, 0011, which is 3. The remaining bits 0001 0010 are the tag, 0x12 or 18.
A full worked example
Problem. A byte-addressable machine has 32-bit addresses. It has a 32 KB cache with 64-byte blocks and 4-way set-associativity. Find the field widths.
- Number of lines = 32768 / 64 = 512.
- Number of sets = 512 / 4 = 128.
- Offset bits = log2(64) = 6.
- Index bits = log2(128) = 7.
- Tag bits = 32 − 7 − 6 = 19.
The same cache can be organized in other ways. The table shows how the fields move.
| Organization | Ways | Sets | Offset | Index | Tag |
|---|---|---|---|---|---|
| Direct-mapped | 1 | 512 | 6 | 9 | 17 |
| 4-way set-associative | 4 | 128 | 6 | 7 | 19 |
| Fully associative | 512 | 1 | 6 | 0 | 26 |
A fully associative cache has one set, so it has no index field. Every block can go in any line, and the hardware compares the tag against every line. A direct-mapped cache has the most index bits and the fewest tag bits.
This function computes the geometry for any cache. It includes split_address so it runs on its own.
def split_address(addr, block_size, num_sets):
offset_bits = block_size.bit_length() - 1
index_bits = num_sets.bit_length() - 1
offset = addr & (block_size - 1)
index = (addr >> offset_bits) & (num_sets - 1)
tag = addr >> (offset_bits + index_bits)
return tag, index, offset
def geometry(addr_bits, cache_bytes, block_size, ways):
lines = cache_bytes // block_size
sets = lines // ways
offset_bits = block_size.bit_length() - 1
index_bits = sets.bit_length() - 1
tag_bits = addr_bits - index_bits - offset_bits
return {"lines": lines, "sets": sets, "offset": offset_bits,
"index": index_bits, "tag": tag_bits}
print(geometry(32, 32 * 1024, 64, 4))
# {'lines': 512, 'sets': 128, 'offset': 6, 'index': 7, 'tag': 19}
print(geometry(32, 32 * 1024, 64, 1)) # direct-mapped: index 9, tag 17
print(geometry(32, 32 * 1024, 64, 512)) # fully associative: index 0, tag 26Block number and set number
There is a second way to compute the same result, and GATE questions often phrase the problem this way.
- Block number = address // block size. This is the address with the offset bits removed.
- Set number = block number mod number of sets.
- Tag = block number // number of sets.
For direct-mapped caches the "set" is a single line, so the line number is the block number mod the number of lines. Taking the remainder by a power of two keeps the low bits, which is why the index is simply a slice of the address.
Consecutive blocks go to consecutive sets and wrap around. Take a direct-mapped cache with 4 lines and 1-byte blocks. Addresses 0 and 4 both have index 0 and tags 0 and 1. They compete for the same line, so they conflict even though three other lines may be empty. This is the weakness that associativity reduces.
Common errors
- Counting the wrong unit. Cache size is usually given in bytes (KB), block size in bytes, and addresses in bits. Convert everything to bytes before dividing, and apply log2 only at the end.
- Dividing by ways twice or not at all. Sets = lines / ways. If you use the number of lines as the number of sets for a 4-way cache, the index is two bits too wide.
- Forgetting the offset applies to bytes. If the block holds 16 words of 4 bytes each in a byte-addressable machine, the block is 64 bytes and the offset is 6 bits, not 4.
- Word-addressable memory. If the question says addresses identify words, not bytes, the offset counts words per block. A 16-bit word address with 8-word blocks has a 3-bit offset. Check the wording, because the same numbers give different answers.
- Using the total address width wrongly. Use the physical address width for a physically tagged cache. A question that gives a virtual address width and asks about a virtually indexed, virtually tagged cache uses the virtual width. Read which one the question means.
- Non-powers of two. The simple bit slicing only works when block size and number of sets are powers of two. GATE questions keep to this, so a result that is not a power of two means you made a mistake earlier.
- Mixing up conflict and tag. Two addresses with the same index but different tags map to the same set. Two addresses with the same tag and index but different offsets are in the same block, and the second access is a hit if the first brought the block in.
Using the split to detect conflicts
You can use the split to predict conflicts without simulating the whole cache. Take the 16-byte blocks and 16 sets from the earlier example. The addresses 0x1234 and 0x2234 have these fields:
- 0x1234: tag 0x12, index 3, offset 4.
- 0x2234: tag 0x22, index 3, offset 4.
Both map to set 3 but carry different tags. In a direct-mapped cache, alternating between them evicts the other each time, so every access misses. In a 2-way cache both fit in set 3 and every access after the first two hits. In general, addresses that differ by a multiple of (number of sets × block size) share a set. Here that distance is 16 × 16 = 256 = 0x100 bytes, and 0x2234 − 0x1234 = 0x1000 is a multiple of it.
Exercise
A byte-addressable machine has 32-bit addresses and a 16 KB cache with 32-byte blocks and 2-way set-associativity. How many bits are in the tag, index and offset? Then give the tag, index and offset of address 0x0001A4C7 as decimal or hexadecimal values.
Show answer
Lines = 16384 / 32 = 512. Sets = 512 / 2 = 256. Offset bits = log2(32) = 5, index bits = log2(256) = 8, tag bits = 32 − 8 − 5 = 19. For 0x0001A4C7, the low 5 bits are 0x07 (0xC7 = 1100 0111, low five bits 00111 = 7), so offset = 7. Shifting right by 5 gives 0x0001A4C7 >> 5 = 0xD26 (0x1A4C7 = 1 1010 0100 1100 0111, dropping 5 bits leaves 1101 0010 0110 = 0xD26). Index = low 8 bits of 0xD26 = 0x26 = 38. Tag = 0xD26 >> 8 = 0xD = 13.
Exercise
A word-addressable memory uses 20-bit word addresses. A direct-mapped cache has 256 lines and each block holds 8 words. How many bits are in the tag, index and offset, and which cache line does word address 0x0A3C5 map to?
Show answer
Offset bits = log2(8) = 3. Index bits = log2(256) = 8. Tag bits = 20 − 8 − 3 = 9. Word address 0x0A3C5 is 1010 0011 1100 0101 in binary. Block number = 0x0A3C5 >> 3 = 0x1478 (1010 0011 1100 0 = 1 0100 0111 1000). Line = block number mod 256 = 0x78 = 120.