gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
huffman.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, 2020 Inria
3  * All rights reserved.
4  *
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.
15  *
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.
27  */
28 
30 
31 #include <cassert>
32 
33 #include "base/logging.hh"
34 
35 namespace gem5
36 {
37 
38 namespace compression
39 {
40 namespace encoder
41 {
42 
43 Huffman::Huffman(uint64_t max_code_length)
44  : Base(), maxCodeLength(max_code_length)
45 {
47  "Code length cannot surpass its underlying container");
48 }
49 
50 void
51 Huffman::sample(uint64_t value, uint64_t frequency)
52 {
53  if (frequency != 0) {
54  trees.push(new Node(value, frequency));
55  }
56 }
57 
58 std::unique_ptr<Huffman::Node>
60 {
61  // Construct tree by assigning left and right nodes. The left path leads
62  // to the most frequent values
63  while (trees.size() > 1) {
64  Node* left = trees.top();
65  trees.pop();
66 
67  Node* right = trees.top();
68  trees.pop();
69 
70  Node* parent = new Node(left, right);
71  trees.push(parent);
72  }
73 
74  // All queue entries have been merged into a single entry containing
75  // the tree
76  Node* root = trees.top();
77  trees.pop();
78  return std::unique_ptr<Node>(root);
79 }
80 
81 void
83 {
84  valueToCode.clear();
85  codeToValue.clear();
86  generateCodes(buildTree().get(), Code());
87 }
88 
89 void
90 Huffman::generateCodes(const Node* node, const Code& current_code)
91 {
92  // Drop all entries with length greater than maxCodeLength
93  if (current_code.length > maxCodeLength) {
94  return;
95  }
96 
97  if (node->isLeaf()) {
98  valueToCode[node->getValue()] = current_code;
99  codeToValue[current_code.code] = node->getValue();
100  } else {
101  Code right_code = current_code;
102  right_code.code = (right_code.code << 1) + 1;
103  right_code.length++;
104  generateCodes(node->getRightSubTree(), right_code);
105 
106  Code left_code = current_code;
107  left_code.code = left_code.code << 1;
108  left_code.length++;
109  generateCodes(node->getLeftSubTree(), left_code);
110  }
111 }
112 
113 Code
114 Huffman::encode(const uint64_t val) const
115 {
116  auto it = valueToCode.find(val);
117  if (it == valueToCode.end()) {
118  // If the value is unknown, generate a dummy code with invalid
119  // length to let the caller know the encoding is invalid
120  Code dummy_code;
121  dummy_code.code = 0;
122  dummy_code.length = 65;
123  return dummy_code;
124  } else {
125  return it->second;
126  }
127 }
128 
129 uint64_t
130 Huffman::decode(const uint64_t code) const
131 {
132  // A code that does not exist cannot be decoded
133  auto it = codeToValue.find(code);
134  assert(it != codeToValue.end());
135  return it->second;
136 }
137 
138 } // namespace encoder
139 } // namespace compression
140 } // namespace gem5
gem5::compression::encoder::Huffman::Node::isLeaf
bool isLeaf() const
Determine if the node is a leaf node by checking if it does not have sub-trees.
Definition: huffman.hh:115
gem5::compression::encoder::Huffman::Huffman
Huffman(uint64_t max_code_length)
Definition: huffman.cc:43
gem5::compression::encoder::Base
Base class for encoders.
Definition: base.hh:57
gem5::X86ISA::val
Bitfield< 63 > val
Definition: misc.hh:776
gem5::compression::encoder::Huffman::decode
uint64_t decode(const uint64_t code) const override
Decode a value.
Definition: huffman.cc:130
gem5::compression::encoder::Huffman::trees
std::priority_queue< Node *, std::vector< Node * >, NodeComparator > trees
Definition: huffman.hh:161
gem5::compression::encoder::Huffman::buildTree
std::unique_ptr< Node > buildTree()
Build a Huffman tree using the values and their respective frequencies, which have been informed thro...
Definition: huffman.cc:59
gem5::compression::encoder::Code::code
uint64_t code
Only the LSB of the code are relevant.
Definition: base.hh:47
gem5::compression::encoder::Code
Definition: base.hh:44
gem5::compression::encoder::Huffman::codeToValue
std::map< uint64_t, uint64_t > codeToValue
Definition: huffman.hh:147
gem5::compression::encoder::Huffman::encode
Code encode(const uint64_t val) const override
The function responsible for the generation of the alternative value.
Definition: huffman.cc:114
gem5::compression::encoder::Huffman::valueToCode
std::map< uint64_t, Code > valueToCode
Table containing the codewords and their respective lengths.
Definition: huffman.hh:146
gem5::compression::encoder::Huffman::Node::getRightSubTree
const Node * getRightSubTree() const
Definition: huffman.hh:133
gem5::compression::encoder::Huffman::maxCodeLength
const unsigned maxCodeLength
Maximum number of bits in a codeword.
Definition: huffman.hh:140
gem5::compression::encoder::Huffman::generateCodes
void generateCodes(const Node *node, const Code &current_code)
Recursive function that generates the huffman codes based on the tree provided.
Definition: huffman.cc:90
gem5::compression::encoder::Huffman::sample
void sample(uint64_t value, uint64_t frequency)
Inserts the value-frequency pair in the tree.
Definition: huffman.cc:51
gem5::compression::encoder::Code::length
unsigned length
Number of bits in the code.
Definition: base.hh:49
gem5::compression::encoder::Huffman::Node::getLeftSubTree
const Node * getLeftSubTree() const
Definition: huffman.hh:132
logging.hh
huffman.hh
fatal_if
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:236
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::compression::encoder::Huffman::generateCodeMaps
void generateCodeMaps()
Generation of the code maps.
Definition: huffman.cc:82
gem5::compression::encoder::Huffman::Node
Node for the Huffman tree.
Definition: huffman.hh:76
gem5::compression::encoder::Huffman::Node::getValue
uint64_t getValue() const
Get the leaf's value.
Definition: huffman.hh:126

Generated on Sun Jul 30 2023 01:56:57 for gem5 by doxygen 1.8.17