diff options
author | Karol Herbst <[email protected]> | 2018-07-12 15:02:27 +0200 |
---|---|---|
committer | Karol Herbst <[email protected]> | 2019-03-05 22:28:29 +0100 |
commit | 272e927d0e9fed6e791d706ff5d895b6c2036fc0 (patch) | |
tree | 2bbe3b7fbef9275137bd4669cb86b72181094379 /src/compiler/nir/nir_builtin_builder.c | |
parent | d0b47ec4df0eafe4f4afddc2a0594b392c27f426 (diff) |
nir/spirv: initial handling of OpenCL.std extension opcodes
Not complete, mostly just adding things as I encounter them in CTS. But
not getting far enough yet to hit most of the OpenCL.std instructions.
Anyway, this is better than nothing and covers the most common builtins.
v2: add hadd proof from Jason
move some of the lowering into opt_algebraic and create new nir opcodes
simplify nextafter lowering
fix normalize lowering for inf
rework upsample to use nir_pack_bits
add missing files to build systems
v3: split lines of iadd/sub_sat expressions
Signed-off-by: Karol Herbst <[email protected]>
Reviewed-by: Jason Ekstrand <[email protected]>
Diffstat (limited to 'src/compiler/nir/nir_builtin_builder.c')
-rw-r--r-- | src/compiler/nir/nir_builtin_builder.c | 112 |
1 files changed, 111 insertions, 1 deletions
diff --git a/src/compiler/nir/nir_builtin_builder.c b/src/compiler/nir/nir_builtin_builder.c index 252a7691f36..a050306765e 100644 --- a/src/compiler/nir/nir_builtin_builder.c +++ b/src/compiler/nir/nir_builtin_builder.c @@ -21,11 +21,13 @@ * IN THE SOFTWARE. */ +#include <math.h> + #include "nir.h" #include "nir_builtin_builder.h" nir_ssa_def* -nir_cross(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y) +nir_cross3(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y) { unsigned yzx[3] = { 1, 2, 0 }; unsigned zxy[3] = { 2, 0, 1 }; @@ -37,6 +39,33 @@ nir_cross(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y) } nir_ssa_def* +nir_cross4(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y) +{ + nir_ssa_def *cross = nir_cross3(b, x, y); + + return nir_vec4(b, + nir_channel(b, cross, 0), + nir_channel(b, cross, 1), + nir_channel(b, cross, 2), + nir_imm_intN_t(b, 0, cross->bit_size)); +} + +nir_ssa_def* +nir_length(nir_builder *b, nir_ssa_def *vec) +{ + nir_ssa_def *finf = nir_imm_floatN_t(b, INFINITY, vec->bit_size); + + nir_ssa_def *abs = nir_fabs(b, vec); + if (vec->num_components == 1) + return abs; + + nir_ssa_def *maxc = nir_fmax_abs_vec_comp(b, abs); + abs = nir_fdiv(b, abs, maxc); + nir_ssa_def *res = nir_fmul(b, nir_fsqrt(b, nir_fdot(b, abs, abs)), maxc); + return nir_bcsel(b, nir_feq(b, maxc, finf), maxc, res); +} + +nir_ssa_def* nir_fast_length(nir_builder *b, nir_ssa_def *vec) { switch (vec->num_components) { @@ -50,6 +79,72 @@ nir_fast_length(nir_builder *b, nir_ssa_def *vec) } nir_ssa_def* +nir_nextafter(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y) +{ + nir_ssa_def *zero = nir_imm_intN_t(b, 0, x->bit_size); + nir_ssa_def *one = nir_imm_intN_t(b, 1, x->bit_size); + + nir_ssa_def *condeq = nir_feq(b, x, y); + nir_ssa_def *conddir = nir_flt(b, x, y); + nir_ssa_def *condzero = nir_feq(b, x, zero); + + /* beware of: +/-0.0 - 1 == NaN */ + nir_ssa_def *xn = + nir_bcsel(b, + condzero, + nir_imm_intN_t(b, (1 << (x->bit_size - 1)) + 1, x->bit_size), + nir_isub(b, x, one)); + + /* beware of -0.0 + 1 == -0x1p-149 */ + nir_ssa_def *xp = nir_bcsel(b, condzero, one, nir_iadd(b, x, one)); + + /* nextafter can be implemented by just +/- 1 on the int value */ + nir_ssa_def *res = + nir_bcsel(b, nir_ixor(b, conddir, nir_flt(b, x, zero)), xp, xn); + + return nir_nan_check2(b, x, y, nir_bcsel(b, condeq, x, res)); +} + +nir_ssa_def* +nir_normalize(nir_builder *b, nir_ssa_def *vec) +{ + if (vec->num_components == 1) + return nir_fsign(b, vec); + + nir_ssa_def *f0 = nir_imm_floatN_t(b, 0.0, vec->bit_size); + nir_ssa_def *f1 = nir_imm_floatN_t(b, 1.0, vec->bit_size); + nir_ssa_def *finf = nir_imm_floatN_t(b, INFINITY, vec->bit_size); + + /* scale the input to increase precision */ + nir_ssa_def *maxc = nir_fmax_abs_vec_comp(b, vec); + nir_ssa_def *svec = nir_fdiv(b, vec, maxc); + /* for inf */ + nir_ssa_def *finfvec = nir_copysign(b, nir_bcsel(b, nir_feq(b, vec, finf), f1, f0), f1); + + nir_ssa_def *temp = nir_bcsel(b, nir_feq(b, maxc, finf), finfvec, svec); + nir_ssa_def *res = nir_fmul(b, temp, nir_frsq(b, nir_fdot(b, temp, temp))); + + return nir_bcsel(b, nir_feq(b, maxc, f0), vec, res); +} + +nir_ssa_def* +nir_rotate(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y) +{ + nir_ssa_def *shift_mask = nir_imm_int(b, x->bit_size - 1); + + if (y->bit_size != 32) + y = nir_u2u32(b, y); + + nir_ssa_def *lshift = nir_iand(b, y, shift_mask); + nir_ssa_def *rshift = nir_isub(b, nir_imm_int(b, x->bit_size), lshift); + + nir_ssa_def *hi = nir_ishl(b, x, lshift); + nir_ssa_def *lo = nir_ushr(b, x, rshift); + + return nir_ior(b, hi, lo); +} + +nir_ssa_def* nir_smoothstep(nir_builder *b, nir_ssa_def *edge0, nir_ssa_def *edge1, nir_ssa_def *x) { nir_ssa_def *f2 = nir_imm_floatN_t(b, 2.0, x->bit_size); @@ -63,3 +158,18 @@ nir_smoothstep(nir_builder *b, nir_ssa_def *edge0, nir_ssa_def *edge1, nir_ssa_d /* result = t * t * (3 - 2 * t) */ return nir_fmul(b, t, nir_fmul(b, t, nir_fsub(b, f3, nir_fmul(b, f2, t)))); } + +nir_ssa_def* +nir_upsample(nir_builder *b, nir_ssa_def *hi, nir_ssa_def *lo) +{ + assert(lo->num_components == hi->num_components); + assert(lo->bit_size == hi->bit_size); + + nir_ssa_def *res[NIR_MAX_VEC_COMPONENTS]; + for (unsigned i = 0; i < lo->num_components; ++i) { + nir_ssa_def *vec = nir_vec2(b, nir_channel(b, lo, i), nir_channel(b, hi, i)); + res[i] = nir_pack_bits(b, vec, vec->bit_size * 2); + } + + return nir_vec(b, res, lo->num_components); +} |