1 /*
2  * Copyright 2019 The libgav1 Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBGAV1_SRC_UTILS_BIT_MASK_SET_H_
18 #define LIBGAV1_SRC_UTILS_BIT_MASK_SET_H_
19 
20 #include <cstdint>
21 
22 namespace libgav1 {
23 
24 // This class is used to check if a given value is equal to one of the several
25 // predetermined values using a bit mask instead of a chain of comparisons and
26 // ||s. This usually results in fewer instructions.
27 //
28 // Usage:
29 //   constexpr BitMaskSet set(value1, value2);
30 //   set.Contains(value1) => returns true.
31 //   set.Contains(value3) => returns false.
32 class BitMaskSet {
33  public:
BitMaskSet(uint32_t mask)34   explicit constexpr BitMaskSet(uint32_t mask) : mask_(mask) {}
35 
BitMaskSet(int v1,int v2)36   constexpr BitMaskSet(int v1, int v2) : mask_((1U << v1) | (1U << v2)) {}
37 
BitMaskSet(int v1,int v2,int v3)38   constexpr BitMaskSet(int v1, int v2, int v3)
39       : mask_((1U << v1) | (1U << v2) | (1U << v3)) {}
40 
BitMaskSet(int v1,int v2,int v3,int v4)41   constexpr BitMaskSet(int v1, int v2, int v3, int v4)
42       : mask_((1U << v1) | (1U << v2) | (1U << v3) | (1U << v4)) {}
43 
BitMaskSet(int v1,int v2,int v3,int v4,int v5)44   constexpr BitMaskSet(int v1, int v2, int v3, int v4, int v5)
45       : mask_((1U << v1) | (1U << v2) | (1U << v3) | (1U << v4) | (1U << v5)) {}
46 
BitMaskSet(int v1,int v2,int v3,int v4,int v5,int v6)47   constexpr BitMaskSet(int v1, int v2, int v3, int v4, int v5, int v6)
48       : mask_((1U << v1) | (1U << v2) | (1U << v3) | (1U << v4) | (1U << v5) |
49               (1U << v6)) {}
50 
BitMaskSet(int v1,int v2,int v3,int v4,int v5,int v6,int v7)51   constexpr BitMaskSet(int v1, int v2, int v3, int v4, int v5, int v6, int v7)
52       : mask_((1U << v1) | (1U << v2) | (1U << v3) | (1U << v4) | (1U << v5) |
53               (1U << v6) | (1U << v7)) {}
54 
BitMaskSet(int v1,int v2,int v3,int v4,int v5,int v6,int v7,int v8,int v9)55   constexpr BitMaskSet(int v1, int v2, int v3, int v4, int v5, int v6, int v7,
56                        int v8, int v9)
57       : mask_((1U << v1) | (1U << v2) | (1U << v3) | (1U << v4) | (1U << v5) |
58               (1U << v6) | (1U << v7) | (1U << v8) | (1U << v9)) {}
59 
BitMaskSet(int v1,int v2,int v3,int v4,int v5,int v6,int v7,int v8,int v9,int v10)60   constexpr BitMaskSet(int v1, int v2, int v3, int v4, int v5, int v6, int v7,
61                        int v8, int v9, int v10)
62       : mask_((1U << v1) | (1U << v2) | (1U << v3) | (1U << v4) | (1U << v5) |
63               (1U << v6) | (1U << v7) | (1U << v8) | (1U << v9) | (1U << v10)) {
64   }
65 
Contains(uint8_t value)66   constexpr bool Contains(uint8_t value) const {
67     return MaskContainsValue(mask_, value);
68   }
69 
MaskContainsValue(uint32_t mask,uint8_t value)70   static constexpr bool MaskContainsValue(uint32_t mask, uint8_t value) {
71     return ((mask >> value) & 1) != 0;
72   }
73 
74  private:
75   const uint32_t mask_;
76 };
77 
78 }  // namespace libgav1
79 #endif  // LIBGAV1_SRC_UTILS_BIT_MASK_SET_H_
80