From: globalsk on
Hello,

I'm trying to understand this fragment of code in Simplescalar's
cache.c code (I've search and look over dozens sites, including
simplescalar.com and there's no help about the coding):

/* cache access macros */
#define CACHE_TAG(cp, addr) ((addr) >> (cp)->tag_shift)
#define CACHE_SET(cp, addr) (((addr) >> (cp)->set_shift) &
(cp)->set_mask)
#define CACHE_BLK(cp, addr) ((addr) & (cp)->blk_mask)
#define CACHE_TAGSET(cp, addr) ((addr) & (cp)->tagset_mask)

I know is about shifting and moving bits around, but don't know how and
specially why is done in this way and for what.

cp is an structure for a cache
addr is a typedef qword_t (64 bits)
md_addr_t blk_mask; idem, typedef qword_t
int set_shift;
md_addr_t set_mask; /* use *after* shift */
int tag_shift;
md_addr_t tag_mask; /* use *after* shift */
md_addr_t tagset_mask; /* used for fast hit detection */

Maybe this is little information, but maybe someone with the knowledge
about caches and this type of coding can help out. Please if someone
can give a helping hand about this and links where they explain in more
detail simplescalar's code will be very appreciated.

Please forgive my syntax mistakes, english is not my mother tongue.

From: globalsk on

Just in case, I'm asking this question for a project at my university,
so is nothing commercial.

On 9 ene, 10:30, "globalsk" <jmilana...(a)gmail.com> wrote:
> Hello,
>
> I'm trying to understand this fragment of code in Simplescalar's
> cache.c code (I've search and look over dozens sites, including
> simplescalar.com and there's no help about the coding):
>
> /* cache access macros */
> #define CACHE_TAG(cp, addr) ((addr) >> (cp)->tag_shift)
> #define CACHE_SET(cp, addr) (((addr) >> (cp)->set_shift) &
> (cp)->set_mask)
> #define CACHE_BLK(cp, addr) ((addr) & (cp)->blk_mask)
> #define CACHE_TAGSET(cp, addr) ((addr) & (cp)->tagset_mask)
>
> I know is about shifting and moving bits around, but don't know how and
> specially why is done in this way and for what.
>
> cp is an structure for a cache
> addr is a typedef qword_t (64 bits)
> md_addr_t blk_mask; idem, typedef qword_t
> int set_shift;
> md_addr_t set_mask; /* use *after* shift */
> int tag_shift;
> md_addr_t tag_mask; /* use *after* shift */
> md_addr_t tagset_mask; /* used for fast hit detection */
>
> Maybe this is little information, but maybe someone with the knowledge
> about caches and this type of coding can help out. Please if someone
> can give a helping hand about this and links where they explain in more
> detail simplescalar's code will be very appreciated.
>
> Please forgive my syntax mistakes, english is not my mother tongue.

From: user923005 on
Instead of trying to use something that you do not have documentation
for and you do not understand, why not try to use this, which is
documented:
http://www.danga.com/memcached/apis.bml

From: globalsk on
More insight about this topic and the core of the question relies in
this part of code also:

cp->blk_mask = bsize-1;
cp->set_shift = log_base2(bsize);
cp->set_mask = nsets-1;
cp->tag_shift = cp->set_shift + log_base2(nsets);
cp->tag_mask = (1 << (32 - cp->tag_shift))-1;
cp->tagset_mask = ~cp->blk_mask;

Don't understand how this masking works.
I know all this is later used for testing cache's hit or miss in a
block:

if (CACHE_TAGSET(cp, addr) == cp->last_tagset)
{
/* hit in the same block */
blk = cp->last_blk;
goto cache_fast_hit;
}),

But if someone can give examples with addresses and bits it will be
clearer.

All this code is for cache simulation as you can see, where you can
define associativity and all the regular parameters for a cache.

globalsk ha escrito:

> Hello,
>
> I'm trying to understand this fragment of code in Simplescalar's
> cache.c code (I've search and look over dozens sites, including
> simplescalar.com and there's no help about the coding):
>
> /* cache access macros */
> #define CACHE_TAG(cp, addr) ((addr) >> (cp)->tag_shift)
> #define CACHE_SET(cp, addr) (((addr) >> (cp)->set_shift) &
> (cp)->set_mask)
> #define CACHE_BLK(cp, addr) ((addr) & (cp)->blk_mask)
> #define CACHE_TAGSET(cp, addr) ((addr) & (cp)->tagset_mask)
>
> I know is about shifting and moving bits around, but don't know how and
> specially why is done in this way and for what.
>
> cp is an structure for a cache
> addr is a typedef qword_t (64 bits)
> md_addr_t blk_mask; idem, typedef qword_t
> int set_shift;
> md_addr_t set_mask; /* use *after* shift */
> int tag_shift;
> md_addr_t tag_mask; /* use *after* shift */
> md_addr_t tagset_mask; /* used for fast hit detection */
>
> Maybe this is little information, but maybe someone with the knowledge
> about caches and this type of coding can help out. Please if someone
> can give a helping hand about this and links where they explain in more
> detail simplescalar's code will be very appreciated.
>
> Please forgive my syntax mistakes, english is not my mother tongue.

From: globalsk on
More insight about this topic and the core of the question relies in
this part of code also:

cp->blk_mask = bsize-1;
cp->set_shift = log_base2(bsize);
cp->set_mask = nsets-1;
cp->tag_shift = cp->set_shift + log_base2(nsets);
cp->tag_mask = (1 << (32 - cp->tag_shift))-1;
cp->tagset_mask = ~cp->blk_mask;

Don't understand how this masking works.
I know all this is later used for testing cache's hit or miss in a
block:

if (CACHE_TAGSET(cp, addr) == cp->last_tagset)
{
/* hit in the same block */
blk = cp->last_blk;
goto cache_fast_hit;
}),

But if someone can give examples with addresses and bits it will be
clearer.

All this code is for cache simulation as you can see, where you can
define associativity and all the regular parameters for a cache.
globalsk ha escrito:

> Hello,
>
> I'm trying to understand this fragment of code in Simplescalar's
> cache.c code (I've search and look over dozens sites, including
> simplescalar.com and there's no help about the coding):
>
> /* cache access macros */
> #define CACHE_TAG(cp, addr) ((addr) >> (cp)->tag_shift)
> #define CACHE_SET(cp, addr) (((addr) >> (cp)->set_shift) &
> (cp)->set_mask)
> #define CACHE_BLK(cp, addr) ((addr) & (cp)->blk_mask)
> #define CACHE_TAGSET(cp, addr) ((addr) & (cp)->tagset_mask)
>
> I know is about shifting and moving bits around, but don't know how and
> specially why is done in this way and for what.
>
> cp is an structure for a cache
> addr is a typedef qword_t (64 bits)
> md_addr_t blk_mask; idem, typedef qword_t
> int set_shift;
> md_addr_t set_mask; /* use *after* shift */
> int tag_shift;
> md_addr_t tag_mask; /* use *after* shift */
> md_addr_t tagset_mask; /* used for fast hit detection */
>
> Maybe this is little information, but maybe someone with the knowledge
> about caches and this type of coding can help out. Please if someone
> can give a helping hand about this and links where they explain in more
> detail simplescalar's code will be very appreciated.
>
> Please forgive my syntax mistakes, english is not my mother tongue.