1 /*
2 * Copyright (c) 2023 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 #include "aarch64_imm_valid.h"
16 #include <iomanip>
17 #include <set>
18
19 namespace maplebe {
20 const std::set<uint64> ValidBitmaskImmSet = {
21 #include "valid_bitmask_imm.txt"
22 };
23 constexpr uint32 kMaxBitTableSize = 5;
24 constexpr std::array<uint64, kMaxBitTableSize> kBitmaskImmMultTable = {
25 0x0000000100000001UL, 0x0001000100010001UL, 0x0101010101010101UL, 0x1111111111111111UL, 0x5555555555555555UL,
26 };
27 namespace aarch64 {
IsBitmaskImmediate(uint64 val,uint32 bitLen)28 bool IsBitmaskImmediate(uint64 val, uint32 bitLen)
29 {
30 DEBUG_ASSERT(val != 0, "IsBitmaskImmediate() don's accept 0 or -1");
31 DEBUG_ASSERT(static_cast<int64>(val) != -1, "IsBitmaskImmediate() don's accept 0 or -1");
32 if ((bitLen == k32BitSize) && (static_cast<int32>(val) == -1)) {
33 return false;
34 }
35 uint64 val2 = val;
36 if (bitLen == k32BitSize) {
37 val2 = (val2 << k32BitSize) | (val2 & ((1ULL << k32BitSize) - 1));
38 }
39 bool expectedOutcome = (ValidBitmaskImmSet.find(val2) != ValidBitmaskImmSet.end());
40
41 if ((val & 0x1) != 0) {
42 /*
43 * we want to work with
44 * 0000000000000000000000000000000000000000000001100000000000000000
45 * instead of
46 * 1111111111111111111111111111111111111111111110011111111111111111
47 */
48 val = ~val;
49 }
50
51 if (bitLen == k32BitSize) {
52 val = (val << k32BitSize) | (val & ((1ULL << k32BitSize) - 1));
53 }
54
55 /* get the least significant bit set and add it to 'val' */
56 uint64 tmpVal = val + (val & static_cast<uint64>(UINT64_MAX - val + 1));
57
58 /* now check if tmp is a power of 2 or tmpVal==0. */
59 DEBUG_ASSERT(tmpVal >= 0, "tmpVal -1 should be unsigned");
60 tmpVal = tmpVal & (tmpVal - 1);
61 if (tmpVal == 0) {
62 if (!expectedOutcome) {
63 #if defined(DEBUG) && DEBUG
64 LogInfo::MapleLogger() << "0x" << std::hex << std::setw(static_cast<int>(k16ByteSize)) << std::setfill('0')
65 << static_cast<uint64>(val) << "\n";
66 #endif
67 return false;
68 }
69 DEBUG_ASSERT(expectedOutcome, "incorrect implementation: not valid value but returning true");
70 /* power of two or zero ; return true */
71 return true;
72 }
73
74 int32 p0 = __builtin_ctzll(val);
75 int32 p1 = __builtin_ctzll(tmpVal);
76 int64 diff = p1 - p0;
77
78 /* check if diff is a power of two; return false if not. */
79 CHECK_FATAL(static_cast<uint64>(diff) >= 1, "value overflow");
80 if ((static_cast<uint64>(diff) & (static_cast<uint64>(diff) - 1)) != 0) {
81 DEBUG_ASSERT(!expectedOutcome, "incorrect implementation: valid value but returning false");
82 return false;
83 }
84
85 uint32 logDiff = static_cast<uint32>(__builtin_ctzll(static_cast<uint64>(diff)));
86 uint64 pattern = val & ((1ULL << static_cast<uint64>(diff)) - 1);
87 #if defined(DEBUG) && DEBUG
88 bool ret = (val == pattern * kBitmaskImmMultTable[kMaxBitTableSize - logDiff]);
89 DEBUG_ASSERT(expectedOutcome == ret, "incorrect implementation: return value does not match expected outcome");
90 return ret;
91 #else
92 return val == pattern * kBitmaskImmMultTable[kMaxBitTableSize - logDiff];
93 #endif
94 }
95 } // namespace aarch64
96 } // namespace maplebe
97