1/*
2 * Copyright (c) 2022-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
16export class Bitops3BitBitsInByte {
17  private static fast3bitlookup(b: int): int {
18    let c: int;
19    let bi3b: int = 0xE994; // 0b1110 1001 1001 0100; // 3 2 2 1  2 1 1 0
20    c = 3 & (bi3b >> ((b << 1) & 14));
21    c += 3 & (bi3b >> ((b >> 2) & 14));
22    c += 3 & (bi3b >> ((b >> 5) & 6));
23    return c;
24  }
25
26  private n1: int = 500;
27  private n2: int = 256;
28  private static readonly expected: int = 512000;
29
30  public run(): void {
31    let sum: int = 0;
32    for (let x: int = 0; x < this.n1; x++) {
33      for (let y: int = 0; y < this.n2; y++) {
34        sum += Bitops3BitBitsInByte.fast3bitlookup(y);
35      }
36    }
37    assert sum == Bitops3BitBitsInByte.expected: "Incorrect result"
38  }
39}
40
41function main(): void {
42  let a = new Bitops3BitBitsInByte;
43  a.run();
44}
45