如何计算 CPU 缓存的标记、索引字段的大小(以位为单位)?

发布于 2024-12-17 01:25:18 字数 500 浏览 5 评论 0原文

我正在编写一个 CPU 缓存模拟器,它将获取以字节为单位的缓存大小每个缓存行的长度(以字节为单位)以及组数/groups 在缓存中。

我已经写了大部分内容,但几个小时以来我一直在努力解决的是计算出需要左/右移动多少位才能提取标签索引 给定地址的字段。

例如,给定地址48,我需要确定标签和索引。

这是我提取标签的方法,但我很确定它是不正确的。

int extractTag(int address, int sets){

    int bits = exp2(sets); // number of bits to shift: 2^sets

    unsigned int tag;
    int tag = address >> (32 - bits);    

    return tag;
}

I'm writing a CPU cache emulator that will take the size of the cache in bytes, the length of each cache line in bytes, and the number of sets/groups in the cache.

I have most of it written, but what I've been struggling with for hours is to figure out how many bits I need to shift left/right to extract the tag and index fields of the given address.

For example, given the address 48, I need to determine the tag and index.

Here's what I have for extracting the tag, but I'm pretty sure it's incorrect.

int extractTag(int address, int sets){

    int bits = exp2(sets); // number of bits to shift: 2^sets

    unsigned int tag;
    int tag = address >> (32 - bits);    

    return tag;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦屿孤独相伴 2024-12-24 01:25:18

假设您有 SETS 组 BLOCK_SIZE 行。地址可以拆分为 tag:index:offset,其中 log2(BLOCK_SIZE) 位用于偏移量,log2(SETS) 位用于索引,其余位用于标记。

您可以这样计算 log2:

int ilog2 (int x)
{
    int result = 0;

    while (x != 0) {
        result++;
        x = x >> 1;
    }
    return result;
}

因此您最终得到:

int extract_tag (int address, int sets, int block_size)
{
    int offset_bits = ilog2(block_size);
    int index_bits = ilog2(sets);

    int tag = address >> (index_bits + offset_bits);
    return tag;
}

int extract_index (int address, int sets, int block_size)
{
    int offset_bits = ilog2(block_size);
    int index_bits = ilog2(sets);

    int index = (address >> offset_bits) & ((1 << index_bits) - 1);
    return index;
}

int extract_offset (int address, int sets, int block_size)
{
    int offset_bits = ilog2(block_size);

    int offset = address & ((1 << offset_bits) - 1);
    return offset;
}

Let's say you have SETS groups of BLOCK_SIZE lines. An address can be split in tag:index:offset with log2(BLOCK_SIZE) bits for the offset, log2(SETS) for the index and the rest for the tag.

You can calculate log2 like this:

int ilog2 (int x)
{
    int result = 0;

    while (x != 0) {
        result++;
        x = x >> 1;
    }
    return result;
}

Thus you end up with:

int extract_tag (int address, int sets, int block_size)
{
    int offset_bits = ilog2(block_size);
    int index_bits = ilog2(sets);

    int tag = address >> (index_bits + offset_bits);
    return tag;
}

int extract_index (int address, int sets, int block_size)
{
    int offset_bits = ilog2(block_size);
    int index_bits = ilog2(sets);

    int index = (address >> offset_bits) & ((1 << index_bits) - 1);
    return index;
}

int extract_offset (int address, int sets, int block_size)
{
    int offset_bits = ilog2(block_size);

    int offset = address & ((1 << offset_bits) - 1);
    return offset;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文