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#include "util/u_cpu_detect.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include <gtest/gtest.h>
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_cistatic inline void
32bf215546Sopenharmony_ciadd_imm(bi_context *ctx)
33bf215546Sopenharmony_ci{
34bf215546Sopenharmony_ci   bi_foreach_instr_global(ctx, I) {
35bf215546Sopenharmony_ci      va_fuse_add_imm(I);
36bf215546Sopenharmony_ci   }
37bf215546Sopenharmony_ci}
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#define CASE(instr, expected) INSTRUCTION_CASE(instr, expected, add_imm)
40bf215546Sopenharmony_ci#define NEGCASE(instr) CASE(instr, instr)
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ciclass AddImm : public testing::Test {
43bf215546Sopenharmony_ciprotected:
44bf215546Sopenharmony_ci   AddImm() {
45bf215546Sopenharmony_ci      mem_ctx = ralloc_context(NULL);
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci      /* For bi_imm_f16 */
48bf215546Sopenharmony_ci      util_cpu_detect();
49bf215546Sopenharmony_ci   }
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   ~AddImm() {
52bf215546Sopenharmony_ci      ralloc_free(mem_ctx);
53bf215546Sopenharmony_ci   }
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci   void *mem_ctx;
56bf215546Sopenharmony_ci};
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ciTEST_F(AddImm, Basic) {
60bf215546Sopenharmony_ci   CASE(bi_mov_i32_to(b, bi_register(63), bi_imm_u32(0xABAD1DEA)),
61bf215546Sopenharmony_ci        bi_iadd_imm_i32_to(b, bi_register(63), bi_zero(), 0xABAD1DEA));
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci   CASE(bi_fadd_f32_to(b, bi_register(1), bi_register(2), bi_imm_f32(42.0)),
64bf215546Sopenharmony_ci        bi_fadd_imm_f32_to(b, bi_register(1), bi_register(2), fui(42.0)));
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   CASE(bi_fadd_f32_to(b, bi_register(1), bi_discard(bi_register(2)), bi_imm_f32(42.0)),
67bf215546Sopenharmony_ci        bi_fadd_imm_f32_to(b, bi_register(1), bi_discard(bi_register(2)), fui(42.0)));
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci   CASE(bi_fadd_f32_to(b, bi_register(1), bi_discard(bi_register(2)), bi_neg(bi_imm_f32(42.0))),
70bf215546Sopenharmony_ci        bi_fadd_imm_f32_to(b, bi_register(1), bi_discard(bi_register(2)), fui(-42.0)));
71bf215546Sopenharmony_ci}
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ciTEST_F(AddImm, Commutativty) {
74bf215546Sopenharmony_ci   CASE(bi_fadd_f32_to(b, bi_register(1), bi_imm_f32(42.0), bi_register(2)),
75bf215546Sopenharmony_ci        bi_fadd_imm_f32_to(b, bi_register(1), bi_register(2), fui(42.0)));
76bf215546Sopenharmony_ci}
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_ciTEST_F(AddImm, NoModifiers) {
79bf215546Sopenharmony_ci   NEGCASE(bi_fadd_f32_to(b, bi_register(1), bi_abs(bi_register(2)), bi_imm_f32(42.0)));
80bf215546Sopenharmony_ci   NEGCASE(bi_fadd_f32_to(b, bi_register(1), bi_neg(bi_register(2)), bi_imm_f32(42.0)));
81bf215546Sopenharmony_ci   NEGCASE(bi_fadd_f32_to(b, bi_register(1), bi_swz_16(bi_register(2), false, false), bi_imm_f32(42.0)));
82bf215546Sopenharmony_ci}
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ciTEST_F(AddImm, NoClamp) {
85bf215546Sopenharmony_ci   NEGCASE({
86bf215546Sopenharmony_ci      bi_instr *I = bi_fadd_f32_to(b, bi_register(1), bi_register(2),
87bf215546Sopenharmony_ci            bi_imm_f32(42.0));
88bf215546Sopenharmony_ci      I->clamp = BI_CLAMP_CLAMP_M1_1;
89bf215546Sopenharmony_ci   });
90bf215546Sopenharmony_ci}
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ciTEST_F(AddImm, OtherTypes) {
93bf215546Sopenharmony_ci   CASE(bi_fadd_v2f16_to(b, bi_register(1), bi_register(2), bi_imm_f16(42.0)),
94bf215546Sopenharmony_ci        bi_fadd_imm_v2f16_to(b, bi_register(1), bi_register(2), 0x51405140));
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   CASE(bi_iadd_u32_to(b, bi_register(1), bi_register(2), bi_imm_u32(0xDEADBEEF), false),
97bf215546Sopenharmony_ci        bi_iadd_imm_i32_to(b, bi_register(1), bi_register(2), 0xDEADBEEF));
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   CASE(bi_iadd_v2u16_to(b, bi_register(1), bi_register(2), bi_imm_u32(0xDEADBEEF), false),
100bf215546Sopenharmony_ci        bi_iadd_imm_v2i16_to(b, bi_register(1), bi_register(2), 0xDEADBEEF));
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci   CASE(bi_iadd_v4u8_to(b, bi_register(1), bi_register(2), bi_imm_u32(0xDEADBEEF), false),
103bf215546Sopenharmony_ci        bi_iadd_imm_v4i8_to(b, bi_register(1), bi_register(2), 0xDEADBEEF));
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   CASE(bi_iadd_s32_to(b, bi_register(1), bi_register(2), bi_imm_u32(0xDEADBEEF), false),
106bf215546Sopenharmony_ci        bi_iadd_imm_i32_to(b, bi_register(1), bi_register(2), 0xDEADBEEF));
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   CASE(bi_iadd_v2s16_to(b, bi_register(1), bi_register(2), bi_imm_u32(0xDEADBEEF), false),
109bf215546Sopenharmony_ci        bi_iadd_imm_v2i16_to(b, bi_register(1), bi_register(2), 0xDEADBEEF));
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   CASE(bi_iadd_v4s8_to(b, bi_register(1), bi_register(2), bi_imm_u32(0xDEADBEEF), false),
112bf215546Sopenharmony_ci        bi_iadd_imm_v4i8_to(b, bi_register(1), bi_register(2), 0xDEADBEEF));
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   NEGCASE(bi_iadd_u32_to(b, bi_register(1), bi_swz_16(bi_register(2), false, false), bi_imm_u32(0xDEADBEEF), false));
115bf215546Sopenharmony_ci   NEGCASE(bi_iadd_v2u16_to(b, bi_register(1), bi_swz_16(bi_register(2), false, false), bi_imm_u32(0xDEADBEEF), false));
116bf215546Sopenharmony_ci   NEGCASE(bi_iadd_u32_to(b, bi_register(1), bi_register(2), bi_imm_u32(0xDEADBEEF), true));
117bf215546Sopenharmony_ci   NEGCASE(bi_iadd_s32_to(b, bi_register(1), bi_swz_16(bi_register(2), false, false), bi_imm_u32(0xDEADBEEF), false));
118bf215546Sopenharmony_ci   NEGCASE(bi_iadd_v2s16_to(b, bi_register(1), bi_swz_16(bi_register(2), false, false), bi_imm_u32(0xDEADBEEF), false));
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci   NEGCASE(bi_iadd_s32_to(b, bi_register(1), bi_register(2), bi_imm_u32(0xDEADBEEF), true));
121bf215546Sopenharmony_ci}
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ciTEST_F(AddImm, Int8) {
124bf215546Sopenharmony_ci   bi_index idx = bi_register(2);
125bf215546Sopenharmony_ci   idx.swizzle = BI_SWIZZLE_B0000;
126bf215546Sopenharmony_ci   NEGCASE(bi_iadd_v4u8_to(b, bi_register(1), idx, bi_imm_u32(0xDEADBEEF), false));
127bf215546Sopenharmony_ci   NEGCASE(bi_iadd_v4s8_to(b, bi_register(1), idx, bi_imm_u32(0xDEADBEEF), false));
128bf215546Sopenharmony_ci}
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ciTEST_F(AddImm, OnlyRTE) {
131bf215546Sopenharmony_ci   NEGCASE({
132bf215546Sopenharmony_ci         bi_instr *I = bi_fadd_f32_to(b, bi_register(1), bi_register(2), bi_imm_f32(42.0));
133bf215546Sopenharmony_ci         I->round = BI_ROUND_RTP;
134bf215546Sopenharmony_ci   });
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci   NEGCASE({
137bf215546Sopenharmony_ci         bi_instr *I = bi_fadd_v2f16_to(b, bi_register(1), bi_register(2), bi_imm_f16(42.0));
138bf215546Sopenharmony_ci         I->round = BI_ROUND_RTZ;
139bf215546Sopenharmony_ci   });
140bf215546Sopenharmony_ci}
141bf215546Sopenharmony_ci
142