P4C
The P4 Compiler
Loading...
Searching...
No Matches
match.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef LIB_MATCH_H_
18#define LIB_MATCH_H_
19
20#include <stdint.h>
21
22#include <iostream>
23
24#include "big_int_util.h"
25
26namespace P4 {
27
36struct match_t {
37 big_int word0, word1;
38 match_t() : word0(0), word1(0) {}
39 match_t(big_int w0, big_int w1) : word0(w0), word1(w1) {}
40 explicit operator bool() const { return (word0 | word1) != 0; }
41 bool operator==(const match_t &a) const { return word0 == a.word0 && word1 == a.word1; }
42 bool operator!=(const match_t &a) const { return word0 != a.word0 || word1 != a.word1; }
43 bool matches(big_int v) const {
44 return (v | word1) == word1 && ((~v & word1) | word0) == word0;
45 }
46 void setwidth(int bits) {
47 big_int mask = (big_int(1) << bits) - 1;
48 word0 &= mask;
49 word1 &= mask;
50 mask &= ~(word0 | word1);
51 word0 |= mask;
52 word1 |= mask;
53 }
54 match_t(int size, big_int val, big_int mask) : word0(~val & mask), word1(val & mask) {
55 setwidth(size);
56 }
57 static match_t dont_care(int size) { return match_t(size, 0, 0); }
58};
59
60std::ostream &operator<<(std::ostream &, const match_t &);
61bool operator>>(const char *, match_t &);
62
63} // namespace P4
64
65#endif /* LIB_MATCH_H_ */
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition match.h:36