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#include "bi_test.h"
26
27unsigned nr_pass = 0;
28unsigned nr_fail = 0;
29
30static void
31bi_test_pack_literal(void)
32{
33   for (unsigned x = 0; x <= 7; ++x)
34      BIT_ASSERT(bi_pack_literal(BI_CLAUSE_SUBWORD_LITERAL_0 + x) == x);
35}
36
37static void
38bi_test_pack_upper(void)
39{
40   struct bi_packed_tuple tuples[] = {
41      { 0, 0x3 << (75 - 64) },
42      { 0, 0x1 << (75 - 64) },
43      { 0, 0x7 << (75 - 64) },
44      { 0, 0x0 << (75 - 64) },
45      { 0, 0x2 << (75 - 64) },
46      { 0, 0x6 << (75 - 64) },
47      { 0, 0x5 << (75 - 64) },
48      { 0, 0x4 << (75 - 64) },
49   };
50
51   BIT_ASSERT(bi_pack_upper(BI_CLAUSE_SUBWORD_UPPER_0 + 0, tuples, 8) == 3);
52   BIT_ASSERT(bi_pack_upper(BI_CLAUSE_SUBWORD_UPPER_0 + 1, tuples, 8) == 1);
53   BIT_ASSERT(bi_pack_upper(BI_CLAUSE_SUBWORD_UPPER_0 + 2, tuples, 8) == 7);
54   BIT_ASSERT(bi_pack_upper(BI_CLAUSE_SUBWORD_UPPER_0 + 3, tuples, 8) == 0);
55   BIT_ASSERT(bi_pack_upper(BI_CLAUSE_SUBWORD_UPPER_0 + 4, tuples, 8) == 2);
56   BIT_ASSERT(bi_pack_upper(BI_CLAUSE_SUBWORD_UPPER_0 + 5, tuples, 8) == 6);
57   BIT_ASSERT(bi_pack_upper(BI_CLAUSE_SUBWORD_UPPER_0 + 6, tuples, 8) == 5);
58   BIT_ASSERT(bi_pack_upper(BI_CLAUSE_SUBWORD_UPPER_0 + 7, tuples, 8) == 4);
59}
60
61static void
62bi_test_pack_tuple_bits(void)
63{
64   struct bi_packed_tuple tuples[] = {
65      { 0x1234567801234567, 0x3A },
66      { 0x9876543299999999, 0x1B },
67      { 0xABCDEF0101234567, 0x7C },
68   };
69
70   BIT_ASSERT(bi_pack_tuple_bits(BI_CLAUSE_SUBWORD_TUPLE_0 + 0, tuples, 8, 0, 30) == 0x01234567);
71   BIT_ASSERT(bi_pack_tuple_bits(BI_CLAUSE_SUBWORD_TUPLE_0 + 1, tuples, 8, 10, 30) == 0xca66666);
72   BIT_ASSERT(bi_pack_tuple_bits(BI_CLAUSE_SUBWORD_TUPLE_0 + 2, tuples, 8, 40, 15) == 0x4def);
73}
74
75#define L(x) (BI_CLAUSE_SUBWORD_LITERAL_0 + x)
76#define U(x) (BI_CLAUSE_SUBWORD_UPPER_0 + x)
77#define Z    BI_CLAUSE_SUBWORD_Z
78
79static void
80bi_test_pack_sync(void)
81{
82   struct bi_packed_tuple tuples[] = {
83      { 0, 0x3 << (75 - 64) },
84      { 0, 0x5 << (75 - 64) },
85      { 0, 0x7 << (75 - 64) },
86      { 0, 0x0 << (75 - 64) },
87      { 0, 0x2 << (75 - 64) },
88      { 0, 0x6 << (75 - 64) },
89      { 0, 0x5 << (75 - 64) },
90      { 0, 0x4 << (75 - 64) },
91   };
92
93   BIT_ASSERT(bi_pack_sync(L(3), L(1), L(7), tuples, 8, false) == 0xCF);
94   BIT_ASSERT(bi_pack_sync(L(3), L(1), U(7), tuples, 8, false) == 0xCC);
95   BIT_ASSERT(bi_pack_sync(L(3), U(1), U(7), tuples, 8, false) == 0xEC);
96   BIT_ASSERT(bi_pack_sync(Z,    U(1), U(7), tuples, 8, false) == 0x2C);
97   BIT_ASSERT(bi_pack_sync(Z,    U(1), U(7), tuples, 8, true)  == 0x6C);
98}
99
100int
101main(int argc, const char **argv)
102{
103   bi_test_pack_literal();
104   bi_test_pack_upper();
105   bi_test_pack_tuple_bits();
106   bi_test_pack_sync();
107
108   TEST_END(nr_pass, nr_fail);
109}
110