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