2 * Copyright (c) 2018 Inria
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Daniel Carvalho
32 * Definition of a basic cache compressor.
35 #include "mem/cache/compressors/base.hh"
40 #include "debug/CacheComp.hh"
41 #include "mem/cache/tags/super_blk.hh"
42 #include "params/BaseCacheCompressor.hh"
44 // Uncomment this line if debugging compression
45 //#define DEBUG_COMPRESSION
47 BaseCacheCompressor::CompressionData::CompressionData()
52 BaseCacheCompressor::CompressionData::~CompressionData()
57 BaseCacheCompressor::CompressionData::setSizeBits(std::size_t size
)
63 BaseCacheCompressor::CompressionData::getSizeBits() const
69 BaseCacheCompressor::CompressionData::getSize() const
71 return std::ceil(_size
/8);
74 BaseCacheCompressor::BaseCacheCompressor(const Params
*p
)
75 : SimObject(p
), blkSize(p
->block_size
)
80 BaseCacheCompressor::compress(const uint64_t* data
, Cycles
& comp_lat
,
81 Cycles
& decomp_lat
, std::size_t& comp_size_bits
)
84 std::unique_ptr
<CompressionData
> comp_data
=
85 compress(data
, comp_lat
, decomp_lat
);
87 // If we are in debug mode apply decompression just after the compression.
88 // If the results do not match, we've got an error
89 #ifdef DEBUG_COMPRESSION
90 uint64_t decomp_data
[blkSize
/8];
92 // Apply decompression
93 decompress(comp_data
.get(), decomp_data
);
95 // Check if decompressed line matches original cache line
96 fatal_if(std::memcmp(data
, decomp_data
, blkSize
),
97 "Decompressed line does not match original line.");
100 // Get compression size
101 comp_size_bits
= comp_data
->getSizeBits();
103 // Print debug information
104 DPRINTF(CacheComp
, "Compressed cache line from %d to %d bits. " \
105 "Compression latency: %llu, decompression latency: %llu\n",
106 blkSize
*8, comp_size_bits
, comp_lat
, decomp_lat
);
110 BaseCacheCompressor::getDecompressionLatency(const CacheBlk
* blk
)
112 const CompressionBlk
* comp_blk
= static_cast<const CompressionBlk
*>(blk
);
114 // If block is compressed, return its decompression latency
115 if (comp_blk
&& comp_blk
->isCompressed()){
116 return comp_blk
->getDecompressionLatency();
119 // Block is not compressed, so there is no decompression latency
124 BaseCacheCompressor::setDecompressionLatency(CacheBlk
* blk
, const Cycles lat
)
127 assert(blk
!= nullptr);
130 static_cast<CompressionBlk
*>(blk
)->setDecompressionLatency(lat
);
134 BaseCacheCompressor::setSizeBits(CacheBlk
* blk
, const std::size_t size_bits
)
137 assert(blk
!= nullptr);
140 static_cast<CompressionBlk
*>(blk
)->setSizeBits(size_bits
);