I’m guessing that cacheconfig.numLines
, cacheconfig.associativity
and cacheconfig.lineSize
are of the type unsigned int
. If they’re not, maybe you’ll need a cast somewhere, or you can change them to be unsigned
.
The first step is to arrange these common subexpressions in one place, and thereby define the variables a
and b
:
unsigned int a = log2(cacheconfig.numLines / cacheconfig.associativity);
unsigned int b = log2(cacheconfig.lineSize);
string binario = bitset<256>(address).to_string();
int InicioDado = 256 - b;
string dado = binario.substr(InicioDado, b);
int InicioIndex = InicioDado - a;
string index = binario.substr(InicioIndex, a);
int InicioTag = 256 - cacheconfig.lineSize;
string tag = binario.substr(InicioTag, 256 - InicioTag - b);
long unsigned int Tag = bitset<256>(tag).to_ulong();
long unsigned int Index = bitset<256>(index).to_ulong();
long unsigned int Dado = bitset<256>(dado).to_ulong();
Let’s eliminate the variables of the type int
intermediaries:
unsigned int a = log2(cacheconfig.numLines / cacheconfig.associativity);
unsigned int b = log2(cacheconfig.lineSize);
string binario = bitset<256>(address).to_string();
string dado = binario.substr(256 - b, b);
string index = binario.substr(256 - b - a, a);
string tag = binario.substr(256 - cacheconfig.lineSize, cacheconfig.lineSize - b);
long unsigned int Tag = bitset<256>(tag).to_ulong();
long unsigned int Index = bitset<256>(index).to_ulong();
long unsigned int Dado = bitset<256>(dado).to_ulong();
Now, let’s see what this does. You take the address
, converts to a binary string using bitset
, Insert this string into some parts and use the bitset
to convert these binary substrings to numbers later. Since this is a numerical calculation, it does not seem ideal to use strings in it. This seems to be a task for bitwise operators <<
, >>
and &
.
First you take the last b
bits, which are the Dado
. The a
previous bits are the Index
. The Tag
It seems a little strange, but it’s the last cacheconfig.lineSize
bits with the exception of the last b
bits. This is strange because there is an intersection between the Tag
and the Index
.
Well, to get the last k
bits of a number x
, being k >= 1
, we can do this:
x & ((1 << k) - 1)
To discard the last k
digits, we do that:
x >> k
Let’s create macros to make it easier (adding the parentheses around each parameter and macro as a whole to be hygienic):
#define OBTER_ULTIMOS_BITS(x, k) ((x) & ((1 << (k)) - 1))
#define DESCARTAR_ULTIMOS_BITS(x, k) ((x) >> (k))
So your code stays like this:
unsigned int a = log2(cacheconfig.numLines / cacheconfig.associativity);
unsigned int b = log2(cacheconfig.lineSize);
long unsigned int Tag = DESCARTAR_ULTIMOS_BITS(OBTER_ULTIMOS_BITS(address, cacheconfig.lineSize), b);
long unsigned int Index = DESCARTAR_ULTIMOS_BITS(OBTER_ULTIMOS_BITS(address, b + a), b);
long unsigned int Dado = OBTER_ULTIMOS_BITS(address, b);
And this code is the same in both C and C++.
Maybe you’ll still need a function that (re)defines your log2
. Here follows an implementation that I copied from that reply. Only include it if you cannot for some reason use the default implementation:
unsigned int log2(unsigned int x) {
unsigned int resultado = 0;
while (x >>= 1) resultado++;
return resultado;
}
Actually, the code compiled without errors... However, the program did not run... I did a check with a printf on the variable a and the problem starts there... the program enters a "long but finite loop" which obviously does not return the correct final value. What can it be?
– Oberdan Santos
@Oberdansantos Give some examples of values for
address
,cacheconfig.numLines
,cacheconfig.associativity
andcacheconfig.lineSize
that reproduce this problem so I can test it here.– Victor Stafusa
I can send you an email with the . zip of the archives?
– Oberdan Santos
@Oberdansantos Can, but I will not have to see today if it is more complicated. Anyway I put a example in C++ and an example in C with the changes I made and the results of both coincide. So I asked for examples of values to understand what is wrong.
– Victor Stafusa
passes your email then, please. I needed it today still, but I’m sure it’s something tiny that I’m missing... it’s worth a try.
– Oberdan Santos
@Oberdansantos victorwssilva arroba gmail point com
– Victor Stafusa