1 /** 2 * Copyright (c) 2021-2022 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 panda::verifier { 26 27 template <typename Int, const Int Min, const Int Max> 28 class TagForInt { 29 public: 30 static constexpr size_t Size = Max - Min + 1; 31 static constexpr size_t Bits = sizeof(size_t) * 8ULL - panda::Clz(Size); 32 33 using type = Int; 34 GetIndexFor(Int i)35 static size_t GetIndexFor(Int i) 36 { 37 ASSERT(Min <= i && i <= Max); 38 return i - Min; 39 } 40 GetValueFor(size_t tag)41 static Int GetValueFor(size_t tag) 42 { 43 ASSERT(tag < Size); 44 return static_cast<Int>(tag) + Min; 45 } 46 }; 47 48 } // namespace panda::verifier 49 50 #endif // !PANDA_VERIFIER_UTIL_TAG_FOR_INT__