nir_builtin_builder.h revision 0c3f8b09
1/* 2 * Copyright © 2018 Red Hat Inc. 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 DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24#ifndef NIR_BUILTIN_BUILDER_H 25#define NIR_BUILTIN_BUILDER_H 26 27#include "nir/nir_builder.h" 28 29/* 30 * Functions are sorted alphabetically with removed type and "fast" prefix. 31 * Definitions for functions in the C file come first. 32 */ 33 34nir_ssa_def* nir_cross3(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y); 35nir_ssa_def* nir_cross4(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y); 36nir_ssa_def* nir_length(nir_builder *b, nir_ssa_def *vec); 37nir_ssa_def* nir_fast_length(nir_builder *b, nir_ssa_def *vec); 38nir_ssa_def* nir_nextafter(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y); 39nir_ssa_def* nir_normalize(nir_builder *b, nir_ssa_def *vec); 40nir_ssa_def* nir_rotate(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y); 41nir_ssa_def* nir_smoothstep(nir_builder *b, nir_ssa_def *edge0, 42 nir_ssa_def *edge1, nir_ssa_def *x); 43nir_ssa_def* nir_upsample(nir_builder *b, nir_ssa_def *hi, nir_ssa_def *lo); 44 45static inline nir_ssa_def * 46nir_nan_check2(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y, nir_ssa_def *res) 47{ 48 return nir_bcsel(b, nir_fne(b, x, x), x, nir_bcsel(b, nir_fne(b, y, y), y, res)); 49} 50 51static inline nir_ssa_def * 52nir_fmax_abs_vec_comp(nir_builder *b, nir_ssa_def *vec) 53{ 54 nir_ssa_def *res = nir_channel(b, vec, 0); 55 for (unsigned i = 1; i < vec->num_components; ++i) 56 res = nir_fmax(b, res, nir_fabs(b, nir_channel(b, vec, i))); 57 return res; 58} 59 60static inline nir_ssa_def * 61nir_iabs_diff(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y) 62{ 63 nir_ssa_def *cond = nir_ige(b, x, y); 64 nir_ssa_def *res0 = nir_isub(b, x, y); 65 nir_ssa_def *res1 = nir_isub(b, y, x); 66 return nir_bcsel(b, cond, res0, res1); 67} 68 69static inline nir_ssa_def * 70nir_uabs_diff(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y) 71{ 72 nir_ssa_def *cond = nir_uge(b, x, y); 73 nir_ssa_def *res0 = nir_isub(b, x, y); 74 nir_ssa_def *res1 = nir_isub(b, y, x); 75 return nir_bcsel(b, cond, res0, res1); 76} 77 78static inline nir_ssa_def * 79nir_bitselect(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y, nir_ssa_def *s) 80{ 81 return nir_ior(b, nir_iand(b, nir_inot(b, s), x), nir_iand(b, s, y)); 82} 83 84static inline nir_ssa_def * 85nir_fclamp(nir_builder *b, 86 nir_ssa_def *x, nir_ssa_def *min_val, nir_ssa_def *max_val) 87{ 88 return nir_fmin(b, nir_fmax(b, x, min_val), max_val); 89} 90 91static inline nir_ssa_def * 92nir_iclamp(nir_builder *b, 93 nir_ssa_def *x, nir_ssa_def *min_val, nir_ssa_def *max_val) 94{ 95 return nir_imin(b, nir_imax(b, x, min_val), max_val); 96} 97 98static inline nir_ssa_def * 99nir_uclamp(nir_builder *b, 100 nir_ssa_def *x, nir_ssa_def *min_val, nir_ssa_def *max_val) 101{ 102 return nir_umin(b, nir_umax(b, x, min_val), max_val); 103} 104 105static inline nir_ssa_def * 106nir_copysign(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y) 107{ 108 uint64_t masks = 1ull << (x->bit_size - 1); 109 uint64_t maskv = ~masks; 110 111 nir_ssa_def *s = nir_imm_intN_t(b, masks, x->bit_size); 112 nir_ssa_def *v = nir_imm_intN_t(b, maskv, x->bit_size); 113 114 return nir_ior(b, nir_iand(b, x, v), nir_iand(b, y, s)); 115} 116 117static inline nir_ssa_def * 118nir_degrees(nir_builder *b, nir_ssa_def *val) 119{ 120 return nir_fmul_imm(b, val, 180.0 / M_PI); 121} 122 123static inline nir_ssa_def * 124nir_fdim(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y) 125{ 126 nir_ssa_def *cond = nir_flt(b, y, x); 127 nir_ssa_def *res = nir_fsub(b, x, y); 128 nir_ssa_def *zero = nir_imm_floatN_t(b, 0.0, x->bit_size); 129 130 // return NaN if either x or y are NaN, else x-y if x>y, else +0.0 131 return nir_nan_check2(b, x, y, nir_bcsel(b, cond, res, zero)); 132} 133 134static inline nir_ssa_def * 135nir_distance(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y) 136{ 137 return nir_length(b, nir_fsub(b, x, y)); 138} 139 140static inline nir_ssa_def * 141nir_fast_distance(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y) 142{ 143 return nir_fast_length(b, nir_fsub(b, x, y)); 144} 145 146static inline nir_ssa_def* 147nir_fast_normalize(nir_builder *b, nir_ssa_def *vec) 148{ 149 return nir_fdiv(b, vec, nir_fast_length(b, vec)); 150} 151 152static inline nir_ssa_def* 153nir_fmad(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y, nir_ssa_def *z) 154{ 155 return nir_fadd(b, nir_fmul(b, x, y), z); 156} 157 158static inline nir_ssa_def* 159nir_maxmag(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y) 160{ 161 nir_ssa_def *xabs = nir_fabs(b, x); 162 nir_ssa_def *yabs = nir_fabs(b, y); 163 164 nir_ssa_def *condy = nir_flt(b, xabs, yabs); 165 nir_ssa_def *condx = nir_flt(b, yabs, xabs); 166 167 return nir_bcsel(b, condy, y, nir_bcsel(b, condx, x, nir_fmax(b, x, y))); 168} 169 170static inline nir_ssa_def* 171nir_minmag(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y) 172{ 173 nir_ssa_def *xabs = nir_fabs(b, x); 174 nir_ssa_def *yabs = nir_fabs(b, y); 175 176 nir_ssa_def *condx = nir_flt(b, xabs, yabs); 177 nir_ssa_def *condy = nir_flt(b, yabs, xabs); 178 179 return nir_bcsel(b, condy, y, nir_bcsel(b, condx, x, nir_fmin(b, x, y))); 180} 181 182#ifdef __vax__ 183#define NAN FLT_MAX 184#endif 185 186static inline nir_ssa_def* 187nir_nan(nir_builder *b, nir_ssa_def *x) 188{ 189 nir_ssa_def *nan = nir_imm_floatN_t(b, NAN, x->bit_size); 190 if (x->num_components == 1) 191 return nan; 192 193 nir_ssa_def *nans[NIR_MAX_VEC_COMPONENTS]; 194 for (unsigned i = 0; i < x->num_components; ++i) 195 nans[i] = nan; 196 197 return nir_vec(b, nans, x->num_components); 198} 199 200static inline nir_ssa_def * 201nir_radians(nir_builder *b, nir_ssa_def *val) 202{ 203 return nir_fmul_imm(b, val, M_PI / 180.0); 204} 205 206static inline nir_ssa_def * 207nir_select(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y, nir_ssa_def *s) 208{ 209 if (s->num_components != 1) { 210 uint64_t mask = 1ull << (s->bit_size - 1); 211 s = nir_iand(b, s, nir_imm_intN_t(b, mask, s->bit_size)); 212 } 213 return nir_bcsel(b, nir_ieq(b, s, nir_imm_intN_t(b, 0, s->bit_size)), x, y); 214} 215 216#endif /* NIR_BUILTIN_BUILDER_H */ 217