• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "va_compiler.h"
25 #include "bi_test.h"
26 #include "bi_builder.h"
27 
28 #include <gtest/gtest.h>
29 
30 static inline void
add_imm(bi_context * ctx)31 add_imm(bi_context *ctx)
32 {
33    bi_foreach_instr_global(ctx, I) {
34       va_lower_constants(ctx, I);
35    }
36 }
37 
38 #define CASE(instr, expected) INSTRUCTION_CASE(instr, expected, add_imm)
39 
40 class LowerConstants : public testing::Test {
41 protected:
LowerConstants()42    LowerConstants() {
43       mem_ctx = ralloc_context(NULL);
44    }
45 
~LowerConstants()46    ~LowerConstants() {
47       ralloc_free(mem_ctx);
48    }
49 
50    void *mem_ctx;
51 };
52 
TEST_F(LowerConstants,Float32)53 TEST_F(LowerConstants, Float32) {
54    CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(0.0)),
55         bi_fadd_f32_to(b, bi_register(0), bi_register(0), va_lut(0)));
56 
57    CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(1.0)),
58         bi_fadd_f32_to(b, bi_register(0), bi_register(0), va_lut(16)));
59 
60    CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(0.1)),
61         bi_fadd_f32_to(b, bi_register(0), bi_register(0), va_lut(17)));
62 }
63 
TEST_F(LowerConstants,WidenFloat16)64 TEST_F(LowerConstants, WidenFloat16) {
65    CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(0.5)),
66         bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_half(va_lut(26), 1)));
67 
68    CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(255.0)),
69         bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_half(va_lut(23), 0)));
70 
71    CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(256.0)),
72         bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_half(va_lut(23), 1)));
73 
74    CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(8.0)),
75         bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_half(va_lut(30), 1)));
76 }
77 
TEST_F(LowerConstants,ReplicateFloat16)78 TEST_F(LowerConstants, ReplicateFloat16) {
79    CASE(bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_imm_f16(255.0)),
80         bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_half(va_lut(23), 0)));
81 
82    CASE(bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_imm_f16(4.0)),
83         bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_half(va_lut(29), 1)));
84 }
85 
TEST_F(LowerConstants,NegateFloat32)86 TEST_F(LowerConstants, NegateFloat32) {
87    CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(-1.0)),
88         bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_neg(va_lut(16))));
89 
90    CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(-255.0)),
91         bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_neg(bi_half(va_lut(23), 0))));
92 }
93 
TEST_F(LowerConstants,NegateReplicateFloat16)94 TEST_F(LowerConstants, NegateReplicateFloat16)
95 {
96    CASE(bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_imm_f16(-255.0)),
97         bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_neg(bi_half(va_lut(23), 0))));
98 }
99 
TEST_F(LowerConstants,NegateVec2Float16)100 TEST_F(LowerConstants, NegateVec2Float16)
101 {
102    CASE(bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_imm_u32(0xBC008000)),
103         bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_neg(va_lut(27))));
104 }
105 
TEST_F(LowerConstants,Int8InInt32)106 TEST_F(LowerConstants, Int8InInt32)
107 {
108    CASE(bi_lshift_or_i32(b, bi_register(0), bi_imm_u32(0), bi_imm_u8(6)),
109         bi_lshift_or_i32(b, bi_register(0), va_lut(0), bi_byte(va_lut(9), 2)));
110 
111    CASE(bi_lshift_or_i32(b, bi_register(0), bi_imm_u32(0), bi_imm_u8(-2)),
112         bi_lshift_or_i32(b, bi_register(0), va_lut(0), bi_byte(va_lut(3), 0)));
113 }
114 
TEST_F(LowerConstants,ZeroExtendForUnsigned)115 TEST_F(LowerConstants, ZeroExtendForUnsigned)
116 {
117    CASE(bi_icmp_u32_to(b, bi_register(0), bi_register(0),
118                        bi_imm_u32(0xFF), BI_CMPF_LT, BI_RESULT_TYPE_I1),
119         bi_icmp_u32_to(b, bi_register(0), bi_register(0),
120                        bi_byte(va_lut(1), 0), BI_CMPF_LT, BI_RESULT_TYPE_I1));
121 
122    CASE(bi_icmp_u32_to(b, bi_register(0), bi_register(0),
123                        bi_imm_u32(0xFFFF), BI_CMPF_LT, BI_RESULT_TYPE_I1),
124         bi_icmp_u32_to(b, bi_register(0), bi_register(0),
125                        bi_half(va_lut(1), 0), BI_CMPF_LT, BI_RESULT_TYPE_I1));
126 }
127 
TEST_F(LowerConstants,SignExtendPositiveForSigned)128 TEST_F(LowerConstants, SignExtendPositiveForSigned)
129 {
130    CASE(bi_icmp_s32_to(b, bi_register(0), bi_register(0),
131                        bi_imm_u32(0x7F), BI_CMPF_LT, BI_RESULT_TYPE_I1),
132         bi_icmp_s32_to(b, bi_register(0), bi_register(0),
133                        bi_byte(va_lut(2), 3), BI_CMPF_LT, BI_RESULT_TYPE_I1));
134 
135    CASE(bi_icmp_s32_to(b, bi_register(0), bi_register(0),
136                        bi_imm_u32(0x7FFF), BI_CMPF_LT, BI_RESULT_TYPE_I1),
137         bi_icmp_s32_to(b, bi_register(0), bi_register(0),
138                        bi_half(va_lut(2), 1), BI_CMPF_LT, BI_RESULT_TYPE_I1));
139 }
140 
TEST_F(LowerConstants,SignExtendNegativeForSigned)141 TEST_F(LowerConstants, SignExtendNegativeForSigned)
142 {
143    CASE(bi_icmp_s32_to(b, bi_register(0), bi_register(0),
144                        bi_imm_u32(0xFFFFFFF8), BI_CMPF_LT, BI_RESULT_TYPE_I1),
145         bi_icmp_s32_to(b, bi_register(0), bi_register(0),
146                        bi_byte(va_lut(23), 0), BI_CMPF_LT, BI_RESULT_TYPE_I1));
147 
148    CASE(bi_icmp_s32_to(b, bi_register(0), bi_register(0),
149                        bi_imm_u32(0xFFFFFAFC), BI_CMPF_LT, BI_RESULT_TYPE_I1),
150         bi_icmp_s32_to(b, bi_register(0), bi_register(0),
151                        bi_half(va_lut(3), 1), BI_CMPF_LT, BI_RESULT_TYPE_I1));
152 }
153 
TEST_F(LowerConstants,DontZeroExtendForSigned)154 TEST_F(LowerConstants, DontZeroExtendForSigned)
155 {
156    CASE(bi_icmp_s32_to(b, bi_register(0), bi_register(0),
157                        bi_imm_u32(0xFF), BI_CMPF_LT, BI_RESULT_TYPE_I1),
158         bi_icmp_s32_to(b, bi_register(0), bi_register(0),
159                        bi_iadd_imm_i32(b, va_lut(0), 0xFF), BI_CMPF_LT, BI_RESULT_TYPE_I1));
160 
161    CASE(bi_icmp_s32_to(b, bi_register(0), bi_register(0),
162                        bi_imm_u32(0xFFFF), BI_CMPF_LT, BI_RESULT_TYPE_I1),
163         bi_icmp_s32_to(b, bi_register(0), bi_register(0),
164                        bi_iadd_imm_i32(b, va_lut(0), 0xFFFF),
165                        BI_CMPF_LT, BI_RESULT_TYPE_I1));
166 }
167 
TEST_F(LowerConstants,DontZeroExtendNegative)168 TEST_F(LowerConstants, DontZeroExtendNegative)
169 {
170    CASE(bi_icmp_u32_to(b, bi_register(0), bi_register(0),
171                        bi_imm_u32(0xFFFFFFF8), BI_CMPF_LT, BI_RESULT_TYPE_I1),
172         bi_icmp_u32_to(b, bi_register(0), bi_register(0),
173                        bi_iadd_imm_i32(b, va_lut(0), 0xFFFFFFF8),
174                        BI_CMPF_LT, BI_RESULT_TYPE_I1));
175 
176    CASE(bi_icmp_u32_to(b, bi_register(0), bi_register(0),
177                        bi_imm_u32(0xFFFFFAFC), BI_CMPF_LT, BI_RESULT_TYPE_I1),
178         bi_icmp_u32_to(b, bi_register(0), bi_register(0),
179                        bi_iadd_imm_i32(b, va_lut(0), 0xFFFFFAFC),
180                        BI_CMPF_LT, BI_RESULT_TYPE_I1));
181 }
182 
TEST_F(LowerConstants,HandleTrickyNegativesFP16)183 TEST_F(LowerConstants, HandleTrickyNegativesFP16)
184 {
185    CASE(bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_imm_f16(-57216.0)),
186         bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_half(va_lut(3), 1)));
187 
188    CASE(bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_imm_f16(57216.0)),
189         bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_neg(bi_half(va_lut(3), 1))));
190 }
191