P4C
The P4 Compiler
Loading...
Searching...
No Matches
hash_function.h
1
19#ifndef BF_P4C_MAU_HASH_FUNCTION_H_
20#define BF_P4C_MAU_HASH_FUNCTION_H_
21
23
24namespace P4 {
25namespace IR {
26namespace MAU {
27
29 Util::SourceInfo srcInfo;
30 enum { IDENTITY, CSUM, XOR, CRC, RANDOM } type = IDENTITY;
31 bool msb = false; // pull msbs for slice
32 bool extend = false; // If not otherwise specified, extend is true for crc
33 bool reverse = false; // crc reverse bits
34 int size = 0;
35 uint64_t poly = 0; // crc polynomial in koopman form (poly-1)/2
36 uint64_t init = 0;
37 uint64_t final_xor = 0;
38
39 bool operator==(const HashFunction &a) const {
40 return type == a.type && size == a.size && msb == a.msb && reverse == a.reverse &&
41 poly == a.poly && init == a.init && final_xor == a.final_xor;
42 }
43 bool operator!=(const HashFunction &a) const { return !(*this == a); }
44 void toJSON(JSONGenerator &json) const;
45 static HashFunction *fromJSON(JSONLoader &);
46 bool setup(const IR::Expression *exp);
47 bool convertPolynomialExtern(const IR::GlobalRef *);
48 static HashFunction identity() {
49 HashFunction rv;
50 rv.type = IDENTITY;
51 return rv;
52 }
53 static HashFunction random() {
54 HashFunction rv;
55 rv.type = RANDOM;
56 return rv;
57 }
58 bool size_from_algorithm() const {
59 if (type == CRC) return true;
60 return false;
61 }
62 bool ordered() const { return type != RANDOM; }
63
64 std::string name() const {
65 std::string algo_name = "";
66 if (type == IDENTITY) {
67 algo_name = "identity";
68 } else if (type == CRC) {
69 algo_name = "crc_" + std::to_string(size);
70 } else if (type == RANDOM) {
71 algo_name = "random";
72 } else if (type == XOR) {
73 algo_name = "xor";
74 }
75 return algo_name;
76 }
77
78 std::string algo_type() const {
79 std::string algo_type = "";
80 if (type == IDENTITY) {
81 algo_type = "identity";
82 } else if (type == CRC) {
83 algo_type = "crc";
84 } else if (type == RANDOM) {
85 algo_type = "random";
86 } else if (type == XOR) {
87 algo_type = "xor";
88 }
89 return algo_type;
90 }
91
92 private:
93 const IR::MethodCallExpression *hash_to_mce(const IR::Expression *);
94 uint64_t toKoopman(uint64_t poly, uint32_t width);
95
96 public:
97 void build_algorithm_t(bfn_hash_algorithm_ *) const;
98 static const IR::Expression *convertHashAlgorithmBFN(Util::SourceInfo srcInfo,
99 IR::ID algorithm);
100 friend std::ostream &operator<<(std::ostream &, const HashFunction &);
101
102 private:
103 static const IR::Expression *convertHashAlgorithmExtern(Util::SourceInfo srcInfo,
104 IR::ID algorithm);
105
106 static const IR::Expression *convertHashAlgorithmInner(Util::SourceInfo srcInfo,
107 IR::ID algorithm, bool msb, bool extend,
108 bool extension_set,
109 const std::string &alg_name);
110};
111
112inline std::ostream &operator<<(std::ostream &out, const IR::MAU::HashFunction &h) {
113 switch (h.type) {
114 case IR::MAU::HashFunction::IDENTITY:
115 out << "identity";
116 break;
117 case IR::MAU::HashFunction::CSUM:
118 out << "csum" << h.size;
119 break;
120 case IR::MAU::HashFunction::XOR:
121 out << "xor" << h.size;
122 break;
123 case IR::MAU::HashFunction::CRC:
124 out << "crc(0x" << std::hex << h.poly;
125 if (h.init) out << " init=0x" << std::hex << h.init;
126 out << ")";
127 if (h.final_xor) out << "^" << std::hex << h.final_xor;
128 break;
129 case IR::MAU::HashFunction::RANDOM:
130 out << "random";
131 break;
132 default:
133 out << "invalid(0x" << std::hex << h.type << ")";
134 break;
135 }
136 return out;
137}
138
139} // end namespace MAU
140} // end namespace IR
141} // end namespace P4
142
143#endif /* BF_P4C_MAU_HASH_FUNCTION_H_ */
Definition json_generator.h:36
Definition json_loader.h:38
Definition source_file.h:131
The namespace encapsulating IR node classes.
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition id.h:28
Definition hash_function.h:28
static const IR::Expression * convertHashAlgorithmBFN(Util::SourceInfo srcInfo, IR::ID algorithm)
Definition hash_function.cpp:172
bool setup(const IR::Expression *exp)
Definition hash_function.cpp:404
Definition bfn_hash_algorithm.h:385