nir: Add neg, abs, and sat opcodes
[mesa.git] / src / glsl / nir / nir_opcodes.h
1 /*
2 * Copyright © 2014 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 DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 /**
29 * This header file defines all the available opcodes in one place. It expands
30 * to a list of macros of the form:
31 *
32 * OPCODE(name, num_inputs, per_component, output_size, output_type,
33 * input_sizes, input_types)
34 *
35 * Which should correspond one-to-one with the nir_op_info structure. It is
36 * included in both ir.h to create the nir_op enum (with members of the form
37 * nir_op_(name)) and and in opcodes.c to create nir_op_infos, which is a
38 * const array of nir_op_info structures for each opcode.
39 */
40
41 #define ARR(...) { __VA_ARGS__ }
42
43 #define UNOP(name, type) OPCODE(name, 1, false, 0, type, ARR(0), ARR(type))
44 #define UNOP_CONVERT(name, in_type, out_type) \
45 OPCODE(name, 1, false, 0, out_type, ARR(0), ARR(in_type))
46 #define UNOP_HORIZ(name, output_size, output_type, input_size, input_type) \
47 OPCODE(name, 1, true, output_size, output_type, ARR(input_size), \
48 ARR(input_type))
49
50 #define UNOP_REDUCE(name, output_size, output_type, input_type) \
51 UNOP_HORIZ(name##2, output_size, output_type, 2, input_type) \
52 UNOP_HORIZ(name##3, output_size, output_type, 3, input_type) \
53 UNOP_HORIZ(name##4, output_size, output_type, 4, input_type)
54
55 /**
56 * These two move instructions differ in what modifiers they support and what
57 * the negate modifier means. Otherwise, they are identical.
58 */
59 UNOP(fmov, nir_type_float)
60 UNOP(imov, nir_type_int)
61
62 UNOP(ineg, nir_type_int)
63 UNOP(fneg, nir_type_float)
64 UNOP(inot, nir_type_int) /* invert every bit of the integer */
65 UNOP(fnot, nir_type_float) /* (src == 0.0) ? 1.0 : 0.0 */
66 UNOP(fsign, nir_type_float)
67 UNOP(isign, nir_type_int)
68 UNOP(iabs, nir_type_int)
69 UNOP(fabs, nir_type_float)
70 UNOP(fsat, nir_type_float)
71 UNOP(frcp, nir_type_float)
72 UNOP(frsq, nir_type_float)
73 UNOP(fsqrt, nir_type_float)
74 UNOP(fexp, nir_type_float) /* < e^x */
75 UNOP(flog, nir_type_float) /* log base e */
76 UNOP(fexp2, nir_type_float)
77 UNOP(flog2, nir_type_float)
78 UNOP_CONVERT(f2i, nir_type_float, nir_type_int) /**< Float-to-integer conversion. */
79 UNOP_CONVERT(f2u, nir_type_float, nir_type_unsigned) /**< Float-to-unsigned conversion. */
80 UNOP_CONVERT(i2f, nir_type_int, nir_type_float) /**< Integer-to-float conversion. */
81 UNOP_CONVERT(f2b, nir_type_float, nir_type_bool) /**< Float-to-boolean conversion */
82 UNOP_CONVERT(b2f, nir_type_bool, nir_type_float) /**< Boolean-to-float conversion */
83 UNOP_CONVERT(i2b, nir_type_int, nir_type_bool) /**< int-to-boolean conversion */
84 UNOP_CONVERT(b2i, nir_type_bool, nir_type_int) /**< Boolean-to-int conversion */
85 UNOP_CONVERT(u2f, nir_type_unsigned, nir_type_float) /**< Unsigned-to-float conversion. */
86
87 UNOP_REDUCE(bany, 1, nir_type_bool, nir_type_bool) /* returns ~0 if any component of src[0] != 0 */
88 UNOP_REDUCE(ball, 1, nir_type_bool, nir_type_bool) /* returns ~0 if all components of src[0] != 0 */
89 UNOP_REDUCE(fany, 1, nir_type_float, nir_type_float) /* returns 1.0 if any component of src[0] != 0 */
90 UNOP_REDUCE(fall, 1, nir_type_float, nir_type_float) /* returns 1.0 if all components of src[0] != 0 */
91
92 /**
93 * \name Unary floating-point rounding operations.
94 */
95 /*@{*/
96 UNOP(ftrunc, nir_type_float)
97 UNOP(fceil, nir_type_float)
98 UNOP(ffloor, nir_type_float)
99 UNOP(ffract, nir_type_float)
100 UNOP(fround_even, nir_type_float)
101 /*@}*/
102
103 /**
104 * \name Trigonometric operations.
105 */
106 /*@{*/
107 UNOP(fsin, nir_type_float)
108 UNOP(fcos, nir_type_float)
109 UNOP(fsin_reduced, nir_type_float)
110 UNOP(fcos_reduced, nir_type_float)
111 /*@}*/
112
113 /**
114 * \name Partial derivatives.
115 */
116 /*@{*/
117 UNOP(fddx, nir_type_float)
118 UNOP(fddy, nir_type_float)
119 UNOP(fddx_fine, nir_type_float)
120 UNOP(fddy_fine, nir_type_float)
121 UNOP(fddx_coarse, nir_type_float)
122 UNOP(fddy_coarse, nir_type_float)
123 /*@}*/
124
125 /**
126 * \name Floating point pack and unpack operations.
127 */
128 /*@{*/
129 UNOP_HORIZ(pack_snorm_2x16, 1, nir_type_unsigned, 2, nir_type_float)
130 UNOP_HORIZ(pack_snorm_4x8, 1, nir_type_unsigned, 4, nir_type_float)
131 UNOP_HORIZ(pack_unorm_2x16, 1, nir_type_unsigned, 2, nir_type_float)
132 UNOP_HORIZ(pack_unorm_4x8, 1, nir_type_unsigned, 4, nir_type_float)
133 UNOP_HORIZ(pack_half_2x16, 1, nir_type_unsigned, 2, nir_type_float)
134 UNOP_HORIZ(unpack_snorm_2x16, 2, nir_type_float, 1, nir_type_unsigned)
135 UNOP_HORIZ(unpack_snorm_4x8, 4, nir_type_float, 1, nir_type_unsigned)
136 UNOP_HORIZ(unpack_unorm_2x16, 2, nir_type_float, 1, nir_type_unsigned)
137 UNOP_HORIZ(unpack_unorm_4x8, 4, nir_type_float, 1, nir_type_unsigned)
138 UNOP_HORIZ(unpack_half_2x16, 2, nir_type_float, 1, nir_type_unsigned)
139 /*@}*/
140
141 /**
142 * \name Lowered floating point unpacking operations.
143 */
144 /*@{*/
145 UNOP_HORIZ(unpack_half_2x16_split_x, 1, nir_type_float, 1, nir_type_unsigned)
146 UNOP_HORIZ(unpack_half_2x16_split_y, 1, nir_type_float, 1, nir_type_unsigned)
147 /*@}*/
148
149 /**
150 * \name Bit operations, part of ARB_gpu_shader5.
151 */
152 /*@{*/
153 UNOP(bitfield_reverse, nir_type_unsigned)
154 UNOP(bit_count, nir_type_unsigned)
155 UNOP_CONVERT(ufind_msb, nir_type_unsigned, nir_type_int)
156 UNOP(ifind_msb, nir_type_int)
157 UNOP(find_lsb, nir_type_int)
158 /*@}*/
159
160 UNOP_HORIZ(fnoise1_1, 1, nir_type_float, 1, nir_type_float)
161 UNOP_HORIZ(fnoise1_2, 1, nir_type_float, 2, nir_type_float)
162 UNOP_HORIZ(fnoise1_3, 1, nir_type_float, 3, nir_type_float)
163 UNOP_HORIZ(fnoise1_4, 1, nir_type_float, 4, nir_type_float)
164 UNOP_HORIZ(fnoise2_1, 2, nir_type_float, 1, nir_type_float)
165 UNOP_HORIZ(fnoise2_2, 2, nir_type_float, 2, nir_type_float)
166 UNOP_HORIZ(fnoise2_3, 2, nir_type_float, 3, nir_type_float)
167 UNOP_HORIZ(fnoise2_4, 2, nir_type_float, 4, nir_type_float)
168 UNOP_HORIZ(fnoise3_1, 3, nir_type_float, 1, nir_type_float)
169 UNOP_HORIZ(fnoise3_2, 3, nir_type_float, 2, nir_type_float)
170 UNOP_HORIZ(fnoise3_3, 3, nir_type_float, 3, nir_type_float)
171 UNOP_HORIZ(fnoise3_4, 3, nir_type_float, 4, nir_type_float)
172 UNOP_HORIZ(fnoise4_1, 4, nir_type_float, 1, nir_type_float)
173 UNOP_HORIZ(fnoise4_2, 4, nir_type_float, 2, nir_type_float)
174 UNOP_HORIZ(fnoise4_3, 4, nir_type_float, 3, nir_type_float)
175 UNOP_HORIZ(fnoise4_4, 4, nir_type_float, 4, nir_type_float)
176
177 #define BINOP(name, type) \
178 OPCODE(name, 2, true, 0, type, ARR(0, 0), ARR(type, type))
179 #define BINOP_CONVERT(name, out_type, in_type) \
180 OPCODE(name, 2, true, 0, out_type, ARR(0, 0), ARR(in_type, in_type))
181 #define BINOP_COMPARE(name, type) BINOP_CONVERT(name, nir_type_bool, type)
182 #define BINOP_HORIZ(name, output_size, output_type, src1_size, src1_type, \
183 src2_size, src2_type) \
184 OPCODE(name, 2, true, output_size, output_type, ARR(src1_size, src2_size), \
185 ARR(src1_type, src2_type))
186 #define BINOP_REDUCE(name, output_size, output_type, src_type) \
187 BINOP_HORIZ(name##2, output_size, output_type, 2, src_type, 2, src_type) \
188 BINOP_HORIZ(name##3, output_size, output_type, 3, src_type, 3, src_type) \
189 BINOP_HORIZ(name##4, output_size, output_type, 4, src_type, 4, src_type) \
190
191 BINOP(fadd, nir_type_float)
192 BINOP(iadd, nir_type_int)
193 BINOP(fsub, nir_type_float)
194 BINOP(isub, nir_type_int)
195
196 BINOP(fmul, nir_type_float)
197 BINOP(imul, nir_type_int) /* low 32-bits of signed/unsigned integer multiply */
198 BINOP(imul_high, nir_type_int) /* high 32-bits of signed integer multiply */
199 BINOP(umul_high, nir_type_unsigned) /* high 32-bits of unsigned integer multiply */
200
201 BINOP(fdiv, nir_type_float)
202 BINOP(idiv, nir_type_int)
203 BINOP(udiv, nir_type_unsigned)
204
205 /**
206 * returns a boolean representing the carry resulting from the addition of
207 * the two unsigned arguments.
208 */
209 BINOP_CONVERT(uadd_carry, nir_type_bool, nir_type_unsigned)
210
211 /**
212 * returns a boolean representing the borrow resulting from the subtraction
213 * of the two unsigned arguments.
214 */
215 BINOP_CONVERT(usub_borrow, nir_type_bool, nir_type_unsigned)
216
217 BINOP(fmod, nir_type_float)
218 BINOP(umod, nir_type_unsigned)
219
220 /**
221 * \name comparisons
222 */
223 /*@{*/
224
225 /**
226 * these integer-aware comparisons return a boolean (0 or ~0)
227 */
228 BINOP_COMPARE(flt, nir_type_float)
229 BINOP_COMPARE(fge, nir_type_float)
230 BINOP_COMPARE(feq, nir_type_float)
231 BINOP_COMPARE(fne, nir_type_float)
232 BINOP_COMPARE(ilt, nir_type_int)
233 BINOP_COMPARE(ige, nir_type_int)
234 BINOP_COMPARE(ieq, nir_type_int)
235 BINOP_COMPARE(ine, nir_type_int)
236 BINOP_COMPARE(ult, nir_type_unsigned)
237 BINOP_COMPARE(uge, nir_type_unsigned)
238
239 /** integer-aware GLSL-style comparisons that compare floats and ints */
240 BINOP_REDUCE(ball_fequal, 1, nir_type_bool, nir_type_float)
241 BINOP_REDUCE(bany_fnequal, 1, nir_type_bool, nir_type_float)
242 BINOP_REDUCE(ball_iequal, 1, nir_type_bool, nir_type_int)
243 BINOP_REDUCE(bany_inequal, 1, nir_type_bool, nir_type_int)
244
245 /** non-integer-aware GLSL-style comparisons that return 0.0 or 1.0 */
246 BINOP_REDUCE(fall_equal, 1, nir_type_float, nir_type_float)
247 BINOP_REDUCE(fany_nequal, 1, nir_type_float, nir_type_float)
248
249 /**
250 * These comparisons for integer-less hardware return 1.0 and 0.0 for true
251 * and false respectively
252 */
253 BINOP(slt, nir_type_float) /* Set on Less Than */
254 BINOP(sge, nir_type_float) /* Set on Greater Than or Equal */
255 BINOP(seq, nir_type_float) /* Set on Equal */
256 BINOP(sne, nir_type_float) /* Set on Not Equal */
257
258 /*@}*/
259
260 BINOP(ishl, nir_type_int)
261 BINOP(ishr, nir_type_int)
262 BINOP(ushr, nir_type_unsigned)
263
264 /**
265 * \name bitwise logic operators
266 *
267 * These are also used as boolean and, or, xor for hardware supporting
268 * integers.
269 */
270 /*@{*/
271 BINOP(iand, nir_type_unsigned)
272 BINOP(ior, nir_type_unsigned)
273 BINOP(ixor, nir_type_unsigned)
274 /*@{*/
275
276 /**
277 * \name floating point logic operators
278 *
279 * These use (src != 0.0) for testing the truth of the input, and output 1.0
280 * for true and 0.0 for false
281 */
282 BINOP(fand, nir_type_float)
283 BINOP(for, nir_type_float)
284 BINOP(fxor, nir_type_float)
285
286 BINOP_REDUCE(fdot, 1, nir_type_float, nir_type_float)
287
288 BINOP(fmin, nir_type_float)
289 BINOP(imin, nir_type_int)
290 BINOP(umin, nir_type_unsigned)
291 BINOP(fmax, nir_type_float)
292 BINOP(imax, nir_type_int)
293 BINOP(umax, nir_type_unsigned)
294
295 BINOP(fpow, nir_type_float)
296
297 BINOP_HORIZ(pack_half_2x16_split, 1, nir_type_unsigned, 1, nir_type_float, 1, nir_type_float)
298
299 BINOP(bfm, nir_type_unsigned)
300
301 BINOP(ldexp, nir_type_unsigned)
302
303 /**
304 * Combines the first component of each input to make a 2-component vector.
305 */
306 BINOP_HORIZ(vec2, 2, nir_type_unsigned, 1, nir_type_unsigned, 1, nir_type_unsigned)
307
308 #define TRIOP(name, type) \
309 OPCODE(name, 3, true, 0, type, ARR(0, 0, 0), ARR(type, type, type))
310 #define TRIOP_HORIZ(name, output_size, src1_size, src2_size, src3_size) \
311 OPCODE(name, 3, false, output_size, nir_type_unsigned, \
312 ARR(src1_size, src2_size, src3_size), \
313 ARR(nir_type_unsigned, nir_type_unsigned, nir_type_unsigned))
314
315 /* fma(a, b, c) = (a * b) + c */
316 TRIOP(ffma, nir_type_float)
317
318 TRIOP(flrp, nir_type_float)
319
320 /**
321 * \name Conditional Select
322 *
323 * A vector conditional select instruction (like ?:, but operating per-
324 * component on vectors). There are two versions, one for floating point
325 * bools (0.0 vs 1.0) and one for integer bools (0 vs ~0).
326 */
327
328 OPCODE(fcsel, 3, true, 0, nir_type_float, ARR(1, 0, 0),
329 ARR(nir_type_float, nir_type_float, nir_type_float))
330 OPCODE(bcsel, 3, true, 0, nir_type_unsigned, ARR(1, 0, 0),
331 ARR(nir_type_bool, nir_type_unsigned, nir_type_unsigned))
332
333 TRIOP(bfi, nir_type_unsigned)
334
335 TRIOP(ubitfield_extract, nir_type_unsigned)
336 OPCODE(ibitfield_extract, 3, true, 0, nir_type_int, ARR(0, 0, 0),
337 ARR(nir_type_int, nir_type_unsigned, nir_type_unsigned))
338
339 /**
340 * Combines the first component of each input to make a 3-component vector.
341 */
342 TRIOP_HORIZ(vec3, 3, 1, 1, 1)
343
344 #define QUADOP(name) \
345 OPCODE(name, 4, true, 0, nir_type_unsigned, ARR(0, 0, 0, 0), \
346 ARR(nir_type_unsigned, nir_type_unsigned, nir_type_unsigned, nir_type_unsigned))
347 #define QUADOP_HORIZ(name, output_size, src1_size, src2_size, src3_size, \
348 src4_size) \
349 OPCODE(name, 4, false, output_size, nir_type_unsigned, \
350 ARR(src1_size, src2_size, src3_size, src4_size), \
351 ARR(nir_type_unsigned, nir_type_unsigned, nir_type_unsigned, nir_type_unsigned))
352
353 QUADOP(bitfield_insert)
354
355 QUADOP_HORIZ(vec4, 4, 1, 1, 1, 1)
356
357 LAST_OPCODE(vec4)