高速 | m | C | B | E | S | t | s | b |
1 | 32 | 2048 | 4 | 4 | 128 | 23 | 7 | 2 |
2 | 32 | 2048 | 4 | 512 | 1 | 30 | 0 | 2 |
3 | 32 | 2048 | 8 | 1 | 256 | 21 | 8 | 3 |
4 | 32 | 2048 | 8 | 128 | 2 | 28 | 1 | 3 |
5 | 32 | 2048 | 32 | 1 | 64 | 21 | 6 | 5 |
6 | 32 | 2048 | 32 | 4 | 16 | 23 | 4 | 5 |
高速 | m | C | B | E | S | t | s | b |
1 | 32 | 8192 | 16 | 1 | 512 | 19 | 9 | 4 |
2 | 32 | 4096 | 4 | 4 | 256 | 22 | 8 | 2 |
3 | 32 | 4096 | 4 | 8 | 128 | 23 | 7 | 2 |
4 | 32 | 2048 | 32 | 4 | 16 | 22 | 4 | 5 |
| 行1 | 行2 |
组0 | 0x0120-0x0123 | - |
组1 | 0x08A4-0x08A7 | 0x0704-0x0707 |
组2 | - | - |
组3 | - | 0x064C-0x064F |
组4 | 0x18F0-0x18F3 | 0x00B0-0x00B3 |
组5 | 0x0E34-0x0E37 | - |
组6 | 0x1238-0x123B | - |
组7 | - | 0x1BDC-0x1BDF |
11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
CT | CT | CT | CT | CT | CT | CT | CT | CI | CI | CO | CO |
操作 | 地址 | 命中? | 读出的值 |
读 | 0x409 | No | - |
写 | 0x40A | Yes | - |
读 | 0x833 | Yes | 0xD0 |
12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
CT | CT | CT | CT | CT | CT | CT | CT | CI | CI | CI | CO | CO |
12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
0 | 0 | 1 | 1 | 1 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 |
参数 | 值 |
块偏移CO | 0x00 |
索引CI | 0x6 |
标记CT | 0x38 |
命中? | Yes |
返回值 | 0xFA |
12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
1 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 1 | 1 | 0 | 0 |
参数 | 值 |
块偏移CO | 0x00 |
索引CI | 0x3 |
标记CT | 0xB7 |
命中? | No |
返回值 | - |
dst数组 | ||||
| 列0 | 列1 | 列2 | 列3 |
行0 | m | h | m | h |
行1 | m | m | h | m |
行2 | m | h | m | h |
行3 | m | m | h | m |
src数组 | ||||
| 列0 | 列1 | 列2 | 列3 |
行0 | m | m | m | m |
行1 | m | m | m | m |
行2 | m | m | m | m |
行3 | m | m | m | m |
dst数组 | ||||
| 列0 | 列1 | 列2 | 列3 |
行0 | m | h | h | h |
行1 | m | h | h | h |
行2 | m | h | h | h |
行3 | m | h | h | h |
src数组 | ||||
| 列0 | 列1 | 列2 | 列3 |
行0 | m | h | h | h |
行1 | m | h | h | h |
行2 | m | h | h | h |
行3 | m | h | h | h |
函数 | N=64 | N=60 |
sumA | 0.25 | 0.25 |
sumB | 1 | 0.25 |
sumC | 0.5 | 0.25 |
{
return head + i*N*4 + j*4;
}
unsigned calc_line(unsigned add, int block_size, const int line)
{
return (add / block_size) % line;
}
int isHit(unsigned cache[], unsigned r, unsigned a, int block_size)
{
a = a / block_size * block_size;
unsigned tmp = cache[r];
cache[r] = a;
return tmp == a;
}
void testA(int block_size, const int line, unsigned add, const int N)
{
unsigned cache[line];
memset(cache, -1, sizeof(cache));
int hit = 0, miss = 0;
for(int i=0; i<N; ++i)
for(int j=0; j<N; ++j)
{
unsigned address = calc_address(add, i, j, N);
unsigned r = calc_line(address, block_size, line);
hit += isHit(cache, r, address, block_size);
}
miss = N*N - hit;
cout << "sumA " << hit << " " << miss << endl;
cout << (double)miss/(N*N) << endl;
}
void testB(int block_size, const int line, unsigned add, const int N)
{
unsigned cache[line];
memset(cache, -1, sizeof(cache));
int hit = 0, miss = 0;
for(int j=0; j<N; ++j)
for(int i=0; i<N; ++i)
{
unsigned address = calc_address(add, i, j, N);
unsigned r = calc_line(address, block_size, line);
hit += isHit(cache, r, address, block_size);
}
miss = N*N - hit;
cout << "sumB " << hit << " " << miss << endl;
cout << (double)miss/(N*N) << endl;
}
void testC(int block_size, const int line, unsigned add, const int N)
{
unsigned cache[line];
memset(cache, -1, sizeof(cache));
int hit = 0, miss = 0;
for(int j=0; j<N; j+=2)
for(int i=0; i<N; i+=2)
{
unsigned a, r;
a = calc_address(add, i, j, N);
r = calc_line(a, block_size, line);
hit += isHit(cache, r, a, block_size);
a = calc_address(add, i+1, j, N);
r = calc_line(a, block_size, line);
hit += isHit(cache, r, a, block_size);
a = calc_address(add, i, j+1, N);
r = calc_line(a, block_size, line);
hit += isHit(cache, r, a, block_size);
a = calc_address(add, i+1, j+1, N);
r = calc_line(a, block_size, line);
hit += isHit(cache, r, a, block_size);
}
miss = N*N - hit;
cout << "sumC " << hit << " " << miss << endl;
cout << (double)miss/(N*N) << endl;
}
void testHit()
{
const unsigned start_address = 0x08000000;
const int cache_size = 4*1024;
const int block_size = 16;
int line = cache_size / block_size;
int N = 64;
cout << N << endl;
testA(block_size, line, start_address, N);
testB(block_size, line, start_address, N);
testC(block_size, line, start_address, N);
cout << "-----" << endl;
N = 60;
cout << N << endl;
testA(block_size, line, start_address, N);
testB(block_size, line, start_address, N);
testC(block_size, line, start_address, N);
}