如何计算 CPU 缓存的标记、索引字段的大小(以位为单位)?
我正在编写一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您有 SETS 组 BLOCK_SIZE 行。地址可以拆分为 tag:index:offset,其中 log2(BLOCK_SIZE) 位用于偏移量,log2(SETS) 位用于索引,其余位用于标记。
您可以这样计算 log2:
因此您最终得到:
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:
Thus you end up with: