1/*
2 * Copyright © 2018 Red Hat Inc.
3 * Copyright © 2015 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25#include <math.h>
26
27#include "nir.h"
28#include "nir_builtin_builder.h"
29
30nir_ssa_def*
31nir_cross3(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y)
32{
33   unsigned yzx[3] = { 1, 2, 0 };
34   unsigned zxy[3] = { 2, 0, 1 };
35
36   return nir_ffma(b, nir_swizzle(b, x, yzx, 3),
37                      nir_swizzle(b, y, zxy, 3),
38                      nir_fneg(b, nir_fmul(b, nir_swizzle(b, x, zxy, 3),
39                                              nir_swizzle(b, y, yzx, 3))));
40}
41
42nir_ssa_def*
43nir_cross4(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y)
44{
45   nir_ssa_def *cross = nir_cross3(b, x, y);
46
47   return nir_vec4(b,
48      nir_channel(b, cross, 0),
49      nir_channel(b, cross, 1),
50      nir_channel(b, cross, 2),
51      nir_imm_intN_t(b, 0, cross->bit_size));
52}
53
54nir_ssa_def*
55nir_fast_length(nir_builder *b, nir_ssa_def *vec)
56{
57   return nir_fsqrt(b, nir_fdot(b, vec, vec));
58}
59
60nir_ssa_def*
61nir_nextafter(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y)
62{
63   nir_ssa_def *zero = nir_imm_intN_t(b, 0, x->bit_size);
64   nir_ssa_def *one = nir_imm_intN_t(b, 1, x->bit_size);
65
66   nir_ssa_def *condeq = nir_feq(b, x, y);
67   nir_ssa_def *conddir = nir_flt(b, x, y);
68   nir_ssa_def *condzero = nir_feq(b, x, zero);
69
70   uint64_t sign_mask = 1ull << (x->bit_size - 1);
71   uint64_t min_abs = 1;
72
73   if (nir_is_denorm_flush_to_zero(b->shader->info.float_controls_execution_mode, x->bit_size)) {
74      switch (x->bit_size) {
75      case 16:
76         min_abs = 1 << 10;
77         break;
78      case 32:
79         min_abs = 1 << 23;
80         break;
81      case 64:
82         min_abs = 1ULL << 52;
83         break;
84      }
85
86      /* Flush denorm to zero to avoid returning a denorm when condeq is true. */
87      x = nir_fmul(b, x, nir_imm_floatN_t(b, 1.0, x->bit_size));
88   }
89
90   /* beware of: +/-0.0 - 1 == NaN */
91   nir_ssa_def *xn =
92      nir_bcsel(b,
93                condzero,
94                nir_imm_intN_t(b, sign_mask | min_abs, x->bit_size),
95                nir_isub(b, x, one));
96
97   /* beware of -0.0 + 1 == -0x1p-149 */
98   nir_ssa_def *xp = nir_bcsel(b, condzero,
99                               nir_imm_intN_t(b, min_abs, x->bit_size),
100                               nir_iadd(b, x, one));
101
102   /* nextafter can be implemented by just +/- 1 on the int value */
103   nir_ssa_def *res =
104      nir_bcsel(b, nir_ixor(b, conddir, nir_flt(b, x, zero)), xp, xn);
105
106   return nir_nan_check2(b, x, y, nir_bcsel(b, condeq, x, res));
107}
108
109nir_ssa_def*
110nir_normalize(nir_builder *b, nir_ssa_def *vec)
111{
112   if (vec->num_components == 1)
113      return nir_fsign(b, vec);
114
115   nir_ssa_def *f0 = nir_imm_floatN_t(b, 0.0, vec->bit_size);
116   nir_ssa_def *f1 = nir_imm_floatN_t(b, 1.0, vec->bit_size);
117   nir_ssa_def *finf = nir_imm_floatN_t(b, INFINITY, vec->bit_size);
118
119   /* scale the input to increase precision */
120   nir_ssa_def *maxc = nir_fmax_abs_vec_comp(b, vec);
121   nir_ssa_def *svec = nir_fdiv(b, vec, maxc);
122   /* for inf */
123   nir_ssa_def *finfvec = nir_copysign(b, nir_bcsel(b, nir_feq(b, vec, finf), f1, f0), f1);
124
125   nir_ssa_def *temp = nir_bcsel(b, nir_feq(b, maxc, finf), finfvec, svec);
126   nir_ssa_def *res = nir_fmul(b, temp, nir_frsq(b, nir_fdot(b, temp, temp)));
127
128   return nir_bcsel(b, nir_feq(b, maxc, f0), vec, res);
129}
130
131nir_ssa_def*
132nir_smoothstep(nir_builder *b, nir_ssa_def *edge0, nir_ssa_def *edge1, nir_ssa_def *x)
133{
134   nir_ssa_def *f2 = nir_imm_floatN_t(b, 2.0, x->bit_size);
135   nir_ssa_def *f3 = nir_imm_floatN_t(b, 3.0, x->bit_size);
136
137   /* t = clamp((x - edge0) / (edge1 - edge0), 0, 1) */
138   nir_ssa_def *t =
139      nir_fsat(b, nir_fdiv(b, nir_fsub(b, x, edge0),
140                              nir_fsub(b, edge1, edge0)));
141
142   /* result = t * t * (3 - 2 * t) */
143   return nir_fmul(b, t, nir_fmul(b, t, nir_a_minus_bc(b, f3, f2, t)));
144}
145
146nir_ssa_def*
147nir_upsample(nir_builder *b, nir_ssa_def *hi, nir_ssa_def *lo)
148{
149   assert(lo->num_components == hi->num_components);
150   assert(lo->bit_size == hi->bit_size);
151
152   nir_ssa_def *res[NIR_MAX_VEC_COMPONENTS];
153   for (unsigned i = 0; i < lo->num_components; ++i) {
154      nir_ssa_def *vec = nir_vec2(b, nir_channel(b, lo, i), nir_channel(b, hi, i));
155      res[i] = nir_pack_bits(b, vec, vec->bit_size * 2);
156   }
157
158   return nir_vec(b, res, lo->num_components);
159}
160
161/**
162 * Compute xs[0] + xs[1] + xs[2] + ... using fadd.
163 */
164static nir_ssa_def *
165build_fsum(nir_builder *b, nir_ssa_def **xs, int terms)
166{
167   nir_ssa_def *accum = xs[0];
168
169   for (int i = 1; i < terms; i++)
170      accum = nir_fadd(b, accum, xs[i]);
171
172   return accum;
173}
174
175nir_ssa_def *
176nir_atan(nir_builder *b, nir_ssa_def *y_over_x)
177{
178   const uint32_t bit_size = y_over_x->bit_size;
179
180   nir_ssa_def *abs_y_over_x = nir_fabs(b, y_over_x);
181   nir_ssa_def *one = nir_imm_floatN_t(b, 1.0f, bit_size);
182
183   /*
184    * range-reduction, first step:
185    *
186    *      / y_over_x         if |y_over_x| <= 1.0;
187    * x = <
188    *      \ 1.0 / y_over_x   otherwise
189    */
190   nir_ssa_def *x = nir_fdiv(b, nir_fmin(b, abs_y_over_x, one),
191                                nir_fmax(b, abs_y_over_x, one));
192
193   /*
194    * approximate atan by evaluating polynomial:
195    *
196    * x   * 0.9999793128310355 - x^3  * 0.3326756418091246 +
197    * x^5 * 0.1938924977115610 - x^7  * 0.1173503194786851 +
198    * x^9 * 0.0536813784310406 - x^11 * 0.0121323213173444
199    */
200   nir_ssa_def *x_2  = nir_fmul(b, x,   x);
201   nir_ssa_def *x_3  = nir_fmul(b, x_2, x);
202   nir_ssa_def *x_5  = nir_fmul(b, x_3, x_2);
203   nir_ssa_def *x_7  = nir_fmul(b, x_5, x_2);
204   nir_ssa_def *x_9  = nir_fmul(b, x_7, x_2);
205   nir_ssa_def *x_11 = nir_fmul(b, x_9, x_2);
206
207   nir_ssa_def *polynomial_terms[] = {
208      nir_fmul_imm(b, x,     0.9999793128310355f),
209      nir_fmul_imm(b, x_3,  -0.3326756418091246f),
210      nir_fmul_imm(b, x_5,   0.1938924977115610f),
211      nir_fmul_imm(b, x_7,  -0.1173503194786851f),
212      nir_fmul_imm(b, x_9,   0.0536813784310406f),
213      nir_fmul_imm(b, x_11, -0.0121323213173444f),
214   };
215
216   nir_ssa_def *tmp =
217      build_fsum(b, polynomial_terms, ARRAY_SIZE(polynomial_terms));
218
219   /* range-reduction fixup */
220   tmp = nir_ffma(b,
221                  nir_b2f(b, nir_flt(b, one, abs_y_over_x), bit_size),
222                  nir_ffma_imm12(b, tmp, -2.0f, M_PI_2),
223                  tmp);
224
225   /* sign fixup */
226   nir_ssa_def *result = nir_fmul(b, tmp, nir_fsign(b, y_over_x));
227
228   /* The fmin and fmax above will filter out NaN values.  This leads to
229    * non-NaN results for NaN inputs.  Work around this by doing
230    *
231    *    !isnan(y_over_x) ? ... : y_over_x;
232    */
233   if (b->exact ||
234       nir_is_float_control_signed_zero_inf_nan_preserve(b->shader->info.float_controls_execution_mode, bit_size)) {
235      const bool exact = b->exact;
236
237      b->exact = true;
238      nir_ssa_def *is_not_nan = nir_feq(b, y_over_x, y_over_x);
239      b->exact = exact;
240
241      /* The extra 1.0*y_over_x ensures that subnormal results are flushed to
242       * zero.
243       */
244      result = nir_bcsel(b, is_not_nan, result, nir_fmul_imm(b, y_over_x, 1.0));
245   }
246
247   return result;
248}
249
250nir_ssa_def *
251nir_atan2(nir_builder *b, nir_ssa_def *y, nir_ssa_def *x)
252{
253   assert(y->bit_size == x->bit_size);
254   const uint32_t bit_size = x->bit_size;
255
256   nir_ssa_def *zero = nir_imm_floatN_t(b, 0, bit_size);
257   nir_ssa_def *one = nir_imm_floatN_t(b, 1, bit_size);
258
259   /* If we're on the left half-plane rotate the coordinates π/2 clock-wise
260    * for the y=0 discontinuity to end up aligned with the vertical
261    * discontinuity of atan(s/t) along t=0.  This also makes sure that we
262    * don't attempt to divide by zero along the vertical line, which may give
263    * unspecified results on non-GLSL 4.1-capable hardware.
264    */
265   nir_ssa_def *flip = nir_fge(b, zero, x);
266   nir_ssa_def *s = nir_bcsel(b, flip, nir_fabs(b, x), y);
267   nir_ssa_def *t = nir_bcsel(b, flip, y, nir_fabs(b, x));
268
269   /* If the magnitude of the denominator exceeds some huge value, scale down
270    * the arguments in order to prevent the reciprocal operation from flushing
271    * its result to zero, which would cause precision problems, and for s
272    * infinite would cause us to return a NaN instead of the correct finite
273    * value.
274    *
275    * If fmin and fmax are respectively the smallest and largest positive
276    * normalized floating point values representable by the implementation,
277    * the constants below should be in agreement with:
278    *
279    *    huge <= 1 / fmin
280    *    scale <= 1 / fmin / fmax (for |t| >= huge)
281    *
282    * In addition scale should be a negative power of two in order to avoid
283    * loss of precision.  The values chosen below should work for most usual
284    * floating point representations with at least the dynamic range of ATI's
285    * 24-bit representation.
286    */
287   const double huge_val = bit_size >= 32 ? 1e18 : 16384;
288   nir_ssa_def *huge = nir_imm_floatN_t(b,  huge_val, bit_size);
289   nir_ssa_def *scale = nir_bcsel(b, nir_fge(b, nir_fabs(b, t), huge),
290                                  nir_imm_floatN_t(b, 0.25, bit_size), one);
291   nir_ssa_def *rcp_scaled_t = nir_frcp(b, nir_fmul(b, t, scale));
292   nir_ssa_def *s_over_t = nir_fmul(b, nir_fmul(b, s, scale), rcp_scaled_t);
293
294   /* For |x| = |y| assume tan = 1 even if infinite (i.e. pretend momentarily
295    * that ∞/∞ = 1) in order to comply with the rather artificial rules
296    * inherited from IEEE 754-2008, namely:
297    *
298    *  "atan2(±∞, −∞) is ±3π/4
299    *   atan2(±∞, +∞) is ±π/4"
300    *
301    * Note that this is inconsistent with the rules for the neighborhood of
302    * zero that are based on iterated limits:
303    *
304    *  "atan2(±0, −0) is ±π
305    *   atan2(±0, +0) is ±0"
306    *
307    * but GLSL specifically allows implementations to deviate from IEEE rules
308    * at (0,0), so we take that license (i.e. pretend that 0/0 = 1 here as
309    * well).
310    */
311   nir_ssa_def *tan = nir_bcsel(b, nir_feq(b, nir_fabs(b, x), nir_fabs(b, y)),
312                                one, nir_fabs(b, s_over_t));
313
314   /* Calculate the arctangent and fix up the result if we had flipped the
315    * coordinate system.
316    */
317   nir_ssa_def *arc =
318      nir_ffma_imm1(b, nir_b2f(b, flip, bit_size), M_PI_2, nir_atan(b, tan));
319
320   /* Rather convoluted calculation of the sign of the result.  When x < 0 we
321    * cannot use fsign because we need to be able to distinguish between
322    * negative and positive zero.  We don't use bitwise arithmetic tricks for
323    * consistency with the GLSL front-end.  When x >= 0 rcp_scaled_t will
324    * always be non-negative so this won't be able to distinguish between
325    * negative and positive zero, but we don't care because atan2 is
326    * continuous along the whole positive y = 0 half-line, so it won't affect
327    * the result significantly.
328    */
329   return nir_bcsel(b, nir_flt(b, nir_fmin(b, y, rcp_scaled_t), zero),
330                    nir_fneg(b, arc), arc);
331}
332
333nir_ssa_def *
334nir_get_texture_size(nir_builder *b, nir_tex_instr *tex)
335{
336   b->cursor = nir_before_instr(&tex->instr);
337
338   nir_tex_instr *txs;
339
340   unsigned num_srcs = 1; /* One for the LOD */
341   for (unsigned i = 0; i < tex->num_srcs; i++) {
342      if (tex->src[i].src_type == nir_tex_src_texture_deref ||
343          tex->src[i].src_type == nir_tex_src_sampler_deref ||
344          tex->src[i].src_type == nir_tex_src_texture_offset ||
345          tex->src[i].src_type == nir_tex_src_sampler_offset ||
346          tex->src[i].src_type == nir_tex_src_texture_handle ||
347          tex->src[i].src_type == nir_tex_src_sampler_handle)
348         num_srcs++;
349   }
350
351   txs = nir_tex_instr_create(b->shader, num_srcs);
352   txs->op = nir_texop_txs;
353   txs->sampler_dim = tex->sampler_dim;
354   txs->is_array = tex->is_array;
355   txs->is_shadow = tex->is_shadow;
356   txs->is_new_style_shadow = tex->is_new_style_shadow;
357   txs->texture_index = tex->texture_index;
358   txs->sampler_index = tex->sampler_index;
359   txs->dest_type = nir_type_int32;
360
361   unsigned idx = 0;
362   for (unsigned i = 0; i < tex->num_srcs; i++) {
363      if (tex->src[i].src_type == nir_tex_src_texture_deref ||
364          tex->src[i].src_type == nir_tex_src_sampler_deref ||
365          tex->src[i].src_type == nir_tex_src_texture_offset ||
366          tex->src[i].src_type == nir_tex_src_sampler_offset ||
367          tex->src[i].src_type == nir_tex_src_texture_handle ||
368          tex->src[i].src_type == nir_tex_src_sampler_handle) {
369         nir_src_copy(&txs->src[idx].src, &tex->src[i].src);
370         txs->src[idx].src_type = tex->src[i].src_type;
371         idx++;
372      }
373   }
374   /* Add in an LOD because some back-ends require it */
375   txs->src[idx].src = nir_src_for_ssa(nir_imm_int(b, 0));
376   txs->src[idx].src_type = nir_tex_src_lod;
377
378   nir_ssa_dest_init(&txs->instr, &txs->dest,
379                     nir_tex_instr_dest_size(txs), 32, NULL);
380   nir_builder_instr_insert(b, &txs->instr);
381
382   return &txs->dest.ssa;
383}
384
385nir_ssa_def *
386nir_get_texture_lod(nir_builder *b, nir_tex_instr *tex)
387{
388   b->cursor = nir_before_instr(&tex->instr);
389
390   nir_tex_instr *tql;
391
392   unsigned num_srcs = 0;
393   for (unsigned i = 0; i < tex->num_srcs; i++) {
394      if (tex->src[i].src_type == nir_tex_src_coord ||
395          tex->src[i].src_type == nir_tex_src_texture_deref ||
396          tex->src[i].src_type == nir_tex_src_sampler_deref ||
397          tex->src[i].src_type == nir_tex_src_texture_offset ||
398          tex->src[i].src_type == nir_tex_src_sampler_offset ||
399          tex->src[i].src_type == nir_tex_src_texture_handle ||
400          tex->src[i].src_type == nir_tex_src_sampler_handle)
401         num_srcs++;
402   }
403
404   tql = nir_tex_instr_create(b->shader, num_srcs);
405   tql->op = nir_texop_lod;
406   tql->coord_components = tex->coord_components;
407   tql->sampler_dim = tex->sampler_dim;
408   tql->is_array = tex->is_array;
409   tql->is_shadow = tex->is_shadow;
410   tql->is_new_style_shadow = tex->is_new_style_shadow;
411   tql->texture_index = tex->texture_index;
412   tql->sampler_index = tex->sampler_index;
413   tql->dest_type = nir_type_float32;
414
415   unsigned idx = 0;
416   for (unsigned i = 0; i < tex->num_srcs; i++) {
417      if (tex->src[i].src_type == nir_tex_src_coord ||
418          tex->src[i].src_type == nir_tex_src_texture_deref ||
419          tex->src[i].src_type == nir_tex_src_sampler_deref ||
420          tex->src[i].src_type == nir_tex_src_texture_offset ||
421          tex->src[i].src_type == nir_tex_src_sampler_offset ||
422          tex->src[i].src_type == nir_tex_src_texture_handle ||
423          tex->src[i].src_type == nir_tex_src_sampler_handle) {
424         nir_src_copy(&tql->src[idx].src, &tex->src[i].src);
425         tql->src[idx].src_type = tex->src[i].src_type;
426         idx++;
427      }
428   }
429
430   nir_ssa_dest_init(&tql->instr, &tql->dest, 2, 32, NULL);
431   nir_builder_instr_insert(b, &tql->instr);
432
433   /* The LOD is the y component of the result */
434   return nir_channel(b, &tql->dest.ssa, 1);
435}
436