intel/fs: Lower 64-bit MOVs after lower_load_payload()
[mesa.git] / src / intel / common / tests / genxml_test.c
1 /*
2 * Copyright © 2019 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <stdbool.h>
27 #include "gen_decoder.h"
28
29 static bool quiet = false;
30
31 struct test_address {
32 uint64_t offset;
33 };
34
35 __attribute__((unused)) static uint64_t
36 _test_combine_address(void *data, void *location,
37 struct test_address address, uint32_t delta)
38 {
39 return address.offset + delta;
40 }
41
42 #define __gen_user_data void
43 #define __gen_combine_address _test_combine_address
44 #define __gen_address_type struct test_address
45
46 #include "gentest_pack.h"
47
48 static void
49 test_struct(struct gen_spec *spec) {
50 /* Fill struct fields and <group> tag */
51 struct GEN9_TEST_STRUCT test1 = {
52 .number1 = 5,
53 .number2 = 1234,
54 };
55
56 for (int i = 0; i < 4; i++) {
57 test1.byte[i] = i * 10 + 5;
58 }
59
60 /* Pack struct into a dw array */
61 uint32_t dw[GEN9_TEST_STRUCT_length];
62 GEN9_TEST_STRUCT_pack(NULL, dw, &test1);
63
64 /* Now decode the packed struct, and make sure it matches the original */
65 struct gen_group *group;
66 group = gen_spec_find_struct(spec, "TEST_STRUCT");
67
68 assert(group != NULL);
69
70 if (!quiet) {
71 printf("\nTEST_STRUCT:\n");
72 gen_print_group(stdout, group, 0, dw, 0, false);
73 }
74
75 struct gen_field_iterator iter;
76 gen_field_iterator_init(&iter, group, dw, 0, false);
77
78 while (gen_field_iterator_next(&iter)) {
79 int idx;
80 if (strcmp(iter.name, "number1") == 0) {
81 uint16_t number = iter.raw_value;
82 assert(number == test1.number1);
83 } else if (strcmp(iter.name, "number2") == 0) {
84 uint16_t number = iter.raw_value;
85 assert(number == test1.number2);
86 } else if (sscanf(iter.name, "byte[%d]", &idx) == 1) {
87 uint8_t number = iter.raw_value;
88 assert(number == test1.byte[idx]);
89 }
90 }
91 }
92
93 static void
94 test_two_levels(struct gen_spec *spec) {
95 struct GEN9_STRUCT_TWO_LEVELS test;
96
97 for (int i = 0; i < 4; i++) {
98 for (int j = 0; j < 8; j++) {
99 test.byte[i][j] = (i * 10 + j) % 256;
100 }
101 }
102
103 uint32_t dw[GEN9_STRUCT_TWO_LEVELS_length];
104 GEN9_STRUCT_TWO_LEVELS_pack(NULL, dw, &test);
105
106 struct gen_group *group;
107 group = gen_spec_find_struct(spec, "STRUCT_TWO_LEVELS");
108
109 assert(group != NULL);
110
111 if (!quiet) {
112 printf("\nSTRUCT_TWO_LEVELS\n");
113 gen_print_group(stdout, group, 0, dw, 0, false);
114 }
115
116 struct gen_field_iterator iter;
117 gen_field_iterator_init(&iter, group, dw, 0, false);
118
119 while (gen_field_iterator_next(&iter)) {
120 int i, j;
121
122 assert(sscanf(iter.name, "byte[%d][%d]", &i, &j) == 2);
123 uint8_t number = iter.raw_value;
124 assert(number == test.byte[i][j]);
125 }
126 }
127
128 int main(int argc, char **argv)
129 {
130 struct gen_spec *spec = gen_spec_load_filename(GENXML_PATH);
131
132 if (argc > 1 && strcmp(argv[1], "-quiet") == 0)
133 quiet = true;
134
135 test_struct(spec);
136 test_two_levels(spec);
137
138 return 0;
139 }