1 /*
2 * Copyright (C) 2021 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "compiler.h"
25
26 #include <gtest/gtest.h>
27
28 #define L(x) ((enum bi_clause_subword)(BI_CLAUSE_SUBWORD_LITERAL_0 + x))
29 #define U(x) ((enum bi_clause_subword)(BI_CLAUSE_SUBWORD_UPPER_0 + x))
30 #define T(x) ((enum bi_clause_subword)(BI_CLAUSE_SUBWORD_TUPLE_0 + x))
31 #define Z BI_CLAUSE_SUBWORD_Z
32
TEST(Packing,PackLiteral)33 TEST(Packing, PackLiteral)
34 {
35 for (unsigned x = 0; x <= 7; ++x)
36 EXPECT_EQ(bi_pack_literal(L(x)), x);
37 }
38
TEST(Packing,PackUpper)39 TEST(Packing, PackUpper)
40 {
41 struct bi_packed_tuple tuples[] = {
42 {0, 0x3 << (75 - 64)}, {0, 0x1 << (75 - 64)}, {0, 0x7 << (75 - 64)},
43 {0, 0x0 << (75 - 64)}, {0, 0x2 << (75 - 64)}, {0, 0x6 << (75 - 64)},
44 {0, 0x5 << (75 - 64)}, {0, 0x4 << (75 - 64)},
45 };
46
47 EXPECT_EQ(bi_pack_upper(U(0), tuples, 8), 3);
48 EXPECT_EQ(bi_pack_upper(U(1), tuples, 8), 1);
49 EXPECT_EQ(bi_pack_upper(U(2), tuples, 8), 7);
50 EXPECT_EQ(bi_pack_upper(U(3), tuples, 8), 0);
51 EXPECT_EQ(bi_pack_upper(U(4), tuples, 8), 2);
52 EXPECT_EQ(bi_pack_upper(U(5), tuples, 8), 6);
53 EXPECT_EQ(bi_pack_upper(U(6), tuples, 8), 5);
54 EXPECT_EQ(bi_pack_upper(U(7), tuples, 8), 4);
55 }
56
TEST(Packing,PackTupleBits)57 TEST(Packing, PackTupleBits)
58 {
59 struct bi_packed_tuple tuples[] = {
60 {0x1234567801234567, 0x3A},
61 {0x9876543299999999, 0x1B},
62 {0xABCDEF0101234567, 0x7C},
63 };
64
65 EXPECT_EQ(bi_pack_tuple_bits(T(0), tuples, 8, 0, 30), 0x01234567);
66 EXPECT_EQ(bi_pack_tuple_bits(T(1), tuples, 8, 10, 30), 0xca66666);
67 EXPECT_EQ(bi_pack_tuple_bits(T(2), tuples, 8, 40, 15), 0x4def);
68 }
69
TEST(Packing,PackSync)70 TEST(Packing, PackSync)
71 {
72 struct bi_packed_tuple tuples[] = {
73 {0, 0x3 << (75 - 64)}, {0, 0x5 << (75 - 64)}, {0, 0x7 << (75 - 64)},
74 {0, 0x0 << (75 - 64)}, {0, 0x2 << (75 - 64)}, {0, 0x6 << (75 - 64)},
75 {0, 0x5 << (75 - 64)}, {0, 0x4 << (75 - 64)},
76 };
77
78 EXPECT_EQ(bi_pack_sync(L(3), L(1), L(7), tuples, 8, false), 0xCF);
79 EXPECT_EQ(bi_pack_sync(L(3), L(1), U(7), tuples, 8, false), 0xCC);
80 EXPECT_EQ(bi_pack_sync(L(3), U(1), U(7), tuples, 8, false), 0xEC);
81 EXPECT_EQ(bi_pack_sync(Z, U(1), U(7), tuples, 8, false), 0x2C);
82 EXPECT_EQ(bi_pack_sync(Z, U(1), U(7), tuples, 8, true), 0x6C);
83 }
84