nir/spirv: initial handling of OpenCL.std extension opcodes
[mesa.git] / src / compiler / nir / nir_builtin_builder.h
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
34 nir_ssa_def* nir_cross3(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y);
35 nir_ssa_def* nir_cross4(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y);
36 nir_ssa_def* nir_length(nir_builder *b, nir_ssa_def *vec);
37 nir_ssa_def* nir_fast_length(nir_builder *b, nir_ssa_def *vec);
38 nir_ssa_def* nir_nextafter(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y);
39 nir_ssa_def* nir_normalize(nir_builder *b, nir_ssa_def *vec);
40 nir_ssa_def* nir_rotate(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y);
41 nir_ssa_def* nir_smoothstep(nir_builder *b, nir_ssa_def *edge0,
42 nir_ssa_def *edge1, nir_ssa_def *x);
43 nir_ssa_def* nir_upsample(nir_builder *b, nir_ssa_def *hi, nir_ssa_def *lo);
44
45 static inline nir_ssa_def *
46 nir_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
51 static inline nir_ssa_def *
52 nir_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
60 static inline nir_ssa_def *
61 nir_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
69 static inline nir_ssa_def *
70 nir_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
78 static inline nir_ssa_def *
79 nir_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
84 static inline nir_ssa_def *
85 nir_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
91 static inline nir_ssa_def *
92 nir_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
98 static inline nir_ssa_def *
99 nir_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
105 static inline nir_ssa_def *
106 nir_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
117 static inline nir_ssa_def *
118 nir_degrees(nir_builder *b, nir_ssa_def *val)
119 {
120 return nir_fmul_imm(b, val, 180.0 / M_PI);
121 }
122
123 static inline nir_ssa_def *
124 nir_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
134 static inline nir_ssa_def *
135 nir_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
140 static inline nir_ssa_def *
141 nir_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
146 static inline nir_ssa_def*
147 nir_fast_normalize(nir_builder *b, nir_ssa_def *vec)
148 {
149 return nir_fdiv(b, vec, nir_fast_length(b, vec));
150 }
151
152 static inline nir_ssa_def*
153 nir_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
158 static inline nir_ssa_def*
159 nir_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
170 static inline nir_ssa_def*
171 nir_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 static inline nir_ssa_def*
183 nir_nan(nir_builder *b, nir_ssa_def *x)
184 {
185 nir_ssa_def *nan = nir_imm_floatN_t(b, NAN, x->bit_size);
186 if (x->num_components == 1)
187 return nan;
188
189 nir_ssa_def *nans[NIR_MAX_VEC_COMPONENTS];
190 for (unsigned i = 0; i < x->num_components; ++i)
191 nans[i] = nan;
192
193 return nir_vec(b, nans, x->num_components);
194 }
195
196 static inline nir_ssa_def *
197 nir_radians(nir_builder *b, nir_ssa_def *val)
198 {
199 return nir_fmul_imm(b, val, M_PI / 180.0);
200 }
201
202 static inline nir_ssa_def *
203 nir_select(nir_builder *b, nir_ssa_def *x, nir_ssa_def *y, nir_ssa_def *s)
204 {
205 if (s->num_components != 1) {
206 uint64_t mask = 1ull << (s->bit_size - 1);
207 s = nir_iand(b, s, nir_imm_intN_t(b, mask, s->bit_size));
208 }
209 return nir_bcsel(b, nir_ieq(b, s, nir_imm_intN_t(b, 0, s->bit_size)), x, y);
210 }
211
212 #endif /* NIR_BUILTIN_BUILDER_H */