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#include "bi_builder.h"
27
28#define TMP() bi_temp(b->shader)
29
30int main(int argc, char **argv)
31{
32   unsigned nr_fail = 0, nr_pass = 0;
33   void *ralloc_ctx = ralloc_context(NULL);
34   bi_builder *b = bit_builder(ralloc_ctx);
35
36   bi_instr *mov = bi_mov_i32_to(b, TMP(), TMP());
37   BIT_ASSERT(bi_can_fma(mov));
38   BIT_ASSERT(bi_can_add(mov));
39   BIT_ASSERT(!bi_must_message(mov));
40   BIT_ASSERT(bi_reads_zero(mov));
41   BIT_ASSERT(bi_reads_temps(mov, 0));
42   BIT_ASSERT(bi_reads_t(mov, 0));
43
44   bi_instr *fma = bi_fma_f32_to(b, TMP(), TMP(), TMP(), bi_zero(), BI_ROUND_NONE);
45   BIT_ASSERT(bi_can_fma(fma));
46   BIT_ASSERT(!bi_can_add(fma));
47   BIT_ASSERT(!bi_must_message(fma));
48   BIT_ASSERT(bi_reads_zero(fma));
49   for (unsigned i = 0; i < 3; ++i) {
50      BIT_ASSERT(bi_reads_temps(fma, i));
51      BIT_ASSERT(bi_reads_t(fma, i));
52   }
53
54   bi_instr *load = bi_load_i128_to(b, TMP(), TMP(), TMP(), BI_SEG_UBO);
55   BIT_ASSERT(!bi_can_fma(load));
56   BIT_ASSERT(bi_can_add(load));
57   BIT_ASSERT(bi_must_message(load));
58   for (unsigned i = 0; i < 2; ++i) {
59      BIT_ASSERT(bi_reads_temps(load, i));
60      BIT_ASSERT(bi_reads_t(load, i));
61   }
62
63   bi_instr *blend = bi_blend_to(b, TMP(), TMP(), TMP(), TMP(), TMP(), 4);
64   BIT_ASSERT(!bi_can_fma(load));
65   BIT_ASSERT(bi_can_add(load));
66   BIT_ASSERT(bi_must_message(blend));
67   for (unsigned i = 0; i < 4; ++i)
68      BIT_ASSERT(bi_reads_temps(blend, i));
69   BIT_ASSERT(!bi_reads_t(blend, 0));
70   BIT_ASSERT(bi_reads_t(blend, 1));
71   BIT_ASSERT(!bi_reads_t(blend, 2));
72   BIT_ASSERT(!bi_reads_t(blend, 3));
73
74   /* Test restrictions on modifiers of same cycle temporaries */
75   bi_instr *fadd = bi_fadd_f32_to(b, TMP(), TMP(), TMP(), BI_ROUND_NONE);
76   BIT_ASSERT(bi_reads_t(fadd, 0));
77
78   for (unsigned i = 0; i < 2; ++i) {
79      for (unsigned j = 0; j < 2; ++j) {
80         bi_instr *fadd = bi_fadd_f32_to(b, TMP(), TMP(), TMP(), BI_ROUND_NONE);
81         fadd->src[i] = bi_swz_16(TMP(), j, j);
82         BIT_ASSERT(bi_reads_t(fadd, 1 - i));
83         BIT_ASSERT(!bi_reads_t(fadd, i));
84      }
85   }
86
87   ralloc_free(ralloc_ctx);
88   TEST_END(nr_pass, nr_fail);
89}
90