DFPC的BDI压缩代码详解

本文深入探讨了一种缓存行压缩算法的具体实现过程,该算法通过对数据块进行多级基底压缩,寻找最佳压缩方案,以减少内存占用。文章详细讲解了如何将数据从字节转换为单词,对比不同压缩策略,选择最优压缩结果,并将其应用到NVMain请求中。
bool FRFCFS::BDICompress (NVMainRequest *request, uint64_t _blockSize, bool flag )
{
    //blocksize就是req的data的size,一般是64
    uint64_t * values = convertByte2Word(request, flag, _blockSize, 8);
    //把64个指向uint8的转换成8个word,一个word64bits
    //也就是论文中说的8*8bytes
    uint64_t bestCSize = _blockSize;
    uint64_t currCSize = _blockSize;
    uint64_t i, pos, bestPos;
    uint64_t words[35];
    uint64_t wordPos[35]; //0~8 chars
    
    uint64_t currWords[35];
    uint64_t currWordPos[35]; //0~8 chars
    bool comFlag = false;
    bestPos = 16;
    //看一条cacheline内的差别,比较value[0]~value[8]
    if( isSameValuePackable( values, _blockSize / 8))
    {
        currCSize = 8;
    }
    if(bestCSize > currCSize)
    {
        //这里我也不太清楚到底是哪种,大概是全0或者全相同中的一种
        //bestPos就=2,word[1]为value高32bits,word[2]低32bits
        bestCSize = currCSize;
        bestPos = bestCSize / 4;
        //假设这个value 1-8是一样的, bestPos = 8/4 =2
        words[0] = 0x0;
        wordPos[0] = 1;
        for(i = 0; i < bestPos; i++)
        {
            words[i+1] = (values[i/2] >> (32*(1-i%2))) & 0xFFFFFFFF;
            wordPos[i+1] = 8;
        }
        //words[1] = value[0]高32位
        //words[2] = value[0]低32位
        bestPos++;
        //然后bestPos=3
    }
    //传入values,8,1,8,....
    //总共有size = 8个,blimit=1,bsize=8为不压缩的大小
    currCSize = multBaseCompression( values, _blockSize / 8, 1, 8, currWords, currWordPos, pos);
    if(bestCSize > currCSize)
    {
        bestCSize = currCSize;
        bestPos = pos;
        words[0] = 0x1;
        wordPos[0] = 1;
        for(i = 0; i < bestPos; i++)
        {
            words[i+1] = currWords[i];
            wordPos[i+1] = currWordPos[i];
        }
        bestPos++;
    }
    currCSize = multBaseCompression( values, _blockSize / 8, 2, 8, currWords, currWordPos, pos);
    if(bestCSize > currCSize)
    {
        bestCSize = currCSize;
        bestPos = pos;
        words[0] = 0x2;
        wordPos[0] = 1;
        for(i = 0; i < bestPos; i++)
        {
            words[i+1] = currWords[i];
            wordPos[i+1] = currWordPos[i];
        }
        bestPos++;
    }
    currCSize = multBaseCompression( values, _blockSize / 8, 4, 8, currWords, currWordPos, pos);
    if(bestCSize > currCSize)
    {
        bestCSize = currCSize;
        bestPos = pos;
        words[0] = 0x3;
        wordPos[0] = 1;
        for(i = 0; i < bestPos; i++)
        {
            words[i+1] = currWords[i];
            wordPos[i+1] = currWordPos[i];
        }
        bestPos++;
    }
    free(values);
    values = convertByte2Word(request, flag, _blockSize, 4);
    if( isSameValuePackable( values, _blockSize / 4))
    {
        currCSize = 4;
    }
    if(bestCSize > currCSize)
    {
        bestCSize = currCSize;
        bestPos = bestCSize / 4;
        words[0] = 0x4;
        wordPos[0] = 1;
        for(i = 0; i < bestPos; i++)
        {
            words[i+1] = (values[i/2] >> (32*(1-i%2))) & 0xFFFFFFFF;
            wordPos[i+1] = 8;
        }
        bestPos++;
    }
    currCSize = multBaseCompression( values, _blockSize / 4, 1, 4, currWords, currWordPos, pos);
    if(bestCSize > currCSize)
    {
        bestCSize = currCSize;
        bestPos = pos;
        words[0] = 0x5;
        wordPos[0] = 1;
        for(i = 0; i < bestPos; i++)
        {
            words[i+1] = currWords[i];
            wordPos[i+1] = currWordPos[i];
        }
        bestPos++;
    }
    currCSize = multBaseCompression( values, _blockSize / 4, 2, 4, currWords, currWordPos, pos);
    if(bestCSize > currCSize)
    {
        bestCSize = currCSize;
        bestPos = pos;
        words[0] = 0x6;
        wordPos[0] = 1;
        for(i = 0; i < bestPos; i++)
        {
            words[i+1] = currWords[i];
            wordPos[i+1] = currWordPos[i];
        }
        bestPos++;
    }
    free(values);
    values = convertByte2Word(request, flag, _blockSize, 2);
    currCSize = multBaseCompression( values, _blockSize / 2, 1, 2, currWords, currWordPos, pos);
    if(bestCSize > currCSize)
    {
        bestCSize = currCSize;
        bestPos = pos;
        words[0] = 0x7;
        wordPos[0] = 1;
        for(i = 0; i < bestPos; i++)
        {
            words[i+1] = currWords[i];
            wordPos[i+1] = currWordPos[i];
        }
        bestPos++;
    }
    free(values);
    values = NULL;
    
    if(bestCSize < _blockSize)
    {
        comFlag = true;
        Word2Byte(request, flag, bestPos, bestCSize, words, wordPos);
    }
    
    return comFlag;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值