1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2018 Intel Corporation
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
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci#include <gtest/gtest.h>
24bf215546Sopenharmony_ci#include "nir.h"
25bf215546Sopenharmony_ci#include "nir_builder.h"
26bf215546Sopenharmony_ci#include "util/half_float.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_cistatic void count_sequence(nir_const_value c[NIR_MAX_VEC_COMPONENTS],
29bf215546Sopenharmony_ci                           nir_alu_type full_type, int first);
30bf215546Sopenharmony_cistatic void negate(nir_const_value dst[NIR_MAX_VEC_COMPONENTS],
31bf215546Sopenharmony_ci                   const nir_const_value src[NIR_MAX_VEC_COMPONENTS],
32bf215546Sopenharmony_ci                   nir_alu_type full_type, unsigned components);
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ciclass const_value_negative_equal_test : public ::testing::Test {
35bf215546Sopenharmony_ciprotected:
36bf215546Sopenharmony_ci   const_value_negative_equal_test()
37bf215546Sopenharmony_ci   {
38bf215546Sopenharmony_ci      glsl_type_singleton_init_or_ref();
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci      memset(c1, 0, sizeof(c1));
41bf215546Sopenharmony_ci      memset(c2, 0, sizeof(c2));
42bf215546Sopenharmony_ci   }
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci   ~const_value_negative_equal_test()
45bf215546Sopenharmony_ci   {
46bf215546Sopenharmony_ci      glsl_type_singleton_decref();
47bf215546Sopenharmony_ci   }
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   nir_const_value c1[NIR_MAX_VEC_COMPONENTS];
50bf215546Sopenharmony_ci   nir_const_value c2[NIR_MAX_VEC_COMPONENTS];
51bf215546Sopenharmony_ci};
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ciclass alu_srcs_negative_equal_test : public ::testing::Test {
54bf215546Sopenharmony_ciprotected:
55bf215546Sopenharmony_ci   alu_srcs_negative_equal_test()
56bf215546Sopenharmony_ci   {
57bf215546Sopenharmony_ci      glsl_type_singleton_init_or_ref();
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci      static const nir_shader_compiler_options options = { };
60bf215546Sopenharmony_ci      bld = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, &options,
61bf215546Sopenharmony_ci                                           "negative equal tests");
62bf215546Sopenharmony_ci      memset(c1, 0, sizeof(c1));
63bf215546Sopenharmony_ci      memset(c2, 0, sizeof(c2));
64bf215546Sopenharmony_ci   }
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   ~alu_srcs_negative_equal_test()
67bf215546Sopenharmony_ci   {
68bf215546Sopenharmony_ci      ralloc_free(bld.shader);
69bf215546Sopenharmony_ci      glsl_type_singleton_decref();
70bf215546Sopenharmony_ci   }
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   struct nir_builder bld;
73bf215546Sopenharmony_ci   nir_const_value c1[NIR_MAX_VEC_COMPONENTS];
74bf215546Sopenharmony_ci   nir_const_value c2[NIR_MAX_VEC_COMPONENTS];
75bf215546Sopenharmony_ci};
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ciTEST_F(const_value_negative_equal_test, float32_zero)
78bf215546Sopenharmony_ci{
79bf215546Sopenharmony_ci   /* Verify that 0.0 negative-equals 0.0. */
80bf215546Sopenharmony_ci   EXPECT_TRUE(nir_const_value_negative_equal(c1[0], c1[0], nir_type_float32));
81bf215546Sopenharmony_ci}
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ciTEST_F(const_value_negative_equal_test, float64_zero)
84bf215546Sopenharmony_ci{
85bf215546Sopenharmony_ci   /* Verify that 0.0 negative-equals 0.0. */
86bf215546Sopenharmony_ci   EXPECT_TRUE(nir_const_value_negative_equal(c1[0], c1[0], nir_type_float64));
87bf215546Sopenharmony_ci}
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci/* Compare an object with non-zero values to itself.  This should always be
90bf215546Sopenharmony_ci * false.
91bf215546Sopenharmony_ci */
92bf215546Sopenharmony_ci#define compare_with_self(full_type)                                    \
93bf215546Sopenharmony_ciTEST_F(const_value_negative_equal_test, full_type ## _self)             \
94bf215546Sopenharmony_ci{                                                                       \
95bf215546Sopenharmony_ci   count_sequence(c1, full_type, 1);                                    \
96bf215546Sopenharmony_ci   EXPECT_FALSE(nir_const_value_negative_equal(c1[0], c1[0], full_type)); \
97bf215546Sopenharmony_ci}
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_cicompare_with_self(nir_type_float16)
100bf215546Sopenharmony_cicompare_with_self(nir_type_float32)
101bf215546Sopenharmony_cicompare_with_self(nir_type_float64)
102bf215546Sopenharmony_cicompare_with_self(nir_type_int8)
103bf215546Sopenharmony_cicompare_with_self(nir_type_uint8)
104bf215546Sopenharmony_cicompare_with_self(nir_type_int16)
105bf215546Sopenharmony_cicompare_with_self(nir_type_uint16)
106bf215546Sopenharmony_cicompare_with_self(nir_type_int32)
107bf215546Sopenharmony_cicompare_with_self(nir_type_uint32)
108bf215546Sopenharmony_cicompare_with_self(nir_type_int64)
109bf215546Sopenharmony_cicompare_with_self(nir_type_uint64)
110bf215546Sopenharmony_ci#undef compare_with_self
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci/* Compare an object with the negation of itself.  This should always be true.
113bf215546Sopenharmony_ci */
114bf215546Sopenharmony_ci#define compare_with_negation(full_type)                                \
115bf215546Sopenharmony_ciTEST_F(const_value_negative_equal_test, full_type ## _trivially_true)   \
116bf215546Sopenharmony_ci{                                                                       \
117bf215546Sopenharmony_ci   count_sequence(c1, full_type, 1);                                    \
118bf215546Sopenharmony_ci   negate(c2, c1, full_type, 1);                                        \
119bf215546Sopenharmony_ci   EXPECT_TRUE(nir_const_value_negative_equal(c1[0], c2[0], full_type)); \
120bf215546Sopenharmony_ci}
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_cicompare_with_negation(nir_type_float16)
123bf215546Sopenharmony_cicompare_with_negation(nir_type_float32)
124bf215546Sopenharmony_cicompare_with_negation(nir_type_float64)
125bf215546Sopenharmony_cicompare_with_negation(nir_type_int8)
126bf215546Sopenharmony_cicompare_with_negation(nir_type_uint8)
127bf215546Sopenharmony_cicompare_with_negation(nir_type_int16)
128bf215546Sopenharmony_cicompare_with_negation(nir_type_uint16)
129bf215546Sopenharmony_cicompare_with_negation(nir_type_int32)
130bf215546Sopenharmony_cicompare_with_negation(nir_type_uint32)
131bf215546Sopenharmony_cicompare_with_negation(nir_type_int64)
132bf215546Sopenharmony_cicompare_with_negation(nir_type_uint64)
133bf215546Sopenharmony_ci#undef compare_with_negation
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ciTEST_F(alu_srcs_negative_equal_test, trivial_float)
136bf215546Sopenharmony_ci{
137bf215546Sopenharmony_ci   nir_ssa_def *two = nir_imm_float(&bld, 2.0f);
138bf215546Sopenharmony_ci   nir_ssa_def *negative_two = nir_imm_float(&bld, -2.0f);
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci   nir_ssa_def *result = nir_fadd(&bld, two, negative_two);
141bf215546Sopenharmony_ci   nir_alu_instr *instr = nir_instr_as_alu(result->parent_instr);
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci   ASSERT_NE((void *) 0, instr);
144bf215546Sopenharmony_ci   EXPECT_TRUE(nir_alu_srcs_negative_equal(instr, instr, 0, 1));
145bf215546Sopenharmony_ci   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 0, 0));
146bf215546Sopenharmony_ci   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 1, 1));
147bf215546Sopenharmony_ci}
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ciTEST_F(alu_srcs_negative_equal_test, trivial_int)
150bf215546Sopenharmony_ci{
151bf215546Sopenharmony_ci   nir_ssa_def *two = nir_imm_int(&bld, 2);
152bf215546Sopenharmony_ci   nir_ssa_def *negative_two = nir_imm_int(&bld, -2);
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci   nir_ssa_def *result = nir_iadd(&bld, two, negative_two);
155bf215546Sopenharmony_ci   nir_alu_instr *instr = nir_instr_as_alu(result->parent_instr);
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci   ASSERT_NE((void *) 0, instr);
158bf215546Sopenharmony_ci   EXPECT_TRUE(nir_alu_srcs_negative_equal(instr, instr, 0, 1));
159bf215546Sopenharmony_ci   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 0, 0));
160bf215546Sopenharmony_ci   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 1, 1));
161bf215546Sopenharmony_ci}
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ciTEST_F(alu_srcs_negative_equal_test, trivial_negation_float)
164bf215546Sopenharmony_ci{
165bf215546Sopenharmony_ci   /* Cannot just do the negation of a nir_load_const_instr because
166bf215546Sopenharmony_ci    * nir_alu_srcs_negative_equal expects that constant folding will convert
167bf215546Sopenharmony_ci    * fneg(2.0) to just -2.0.
168bf215546Sopenharmony_ci    */
169bf215546Sopenharmony_ci   nir_ssa_def *two = nir_imm_float(&bld, 2.0f);
170bf215546Sopenharmony_ci   nir_ssa_def *two_plus_two = nir_fadd(&bld, two, two);
171bf215546Sopenharmony_ci   nir_ssa_def *negation = nir_fneg(&bld, two_plus_two);
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci   nir_ssa_def *result = nir_fadd(&bld, two_plus_two, negation);
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci   nir_alu_instr *instr = nir_instr_as_alu(result->parent_instr);
176bf215546Sopenharmony_ci
177bf215546Sopenharmony_ci   ASSERT_NE((void *) 0, instr);
178bf215546Sopenharmony_ci   EXPECT_TRUE(nir_alu_srcs_negative_equal(instr, instr, 0, 1));
179bf215546Sopenharmony_ci   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 0, 0));
180bf215546Sopenharmony_ci   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 1, 1));
181bf215546Sopenharmony_ci}
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ciTEST_F(alu_srcs_negative_equal_test, trivial_negation_int)
184bf215546Sopenharmony_ci{
185bf215546Sopenharmony_ci   /* Cannot just do the negation of a nir_load_const_instr because
186bf215546Sopenharmony_ci    * nir_alu_srcs_negative_equal expects that constant folding will convert
187bf215546Sopenharmony_ci    * ineg(2) to just -2.
188bf215546Sopenharmony_ci    */
189bf215546Sopenharmony_ci   nir_ssa_def *two = nir_imm_int(&bld, 2);
190bf215546Sopenharmony_ci   nir_ssa_def *two_plus_two = nir_iadd(&bld, two, two);
191bf215546Sopenharmony_ci   nir_ssa_def *negation = nir_ineg(&bld, two_plus_two);
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci   nir_ssa_def *result = nir_iadd(&bld, two_plus_two, negation);
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_ci   nir_alu_instr *instr = nir_instr_as_alu(result->parent_instr);
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ci   ASSERT_NE((void *) 0, instr);
198bf215546Sopenharmony_ci   EXPECT_TRUE(nir_alu_srcs_negative_equal(instr, instr, 0, 1));
199bf215546Sopenharmony_ci   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 0, 0));
200bf215546Sopenharmony_ci   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 1, 1));
201bf215546Sopenharmony_ci}
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_ci/* Compare an object with non-zero values to itself.  This should always be
204bf215546Sopenharmony_ci * false.
205bf215546Sopenharmony_ci */
206bf215546Sopenharmony_ci#define compare_with_self(full_type)                                    \
207bf215546Sopenharmony_ciTEST_F(alu_srcs_negative_equal_test, full_type ## _self)                \
208bf215546Sopenharmony_ci{                                                                       \
209bf215546Sopenharmony_ci   count_sequence(c1, full_type, 1);                                    \
210bf215546Sopenharmony_ci   nir_ssa_def *a = nir_build_imm(&bld,                                 \
211bf215546Sopenharmony_ci                                  NIR_MAX_VEC_COMPONENTS,               \
212bf215546Sopenharmony_ci                                  nir_alu_type_get_type_size(full_type), \
213bf215546Sopenharmony_ci                                  c1);                                  \
214bf215546Sopenharmony_ci   nir_ssa_def *result;                                                 \
215bf215546Sopenharmony_ci   if (nir_alu_type_get_base_type(full_type) == nir_type_float)         \
216bf215546Sopenharmony_ci      result = nir_fadd(&bld, a, a);                                    \
217bf215546Sopenharmony_ci   else                                                                 \
218bf215546Sopenharmony_ci      result = nir_iadd(&bld, a, a);                                    \
219bf215546Sopenharmony_ci   nir_alu_instr *instr = nir_instr_as_alu(result->parent_instr);       \
220bf215546Sopenharmony_ci   ASSERT_NE((void *) 0, instr);                                        \
221bf215546Sopenharmony_ci   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 0, 0));       \
222bf215546Sopenharmony_ci   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 0, 1));       \
223bf215546Sopenharmony_ci   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 1, 0));       \
224bf215546Sopenharmony_ci   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 1, 1));       \
225bf215546Sopenharmony_ci}
226bf215546Sopenharmony_ci
227bf215546Sopenharmony_cicompare_with_self(nir_type_float16)
228bf215546Sopenharmony_cicompare_with_self(nir_type_float32)
229bf215546Sopenharmony_cicompare_with_self(nir_type_float64)
230bf215546Sopenharmony_cicompare_with_self(nir_type_int8)
231bf215546Sopenharmony_cicompare_with_self(nir_type_uint8)
232bf215546Sopenharmony_cicompare_with_self(nir_type_int16)
233bf215546Sopenharmony_cicompare_with_self(nir_type_uint16)
234bf215546Sopenharmony_cicompare_with_self(nir_type_int32)
235bf215546Sopenharmony_cicompare_with_self(nir_type_uint32)
236bf215546Sopenharmony_cicompare_with_self(nir_type_int64)
237bf215546Sopenharmony_cicompare_with_self(nir_type_uint64)
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_ci/* Compare an object with the negation of itself.  This should always be true.
240bf215546Sopenharmony_ci */
241bf215546Sopenharmony_ci#define compare_with_negation(full_type)                                \
242bf215546Sopenharmony_ciTEST_F(alu_srcs_negative_equal_test, full_type ## _trivially_true)      \
243bf215546Sopenharmony_ci{                                                                       \
244bf215546Sopenharmony_ci   count_sequence(c1, full_type, 1);                                    \
245bf215546Sopenharmony_ci   negate(c2, c1, full_type, NIR_MAX_VEC_COMPONENTS);                   \
246bf215546Sopenharmony_ci   nir_ssa_def *a = nir_build_imm(&bld,                                 \
247bf215546Sopenharmony_ci                                  NIR_MAX_VEC_COMPONENTS,               \
248bf215546Sopenharmony_ci                                  nir_alu_type_get_type_size(full_type), \
249bf215546Sopenharmony_ci                                  c1);                                  \
250bf215546Sopenharmony_ci   nir_ssa_def *b = nir_build_imm(&bld,                                 \
251bf215546Sopenharmony_ci                                  NIR_MAX_VEC_COMPONENTS,               \
252bf215546Sopenharmony_ci                                  nir_alu_type_get_type_size(full_type), \
253bf215546Sopenharmony_ci                                  c2);                                  \
254bf215546Sopenharmony_ci   nir_ssa_def *result;                                                 \
255bf215546Sopenharmony_ci   if (nir_alu_type_get_base_type(full_type) == nir_type_float)         \
256bf215546Sopenharmony_ci      result = nir_fadd(&bld, a, b);                                    \
257bf215546Sopenharmony_ci   else                                                                 \
258bf215546Sopenharmony_ci      result = nir_iadd(&bld, a, b);                                    \
259bf215546Sopenharmony_ci   nir_alu_instr *instr = nir_instr_as_alu(result->parent_instr);       \
260bf215546Sopenharmony_ci   ASSERT_NE((void *) 0, instr);                                        \
261bf215546Sopenharmony_ci   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 0, 0));       \
262bf215546Sopenharmony_ci   EXPECT_TRUE(nir_alu_srcs_negative_equal(instr, instr, 0, 1));        \
263bf215546Sopenharmony_ci   EXPECT_TRUE(nir_alu_srcs_negative_equal(instr, instr, 1, 0));        \
264bf215546Sopenharmony_ci   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 1, 1));       \
265bf215546Sopenharmony_ci}
266bf215546Sopenharmony_ci
267bf215546Sopenharmony_cicompare_with_negation(nir_type_float16)
268bf215546Sopenharmony_cicompare_with_negation(nir_type_float32)
269bf215546Sopenharmony_cicompare_with_negation(nir_type_float64)
270bf215546Sopenharmony_cicompare_with_negation(nir_type_int8)
271bf215546Sopenharmony_cicompare_with_negation(nir_type_uint8)
272bf215546Sopenharmony_cicompare_with_negation(nir_type_int16)
273bf215546Sopenharmony_cicompare_with_negation(nir_type_uint16)
274bf215546Sopenharmony_cicompare_with_negation(nir_type_int32)
275bf215546Sopenharmony_cicompare_with_negation(nir_type_uint32)
276bf215546Sopenharmony_cicompare_with_negation(nir_type_int64)
277bf215546Sopenharmony_cicompare_with_negation(nir_type_uint64)
278bf215546Sopenharmony_ci
279bf215546Sopenharmony_ciTEST_F(alu_srcs_negative_equal_test, swizzle_scalar_to_vector)
280bf215546Sopenharmony_ci{
281bf215546Sopenharmony_ci   nir_ssa_def *v = nir_imm_vec2(&bld, 1.0, -1.0);
282bf215546Sopenharmony_ci   const uint8_t s0[4] = { 0, 0, 0, 0 };
283bf215546Sopenharmony_ci   const uint8_t s1[4] = { 1, 1, 1, 1 };
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci   /* We can't use nir_swizzle here because it inserts an extra MOV. */
286bf215546Sopenharmony_ci   nir_alu_instr *instr = nir_alu_instr_create(bld.shader, nir_op_fadd);
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_ci   instr->src[0].src = nir_src_for_ssa(v);
289bf215546Sopenharmony_ci   instr->src[1].src = nir_src_for_ssa(v);
290bf215546Sopenharmony_ci
291bf215546Sopenharmony_ci   memcpy(&instr->src[0].swizzle, s0, sizeof(s0));
292bf215546Sopenharmony_ci   memcpy(&instr->src[1].swizzle, s1, sizeof(s1));
293bf215546Sopenharmony_ci
294bf215546Sopenharmony_ci   nir_builder_alu_instr_finish_and_insert(&bld, instr);
295bf215546Sopenharmony_ci
296bf215546Sopenharmony_ci   EXPECT_TRUE(nir_alu_srcs_negative_equal(instr, instr, 0, 1));
297bf215546Sopenharmony_ci}
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_ciTEST_F(alu_srcs_negative_equal_test, unused_components_mismatch)
300bf215546Sopenharmony_ci{
301bf215546Sopenharmony_ci   nir_ssa_def *v1 = nir_imm_vec4(&bld, -2.0, 18.0, 43.0,  1.0);
302bf215546Sopenharmony_ci   nir_ssa_def *v2 = nir_imm_vec4(&bld,  2.0, 99.0, 76.0, -1.0);
303bf215546Sopenharmony_ci
304bf215546Sopenharmony_ci   nir_ssa_def *result = nir_fadd(&bld, v1, v2);
305bf215546Sopenharmony_ci
306bf215546Sopenharmony_ci   nir_alu_instr *instr = nir_instr_as_alu(result->parent_instr);
307bf215546Sopenharmony_ci
308bf215546Sopenharmony_ci   /* Disable the channels that aren't negations of each other. */
309bf215546Sopenharmony_ci   nir_register *reg = nir_local_reg_create(bld.impl);
310bf215546Sopenharmony_ci   nir_instr_rewrite_dest(&instr->instr, &instr->dest.dest, nir_dest_for_reg(reg));
311bf215546Sopenharmony_ci   instr->dest.write_mask = 8 + 1;
312bf215546Sopenharmony_ci
313bf215546Sopenharmony_ci   EXPECT_TRUE(nir_alu_srcs_negative_equal(instr, instr, 0, 1));
314bf215546Sopenharmony_ci}
315bf215546Sopenharmony_ci
316bf215546Sopenharmony_cistatic void
317bf215546Sopenharmony_cicount_sequence(nir_const_value c[NIR_MAX_VEC_COMPONENTS],
318bf215546Sopenharmony_ci               nir_alu_type full_type, int first)
319bf215546Sopenharmony_ci{
320bf215546Sopenharmony_ci   switch (full_type) {
321bf215546Sopenharmony_ci   case nir_type_float16:
322bf215546Sopenharmony_ci      for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
323bf215546Sopenharmony_ci         c[i].u16 = _mesa_float_to_half(float(i + first));
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_ci      break;
326bf215546Sopenharmony_ci
327bf215546Sopenharmony_ci   case nir_type_float32:
328bf215546Sopenharmony_ci      for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
329bf215546Sopenharmony_ci         c[i].f32 = float(i + first);
330bf215546Sopenharmony_ci
331bf215546Sopenharmony_ci      break;
332bf215546Sopenharmony_ci
333bf215546Sopenharmony_ci   case nir_type_float64:
334bf215546Sopenharmony_ci      for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
335bf215546Sopenharmony_ci         c[i].f64 = double(i + first);
336bf215546Sopenharmony_ci
337bf215546Sopenharmony_ci      break;
338bf215546Sopenharmony_ci
339bf215546Sopenharmony_ci   case nir_type_int8:
340bf215546Sopenharmony_ci   case nir_type_uint8:
341bf215546Sopenharmony_ci      for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
342bf215546Sopenharmony_ci         c[i].i8 = i + first;
343bf215546Sopenharmony_ci
344bf215546Sopenharmony_ci      break;
345bf215546Sopenharmony_ci
346bf215546Sopenharmony_ci   case nir_type_int16:
347bf215546Sopenharmony_ci   case nir_type_uint16:
348bf215546Sopenharmony_ci      for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
349bf215546Sopenharmony_ci         c[i].i16 = i + first;
350bf215546Sopenharmony_ci
351bf215546Sopenharmony_ci      break;
352bf215546Sopenharmony_ci
353bf215546Sopenharmony_ci   case nir_type_int32:
354bf215546Sopenharmony_ci   case nir_type_uint32:
355bf215546Sopenharmony_ci      for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
356bf215546Sopenharmony_ci         c[i].i32 = i + first;
357bf215546Sopenharmony_ci
358bf215546Sopenharmony_ci      break;
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_ci   case nir_type_int64:
361bf215546Sopenharmony_ci   case nir_type_uint64:
362bf215546Sopenharmony_ci      for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
363bf215546Sopenharmony_ci         c[i].i64 = i + first;
364bf215546Sopenharmony_ci
365bf215546Sopenharmony_ci      break;
366bf215546Sopenharmony_ci
367bf215546Sopenharmony_ci   case nir_type_bool:
368bf215546Sopenharmony_ci   default:
369bf215546Sopenharmony_ci      unreachable("invalid base type");
370bf215546Sopenharmony_ci   }
371bf215546Sopenharmony_ci}
372bf215546Sopenharmony_ci
373bf215546Sopenharmony_cistatic void
374bf215546Sopenharmony_cinegate(nir_const_value dst[NIR_MAX_VEC_COMPONENTS],
375bf215546Sopenharmony_ci       const nir_const_value src[NIR_MAX_VEC_COMPONENTS],
376bf215546Sopenharmony_ci       nir_alu_type full_type, unsigned components)
377bf215546Sopenharmony_ci{
378bf215546Sopenharmony_ci   switch (full_type) {
379bf215546Sopenharmony_ci   case nir_type_float16:
380bf215546Sopenharmony_ci      for (unsigned i = 0; i < components; i++)
381bf215546Sopenharmony_ci         dst[i].u16 = _mesa_float_to_half(-_mesa_half_to_float(src[i].u16));
382bf215546Sopenharmony_ci
383bf215546Sopenharmony_ci      break;
384bf215546Sopenharmony_ci
385bf215546Sopenharmony_ci   case nir_type_float32:
386bf215546Sopenharmony_ci      for (unsigned i = 0; i < components; i++)
387bf215546Sopenharmony_ci         dst[i].f32 = -src[i].f32;
388bf215546Sopenharmony_ci
389bf215546Sopenharmony_ci      break;
390bf215546Sopenharmony_ci
391bf215546Sopenharmony_ci   case nir_type_float64:
392bf215546Sopenharmony_ci      for (unsigned i = 0; i < components; i++)
393bf215546Sopenharmony_ci         dst[i].f64 = -src[i].f64;
394bf215546Sopenharmony_ci
395bf215546Sopenharmony_ci      break;
396bf215546Sopenharmony_ci
397bf215546Sopenharmony_ci   case nir_type_int8:
398bf215546Sopenharmony_ci   case nir_type_uint8:
399bf215546Sopenharmony_ci      for (unsigned i = 0; i < components; i++)
400bf215546Sopenharmony_ci         dst[i].i8 = -src[i].i8;
401bf215546Sopenharmony_ci
402bf215546Sopenharmony_ci      break;
403bf215546Sopenharmony_ci
404bf215546Sopenharmony_ci   case nir_type_int16:
405bf215546Sopenharmony_ci   case nir_type_uint16:
406bf215546Sopenharmony_ci      for (unsigned i = 0; i < components; i++)
407bf215546Sopenharmony_ci         dst[i].i16 = -src[i].i16;
408bf215546Sopenharmony_ci
409bf215546Sopenharmony_ci      break;
410bf215546Sopenharmony_ci
411bf215546Sopenharmony_ci   case nir_type_int32:
412bf215546Sopenharmony_ci   case nir_type_uint32:
413bf215546Sopenharmony_ci      for (unsigned i = 0; i < components; i++)
414bf215546Sopenharmony_ci         dst[i].i32 = -src[i].i32;
415bf215546Sopenharmony_ci
416bf215546Sopenharmony_ci      break;
417bf215546Sopenharmony_ci
418bf215546Sopenharmony_ci   case nir_type_int64:
419bf215546Sopenharmony_ci   case nir_type_uint64:
420bf215546Sopenharmony_ci      for (unsigned i = 0; i < components; i++)
421bf215546Sopenharmony_ci         dst[i].i64 = -src[i].i64;
422bf215546Sopenharmony_ci
423bf215546Sopenharmony_ci      break;
424bf215546Sopenharmony_ci
425bf215546Sopenharmony_ci   case nir_type_bool:
426bf215546Sopenharmony_ci   default:
427bf215546Sopenharmony_ci      unreachable("invalid base type");
428bf215546Sopenharmony_ci   }
429bf215546Sopenharmony_ci}
430