1/*
2 * Copyright © 2018 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23#include <gtest/gtest.h>
24#include "nir.h"
25#include "nir_builder.h"
26#include "util/half_float.h"
27
28static void count_sequence(nir_const_value c[NIR_MAX_VEC_COMPONENTS],
29                           nir_alu_type base_type, unsigned bits, int first);
30static void negate(nir_const_value dst[NIR_MAX_VEC_COMPONENTS],
31                   const nir_const_value src[NIR_MAX_VEC_COMPONENTS],
32                   nir_alu_type base_type, unsigned bits, unsigned components);
33
34class const_value_negative_equal_test : public ::testing::Test {
35protected:
36   const_value_negative_equal_test()
37   {
38      memset(c1, 0, sizeof(c1));
39      memset(c2, 0, sizeof(c2));
40   }
41
42   ~const_value_negative_equal_test()
43   {
44      /* empty */
45   }
46
47   nir_const_value c1[NIR_MAX_VEC_COMPONENTS];
48   nir_const_value c2[NIR_MAX_VEC_COMPONENTS];
49};
50
51class alu_srcs_negative_equal_test : public ::testing::Test {
52protected:
53   alu_srcs_negative_equal_test()
54   {
55      static const nir_shader_compiler_options options = { };
56      nir_builder_init_simple_shader(&bld, NULL, MESA_SHADER_VERTEX, &options);
57   }
58
59   ~alu_srcs_negative_equal_test()
60   {
61      ralloc_free(bld.shader);
62   }
63
64   struct nir_builder bld;
65};
66
67TEST_F(const_value_negative_equal_test, float32_zero)
68{
69   /* Verify that 0.0 negative-equals 0.0. */
70   EXPECT_TRUE(nir_const_value_negative_equal(c1, c1, NIR_MAX_VEC_COMPONENTS,
71                                              nir_type_float, 32));
72}
73
74TEST_F(const_value_negative_equal_test, float64_zero)
75{
76   /* Verify that 0.0 negative-equals 0.0. */
77   EXPECT_TRUE(nir_const_value_negative_equal(c1, c1, NIR_MAX_VEC_COMPONENTS,
78                                              nir_type_float, 64));
79}
80
81/* Compare an object with non-zero values to itself.  This should always be
82 * false.
83 */
84#define compare_with_self(base_type, bits) \
85TEST_F(const_value_negative_equal_test, base_type ## bits ## _self)     \
86{                                                                       \
87   count_sequence(c1, base_type, bits, 1);                              \
88   EXPECT_FALSE(nir_const_value_negative_equal(c1, c1,                  \
89                                               NIR_MAX_VEC_COMPONENTS,  \
90                                               base_type, bits));       \
91}
92
93compare_with_self(nir_type_float, 16)
94compare_with_self(nir_type_float, 32)
95compare_with_self(nir_type_float, 64)
96compare_with_self(nir_type_int, 8)
97compare_with_self(nir_type_uint, 8)
98compare_with_self(nir_type_int, 16)
99compare_with_self(nir_type_uint, 16)
100compare_with_self(nir_type_int, 32)
101compare_with_self(nir_type_uint, 32)
102compare_with_self(nir_type_int, 64)
103compare_with_self(nir_type_uint, 64)
104
105/* Compare an object with the negation of itself.  This should always be true.
106 */
107#define compare_with_negation(base_type, bits) \
108TEST_F(const_value_negative_equal_test, base_type ## bits ## _trivially_true) \
109{                                                                       \
110   count_sequence(c1, base_type, bits, 1);                              \
111   negate(c2, c1, base_type, bits, NIR_MAX_VEC_COMPONENTS);             \
112   EXPECT_TRUE(nir_const_value_negative_equal(c1, c2,                   \
113                                              NIR_MAX_VEC_COMPONENTS,   \
114                                              base_type, bits));        \
115}
116
117compare_with_negation(nir_type_float, 16)
118compare_with_negation(nir_type_float, 32)
119compare_with_negation(nir_type_float, 64)
120compare_with_negation(nir_type_int, 8)
121compare_with_negation(nir_type_uint, 8)
122compare_with_negation(nir_type_int, 16)
123compare_with_negation(nir_type_uint, 16)
124compare_with_negation(nir_type_int, 32)
125compare_with_negation(nir_type_uint, 32)
126compare_with_negation(nir_type_int, 64)
127compare_with_negation(nir_type_uint, 64)
128
129/* Compare fewer than the maximum possible components.  All of the components
130 * that are compared a negative-equal, but the extra components are not.
131 */
132#define compare_fewer_components(base_type, bits) \
133TEST_F(const_value_negative_equal_test, base_type ## bits ## _fewer_components) \
134{                                                                       \
135   count_sequence(c1, base_type, bits, 1);                              \
136   negate(c2, c1, base_type, bits, 3);                                  \
137   EXPECT_TRUE(nir_const_value_negative_equal(c1, c2, 3, base_type, bits)); \
138   EXPECT_FALSE(nir_const_value_negative_equal(c1, c2,                  \
139                                               NIR_MAX_VEC_COMPONENTS,  \
140                                               base_type, bits));       \
141}
142
143compare_fewer_components(nir_type_float, 16)
144compare_fewer_components(nir_type_float, 32)
145compare_fewer_components(nir_type_float, 64)
146compare_fewer_components(nir_type_int, 8)
147compare_fewer_components(nir_type_uint, 8)
148compare_fewer_components(nir_type_int, 16)
149compare_fewer_components(nir_type_uint, 16)
150compare_fewer_components(nir_type_int, 32)
151compare_fewer_components(nir_type_uint, 32)
152compare_fewer_components(nir_type_int, 64)
153compare_fewer_components(nir_type_uint, 64)
154
155TEST_F(alu_srcs_negative_equal_test, trivial_float)
156{
157   nir_ssa_def *two = nir_imm_float(&bld, 2.0f);
158   nir_ssa_def *negative_two = nir_imm_float(&bld, -2.0f);
159
160   nir_ssa_def *result = nir_fadd(&bld, two, negative_two);
161   nir_alu_instr *instr = nir_instr_as_alu(result->parent_instr);
162
163   ASSERT_NE((void *) 0, instr);
164   EXPECT_TRUE(nir_alu_srcs_negative_equal(instr, instr, 0, 1));
165   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 0, 0));
166   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 1, 1));
167}
168
169TEST_F(alu_srcs_negative_equal_test, trivial_int)
170{
171   nir_ssa_def *two = nir_imm_int(&bld, 2);
172   nir_ssa_def *negative_two = nir_imm_int(&bld, -2);
173
174   nir_ssa_def *result = nir_iadd(&bld, two, negative_two);
175   nir_alu_instr *instr = nir_instr_as_alu(result->parent_instr);
176
177   ASSERT_NE((void *) 0, instr);
178   EXPECT_TRUE(nir_alu_srcs_negative_equal(instr, instr, 0, 1));
179   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 0, 0));
180   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 1, 1));
181}
182
183TEST_F(alu_srcs_negative_equal_test, trivial_negation_float)
184{
185   /* Cannot just do the negation of a nir_load_const_instr because
186    * nir_alu_srcs_negative_equal expects that constant folding will convert
187    * fneg(2.0) to just -2.0.
188    */
189   nir_ssa_def *two = nir_imm_float(&bld, 2.0f);
190   nir_ssa_def *two_plus_two = nir_fadd(&bld, two, two);
191   nir_ssa_def *negation = nir_fneg(&bld, two_plus_two);
192
193   nir_ssa_def *result = nir_fadd(&bld, two_plus_two, negation);
194
195   nir_alu_instr *instr = nir_instr_as_alu(result->parent_instr);
196
197   ASSERT_NE((void *) 0, instr);
198   EXPECT_TRUE(nir_alu_srcs_negative_equal(instr, instr, 0, 1));
199   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 0, 0));
200   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 1, 1));
201}
202
203TEST_F(alu_srcs_negative_equal_test, trivial_negation_int)
204{
205   /* Cannot just do the negation of a nir_load_const_instr because
206    * nir_alu_srcs_negative_equal expects that constant folding will convert
207    * ineg(2) to just -2.
208    */
209   nir_ssa_def *two = nir_imm_int(&bld, 2);
210   nir_ssa_def *two_plus_two = nir_iadd(&bld, two, two);
211   nir_ssa_def *negation = nir_ineg(&bld, two_plus_two);
212
213   nir_ssa_def *result = nir_iadd(&bld, two_plus_two, negation);
214
215   nir_alu_instr *instr = nir_instr_as_alu(result->parent_instr);
216
217   ASSERT_NE((void *) 0, instr);
218   EXPECT_TRUE(nir_alu_srcs_negative_equal(instr, instr, 0, 1));
219   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 0, 0));
220   EXPECT_FALSE(nir_alu_srcs_negative_equal(instr, instr, 1, 1));
221}
222
223static void
224count_sequence(nir_const_value c[NIR_MAX_VEC_COMPONENTS], nir_alu_type base_type, unsigned bits, int first)
225{
226   switch (base_type) {
227   case nir_type_float:
228      switch (bits) {
229      case 16:
230         for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
231            c[i].u16 = _mesa_float_to_half(float(i + first));
232
233         break;
234
235      case 32:
236         for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
237            c[i].f32 = float(i + first);
238
239         break;
240
241      case 64:
242         for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
243            c[i].f64 = double(i + first);
244
245         break;
246
247      default:
248         unreachable("unknown bit size");
249      }
250
251      break;
252
253   case nir_type_int:
254   case nir_type_uint:
255      switch (bits) {
256      case 8:
257         for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
258            c[i].i8 = i + first;
259
260         break;
261
262      case 16:
263         for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
264            c[i].i16 = i + first;
265
266         break;
267
268      case 32:
269         for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
270            c[i].i32 = i + first;
271
272         break;
273
274      case 64:
275         for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
276            c[i].i64 = i + first;
277
278         break;
279
280      default:
281         unreachable("unknown bit size");
282      }
283
284      break;
285
286   case nir_type_bool:
287   default:
288      unreachable("invalid base type");
289   }
290}
291
292static void
293negate(nir_const_value dst[NIR_MAX_VEC_COMPONENTS],
294       const nir_const_value src[NIR_MAX_VEC_COMPONENTS],
295       nir_alu_type base_type, unsigned bits, unsigned components)
296{
297   switch (base_type) {
298   case nir_type_float:
299      switch (bits) {
300      case 16:
301         for (unsigned i = 0; i < components; i++)
302            dst[i].u16 = _mesa_float_to_half(-_mesa_half_to_float(src[i].u16));
303
304         break;
305
306      case 32:
307         for (unsigned i = 0; i < components; i++)
308            dst[i].f32 = -src[i].f32;
309
310         break;
311
312      case 64:
313         for (unsigned i = 0; i < components; i++)
314            dst[i].f64 = -src[i].f64;
315
316         break;
317
318      default:
319         unreachable("unknown bit size");
320      }
321
322      break;
323
324   case nir_type_int:
325   case nir_type_uint:
326      switch (bits) {
327      case 8:
328         for (unsigned i = 0; i < components; i++)
329            dst[i].i8 = -src[i].i8;
330
331         break;
332
333      case 16:
334         for (unsigned i = 0; i < components; i++)
335            dst[i].i16 = -src[i].i16;
336
337         break;
338
339      case 32:
340         for (unsigned i = 0; i < components; i++)
341            dst[i].i32 = -src[i].i32;
342
343         break;
344
345      case 64:
346         for (unsigned i = 0; i < components; i++)
347            dst[i].i64 = -src[i].i64;
348
349         break;
350
351      default:
352         unreachable("unknown bit size");
353      }
354
355      break;
356
357   case nir_type_bool:
358   default:
359      unreachable("invalid base type");
360   }
361}
362