1 /** 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PANDA_VERIFIER_UTIL_TAG_FOR_INT_ 17 #define PANDA_VERIFIER_UTIL_TAG_FOR_INT_ 18 19 #include <cstddef> 20 21 #include "utils/bit_utils.h" 22 23 #include "macros.h" 24 25 namespace ark::verifier { 26 27 template <typename Int, const Int MIN_INT, const Int MAX_INT> 28 class TagForInt { 29 public: 30 static constexpr size_t SIZE = MAX_INT - MIN_INT + 1; 31 static constexpr size_t BITS = sizeof(size_t) * 8ULL - ark::Clz(SIZE); 32 33 using Type = Int; 34 GetIndexFor(Int i)35 static size_t GetIndexFor(Int i) 36 { 37 ASSERT(MIN_INT <= i); 38 ASSERT(i <= MAX_INT); 39 return i - MIN_INT; 40 } 41 GetValueFor(size_t tag)42 static Int GetValueFor(size_t tag) 43 { 44 ASSERT(tag < SIZE); 45 return static_cast<Int>(tag) + MIN_INT; 46 } 47 }; 48 49 } // namespace ark::verifier 50 51 #endif // !PANDA_VERIFIER_UTIL_TAG_FOR_INT_