2 * Copyright (c) 2018-2019 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 CPack compression, from "C-Pack: A High-Performance
33 * Microprocessor Cache Compression Algorithm".
36 #ifndef __MEM_CACHE_COMPRESSORS_CPACK_HH__
37 #define __MEM_CACHE_COMPRESSORS_CPACK_HH__
43 #include "base/types.hh"
44 #include "mem/cache/compressors/dictionary_compressor.hh"
48 class CPack : public DictionaryCompressor<uint32_t>
51 using DictionaryEntry = DictionaryCompressor<uint32_t>::DictionaryEntry;
53 // Forward declaration of all possible patterns
62 * The patterns proposed in the paper. Each letter represents a byte:
63 * Z is a null byte, M is a dictionary match, X is a new value.
64 * These are used as indexes to reference the pattern data. If a new
65 * pattern is added, it must be done before NUM_PATTERNS.
68 ZZZZ, XXXX, MMMM, MMXX, ZZZX, MMMX, NUM_PATTERNS
72 * Convenience factory declaration. The templates must be organized by
73 * size, with the smallest first, and "no-match" last.
75 using PatternFactory = Factory<PatternZZZZ, PatternMMMM, PatternZZZX,
76 PatternMMMX, PatternMMXX, PatternXXXX>;
78 uint64_t getNumPatterns() const override { return NUM_PATTERNS; }
81 getName(int number) const override
83 static std::map<int, std::string> patternNames = {
84 {ZZZZ, "ZZZZ"}, {XXXX, "XXXX"}, {MMMM, "MMMM"},
85 {MMXX, "MMXX"}, {ZZZX, "ZZZX"}, {MMMX, "MMMX"}
88 return patternNames[number];
91 std::unique_ptr<Pattern> getPattern(
92 const DictionaryEntry& bytes,
93 const DictionaryEntry& dict_bytes,
94 const int match_location) const override
96 return PatternFactory::getPattern(bytes, dict_bytes, match_location);
99 void addToDictionary(DictionaryEntry data) override;
104 * @param data The cache line to be compressed.
105 * @param comp_lat Compression latency in number of cycles.
106 * @param decomp_lat Decompression latency in number of cycles.
107 * @return Cache line after compression.
109 std::unique_ptr<BaseCacheCompressor::CompressionData> compress(
110 const uint64_t* data, Cycles& comp_lat, Cycles& decomp_lat) override;
113 /** Convenience typedef. */
114 typedef CPackParams Params;
117 * Default constructor.
119 CPack(const Params *p);
122 * Default destructor.
127 class CPack::PatternZZZZ : public DictionaryCompressor::Pattern
130 PatternZZZZ(const DictionaryEntry bytes, const int match_location)
131 : Pattern(ZZZZ, 0x0, 2, 0, 0, false) {}
133 static bool isPattern(const DictionaryEntry& bytes,
134 const DictionaryEntry& dict_bytes,
135 const int match_location)
137 return (bytes[3] == 0) && (bytes[2] == 0) && (bytes[1] == 0) &&
142 decompress(const DictionaryEntry dict_bytes) const override
148 class CPack::PatternXXXX : public UncompressedPattern
151 PatternXXXX(const DictionaryEntry bytes, const int match_location)
152 : UncompressedPattern(XXXX, 0x1, 2, match_location, bytes)
157 class CPack::PatternMMMM : public MaskedPattern<0xFFFFFFFF>
160 PatternMMMM(const DictionaryEntry bytes, const int match_location)
161 : MaskedPattern<0xFFFFFFFF>(MMMM, 0x2, 6, match_location, bytes, true)
166 class CPack::PatternMMXX : public MaskedPattern<0xFFFF0000>
169 PatternMMXX(const DictionaryEntry bytes, const int match_location)
170 : MaskedPattern<0xFFFF0000>(MMXX, 0xC, 8, match_location, bytes, true)
175 class CPack::PatternZZZX : public DictionaryCompressor::Pattern
179 * A copy of the unmatched byte.
184 PatternZZZX(const DictionaryEntry bytes, const int match_location)
185 : Pattern(ZZZX, 0xD, 4, 1, 0, false), byte(bytes[0]) {}
187 static bool isPattern(const DictionaryEntry& bytes,
188 const DictionaryEntry& dict_bytes,
189 const int match_location)
191 return (bytes[3] == 0) && (bytes[2] == 0) && (bytes[1] == 0) &&
196 decompress(const DictionaryEntry dict_bytes) const override
198 return {byte, 0, 0, 0};
202 class CPack::PatternMMMX : public MaskedPattern<0xFFFFFF00>
205 PatternMMMX(const DictionaryEntry bytes, const int match_location)
206 : MaskedPattern<0xFFFFFF00>(MMMX, 0xE, 8, match_location, bytes, true)
211 #endif //__MEM_CACHE_COMPRESSORS_CPACK_HH__