1 /*
2 * Copyright (C) 2022 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 "bi_builder.h"
25 #include "bi_test.h"
26 #include "compiler.h"
27
28 #include <gtest/gtest.h>
29
30 #define CASE(instr, expected) \
31 INSTRUCTION_CASE(instr, expected, bi_lower_swizzle)
32 #define NEGCASE(instr) CASE(instr, instr)
33
34 class LowerSwizzle : public testing::Test {
35 protected:
LowerSwizzle()36 LowerSwizzle()
37 {
38 mem_ctx = ralloc_context(NULL);
39
40 reg = bi_register(0);
41 x = bi_register(1);
42 y = bi_register(2);
43 z = bi_register(3);
44 w = bi_register(4);
45
46 x3210 = x;
47 x3210.swizzle = BI_SWIZZLE_B3210;
48 }
49
~LowerSwizzle()50 ~LowerSwizzle()
51 {
52 ralloc_free(mem_ctx);
53 }
54
55 void *mem_ctx;
56
57 bi_index reg, x, y, z, w;
58 bi_index x3210;
59 };
60
TEST_F(LowerSwizzle,Csel16)61 TEST_F(LowerSwizzle, Csel16)
62 {
63 CASE(bi_csel_v2f16_to(b, reg, bi_half(x, 0), y, z, w, BI_CMPF_NE),
64 bi_csel_v2f16_to(b, reg, bi_swz_v2i16(b, bi_half(x, 0)), y, z, w,
65 BI_CMPF_NE));
66 }
67
TEST_F(LowerSwizzle,Fma16)68 TEST_F(LowerSwizzle, Fma16)
69 {
70 NEGCASE(bi_fadd_v2f16_to(b, reg, bi_half(x, 0), y));
71 NEGCASE(bi_fma_v2f16_to(b, reg, bi_half(x, 0), y, z));
72 }
73
TEST_F(LowerSwizzle,ClzHadd8)74 TEST_F(LowerSwizzle, ClzHadd8)
75 {
76 CASE(bi_clz_v4u8_to(b, reg, x3210, true),
77 bi_clz_v4u8_to(b, reg, bi_swz_v4i8(b, x3210), true));
78
79 CASE(bi_hadd_v4u8_to(b, reg, y, x3210, BI_ROUND_RTP),
80 bi_hadd_v4u8_to(b, reg, y, bi_swz_v4i8(b, x3210), BI_ROUND_RTP));
81 }
82
TEST_F(LowerSwizzle,FirstShift8)83 TEST_F(LowerSwizzle, FirstShift8)
84 {
85 enum bi_opcode ops[] = {
86 BI_OPCODE_LSHIFT_AND_V4I8, BI_OPCODE_LSHIFT_OR_V4I8,
87 BI_OPCODE_LSHIFT_XOR_V4I8, BI_OPCODE_RSHIFT_AND_V4I8,
88 BI_OPCODE_RSHIFT_OR_V4I8, BI_OPCODE_RSHIFT_XOR_V4I8,
89 };
90
91 for (unsigned i = 0; i < ARRAY_SIZE(ops); ++i) {
92 CASE(
93 {
94 bi_instr *I = bi_lshift_and_v4i8_to(b, reg, x3210, y, z);
95 I->op = ops[i];
96 },
97 {
98 bi_instr *I =
99 bi_lshift_and_v4i8_to(b, reg, bi_swz_v4i8(b, x3210), y, z);
100 I->op = ops[i];
101 });
102 }
103 }
104
TEST_F(LowerSwizzle,ShiftWithNot)105 TEST_F(LowerSwizzle, ShiftWithNot)
106 {
107 CASE(bi_lshift_and_v4i8_to(b, reg, y, bi_neg(x3210), z),
108 bi_lshift_and_v4i8_to(b, reg, y, bi_neg(bi_swz_v4i8(b, x3210)), z));
109 }
110
TEST_F(LowerSwizzle,FClampSwap)111 TEST_F(LowerSwizzle, FClampSwap)
112 {
113 CASE(bi_fclamp_v2f16_to(b, reg, bi_swz_16(x, 1, 0)),
114 bi_swz_v2i16_to(b, reg, bi_swz_16(bi_fclamp_v2f16(b, x), 1, 0)));
115 }
116