Fix copyright lines
[folly.git] / folly / experimental / test / InstructionsTest.cpp
1 /*
2  * Copyright 2016-present Facebook, Inc.
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 #include <folly/experimental/Instructions.h>
18
19 #include <glog/logging.h>
20
21 #include <folly/portability/GTest.h>
22
23 using namespace folly;
24 using namespace folly::compression::instructions;
25
26 TEST(Instructions, BitExtraction) {
27   uint64_t value =
28       0b11111110'11011100'10111010'10011000'01110110'01010100'00110010'00010000;
29
30   if (not Haswell::supported()) {
31     return;
32   }
33
34   LOG(INFO) << "Testing Haswell on supported machine";
35
36   // Extract 4 bits a time, starting from bit 0
37   uint64_t expected = 0;
38   for (int i = 0; i < 64 - 4; i += 4) {
39     EXPECT_EQ(expected, Default::bextr(value, i, 4));
40     EXPECT_EQ(expected, Haswell::bextr(value, i, 4));
41     ++expected;
42   }
43
44   // Extract 8 bits a time, starting from bit 1
45   uint64_t value2 = value << 1;
46   uint64_t lower = 0;
47   uint64_t upper = 1;
48   for (int i = 1; i < 64 - 8; i += 4) {
49     expected = (lower & 0xF) | ((upper & 0xF) << 4);
50     EXPECT_EQ(expected, Default::bextr(value2, i, 8));
51     EXPECT_EQ(expected, Haswell::bextr(value2, i, 8));
52     ++lower;
53     ++upper;
54   }
55
56   // Extract 16 bits a time, starting from bit 2
57   uint64_t value3 = value << 2;
58   uint64_t part0 = 0;
59   uint64_t part1 = 1;
60   uint64_t part2 = 2;
61   uint64_t part3 = 3;
62   for (int i = 2; i < 64 - 16; i += 4) {
63     expected = (part0 & 0xF) | ((part1 & 0xF) << 4) | ((part2 & 0xF) << 8) |
64         ((part3 & 0xF) << 12);
65     EXPECT_EQ(expected, Default::bextr(value3, i, 16));
66     EXPECT_EQ(expected, Haswell::bextr(value3, i, 16));
67     ++part0;
68     ++part1;
69     ++part2;
70     ++part3;
71   }
72
73   // Extract 32 bits
74   expected = 0b1011'1010'1001'1000'0111'0110'0101'0100;
75   EXPECT_EQ(expected, Default::bextr(value, 16, 32));
76   EXPECT_EQ(expected, Haswell::bextr(value, 16, 32));
77
78   // Extract all 64 bits
79   EXPECT_EQ(value, Default::bextr(value, 0, 64));
80   EXPECT_EQ(value, Haswell::bextr(value, 0, 64));
81
82   // Extract 0 bits
83   EXPECT_EQ(0, Default::bextr(value, 4, 0));
84   EXPECT_EQ(0, Haswell::bextr(value, 4, 0));
85
86   // Make sure only up to 63-th bits will be extracted
87   EXPECT_EQ(0b1111, Default::bextr(value, 60, 5));
88   EXPECT_EQ(0b1111, Haswell::bextr(value, 60, 5));
89
90   EXPECT_EQ(0, Default::bextr(value, 64, 8));
91   EXPECT_EQ(0, Haswell::bextr(value, 64, 8));
92
93   EXPECT_EQ(value, Default::bextr(value, 0, 65));
94   EXPECT_EQ(value, Haswell::bextr(value, 0, 65));
95 }