1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2021 Collabora, Ltd.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "va_compiler.h"
25bf215546Sopenharmony_ci#include "bi_test.h"
26bf215546Sopenharmony_ci#include "bi_builder.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include <gtest/gtest.h>
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#define CASE(instr, expected) do { \
31bf215546Sopenharmony_ci   if (va_validate_fau(instr) != expected) { \
32bf215546Sopenharmony_ci      fprintf(stderr, "Incorrect validation for:\n"); \
33bf215546Sopenharmony_ci      bi_print_instr(instr, stderr); \
34bf215546Sopenharmony_ci      fprintf(stderr, "\n"); \
35bf215546Sopenharmony_ci      ADD_FAILURE(); \
36bf215546Sopenharmony_ci   } \
37bf215546Sopenharmony_ci} while(0)
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#define VALID(instr) CASE(instr, true)
40bf215546Sopenharmony_ci#define INVALID(instr) CASE(instr, false)
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ciclass ValidateFau : public testing::Test {
43bf215546Sopenharmony_ciprotected:
44bf215546Sopenharmony_ci   ValidateFau() {
45bf215546Sopenharmony_ci      mem_ctx = ralloc_context(NULL);
46bf215546Sopenharmony_ci      b = bit_builder(mem_ctx);
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci      zero = bi_fau((enum bir_fau) (BIR_FAU_IMMEDIATE | 0), false);
49bf215546Sopenharmony_ci      imm1 = bi_fau((enum bir_fau) (BIR_FAU_IMMEDIATE | 1), false);
50bf215546Sopenharmony_ci      imm2 = bi_fau((enum bir_fau) (BIR_FAU_IMMEDIATE | 2), false);
51bf215546Sopenharmony_ci      unif = bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 5), false);
52bf215546Sopenharmony_ci      unif_hi = bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 5), true);
53bf215546Sopenharmony_ci      unif2 = bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 6), false);
54bf215546Sopenharmony_ci      core_id = bi_fau(BIR_FAU_CORE_ID, false);
55bf215546Sopenharmony_ci      lane_id = bi_fau(BIR_FAU_LANE_ID, false);
56bf215546Sopenharmony_ci   }
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   ~ValidateFau() {
59bf215546Sopenharmony_ci      ralloc_free(mem_ctx);
60bf215546Sopenharmony_ci   }
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci   void *mem_ctx;
63bf215546Sopenharmony_ci   bi_builder *b;
64bf215546Sopenharmony_ci   bi_index zero, imm1, imm2, unif, unif_hi, unif2, core_id, lane_id;
65bf215546Sopenharmony_ci};
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ciTEST_F(ValidateFau, One64BitUniformSlot)
68bf215546Sopenharmony_ci{
69bf215546Sopenharmony_ci   VALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), bi_register(3),
70bf215546Sopenharmony_ci            unif));
71bf215546Sopenharmony_ci   VALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), unif_hi, unif));
72bf215546Sopenharmony_ci   VALID(bi_fma_f32_to(b, bi_register(1), unif, unif, unif_hi));
73bf215546Sopenharmony_ci   INVALID(bi_fma_f32_to(b, bi_register(1), unif, unif2, bi_register(1)));
74bf215546Sopenharmony_ci   INVALID(bi_fma_f32_to(b, bi_register(1), unif, unif2, unif_hi));
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci   /* Crafted case that appears correct at first glance and was erronously
77bf215546Sopenharmony_ci    * marked as valid in early versions of the validator.
78bf215546Sopenharmony_ci    */
79bf215546Sopenharmony_ci   INVALID(bi_fma_f32_to(b, bi_register(1), bi_register(2),
80bf215546Sopenharmony_ci                         bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 0), false),
81bf215546Sopenharmony_ci                         bi_fau((enum bir_fau) (BIR_FAU_UNIFORM | 1), true)));
82bf215546Sopenharmony_ci}
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ciTEST_F(ValidateFau, Combined64BitUniformsConstants)
85bf215546Sopenharmony_ci{
86bf215546Sopenharmony_ci   VALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), unif_hi, unif));
87bf215546Sopenharmony_ci   VALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), zero, unif));
88bf215546Sopenharmony_ci   VALID(bi_fma_f32_to(b, bi_register(1), zero, imm1, imm1));
89bf215546Sopenharmony_ci   INVALID(bi_fma_f32_to(b, bi_register(1), zero, unif_hi, unif));
90bf215546Sopenharmony_ci   INVALID(bi_fma_f32_to(b, bi_register(1), zero, imm1, imm2));
91bf215546Sopenharmony_ci}
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ciTEST_F(ValidateFau, UniformsOnlyInDefaultMode)
94bf215546Sopenharmony_ci{
95bf215546Sopenharmony_ci   INVALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), unif_hi, lane_id));
96bf215546Sopenharmony_ci   INVALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), unif_hi, core_id));
97bf215546Sopenharmony_ci}
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ciTEST_F(ValidateFau, SingleSpecialImmediate)
100bf215546Sopenharmony_ci{
101bf215546Sopenharmony_ci   VALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), bi_register(2),
102bf215546Sopenharmony_ci            lane_id));
103bf215546Sopenharmony_ci   VALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), bi_register(2),
104bf215546Sopenharmony_ci            core_id));
105bf215546Sopenharmony_ci   INVALID(bi_fma_f32_to(b, bi_register(1), bi_register(2), lane_id,
106bf215546Sopenharmony_ci            core_id));
107bf215546Sopenharmony_ci}
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ciTEST_F(ValidateFau, SmokeTests)
110bf215546Sopenharmony_ci{
111bf215546Sopenharmony_ci   VALID(bi_mov_i32_to(b, bi_register(1), bi_register(2)));
112bf215546Sopenharmony_ci   VALID(bi_mov_i32_to(b, bi_register(1), unif));
113bf215546Sopenharmony_ci   VALID(bi_fma_f32_to(b, bi_register(1), bi_discard(bi_register(1)),
114bf215546Sopenharmony_ci                        unif, bi_neg(zero)));
115bf215546Sopenharmony_ci}
116