# CS:APP Ch6 Memory Hierarchy Ex. 6.10 解析 ## Presumption * float is 4 bytes * x is loaded to a contiguous memory address start from address 0 * y starts from address 32, * assume a block is 16 bytes (sufficient to hold 4 float variables) * Cache: * 2 sets * Capacity = 32 bytes ### Compute s, e, b from `(S,E,B,m) = (2, 1, 16, 32)` * Capacity `(C)`: * C = S * E * B * 64 bytes = 2 * E * 16bytes * `E = 2` * sets: * S = 2^s^ = 2, * so s = 1 * blocks: * B = 2^b^ = 16, * so b = 4 * tag: * s + b + t = m, * 1 + 4 + t = 32, * so, t = 27 Memory layout: ```bash addr. 32: [0000...01 0 0000] |_______| |_| |__| t s b is at set 0 ``` ## Code ```c= float dotprod(float x[8], float y[8]) { float sum = 0.0; int i; for (i = 0; i < 8; i++) sum += x[i] * y [i]; return sum; } ``` ## Before padding | n | addr (DEC) | set | | addr (DEC) | set | |------|------------|-----|------|------------|-----| | x[0] | 0 | 0 | y[0] | 32 | 0 | | x[1] | 4 | 0 | y[1] | 36 | 0 | | x[2] | 8 | 0 | y[2] | 40 | 0 | | x[3] | 12 | 0 | y[3] | 44 | 0 | | x[4] | 16 | 1 | y[4] | 48 | 1 | | x[5] | 20 | 1 | y[5] | 52 | 1 | | x[6] | 24 | 1 | y[6] | 56 | 1 | | x[7] | 28 | 1 | y[7] | 60 | 1 | ## After padding (`x[8]` - `x[11]`) * :question: Why we need padding? * Because we want the term * `sum += x[i] * y [i];` * `x[i] and y[i]` not in the same set | n | addr (DEC) | set | | addr (DEC) | set | |------|------------|-----|------|------------|-----| | x[0] | 0 | 0 | y[0] | 48 | 1 | | x[1] | 4 | 0 | y[1] | 52 | 1 | | x[2] | 8 | 0 | y[2] | 56 | 1 | | x[3] | 12 | 0 | y[3] | 60 | 1 | | x[4] | 16 | 1 | y[4] | 64 | 0 | | x[5] | 20 | 1 | y[5] | 68 | 0 | | x[6] | 24 | 1 | y[6] | 72 | 0 | | x[7] | 28 | 1 | y[7] | 76 | 0 | | x[8] | 32 | 0 | x[9] | 36 | 0 | x[10] | 40 | 0 | x[11] | 44 | 0 * After padding, for example, if we do