1bf215546Sopenharmony_ci#
2bf215546Sopenharmony_ci# Copyright (C) 2014 Connor Abbott
3bf215546Sopenharmony_ci#
4bf215546Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci# copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci# to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci# the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci# and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci# Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci#
11bf215546Sopenharmony_ci# The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci# paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci# Software.
14bf215546Sopenharmony_ci#
15bf215546Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci# IN THE SOFTWARE.
22bf215546Sopenharmony_ci#
23bf215546Sopenharmony_ci# Authors:
24bf215546Sopenharmony_ci#    Connor Abbott (cwabbott0@gmail.com)
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ciimport re
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci# Class that represents all the information we have about the opcode
29bf215546Sopenharmony_ci# NOTE: this must be kept in sync with nir_op_info
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ciclass Opcode(object):
32bf215546Sopenharmony_ci   """Class that represents all the information we have about the opcode
33bf215546Sopenharmony_ci   NOTE: this must be kept in sync with nir_op_info
34bf215546Sopenharmony_ci   """
35bf215546Sopenharmony_ci   def __init__(self, name, output_size, output_type, input_sizes,
36bf215546Sopenharmony_ci                input_types, is_conversion, algebraic_properties, const_expr):
37bf215546Sopenharmony_ci      """Parameters:
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci      - name is the name of the opcode (prepend nir_op_ for the enum name)
40bf215546Sopenharmony_ci      - all types are strings that get nir_type_ prepended to them
41bf215546Sopenharmony_ci      - input_types is a list of types
42bf215546Sopenharmony_ci      - is_conversion is true if this opcode represents a type conversion
43bf215546Sopenharmony_ci      - algebraic_properties is a space-seperated string, where nir_op_is_ is
44bf215546Sopenharmony_ci        prepended before each entry
45bf215546Sopenharmony_ci      - const_expr is an expression or series of statements that computes the
46bf215546Sopenharmony_ci        constant value of the opcode given the constant values of its inputs.
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci      Constant expressions are formed from the variables src0, src1, ...,
49bf215546Sopenharmony_ci      src(N-1), where N is the number of arguments.  The output of the
50bf215546Sopenharmony_ci      expression should be stored in the dst variable.  Per-component input
51bf215546Sopenharmony_ci      and output variables will be scalars and non-per-component input and
52bf215546Sopenharmony_ci      output variables will be a struct with fields named x, y, z, and w
53bf215546Sopenharmony_ci      all of the correct type.  Input and output variables can be assumed
54bf215546Sopenharmony_ci      to already be of the correct type and need no conversion.  In
55bf215546Sopenharmony_ci      particular, the conversion from the C bool type to/from  NIR_TRUE and
56bf215546Sopenharmony_ci      NIR_FALSE happens automatically.
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci      For per-component instructions, the entire expression will be
59bf215546Sopenharmony_ci      executed once for each component.  For non-per-component
60bf215546Sopenharmony_ci      instructions, the expression is expected to store the correct values
61bf215546Sopenharmony_ci      in dst.x, dst.y, etc.  If "dst" does not exist anywhere in the
62bf215546Sopenharmony_ci      constant expression, an assignment to dst will happen automatically
63bf215546Sopenharmony_ci      and the result will be equivalent to "dst = <expression>" for
64bf215546Sopenharmony_ci      per-component instructions and "dst.x = dst.y = ... = <expression>"
65bf215546Sopenharmony_ci      for non-per-component instructions.
66bf215546Sopenharmony_ci      """
67bf215546Sopenharmony_ci      assert isinstance(name, str)
68bf215546Sopenharmony_ci      assert isinstance(output_size, int)
69bf215546Sopenharmony_ci      assert isinstance(output_type, str)
70bf215546Sopenharmony_ci      assert isinstance(input_sizes, list)
71bf215546Sopenharmony_ci      assert isinstance(input_sizes[0], int)
72bf215546Sopenharmony_ci      assert isinstance(input_types, list)
73bf215546Sopenharmony_ci      assert isinstance(input_types[0], str)
74bf215546Sopenharmony_ci      assert isinstance(is_conversion, bool)
75bf215546Sopenharmony_ci      assert isinstance(algebraic_properties, str)
76bf215546Sopenharmony_ci      assert isinstance(const_expr, str)
77bf215546Sopenharmony_ci      assert len(input_sizes) == len(input_types)
78bf215546Sopenharmony_ci      assert 0 <= output_size <= 5 or (output_size == 8) or (output_size == 16)
79bf215546Sopenharmony_ci      for size in input_sizes:
80bf215546Sopenharmony_ci         assert 0 <= size <= 5 or (size == 8) or (size == 16)
81bf215546Sopenharmony_ci         if output_size != 0:
82bf215546Sopenharmony_ci            assert size != 0
83bf215546Sopenharmony_ci      self.name = name
84bf215546Sopenharmony_ci      self.num_inputs = len(input_sizes)
85bf215546Sopenharmony_ci      self.output_size = output_size
86bf215546Sopenharmony_ci      self.output_type = output_type
87bf215546Sopenharmony_ci      self.input_sizes = input_sizes
88bf215546Sopenharmony_ci      self.input_types = input_types
89bf215546Sopenharmony_ci      self.is_conversion = is_conversion
90bf215546Sopenharmony_ci      self.algebraic_properties = algebraic_properties
91bf215546Sopenharmony_ci      self.const_expr = const_expr
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci# helper variables for strings
94bf215546Sopenharmony_citfloat = "float"
95bf215546Sopenharmony_citint = "int"
96bf215546Sopenharmony_citbool = "bool"
97bf215546Sopenharmony_citbool1 = "bool1"
98bf215546Sopenharmony_citbool8 = "bool8"
99bf215546Sopenharmony_citbool16 = "bool16"
100bf215546Sopenharmony_citbool32 = "bool32"
101bf215546Sopenharmony_cituint = "uint"
102bf215546Sopenharmony_cituint8 = "uint8"
103bf215546Sopenharmony_citint16 = "int16"
104bf215546Sopenharmony_cituint16 = "uint16"
105bf215546Sopenharmony_citfloat16 = "float16"
106bf215546Sopenharmony_citfloat32 = "float32"
107bf215546Sopenharmony_citint32 = "int32"
108bf215546Sopenharmony_cituint32 = "uint32"
109bf215546Sopenharmony_citint64 = "int64"
110bf215546Sopenharmony_cituint64 = "uint64"
111bf215546Sopenharmony_citfloat64 = "float64"
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci_TYPE_SPLIT_RE = re.compile(r'(?P<type>int|uint|float|bool)(?P<bits>\d+)?')
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_cidef type_has_size(type_):
116bf215546Sopenharmony_ci    m = _TYPE_SPLIT_RE.match(type_)
117bf215546Sopenharmony_ci    assert m is not None, 'Invalid NIR type string: "{}"'.format(type_)
118bf215546Sopenharmony_ci    return m.group('bits') is not None
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_cidef type_size(type_):
121bf215546Sopenharmony_ci    m = _TYPE_SPLIT_RE.match(type_)
122bf215546Sopenharmony_ci    assert m is not None, 'Invalid NIR type string: "{}"'.format(type_)
123bf215546Sopenharmony_ci    assert m.group('bits') is not None, \
124bf215546Sopenharmony_ci           'NIR type string has no bit size: "{}"'.format(type_)
125bf215546Sopenharmony_ci    return int(m.group('bits'))
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_cidef type_sizes(type_):
128bf215546Sopenharmony_ci    if type_has_size(type_):
129bf215546Sopenharmony_ci        return [type_size(type_)]
130bf215546Sopenharmony_ci    elif type_ == 'bool':
131bf215546Sopenharmony_ci        return [1, 8, 16, 32]
132bf215546Sopenharmony_ci    elif type_ == 'float':
133bf215546Sopenharmony_ci        return [16, 32, 64]
134bf215546Sopenharmony_ci    else:
135bf215546Sopenharmony_ci        return [1, 8, 16, 32, 64]
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_cidef type_base_type(type_):
138bf215546Sopenharmony_ci    m = _TYPE_SPLIT_RE.match(type_)
139bf215546Sopenharmony_ci    assert m is not None, 'Invalid NIR type string: "{}"'.format(type_)
140bf215546Sopenharmony_ci    return m.group('type')
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci# Operation where the first two sources are commutative.
143bf215546Sopenharmony_ci#
144bf215546Sopenharmony_ci# For 2-source operations, this just mathematical commutativity.  Some
145bf215546Sopenharmony_ci# 3-source operations, like ffma, are only commutative in the first two
146bf215546Sopenharmony_ci# sources.
147bf215546Sopenharmony_ci_2src_commutative = "2src_commutative "
148bf215546Sopenharmony_ciassociative = "associative "
149bf215546Sopenharmony_ciselection = "selection "
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci# global dictionary of opcodes
152bf215546Sopenharmony_ciopcodes = {}
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_cidef opcode(name, output_size, output_type, input_sizes, input_types,
155bf215546Sopenharmony_ci           is_conversion, algebraic_properties, const_expr):
156bf215546Sopenharmony_ci   assert name not in opcodes
157bf215546Sopenharmony_ci   opcodes[name] = Opcode(name, output_size, output_type, input_sizes,
158bf215546Sopenharmony_ci                          input_types, is_conversion, algebraic_properties,
159bf215546Sopenharmony_ci                          const_expr)
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_cidef unop_convert(name, out_type, in_type, const_expr):
162bf215546Sopenharmony_ci   opcode(name, 0, out_type, [0], [in_type], False, "", const_expr)
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_cidef unop(name, ty, const_expr):
165bf215546Sopenharmony_ci   opcode(name, 0, ty, [0], [ty], False, "", const_expr)
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_cidef unop_horiz(name, output_size, output_type, input_size, input_type,
168bf215546Sopenharmony_ci               const_expr):
169bf215546Sopenharmony_ci   opcode(name, output_size, output_type, [input_size], [input_type],
170bf215546Sopenharmony_ci          False, "", const_expr)
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_cidef unop_reduce(name, output_size, output_type, input_type, prereduce_expr,
173bf215546Sopenharmony_ci                reduce_expr, final_expr):
174bf215546Sopenharmony_ci   def prereduce(src):
175bf215546Sopenharmony_ci      return "(" + prereduce_expr.format(src=src) + ")"
176bf215546Sopenharmony_ci   def final(src):
177bf215546Sopenharmony_ci      return final_expr.format(src="(" + src + ")")
178bf215546Sopenharmony_ci   def reduce_(src0, src1):
179bf215546Sopenharmony_ci      return reduce_expr.format(src0=src0, src1=src1)
180bf215546Sopenharmony_ci   src0 = prereduce("src0.x")
181bf215546Sopenharmony_ci   src1 = prereduce("src0.y")
182bf215546Sopenharmony_ci   src2 = prereduce("src0.z")
183bf215546Sopenharmony_ci   src3 = prereduce("src0.w")
184bf215546Sopenharmony_ci   unop_horiz(name + "2", output_size, output_type, 2, input_type,
185bf215546Sopenharmony_ci              final(reduce_(src0, src1)))
186bf215546Sopenharmony_ci   unop_horiz(name + "3", output_size, output_type, 3, input_type,
187bf215546Sopenharmony_ci              final(reduce_(reduce_(src0, src1), src2)))
188bf215546Sopenharmony_ci   unop_horiz(name + "4", output_size, output_type, 4, input_type,
189bf215546Sopenharmony_ci              final(reduce_(reduce_(src0, src1), reduce_(src2, src3))))
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_cidef unop_numeric_convert(name, out_type, in_type, const_expr):
192bf215546Sopenharmony_ci   opcode(name, 0, out_type, [0], [in_type], True, "", const_expr)
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_ciunop("mov", tuint, "src0")
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ciunop("ineg", tint, "-src0")
197bf215546Sopenharmony_ciunop("fneg", tfloat, "-src0")
198bf215546Sopenharmony_ciunop("inot", tint, "~src0") # invert every bit of the integer
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ci# nir_op_fsign roughly implements the OpenGL / Vulkan rules for sign(float).
201bf215546Sopenharmony_ci# The GLSL.std.450 FSign instruction is defined as:
202bf215546Sopenharmony_ci#
203bf215546Sopenharmony_ci#    Result is 1.0 if x > 0, 0.0 if x = 0, or -1.0 if x < 0.
204bf215546Sopenharmony_ci#
205bf215546Sopenharmony_ci# If the source is equal to zero, there is a preference for the result to have
206bf215546Sopenharmony_ci# the same sign, but this is not required (it is required by OpenCL).  If the
207bf215546Sopenharmony_ci# source is not a number, there is a preference for the result to be +0.0, but
208bf215546Sopenharmony_ci# this is not required (it is required by OpenCL).  If the source is not a
209bf215546Sopenharmony_ci# number, and the result is not +0.0, the result should definitely **not** be
210bf215546Sopenharmony_ci# NaN.
211bf215546Sopenharmony_ci#
212bf215546Sopenharmony_ci# The values returned for constant folding match the behavior required by
213bf215546Sopenharmony_ci# OpenCL.
214bf215546Sopenharmony_ciunop("fsign", tfloat, ("bit_size == 64 ? " +
215bf215546Sopenharmony_ci                       "(isnan(src0) ? 0.0  : ((src0 == 0.0 ) ? src0 : (src0 > 0.0 ) ? 1.0  : -1.0 )) : " +
216bf215546Sopenharmony_ci                       "(isnan(src0) ? 0.0f : ((src0 == 0.0f) ? src0 : (src0 > 0.0f) ? 1.0f : -1.0f))"))
217bf215546Sopenharmony_ciunop("isign", tint, "(src0 == 0) ? 0 : ((src0 > 0) ? 1 : -1)")
218bf215546Sopenharmony_ciunop("iabs", tint, "(src0 < 0) ? -src0 : src0")
219bf215546Sopenharmony_ciunop("fabs", tfloat, "fabs(src0)")
220bf215546Sopenharmony_ciunop("fsat", tfloat, ("fmin(fmax(src0, 0.0), 1.0)"))
221bf215546Sopenharmony_ciunop("frcp", tfloat, "bit_size == 64 ? 1.0 / src0 : 1.0f / src0")
222bf215546Sopenharmony_ciunop("frsq", tfloat, "bit_size == 64 ? 1.0 / sqrt(src0) : 1.0f / sqrtf(src0)")
223bf215546Sopenharmony_ciunop("fsqrt", tfloat, "bit_size == 64 ? sqrt(src0) : sqrtf(src0)")
224bf215546Sopenharmony_ciunop("fexp2", tfloat, "exp2f(src0)")
225bf215546Sopenharmony_ciunop("flog2", tfloat, "log2f(src0)")
226bf215546Sopenharmony_ci
227bf215546Sopenharmony_ci# Generate all of the numeric conversion opcodes
228bf215546Sopenharmony_cifor src_t in [tint, tuint, tfloat, tbool]:
229bf215546Sopenharmony_ci   if src_t == tbool:
230bf215546Sopenharmony_ci      dst_types = [tfloat, tint, tbool]
231bf215546Sopenharmony_ci   elif src_t == tint:
232bf215546Sopenharmony_ci      dst_types = [tfloat, tint, tbool]
233bf215546Sopenharmony_ci   elif src_t == tuint:
234bf215546Sopenharmony_ci      dst_types = [tfloat, tuint]
235bf215546Sopenharmony_ci   elif src_t == tfloat:
236bf215546Sopenharmony_ci      dst_types = [tint, tuint, tfloat, tbool]
237bf215546Sopenharmony_ci
238bf215546Sopenharmony_ci   for dst_t in dst_types:
239bf215546Sopenharmony_ci      for dst_bit_size in type_sizes(dst_t):
240bf215546Sopenharmony_ci          if dst_bit_size == 16 and dst_t == tfloat and src_t == tfloat:
241bf215546Sopenharmony_ci              rnd_modes = ['_rtne', '_rtz', '']
242bf215546Sopenharmony_ci              for rnd_mode in rnd_modes:
243bf215546Sopenharmony_ci                  if rnd_mode == '_rtne':
244bf215546Sopenharmony_ci                      conv_expr = """
245bf215546Sopenharmony_ci                      if (bit_size > 16) {
246bf215546Sopenharmony_ci                         dst = _mesa_half_to_float(_mesa_float_to_float16_rtne(src0));
247bf215546Sopenharmony_ci                      } else {
248bf215546Sopenharmony_ci                         dst = src0;
249bf215546Sopenharmony_ci                      }
250bf215546Sopenharmony_ci                      """
251bf215546Sopenharmony_ci                  elif rnd_mode == '_rtz':
252bf215546Sopenharmony_ci                      conv_expr = """
253bf215546Sopenharmony_ci                      if (bit_size > 16) {
254bf215546Sopenharmony_ci                         dst = _mesa_half_to_float(_mesa_float_to_float16_rtz(src0));
255bf215546Sopenharmony_ci                      } else {
256bf215546Sopenharmony_ci                         dst = src0;
257bf215546Sopenharmony_ci                      }
258bf215546Sopenharmony_ci                      """
259bf215546Sopenharmony_ci                  else:
260bf215546Sopenharmony_ci                      conv_expr = "src0"
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_ci                  unop_numeric_convert("{0}2{1}{2}{3}".format(src_t[0],
263bf215546Sopenharmony_ci                                                              dst_t[0],
264bf215546Sopenharmony_ci                                                              dst_bit_size,
265bf215546Sopenharmony_ci                                                              rnd_mode),
266bf215546Sopenharmony_ci                                       dst_t + str(dst_bit_size),
267bf215546Sopenharmony_ci                                       src_t, conv_expr)
268bf215546Sopenharmony_ci          elif dst_bit_size == 32 and dst_t == tfloat and src_t == tfloat:
269bf215546Sopenharmony_ci              conv_expr = """
270bf215546Sopenharmony_ci              if (bit_size > 32 && nir_is_rounding_mode_rtz(execution_mode, 32)) {
271bf215546Sopenharmony_ci                 dst = _mesa_double_to_float_rtz(src0);
272bf215546Sopenharmony_ci              } else {
273bf215546Sopenharmony_ci                 dst = src0;
274bf215546Sopenharmony_ci              }
275bf215546Sopenharmony_ci              """
276bf215546Sopenharmony_ci              unop_numeric_convert("{0}2{1}{2}".format(src_t[0], dst_t[0],
277bf215546Sopenharmony_ci                                                       dst_bit_size),
278bf215546Sopenharmony_ci                                   dst_t + str(dst_bit_size), src_t, conv_expr)
279bf215546Sopenharmony_ci          else:
280bf215546Sopenharmony_ci              conv_expr = "src0 != 0" if dst_t == tbool else "src0"
281bf215546Sopenharmony_ci              unop_numeric_convert("{0}2{1}{2}".format(src_t[0], dst_t[0],
282bf215546Sopenharmony_ci                                                       dst_bit_size),
283bf215546Sopenharmony_ci                                   dst_t + str(dst_bit_size), src_t, conv_expr)
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci# Special opcode that is the same as f2f16, i2i16, u2u16 except that it is safe
286bf215546Sopenharmony_ci# to remove it if the result is immediately converted back to 32 bits again.
287bf215546Sopenharmony_ci# This is generated as part of the precision lowering pass. mp stands for medium
288bf215546Sopenharmony_ci# precision.
289bf215546Sopenharmony_ciunop_numeric_convert("f2fmp", tfloat16, tfloat32, opcodes["f2f16"].const_expr)
290bf215546Sopenharmony_ciunop_numeric_convert("i2imp", tint16, tint32, opcodes["i2i16"].const_expr)
291bf215546Sopenharmony_ci# u2ump isn't defined, because the behavior is equal to i2imp
292bf215546Sopenharmony_ciunop_numeric_convert("f2imp", tint16, tfloat32, opcodes["f2i16"].const_expr)
293bf215546Sopenharmony_ciunop_numeric_convert("f2ump", tuint16, tfloat32, opcodes["f2u16"].const_expr)
294bf215546Sopenharmony_ciunop_numeric_convert("i2fmp", tfloat16, tint32, opcodes["i2f16"].const_expr)
295bf215546Sopenharmony_ciunop_numeric_convert("u2fmp", tfloat16, tuint32, opcodes["u2f16"].const_expr)
296bf215546Sopenharmony_ci
297bf215546Sopenharmony_ci# Unary floating-point rounding operations.
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ciunop("ftrunc", tfloat, "bit_size == 64 ? trunc(src0) : truncf(src0)")
301bf215546Sopenharmony_ciunop("fceil", tfloat, "bit_size == 64 ? ceil(src0) : ceilf(src0)")
302bf215546Sopenharmony_ciunop("ffloor", tfloat, "bit_size == 64 ? floor(src0) : floorf(src0)")
303bf215546Sopenharmony_ciunop("ffract", tfloat, "src0 - (bit_size == 64 ? floor(src0) : floorf(src0))")
304bf215546Sopenharmony_ciunop("fround_even", tfloat, "bit_size == 64 ? _mesa_roundeven(src0) : _mesa_roundevenf(src0)")
305bf215546Sopenharmony_ci
306bf215546Sopenharmony_ciunop("fquantize2f16", tfloat, "(fabs(src0) < ldexpf(1.0, -14)) ? copysignf(0.0f, src0) : _mesa_half_to_float(_mesa_float_to_half(src0))")
307bf215546Sopenharmony_ci
308bf215546Sopenharmony_ci# Trigonometric operations.
309bf215546Sopenharmony_ci
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_ciunop("fsin", tfloat, "bit_size == 64 ? sin(src0) : sinf(src0)")
312bf215546Sopenharmony_ciunop("fcos", tfloat, "bit_size == 64 ? cos(src0) : cosf(src0)")
313bf215546Sopenharmony_ci
314bf215546Sopenharmony_ci# dfrexp
315bf215546Sopenharmony_ciunop_convert("frexp_exp", tint32, tfloat, "frexp(src0, &dst);")
316bf215546Sopenharmony_ciunop_convert("frexp_sig", tfloat, tfloat, "int n; dst = frexp(src0, &n);")
317bf215546Sopenharmony_ci
318bf215546Sopenharmony_ci# Partial derivatives.
319bf215546Sopenharmony_ci
320bf215546Sopenharmony_ci
321bf215546Sopenharmony_ciunop("fddx", tfloat, "0.0") # the derivative of a constant is 0.
322bf215546Sopenharmony_ciunop("fddy", tfloat, "0.0")
323bf215546Sopenharmony_ciunop("fddx_fine", tfloat, "0.0")
324bf215546Sopenharmony_ciunop("fddy_fine", tfloat, "0.0")
325bf215546Sopenharmony_ciunop("fddx_coarse", tfloat, "0.0")
326bf215546Sopenharmony_ciunop("fddy_coarse", tfloat, "0.0")
327bf215546Sopenharmony_ci
328bf215546Sopenharmony_ci
329bf215546Sopenharmony_ci# Floating point pack and unpack operations.
330bf215546Sopenharmony_ci
331bf215546Sopenharmony_cidef pack_2x16(fmt):
332bf215546Sopenharmony_ci   unop_horiz("pack_" + fmt + "_2x16", 1, tuint32, 2, tfloat32, """
333bf215546Sopenharmony_cidst.x = (uint32_t) pack_fmt_1x16(src0.x);
334bf215546Sopenharmony_cidst.x |= ((uint32_t) pack_fmt_1x16(src0.y)) << 16;
335bf215546Sopenharmony_ci""".replace("fmt", fmt))
336bf215546Sopenharmony_ci
337bf215546Sopenharmony_cidef pack_4x8(fmt):
338bf215546Sopenharmony_ci   unop_horiz("pack_" + fmt + "_4x8", 1, tuint32, 4, tfloat32, """
339bf215546Sopenharmony_cidst.x = (uint32_t) pack_fmt_1x8(src0.x);
340bf215546Sopenharmony_cidst.x |= ((uint32_t) pack_fmt_1x8(src0.y)) << 8;
341bf215546Sopenharmony_cidst.x |= ((uint32_t) pack_fmt_1x8(src0.z)) << 16;
342bf215546Sopenharmony_cidst.x |= ((uint32_t) pack_fmt_1x8(src0.w)) << 24;
343bf215546Sopenharmony_ci""".replace("fmt", fmt))
344bf215546Sopenharmony_ci
345bf215546Sopenharmony_cidef unpack_2x16(fmt):
346bf215546Sopenharmony_ci   unop_horiz("unpack_" + fmt + "_2x16", 2, tfloat32, 1, tuint32, """
347bf215546Sopenharmony_cidst.x = unpack_fmt_1x16((uint16_t)(src0.x & 0xffff));
348bf215546Sopenharmony_cidst.y = unpack_fmt_1x16((uint16_t)(src0.x << 16));
349bf215546Sopenharmony_ci""".replace("fmt", fmt))
350bf215546Sopenharmony_ci
351bf215546Sopenharmony_cidef unpack_4x8(fmt):
352bf215546Sopenharmony_ci   unop_horiz("unpack_" + fmt + "_4x8", 4, tfloat32, 1, tuint32, """
353bf215546Sopenharmony_cidst.x = unpack_fmt_1x8((uint8_t)(src0.x & 0xff));
354bf215546Sopenharmony_cidst.y = unpack_fmt_1x8((uint8_t)((src0.x >> 8) & 0xff));
355bf215546Sopenharmony_cidst.z = unpack_fmt_1x8((uint8_t)((src0.x >> 16) & 0xff));
356bf215546Sopenharmony_cidst.w = unpack_fmt_1x8((uint8_t)(src0.x >> 24));
357bf215546Sopenharmony_ci""".replace("fmt", fmt))
358bf215546Sopenharmony_ci
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_cipack_2x16("snorm")
361bf215546Sopenharmony_cipack_4x8("snorm")
362bf215546Sopenharmony_cipack_2x16("unorm")
363bf215546Sopenharmony_cipack_4x8("unorm")
364bf215546Sopenharmony_cipack_2x16("half")
365bf215546Sopenharmony_ciunpack_2x16("snorm")
366bf215546Sopenharmony_ciunpack_4x8("snorm")
367bf215546Sopenharmony_ciunpack_2x16("unorm")
368bf215546Sopenharmony_ciunpack_4x8("unorm")
369bf215546Sopenharmony_ciunpack_2x16("half")
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci# Convert two unsigned integers into a packed unsigned short (clamp is applied).
372bf215546Sopenharmony_ciunop_horiz("pack_uint_2x16", 1, tuint32, 2, tuint32, """
373bf215546Sopenharmony_cidst.x = _mesa_unsigned_to_unsigned(src0.x, 16);
374bf215546Sopenharmony_cidst.x |= _mesa_unsigned_to_unsigned(src0.y, 16) << 16;
375bf215546Sopenharmony_ci""")
376bf215546Sopenharmony_ci
377bf215546Sopenharmony_ci# Convert two signed integers into a packed signed short (clamp is applied).
378bf215546Sopenharmony_ciunop_horiz("pack_sint_2x16", 1, tint32, 2, tint32, """
379bf215546Sopenharmony_cidst.x = _mesa_signed_to_signed(src0.x, 16) & 0xffff;
380bf215546Sopenharmony_cidst.x |= _mesa_signed_to_signed(src0.y, 16) << 16;
381bf215546Sopenharmony_ci""")
382bf215546Sopenharmony_ci
383bf215546Sopenharmony_ciunop_horiz("pack_uvec2_to_uint", 1, tuint32, 2, tuint32, """
384bf215546Sopenharmony_cidst.x = (src0.x & 0xffff) | (src0.y << 16);
385bf215546Sopenharmony_ci""")
386bf215546Sopenharmony_ci
387bf215546Sopenharmony_ciunop_horiz("pack_uvec4_to_uint", 1, tuint32, 4, tuint32, """
388bf215546Sopenharmony_cidst.x = (src0.x <<  0) |
389bf215546Sopenharmony_ci        (src0.y <<  8) |
390bf215546Sopenharmony_ci        (src0.z << 16) |
391bf215546Sopenharmony_ci        (src0.w << 24);
392bf215546Sopenharmony_ci""")
393bf215546Sopenharmony_ci
394bf215546Sopenharmony_ciunop_horiz("pack_32_4x8", 1, tuint32, 4, tuint8,
395bf215546Sopenharmony_ci           "dst.x = src0.x | ((uint32_t)src0.y << 8) | ((uint32_t)src0.z << 16) | ((uint32_t)src0.w << 24);")
396bf215546Sopenharmony_ci
397bf215546Sopenharmony_ciunop_horiz("pack_32_2x16", 1, tuint32, 2, tuint16,
398bf215546Sopenharmony_ci           "dst.x = src0.x | ((uint32_t)src0.y << 16);")
399bf215546Sopenharmony_ci
400bf215546Sopenharmony_ciunop_horiz("pack_64_2x32", 1, tuint64, 2, tuint32,
401bf215546Sopenharmony_ci           "dst.x = src0.x | ((uint64_t)src0.y << 32);")
402bf215546Sopenharmony_ci
403bf215546Sopenharmony_ciunop_horiz("pack_64_4x16", 1, tuint64, 4, tuint16,
404bf215546Sopenharmony_ci           "dst.x = src0.x | ((uint64_t)src0.y << 16) | ((uint64_t)src0.z << 32) | ((uint64_t)src0.w << 48);")
405bf215546Sopenharmony_ci
406bf215546Sopenharmony_ciunop_horiz("unpack_64_2x32", 2, tuint32, 1, tuint64,
407bf215546Sopenharmony_ci           "dst.x = src0.x; dst.y = src0.x >> 32;")
408bf215546Sopenharmony_ci
409bf215546Sopenharmony_ciunop_horiz("unpack_64_4x16", 4, tuint16, 1, tuint64,
410bf215546Sopenharmony_ci           "dst.x = src0.x; dst.y = src0.x >> 16; dst.z = src0.x >> 32; dst.w = src0.w >> 48;")
411bf215546Sopenharmony_ci
412bf215546Sopenharmony_ciunop_horiz("unpack_32_2x16", 2, tuint16, 1, tuint32,
413bf215546Sopenharmony_ci           "dst.x = src0.x; dst.y = src0.x >> 16;")
414bf215546Sopenharmony_ci
415bf215546Sopenharmony_ciunop_horiz("unpack_32_4x8", 4, tuint8, 1, tuint32,
416bf215546Sopenharmony_ci           "dst.x = src0.x; dst.y = src0.x >> 8; dst.z = src0.x >> 16; dst.w = src0.x >> 24;")
417bf215546Sopenharmony_ci
418bf215546Sopenharmony_ciunop_horiz("unpack_half_2x16_flush_to_zero", 2, tfloat32, 1, tuint32, """
419bf215546Sopenharmony_cidst.x = unpack_half_1x16_flush_to_zero((uint16_t)(src0.x & 0xffff));
420bf215546Sopenharmony_cidst.y = unpack_half_1x16_flush_to_zero((uint16_t)(src0.x << 16));
421bf215546Sopenharmony_ci""")
422bf215546Sopenharmony_ci
423bf215546Sopenharmony_ci# Lowered floating point unpacking operations.
424bf215546Sopenharmony_ci
425bf215546Sopenharmony_ciunop_convert("unpack_half_2x16_split_x", tfloat32, tuint32,
426bf215546Sopenharmony_ci             "unpack_half_1x16((uint16_t)(src0 & 0xffff))")
427bf215546Sopenharmony_ciunop_convert("unpack_half_2x16_split_y", tfloat32, tuint32,
428bf215546Sopenharmony_ci             "unpack_half_1x16((uint16_t)(src0 >> 16))")
429bf215546Sopenharmony_ci
430bf215546Sopenharmony_ciunop_convert("unpack_half_2x16_split_x_flush_to_zero", tfloat32, tuint32,
431bf215546Sopenharmony_ci             "unpack_half_1x16_flush_to_zero((uint16_t)(src0 & 0xffff))")
432bf215546Sopenharmony_ciunop_convert("unpack_half_2x16_split_y_flush_to_zero", tfloat32, tuint32,
433bf215546Sopenharmony_ci             "unpack_half_1x16_flush_to_zero((uint16_t)(src0 >> 16))")
434bf215546Sopenharmony_ci
435bf215546Sopenharmony_ciunop_convert("unpack_32_2x16_split_x", tuint16, tuint32, "src0")
436bf215546Sopenharmony_ciunop_convert("unpack_32_2x16_split_y", tuint16, tuint32, "src0 >> 16")
437bf215546Sopenharmony_ci
438bf215546Sopenharmony_ciunop_convert("unpack_64_2x32_split_x", tuint32, tuint64, "src0")
439bf215546Sopenharmony_ciunop_convert("unpack_64_2x32_split_y", tuint32, tuint64, "src0 >> 32")
440bf215546Sopenharmony_ci
441bf215546Sopenharmony_ci# Bit operations, part of ARB_gpu_shader5.
442bf215546Sopenharmony_ci
443bf215546Sopenharmony_ci
444bf215546Sopenharmony_ciunop("bitfield_reverse", tuint32, """
445bf215546Sopenharmony_ci/* we're not winning any awards for speed here, but that's ok */
446bf215546Sopenharmony_cidst = 0;
447bf215546Sopenharmony_cifor (unsigned bit = 0; bit < 32; bit++)
448bf215546Sopenharmony_ci   dst |= ((src0 >> bit) & 1) << (31 - bit);
449bf215546Sopenharmony_ci""")
450bf215546Sopenharmony_ciunop_convert("bit_count", tuint32, tuint, """
451bf215546Sopenharmony_cidst = 0;
452bf215546Sopenharmony_cifor (unsigned bit = 0; bit < bit_size; bit++) {
453bf215546Sopenharmony_ci   if ((src0 >> bit) & 1)
454bf215546Sopenharmony_ci      dst++;
455bf215546Sopenharmony_ci}
456bf215546Sopenharmony_ci""")
457bf215546Sopenharmony_ci
458bf215546Sopenharmony_ciunop_convert("ufind_msb", tint32, tuint, """
459bf215546Sopenharmony_cidst = -1;
460bf215546Sopenharmony_cifor (int bit = bit_size - 1; bit >= 0; bit--) {
461bf215546Sopenharmony_ci   if ((src0 >> bit) & 1) {
462bf215546Sopenharmony_ci      dst = bit;
463bf215546Sopenharmony_ci      break;
464bf215546Sopenharmony_ci   }
465bf215546Sopenharmony_ci}
466bf215546Sopenharmony_ci""")
467bf215546Sopenharmony_ci
468bf215546Sopenharmony_ciunop_convert("ufind_msb_rev", tint32, tuint, """
469bf215546Sopenharmony_cidst = -1;
470bf215546Sopenharmony_cifor (int bit = 0; bit < bit_size; bit++) {
471bf215546Sopenharmony_ci   if ((src0 << bit) & 0x80000000) {
472bf215546Sopenharmony_ci      dst = bit;
473bf215546Sopenharmony_ci      break;
474bf215546Sopenharmony_ci   }
475bf215546Sopenharmony_ci}
476bf215546Sopenharmony_ci""")
477bf215546Sopenharmony_ci
478bf215546Sopenharmony_ciunop("uclz", tuint32, """
479bf215546Sopenharmony_ciint bit;
480bf215546Sopenharmony_cifor (bit = bit_size - 1; bit >= 0; bit--) {
481bf215546Sopenharmony_ci   if ((src0 & (1u << bit)) != 0)
482bf215546Sopenharmony_ci      break;
483bf215546Sopenharmony_ci}
484bf215546Sopenharmony_cidst = (unsigned)(bit_size - bit - 1);
485bf215546Sopenharmony_ci""")
486bf215546Sopenharmony_ci
487bf215546Sopenharmony_ciunop("ifind_msb", tint32, """
488bf215546Sopenharmony_cidst = -1;
489bf215546Sopenharmony_cifor (int bit = bit_size - 1; bit >= 0; bit--) {
490bf215546Sopenharmony_ci   /* If src0 < 0, we're looking for the first 0 bit.
491bf215546Sopenharmony_ci    * if src0 >= 0, we're looking for the first 1 bit.
492bf215546Sopenharmony_ci    */
493bf215546Sopenharmony_ci   if ((((src0 >> bit) & 1) && (src0 >= 0)) ||
494bf215546Sopenharmony_ci      (!((src0 >> bit) & 1) && (src0 < 0))) {
495bf215546Sopenharmony_ci      dst = bit;
496bf215546Sopenharmony_ci      break;
497bf215546Sopenharmony_ci   }
498bf215546Sopenharmony_ci}
499bf215546Sopenharmony_ci""")
500bf215546Sopenharmony_ci
501bf215546Sopenharmony_ciunop_convert("ifind_msb_rev", tint32, tint, """
502bf215546Sopenharmony_cidst = -1;
503bf215546Sopenharmony_ci/* We are looking for the highest bit that's not the same as the sign bit. */
504bf215546Sopenharmony_ciuint32_t sign = src0 & 0x80000000u;
505bf215546Sopenharmony_cifor (int bit = 0; bit < 32; bit++) {
506bf215546Sopenharmony_ci   if (((src0 << bit) & 0x80000000u) != sign) {
507bf215546Sopenharmony_ci      dst = bit;
508bf215546Sopenharmony_ci      break;
509bf215546Sopenharmony_ci   }
510bf215546Sopenharmony_ci}
511bf215546Sopenharmony_ci""")
512bf215546Sopenharmony_ci
513bf215546Sopenharmony_ciunop_convert("find_lsb", tint32, tint, """
514bf215546Sopenharmony_cidst = -1;
515bf215546Sopenharmony_cifor (unsigned bit = 0; bit < bit_size; bit++) {
516bf215546Sopenharmony_ci   if ((src0 >> bit) & 1) {
517bf215546Sopenharmony_ci      dst = bit;
518bf215546Sopenharmony_ci      break;
519bf215546Sopenharmony_ci   }
520bf215546Sopenharmony_ci}
521bf215546Sopenharmony_ci""")
522bf215546Sopenharmony_ci
523bf215546Sopenharmony_ci# AMD_gcn_shader extended instructions
524bf215546Sopenharmony_ciunop_horiz("cube_face_coord_amd", 2, tfloat32, 3, tfloat32, """
525bf215546Sopenharmony_cidst.x = dst.y = 0.0;
526bf215546Sopenharmony_cifloat absX = fabsf(src0.x);
527bf215546Sopenharmony_cifloat absY = fabsf(src0.y);
528bf215546Sopenharmony_cifloat absZ = fabsf(src0.z);
529bf215546Sopenharmony_ci
530bf215546Sopenharmony_cifloat ma = 0.0;
531bf215546Sopenharmony_ciif (absX >= absY && absX >= absZ) { ma = 2 * src0.x; }
532bf215546Sopenharmony_ciif (absY >= absX && absY >= absZ) { ma = 2 * src0.y; }
533bf215546Sopenharmony_ciif (absZ >= absX && absZ >= absY) { ma = 2 * src0.z; }
534bf215546Sopenharmony_ci
535bf215546Sopenharmony_ciif (src0.x >= 0 && absX >= absY && absX >= absZ) { dst.x = -src0.z; dst.y = -src0.y; }
536bf215546Sopenharmony_ciif (src0.x < 0 && absX >= absY && absX >= absZ) { dst.x = src0.z; dst.y = -src0.y; }
537bf215546Sopenharmony_ciif (src0.y >= 0 && absY >= absX && absY >= absZ) { dst.x = src0.x; dst.y = src0.z; }
538bf215546Sopenharmony_ciif (src0.y < 0 && absY >= absX && absY >= absZ) { dst.x = src0.x; dst.y = -src0.z; }
539bf215546Sopenharmony_ciif (src0.z >= 0 && absZ >= absX && absZ >= absY) { dst.x = src0.x; dst.y = -src0.y; }
540bf215546Sopenharmony_ciif (src0.z < 0 && absZ >= absX && absZ >= absY) { dst.x = -src0.x; dst.y = -src0.y; }
541bf215546Sopenharmony_ci
542bf215546Sopenharmony_cidst.x = dst.x * (1.0f / ma) + 0.5f;
543bf215546Sopenharmony_cidst.y = dst.y * (1.0f / ma) + 0.5f;
544bf215546Sopenharmony_ci""")
545bf215546Sopenharmony_ci
546bf215546Sopenharmony_ciunop_horiz("cube_face_index_amd", 1, tfloat32, 3, tfloat32, """
547bf215546Sopenharmony_cidst.x = 0.0;
548bf215546Sopenharmony_cifloat absX = fabsf(src0.x);
549bf215546Sopenharmony_cifloat absY = fabsf(src0.y);
550bf215546Sopenharmony_cifloat absZ = fabsf(src0.z);
551bf215546Sopenharmony_ciif (src0.x >= 0 && absX >= absY && absX >= absZ) dst.x = 0;
552bf215546Sopenharmony_ciif (src0.x < 0 && absX >= absY && absX >= absZ) dst.x = 1;
553bf215546Sopenharmony_ciif (src0.y >= 0 && absY >= absX && absY >= absZ) dst.x = 2;
554bf215546Sopenharmony_ciif (src0.y < 0 && absY >= absX && absY >= absZ) dst.x = 3;
555bf215546Sopenharmony_ciif (src0.z >= 0 && absZ >= absX && absZ >= absY) dst.x = 4;
556bf215546Sopenharmony_ciif (src0.z < 0 && absZ >= absX && absZ >= absY) dst.x = 5;
557bf215546Sopenharmony_ci""")
558bf215546Sopenharmony_ci
559bf215546Sopenharmony_ci# Sum of vector components
560bf215546Sopenharmony_ciunop_reduce("fsum", 1, tfloat, tfloat, "{src}", "{src0} + {src1}", "{src}")
561bf215546Sopenharmony_ci
562bf215546Sopenharmony_cidef binop_convert(name, out_type, in_type, alg_props, const_expr):
563bf215546Sopenharmony_ci   opcode(name, 0, out_type, [0, 0], [in_type, in_type],
564bf215546Sopenharmony_ci          False, alg_props, const_expr)
565bf215546Sopenharmony_ci
566bf215546Sopenharmony_cidef binop(name, ty, alg_props, const_expr):
567bf215546Sopenharmony_ci   binop_convert(name, ty, ty, alg_props, const_expr)
568bf215546Sopenharmony_ci
569bf215546Sopenharmony_cidef binop_compare(name, ty, alg_props, const_expr):
570bf215546Sopenharmony_ci   binop_convert(name, tbool1, ty, alg_props, const_expr)
571bf215546Sopenharmony_ci
572bf215546Sopenharmony_cidef binop_compare8(name, ty, alg_props, const_expr):
573bf215546Sopenharmony_ci   binop_convert(name, tbool8, ty, alg_props, const_expr)
574bf215546Sopenharmony_ci
575bf215546Sopenharmony_cidef binop_compare16(name, ty, alg_props, const_expr):
576bf215546Sopenharmony_ci   binop_convert(name, tbool16, ty, alg_props, const_expr)
577bf215546Sopenharmony_ci
578bf215546Sopenharmony_cidef binop_compare32(name, ty, alg_props, const_expr):
579bf215546Sopenharmony_ci   binop_convert(name, tbool32, ty, alg_props, const_expr)
580bf215546Sopenharmony_ci
581bf215546Sopenharmony_cidef binop_compare_all_sizes(name, ty, alg_props, const_expr):
582bf215546Sopenharmony_ci   binop_compare(name, ty, alg_props, const_expr)
583bf215546Sopenharmony_ci   binop_compare8(name + "8", ty, alg_props, const_expr)
584bf215546Sopenharmony_ci   binop_compare16(name + "16", ty, alg_props, const_expr)
585bf215546Sopenharmony_ci   binop_compare32(name + "32", ty, alg_props, const_expr)
586bf215546Sopenharmony_ci
587bf215546Sopenharmony_cidef binop_horiz(name, out_size, out_type, src1_size, src1_type, src2_size,
588bf215546Sopenharmony_ci                src2_type, const_expr):
589bf215546Sopenharmony_ci   opcode(name, out_size, out_type, [src1_size, src2_size], [src1_type, src2_type],
590bf215546Sopenharmony_ci          False, "", const_expr)
591bf215546Sopenharmony_ci
592bf215546Sopenharmony_cidef binop_reduce(name, output_size, output_type, src_type, prereduce_expr,
593bf215546Sopenharmony_ci                 reduce_expr, final_expr, suffix=""):
594bf215546Sopenharmony_ci   def final(src):
595bf215546Sopenharmony_ci      return final_expr.format(src= "(" + src + ")")
596bf215546Sopenharmony_ci   def reduce_(src0, src1):
597bf215546Sopenharmony_ci      return reduce_expr.format(src0=src0, src1=src1)
598bf215546Sopenharmony_ci   def prereduce(src0, src1):
599bf215546Sopenharmony_ci      return "(" + prereduce_expr.format(src0=src0, src1=src1) + ")"
600bf215546Sopenharmony_ci   srcs = [prereduce("src0." + letter, "src1." + letter) for letter in "xyzwefghijklmnop"]
601bf215546Sopenharmony_ci   def pairwise_reduce(start, size):
602bf215546Sopenharmony_ci      if (size == 1):
603bf215546Sopenharmony_ci         return srcs[start]
604bf215546Sopenharmony_ci      return reduce_(pairwise_reduce(start + size // 2, size // 2), pairwise_reduce(start, size // 2))
605bf215546Sopenharmony_ci   for size in [2, 4, 8, 16]:
606bf215546Sopenharmony_ci      opcode(name + str(size) + suffix, output_size, output_type,
607bf215546Sopenharmony_ci             [size, size], [src_type, src_type], False, _2src_commutative,
608bf215546Sopenharmony_ci             final(pairwise_reduce(0, size)))
609bf215546Sopenharmony_ci   opcode(name + "3" + suffix, output_size, output_type,
610bf215546Sopenharmony_ci          [3, 3], [src_type, src_type], False, _2src_commutative,
611bf215546Sopenharmony_ci          final(reduce_(reduce_(srcs[2], srcs[1]), srcs[0])))
612bf215546Sopenharmony_ci   opcode(name + "5" + suffix, output_size, output_type,
613bf215546Sopenharmony_ci          [5, 5], [src_type, src_type], False, _2src_commutative,
614bf215546Sopenharmony_ci          final(reduce_(srcs[4], reduce_(reduce_(srcs[3], srcs[2]), reduce_(srcs[1], srcs[0])))))
615bf215546Sopenharmony_ci
616bf215546Sopenharmony_cidef binop_reduce_all_sizes(name, output_size, src_type, prereduce_expr,
617bf215546Sopenharmony_ci                           reduce_expr, final_expr):
618bf215546Sopenharmony_ci   binop_reduce(name, output_size, tbool1, src_type,
619bf215546Sopenharmony_ci                prereduce_expr, reduce_expr, final_expr)
620bf215546Sopenharmony_ci   binop_reduce("b8" + name[1:], output_size, tbool8, src_type,
621bf215546Sopenharmony_ci                prereduce_expr, reduce_expr, final_expr)
622bf215546Sopenharmony_ci   binop_reduce("b16" + name[1:], output_size, tbool16, src_type,
623bf215546Sopenharmony_ci                prereduce_expr, reduce_expr, final_expr)
624bf215546Sopenharmony_ci   binop_reduce("b32" + name[1:], output_size, tbool32, src_type,
625bf215546Sopenharmony_ci                prereduce_expr, reduce_expr, final_expr)
626bf215546Sopenharmony_ci
627bf215546Sopenharmony_cibinop("fadd", tfloat, _2src_commutative + associative,"""
628bf215546Sopenharmony_ciif (nir_is_rounding_mode_rtz(execution_mode, bit_size)) {
629bf215546Sopenharmony_ci   if (bit_size == 64)
630bf215546Sopenharmony_ci      dst = _mesa_double_add_rtz(src0, src1);
631bf215546Sopenharmony_ci   else
632bf215546Sopenharmony_ci      dst = _mesa_double_to_float_rtz((double)src0 + (double)src1);
633bf215546Sopenharmony_ci} else {
634bf215546Sopenharmony_ci   dst = src0 + src1;
635bf215546Sopenharmony_ci}
636bf215546Sopenharmony_ci""")
637bf215546Sopenharmony_cibinop("iadd", tint, _2src_commutative + associative, "(uint64_t)src0 + (uint64_t)src1")
638bf215546Sopenharmony_cibinop("iadd_sat", tint, _2src_commutative, """
639bf215546Sopenharmony_ci      src1 > 0 ?
640bf215546Sopenharmony_ci         (src0 + src1 < src0 ? u_intN_max(bit_size) : src0 + src1) :
641bf215546Sopenharmony_ci         (src0 < src0 + src1 ? u_intN_min(bit_size) : src0 + src1)
642bf215546Sopenharmony_ci""")
643bf215546Sopenharmony_cibinop("uadd_sat", tuint, _2src_commutative,
644bf215546Sopenharmony_ci      "(src0 + src1) < src0 ? u_uintN_max(sizeof(src0) * 8) : (src0 + src1)")
645bf215546Sopenharmony_cibinop("isub_sat", tint, "", """
646bf215546Sopenharmony_ci      src1 < 0 ?
647bf215546Sopenharmony_ci         (src0 - src1 < src0 ? u_intN_max(bit_size) : src0 - src1) :
648bf215546Sopenharmony_ci         (src0 < src0 - src1 ? u_intN_min(bit_size) : src0 - src1)
649bf215546Sopenharmony_ci""")
650bf215546Sopenharmony_cibinop("usub_sat", tuint, "", "src0 < src1 ? 0 : src0 - src1")
651bf215546Sopenharmony_ci
652bf215546Sopenharmony_cibinop("fsub", tfloat, "", """
653bf215546Sopenharmony_ciif (nir_is_rounding_mode_rtz(execution_mode, bit_size)) {
654bf215546Sopenharmony_ci   if (bit_size == 64)
655bf215546Sopenharmony_ci      dst = _mesa_double_sub_rtz(src0, src1);
656bf215546Sopenharmony_ci   else
657bf215546Sopenharmony_ci      dst = _mesa_double_to_float_rtz((double)src0 - (double)src1);
658bf215546Sopenharmony_ci} else {
659bf215546Sopenharmony_ci   dst = src0 - src1;
660bf215546Sopenharmony_ci}
661bf215546Sopenharmony_ci""")
662bf215546Sopenharmony_cibinop("isub", tint, "", "src0 - src1")
663bf215546Sopenharmony_cibinop_convert("uabs_isub", tuint, tint, "", """
664bf215546Sopenharmony_ci              src1 > src0 ? (uint64_t) src1 - (uint64_t) src0
665bf215546Sopenharmony_ci                          : (uint64_t) src0 - (uint64_t) src1
666bf215546Sopenharmony_ci""")
667bf215546Sopenharmony_cibinop("uabs_usub", tuint, "", "(src1 > src0) ? (src1 - src0) : (src0 - src1)")
668bf215546Sopenharmony_ci
669bf215546Sopenharmony_cibinop("fmul", tfloat, _2src_commutative + associative, """
670bf215546Sopenharmony_ciif (nir_is_rounding_mode_rtz(execution_mode, bit_size)) {
671bf215546Sopenharmony_ci   if (bit_size == 64)
672bf215546Sopenharmony_ci      dst = _mesa_double_mul_rtz(src0, src1);
673bf215546Sopenharmony_ci   else
674bf215546Sopenharmony_ci      dst = _mesa_double_to_float_rtz((double)src0 * (double)src1);
675bf215546Sopenharmony_ci} else {
676bf215546Sopenharmony_ci   dst = src0 * src1;
677bf215546Sopenharmony_ci}
678bf215546Sopenharmony_ci""")
679bf215546Sopenharmony_ci
680bf215546Sopenharmony_ci# Unlike fmul, anything (even infinity or NaN) multiplied by zero is always zero.
681bf215546Sopenharmony_ci# fmulz(0.0, inf) and fmulz(0.0, nan) must be +/-0.0, even if
682bf215546Sopenharmony_ci# SIGNED_ZERO_INF_NAN_PRESERVE is not used. If SIGNED_ZERO_INF_NAN_PRESERVE is used, then
683bf215546Sopenharmony_ci# the result must be a positive zero if either operand is zero.
684bf215546Sopenharmony_cibinop("fmulz", tfloat32, _2src_commutative + associative, """
685bf215546Sopenharmony_ciif (src0 == 0.0 || src1 == 0.0)
686bf215546Sopenharmony_ci   dst = 0.0;
687bf215546Sopenharmony_cielse if (nir_is_rounding_mode_rtz(execution_mode, 32))
688bf215546Sopenharmony_ci   dst = _mesa_double_to_float_rtz((double)src0 * (double)src1);
689bf215546Sopenharmony_cielse
690bf215546Sopenharmony_ci   dst = src0 * src1;
691bf215546Sopenharmony_ci""")
692bf215546Sopenharmony_ci
693bf215546Sopenharmony_ci# low 32-bits of signed/unsigned integer multiply
694bf215546Sopenharmony_cibinop("imul", tint, _2src_commutative + associative, """
695bf215546Sopenharmony_ci   /* Use 64-bit multiplies to prevent overflow of signed arithmetic */
696bf215546Sopenharmony_ci   dst = (uint64_t)src0 * (uint64_t)src1;
697bf215546Sopenharmony_ci""")
698bf215546Sopenharmony_ci
699bf215546Sopenharmony_ci# Generate 64 bit result from 2 32 bits quantity
700bf215546Sopenharmony_cibinop_convert("imul_2x32_64", tint64, tint32, _2src_commutative,
701bf215546Sopenharmony_ci              "(int64_t)src0 * (int64_t)src1")
702bf215546Sopenharmony_cibinop_convert("umul_2x32_64", tuint64, tuint32, _2src_commutative,
703bf215546Sopenharmony_ci              "(uint64_t)src0 * (uint64_t)src1")
704bf215546Sopenharmony_ci
705bf215546Sopenharmony_ci# high 32-bits of signed integer multiply
706bf215546Sopenharmony_cibinop("imul_high", tint, _2src_commutative, """
707bf215546Sopenharmony_ciif (bit_size == 64) {
708bf215546Sopenharmony_ci   /* We need to do a full 128-bit x 128-bit multiply in order for the sign
709bf215546Sopenharmony_ci    * extension to work properly.  The casts are kind-of annoying but needed
710bf215546Sopenharmony_ci    * to prevent compiler warnings.
711bf215546Sopenharmony_ci    */
712bf215546Sopenharmony_ci   uint32_t src0_u32[4] = {
713bf215546Sopenharmony_ci      src0,
714bf215546Sopenharmony_ci      (int64_t)src0 >> 32,
715bf215546Sopenharmony_ci      (int64_t)src0 >> 63,
716bf215546Sopenharmony_ci      (int64_t)src0 >> 63,
717bf215546Sopenharmony_ci   };
718bf215546Sopenharmony_ci   uint32_t src1_u32[4] = {
719bf215546Sopenharmony_ci      src1,
720bf215546Sopenharmony_ci      (int64_t)src1 >> 32,
721bf215546Sopenharmony_ci      (int64_t)src1 >> 63,
722bf215546Sopenharmony_ci      (int64_t)src1 >> 63,
723bf215546Sopenharmony_ci   };
724bf215546Sopenharmony_ci   uint32_t prod_u32[4];
725bf215546Sopenharmony_ci   ubm_mul_u32arr(prod_u32, src0_u32, src1_u32);
726bf215546Sopenharmony_ci   dst = (uint64_t)prod_u32[2] | ((uint64_t)prod_u32[3] << 32);
727bf215546Sopenharmony_ci} else {
728bf215546Sopenharmony_ci   /* First, sign-extend to 64-bit, then convert to unsigned to prevent
729bf215546Sopenharmony_ci    * potential overflow of signed multiply */
730bf215546Sopenharmony_ci   dst = ((uint64_t)(int64_t)src0 * (uint64_t)(int64_t)src1) >> bit_size;
731bf215546Sopenharmony_ci}
732bf215546Sopenharmony_ci""")
733bf215546Sopenharmony_ci
734bf215546Sopenharmony_ci# high 32-bits of unsigned integer multiply
735bf215546Sopenharmony_cibinop("umul_high", tuint, _2src_commutative, """
736bf215546Sopenharmony_ciif (bit_size == 64) {
737bf215546Sopenharmony_ci   /* The casts are kind-of annoying but needed to prevent compiler warnings. */
738bf215546Sopenharmony_ci   uint32_t src0_u32[2] = { src0, (uint64_t)src0 >> 32 };
739bf215546Sopenharmony_ci   uint32_t src1_u32[2] = { src1, (uint64_t)src1 >> 32 };
740bf215546Sopenharmony_ci   uint32_t prod_u32[4];
741bf215546Sopenharmony_ci   ubm_mul_u32arr(prod_u32, src0_u32, src1_u32);
742bf215546Sopenharmony_ci   dst = (uint64_t)prod_u32[2] | ((uint64_t)prod_u32[3] << 32);
743bf215546Sopenharmony_ci} else {
744bf215546Sopenharmony_ci   dst = ((uint64_t)src0 * (uint64_t)src1) >> bit_size;
745bf215546Sopenharmony_ci}
746bf215546Sopenharmony_ci""")
747bf215546Sopenharmony_ci
748bf215546Sopenharmony_ci# low 32-bits of unsigned integer multiply
749bf215546Sopenharmony_cibinop("umul_low", tuint32, _2src_commutative, """
750bf215546Sopenharmony_ciuint64_t mask = (1 << (bit_size / 2)) - 1;
751bf215546Sopenharmony_cidst = ((uint64_t)src0 & mask) * ((uint64_t)src1 & mask);
752bf215546Sopenharmony_ci""")
753bf215546Sopenharmony_ci
754bf215546Sopenharmony_ci# Multiply 32-bits with low 16-bits.
755bf215546Sopenharmony_cibinop("imul_32x16", tint32, "", "src0 * (int16_t) src1")
756bf215546Sopenharmony_cibinop("umul_32x16", tuint32, "", "src0 * (uint16_t) src1")
757bf215546Sopenharmony_ci
758bf215546Sopenharmony_cibinop("fdiv", tfloat, "", "src0 / src1")
759bf215546Sopenharmony_cibinop("idiv", tint, "", "src1 == 0 ? 0 : (src0 / src1)")
760bf215546Sopenharmony_cibinop("udiv", tuint, "", "src1 == 0 ? 0 : (src0 / src1)")
761bf215546Sopenharmony_ci
762bf215546Sopenharmony_ci# returns an integer (1 or 0) representing the carry resulting from the
763bf215546Sopenharmony_ci# addition of the two unsigned arguments.
764bf215546Sopenharmony_ci
765bf215546Sopenharmony_cibinop_convert("uadd_carry", tuint, tuint, _2src_commutative, "src0 + src1 < src0")
766bf215546Sopenharmony_ci
767bf215546Sopenharmony_ci# returns an integer (1 or 0) representing the borrow resulting from the
768bf215546Sopenharmony_ci# subtraction of the two unsigned arguments.
769bf215546Sopenharmony_ci
770bf215546Sopenharmony_cibinop_convert("usub_borrow", tuint, tuint, "", "src0 < src1")
771bf215546Sopenharmony_ci
772bf215546Sopenharmony_ci# hadd: (a + b) >> 1 (without overflow)
773bf215546Sopenharmony_ci# x + y = x - (x & ~y) + (x & ~y) + y - (~x & y) + (~x & y)
774bf215546Sopenharmony_ci#       =      (x & y) + (x & ~y) +      (x & y) + (~x & y)
775bf215546Sopenharmony_ci#       = 2 *  (x & y) + (x & ~y) +                (~x & y)
776bf215546Sopenharmony_ci#       =     ((x & y) << 1) + (x ^ y)
777bf215546Sopenharmony_ci#
778bf215546Sopenharmony_ci# Since we know that the bottom bit of (x & y) << 1 is zero,
779bf215546Sopenharmony_ci#
780bf215546Sopenharmony_ci# (x + y) >> 1 = (((x & y) << 1) + (x ^ y)) >> 1
781bf215546Sopenharmony_ci#              =   (x & y) +      ((x ^ y)  >> 1)
782bf215546Sopenharmony_cibinop("ihadd", tint, _2src_commutative, "(src0 & src1) + ((src0 ^ src1) >> 1)")
783bf215546Sopenharmony_cibinop("uhadd", tuint, _2src_commutative, "(src0 & src1) + ((src0 ^ src1) >> 1)")
784bf215546Sopenharmony_ci
785bf215546Sopenharmony_ci# rhadd: (a + b + 1) >> 1 (without overflow)
786bf215546Sopenharmony_ci# x + y + 1 = x + (~x & y) - (~x & y) + y + (x & ~y) - (x & ~y) + 1
787bf215546Sopenharmony_ci#           =      (x | y) - (~x & y) +      (x | y) - (x & ~y) + 1
788bf215546Sopenharmony_ci#           = 2 *  (x | y) - ((~x & y) +               (x & ~y)) + 1
789bf215546Sopenharmony_ci#           =     ((x | y) << 1) - (x ^ y) + 1
790bf215546Sopenharmony_ci#
791bf215546Sopenharmony_ci# Since we know that the bottom bit of (x & y) << 1 is zero,
792bf215546Sopenharmony_ci#
793bf215546Sopenharmony_ci# (x + y + 1) >> 1 = (x | y) + (-(x ^ y) + 1) >> 1)
794bf215546Sopenharmony_ci#                  = (x | y) -  ((x ^ y)      >> 1)
795bf215546Sopenharmony_cibinop("irhadd", tint, _2src_commutative, "(src0 | src1) - ((src0 ^ src1) >> 1)")
796bf215546Sopenharmony_cibinop("urhadd", tuint, _2src_commutative, "(src0 | src1) - ((src0 ^ src1) >> 1)")
797bf215546Sopenharmony_ci
798bf215546Sopenharmony_cibinop("umod", tuint, "", "src1 == 0 ? 0 : src0 % src1")
799bf215546Sopenharmony_ci
800bf215546Sopenharmony_ci# For signed integers, there are several different possible definitions of
801bf215546Sopenharmony_ci# "modulus" or "remainder".  We follow the conventions used by LLVM and
802bf215546Sopenharmony_ci# SPIR-V.  The irem opcode implements the standard C/C++ signed "%"
803bf215546Sopenharmony_ci# operation while the imod opcode implements the more mathematical
804bf215546Sopenharmony_ci# "modulus" operation.  For details on the difference, see
805bf215546Sopenharmony_ci#
806bf215546Sopenharmony_ci# http://mathforum.org/library/drmath/view/52343.html
807bf215546Sopenharmony_ci
808bf215546Sopenharmony_cibinop("irem", tint, "", "src1 == 0 ? 0 : src0 % src1")
809bf215546Sopenharmony_cibinop("imod", tint, "",
810bf215546Sopenharmony_ci      "src1 == 0 ? 0 : ((src0 % src1 == 0 || (src0 >= 0) == (src1 >= 0)) ?"
811bf215546Sopenharmony_ci      "                 src0 % src1 : src0 % src1 + src1)")
812bf215546Sopenharmony_cibinop("fmod", tfloat, "", "src0 - src1 * floorf(src0 / src1)")
813bf215546Sopenharmony_cibinop("frem", tfloat, "", "src0 - src1 * truncf(src0 / src1)")
814bf215546Sopenharmony_ci
815bf215546Sopenharmony_ci#
816bf215546Sopenharmony_ci# Comparisons
817bf215546Sopenharmony_ci#
818bf215546Sopenharmony_ci
819bf215546Sopenharmony_ci
820bf215546Sopenharmony_ci# these integer-aware comparisons return a boolean (0 or ~0)
821bf215546Sopenharmony_ci
822bf215546Sopenharmony_cibinop_compare_all_sizes("flt", tfloat, "", "src0 < src1")
823bf215546Sopenharmony_cibinop_compare_all_sizes("fge", tfloat, "", "src0 >= src1")
824bf215546Sopenharmony_cibinop_compare_all_sizes("feq", tfloat, _2src_commutative, "src0 == src1")
825bf215546Sopenharmony_cibinop_compare_all_sizes("fneu", tfloat, _2src_commutative, "src0 != src1")
826bf215546Sopenharmony_cibinop_compare_all_sizes("ilt", tint, "", "src0 < src1")
827bf215546Sopenharmony_cibinop_compare_all_sizes("ige", tint, "", "src0 >= src1")
828bf215546Sopenharmony_cibinop_compare_all_sizes("ieq", tint, _2src_commutative, "src0 == src1")
829bf215546Sopenharmony_cibinop_compare_all_sizes("ine", tint, _2src_commutative, "src0 != src1")
830bf215546Sopenharmony_cibinop_compare_all_sizes("ult", tuint, "", "src0 < src1")
831bf215546Sopenharmony_cibinop_compare_all_sizes("uge", tuint, "", "src0 >= src1")
832bf215546Sopenharmony_ci
833bf215546Sopenharmony_ci# integer-aware GLSL-style comparisons that compare floats and ints
834bf215546Sopenharmony_ci
835bf215546Sopenharmony_cibinop_reduce_all_sizes("ball_fequal",  1, tfloat, "{src0} == {src1}",
836bf215546Sopenharmony_ci                       "{src0} && {src1}", "{src}")
837bf215546Sopenharmony_cibinop_reduce_all_sizes("bany_fnequal", 1, tfloat, "{src0} != {src1}",
838bf215546Sopenharmony_ci                       "{src0} || {src1}", "{src}")
839bf215546Sopenharmony_cibinop_reduce_all_sizes("ball_iequal",  1, tint, "{src0} == {src1}",
840bf215546Sopenharmony_ci                       "{src0} && {src1}", "{src}")
841bf215546Sopenharmony_cibinop_reduce_all_sizes("bany_inequal", 1, tint, "{src0} != {src1}",
842bf215546Sopenharmony_ci                       "{src0} || {src1}", "{src}")
843bf215546Sopenharmony_ci
844bf215546Sopenharmony_ci# non-integer-aware GLSL-style comparisons that return 0.0 or 1.0
845bf215546Sopenharmony_ci
846bf215546Sopenharmony_cibinop_reduce("fall_equal",  1, tfloat32, tfloat32, "{src0} == {src1}",
847bf215546Sopenharmony_ci             "{src0} && {src1}", "{src} ? 1.0f : 0.0f")
848bf215546Sopenharmony_cibinop_reduce("fany_nequal", 1, tfloat32, tfloat32, "{src0} != {src1}",
849bf215546Sopenharmony_ci             "{src0} || {src1}", "{src} ? 1.0f : 0.0f")
850bf215546Sopenharmony_ci
851bf215546Sopenharmony_ci# These comparisons for integer-less hardware return 1.0 and 0.0 for true
852bf215546Sopenharmony_ci# and false respectively
853bf215546Sopenharmony_ci
854bf215546Sopenharmony_cibinop("slt", tfloat, "", "(src0 < src1) ? 1.0f : 0.0f") # Set on Less Than
855bf215546Sopenharmony_cibinop("sge", tfloat, "", "(src0 >= src1) ? 1.0f : 0.0f") # Set on Greater or Equal
856bf215546Sopenharmony_cibinop("seq", tfloat, _2src_commutative, "(src0 == src1) ? 1.0f : 0.0f") # Set on Equal
857bf215546Sopenharmony_cibinop("sne", tfloat, _2src_commutative, "(src0 != src1) ? 1.0f : 0.0f") # Set on Not Equal
858bf215546Sopenharmony_ci
859bf215546Sopenharmony_ci# SPIRV shifts are undefined for shift-operands >= bitsize,
860bf215546Sopenharmony_ci# but SM5 shifts are defined to use only the least significant bits.
861bf215546Sopenharmony_ci# The NIR definition is according to the SM5 specification.
862bf215546Sopenharmony_ciopcode("ishl", 0, tint, [0, 0], [tint, tuint32], False, "",
863bf215546Sopenharmony_ci       "(uint64_t)src0 << (src1 & (sizeof(src0) * 8 - 1))")
864bf215546Sopenharmony_ciopcode("ishr", 0, tint, [0, 0], [tint, tuint32], False, "",
865bf215546Sopenharmony_ci       "src0 >> (src1 & (sizeof(src0) * 8 - 1))")
866bf215546Sopenharmony_ciopcode("ushr", 0, tuint, [0, 0], [tuint, tuint32], False, "",
867bf215546Sopenharmony_ci       "src0 >> (src1 & (sizeof(src0) * 8 - 1))")
868bf215546Sopenharmony_ci
869bf215546Sopenharmony_ciopcode("urol", 0, tuint, [0, 0], [tuint, tuint32], False, "", """
870bf215546Sopenharmony_ci   uint32_t rotate_mask = sizeof(src0) * 8 - 1;
871bf215546Sopenharmony_ci   dst = (src0 << (src1 & rotate_mask)) |
872bf215546Sopenharmony_ci         (src0 >> (-src1 & rotate_mask));
873bf215546Sopenharmony_ci""")
874bf215546Sopenharmony_ciopcode("uror", 0, tuint, [0, 0], [tuint, tuint32], False, "", """
875bf215546Sopenharmony_ci   uint32_t rotate_mask = sizeof(src0) * 8 - 1;
876bf215546Sopenharmony_ci   dst = (src0 >> (src1 & rotate_mask)) |
877bf215546Sopenharmony_ci         (src0 << (-src1 & rotate_mask));
878bf215546Sopenharmony_ci""")
879bf215546Sopenharmony_ci
880bf215546Sopenharmony_ci# bitwise logic operators
881bf215546Sopenharmony_ci#
882bf215546Sopenharmony_ci# These are also used as boolean and, or, xor for hardware supporting
883bf215546Sopenharmony_ci# integers.
884bf215546Sopenharmony_ci
885bf215546Sopenharmony_ci
886bf215546Sopenharmony_cibinop("iand", tuint, _2src_commutative + associative, "src0 & src1")
887bf215546Sopenharmony_cibinop("ior", tuint, _2src_commutative + associative, "src0 | src1")
888bf215546Sopenharmony_cibinop("ixor", tuint, _2src_commutative + associative, "src0 ^ src1")
889bf215546Sopenharmony_ci
890bf215546Sopenharmony_ci
891bf215546Sopenharmony_cibinop_reduce("fdot", 1, tfloat, tfloat, "{src0} * {src1}", "{src0} + {src1}",
892bf215546Sopenharmony_ci             "{src}")
893bf215546Sopenharmony_ci
894bf215546Sopenharmony_cibinop_reduce("fdot", 0, tfloat, tfloat,
895bf215546Sopenharmony_ci             "{src0} * {src1}", "{src0} + {src1}", "{src}",
896bf215546Sopenharmony_ci             suffix="_replicated")
897bf215546Sopenharmony_ci
898bf215546Sopenharmony_ciopcode("fdph", 1, tfloat, [3, 4], [tfloat, tfloat], False, "",
899bf215546Sopenharmony_ci       "src0.x * src1.x + src0.y * src1.y + src0.z * src1.z + src1.w")
900bf215546Sopenharmony_ciopcode("fdph_replicated", 0, tfloat, [3, 4], [tfloat, tfloat], False, "",
901bf215546Sopenharmony_ci       "src0.x * src1.x + src0.y * src1.y + src0.z * src1.z + src1.w")
902bf215546Sopenharmony_ci
903bf215546Sopenharmony_cibinop("fmin", tfloat, _2src_commutative + associative, "fmin(src0, src1)")
904bf215546Sopenharmony_cibinop("imin", tint, _2src_commutative + associative, "src1 > src0 ? src0 : src1")
905bf215546Sopenharmony_cibinop("umin", tuint, _2src_commutative + associative, "src1 > src0 ? src0 : src1")
906bf215546Sopenharmony_cibinop("fmax", tfloat, _2src_commutative + associative, "fmax(src0, src1)")
907bf215546Sopenharmony_cibinop("imax", tint, _2src_commutative + associative, "src1 > src0 ? src1 : src0")
908bf215546Sopenharmony_cibinop("umax", tuint, _2src_commutative + associative, "src1 > src0 ? src1 : src0")
909bf215546Sopenharmony_ci
910bf215546Sopenharmony_cibinop("fpow", tfloat, "", "bit_size == 64 ? powf(src0, src1) : pow(src0, src1)")
911bf215546Sopenharmony_ci
912bf215546Sopenharmony_cibinop_horiz("pack_half_2x16_split", 1, tuint32, 1, tfloat32, 1, tfloat32,
913bf215546Sopenharmony_ci            "pack_half_1x16(src0.x) | (pack_half_1x16(src1.x) << 16)")
914bf215546Sopenharmony_ci
915bf215546Sopenharmony_cibinop_convert("pack_64_2x32_split", tuint64, tuint32, "",
916bf215546Sopenharmony_ci              "src0 | ((uint64_t)src1 << 32)")
917bf215546Sopenharmony_ci
918bf215546Sopenharmony_cibinop_convert("pack_32_2x16_split", tuint32, tuint16, "",
919bf215546Sopenharmony_ci              "src0 | ((uint32_t)src1 << 16)")
920bf215546Sopenharmony_ci
921bf215546Sopenharmony_ciopcode("pack_32_4x8_split", 0, tuint32, [0, 0, 0, 0], [tuint8, tuint8, tuint8, tuint8],
922bf215546Sopenharmony_ci       False, "",
923bf215546Sopenharmony_ci       "src0 | ((uint32_t)src1 << 8) | ((uint32_t)src2 << 16) | ((uint32_t)src3 << 24)")
924bf215546Sopenharmony_ci
925bf215546Sopenharmony_ci# bfm implements the behavior of the first operation of the SM5 "bfi" assembly
926bf215546Sopenharmony_ci# and that of the "bfi1" i965 instruction. That is, the bits and offset values
927bf215546Sopenharmony_ci# are from the low five bits of src0 and src1, respectively.
928bf215546Sopenharmony_cibinop_convert("bfm", tuint32, tint32, "", """
929bf215546Sopenharmony_ciint bits = src0 & 0x1F;
930bf215546Sopenharmony_ciint offset = src1 & 0x1F;
931bf215546Sopenharmony_cidst = ((1u << bits) - 1) << offset;
932bf215546Sopenharmony_ci""")
933bf215546Sopenharmony_ci
934bf215546Sopenharmony_ciopcode("ldexp", 0, tfloat, [0, 0], [tfloat, tint32], False, "", """
935bf215546Sopenharmony_cidst = (bit_size == 64) ? ldexp(src0, src1) : ldexpf(src0, src1);
936bf215546Sopenharmony_ci/* flush denormals to zero. */
937bf215546Sopenharmony_ciif (!isnormal(dst))
938bf215546Sopenharmony_ci   dst = copysignf(0.0f, src0);
939bf215546Sopenharmony_ci""")
940bf215546Sopenharmony_ci
941bf215546Sopenharmony_ci# Combines the first component of each input to make a 2-component vector.
942bf215546Sopenharmony_ci
943bf215546Sopenharmony_cibinop_horiz("vec2", 2, tuint, 1, tuint, 1, tuint, """
944bf215546Sopenharmony_cidst.x = src0.x;
945bf215546Sopenharmony_cidst.y = src1.x;
946bf215546Sopenharmony_ci""")
947bf215546Sopenharmony_ci
948bf215546Sopenharmony_ci# Byte extraction
949bf215546Sopenharmony_cibinop("extract_u8", tuint, "", "(uint8_t)(src0 >> (src1 * 8))")
950bf215546Sopenharmony_cibinop("extract_i8", tint, "", "(int8_t)(src0 >> (src1 * 8))")
951bf215546Sopenharmony_ci
952bf215546Sopenharmony_ci# Word extraction
953bf215546Sopenharmony_cibinop("extract_u16", tuint, "", "(uint16_t)(src0 >> (src1 * 16))")
954bf215546Sopenharmony_cibinop("extract_i16", tint, "", "(int16_t)(src0 >> (src1 * 16))")
955bf215546Sopenharmony_ci
956bf215546Sopenharmony_ci# Byte/word insertion
957bf215546Sopenharmony_cibinop("insert_u8", tuint, "", "(src0 & 0xff) << (src1 * 8)")
958bf215546Sopenharmony_cibinop("insert_u16", tuint, "", "(src0 & 0xffff) << (src1 * 16)")
959bf215546Sopenharmony_ci
960bf215546Sopenharmony_ci
961bf215546Sopenharmony_cidef triop(name, ty, alg_props, const_expr):
962bf215546Sopenharmony_ci   opcode(name, 0, ty, [0, 0, 0], [ty, ty, ty], False, alg_props, const_expr)
963bf215546Sopenharmony_cidef triop_horiz(name, output_size, src1_size, src2_size, src3_size, const_expr):
964bf215546Sopenharmony_ci   opcode(name, output_size, tuint,
965bf215546Sopenharmony_ci   [src1_size, src2_size, src3_size],
966bf215546Sopenharmony_ci   [tuint, tuint, tuint], False, "", const_expr)
967bf215546Sopenharmony_ci
968bf215546Sopenharmony_citriop("ffma", tfloat, _2src_commutative, """
969bf215546Sopenharmony_ciif (nir_is_rounding_mode_rtz(execution_mode, bit_size)) {
970bf215546Sopenharmony_ci   if (bit_size == 64)
971bf215546Sopenharmony_ci      dst = _mesa_double_fma_rtz(src0, src1, src2);
972bf215546Sopenharmony_ci   else if (bit_size == 32)
973bf215546Sopenharmony_ci      dst = _mesa_float_fma_rtz(src0, src1, src2);
974bf215546Sopenharmony_ci   else
975bf215546Sopenharmony_ci      dst = _mesa_double_to_float_rtz(_mesa_double_fma_rtz(src0, src1, src2));
976bf215546Sopenharmony_ci} else {
977bf215546Sopenharmony_ci   if (bit_size == 32)
978bf215546Sopenharmony_ci      dst = fmaf(src0, src1, src2);
979bf215546Sopenharmony_ci   else
980bf215546Sopenharmony_ci      dst = fma(src0, src1, src2);
981bf215546Sopenharmony_ci}
982bf215546Sopenharmony_ci""")
983bf215546Sopenharmony_ci
984bf215546Sopenharmony_ci# Unlike ffma, anything (even infinity or NaN) multiplied by zero is always zero.
985bf215546Sopenharmony_ci# ffmaz(0.0, inf, src2) and ffmaz(0.0, nan, src2) must be +/-0.0 + src2, even if
986bf215546Sopenharmony_ci# SIGNED_ZERO_INF_NAN_PRESERVE is not used. If SIGNED_ZERO_INF_NAN_PRESERVE is used, then
987bf215546Sopenharmony_ci# the result must be a positive zero plus src2 if either src0 or src1 is zero.
988bf215546Sopenharmony_citriop("ffmaz", tfloat32, _2src_commutative, """
989bf215546Sopenharmony_ciif (src0 == 0.0 || src1 == 0.0)
990bf215546Sopenharmony_ci   dst = 0.0 + src2;
991bf215546Sopenharmony_cielse if (nir_is_rounding_mode_rtz(execution_mode, 32))
992bf215546Sopenharmony_ci   dst = _mesa_float_fma_rtz(src0, src1, src2);
993bf215546Sopenharmony_cielse
994bf215546Sopenharmony_ci   dst = fmaf(src0, src1, src2);
995bf215546Sopenharmony_ci""")
996bf215546Sopenharmony_ci
997bf215546Sopenharmony_citriop("flrp", tfloat, "", "src0 * (1 - src2) + src1 * src2")
998bf215546Sopenharmony_ci
999bf215546Sopenharmony_ci# Ternary addition
1000bf215546Sopenharmony_citriop("iadd3", tint, _2src_commutative + associative, "src0 + src1 + src2")
1001bf215546Sopenharmony_ci
1002bf215546Sopenharmony_ci# Conditional Select
1003bf215546Sopenharmony_ci#
1004bf215546Sopenharmony_ci# A vector conditional select instruction (like ?:, but operating per-
1005bf215546Sopenharmony_ci# component on vectors). There are two versions, one for floating point
1006bf215546Sopenharmony_ci# bools (0.0 vs 1.0) and one for integer bools (0 vs ~0).
1007bf215546Sopenharmony_ci
1008bf215546Sopenharmony_citriop("fcsel", tfloat32, selection, "(src0 != 0.0f) ? src1 : src2")
1009bf215546Sopenharmony_ci
1010bf215546Sopenharmony_ciopcode("bcsel", 0, tuint, [0, 0, 0],
1011bf215546Sopenharmony_ci       [tbool1, tuint, tuint], False, selection, "src0 ? src1 : src2")
1012bf215546Sopenharmony_ciopcode("b8csel", 0, tuint, [0, 0, 0],
1013bf215546Sopenharmony_ci       [tbool8, tuint, tuint], False, selection, "src0 ? src1 : src2")
1014bf215546Sopenharmony_ciopcode("b16csel", 0, tuint, [0, 0, 0],
1015bf215546Sopenharmony_ci       [tbool16, tuint, tuint], False, selection, "src0 ? src1 : src2")
1016bf215546Sopenharmony_ciopcode("b32csel", 0, tuint, [0, 0, 0],
1017bf215546Sopenharmony_ci       [tbool32, tuint, tuint], False, selection, "src0 ? src1 : src2")
1018bf215546Sopenharmony_ci
1019bf215546Sopenharmony_citriop("i32csel_gt", tint32, selection, "(src0 > 0) ? src1 : src2")
1020bf215546Sopenharmony_citriop("i32csel_ge", tint32, selection, "(src0 >= 0) ? src1 : src2")
1021bf215546Sopenharmony_ci
1022bf215546Sopenharmony_citriop("fcsel_gt", tfloat32, selection, "(src0 > 0.0f) ? src1 : src2")
1023bf215546Sopenharmony_citriop("fcsel_ge", tfloat32, selection, "(src0 >= 0.0f) ? src1 : src2")
1024bf215546Sopenharmony_ci
1025bf215546Sopenharmony_ci# SM5 bfi assembly
1026bf215546Sopenharmony_citriop("bfi", tuint32, "", """
1027bf215546Sopenharmony_ciunsigned mask = src0, insert = src1, base = src2;
1028bf215546Sopenharmony_ciif (mask == 0) {
1029bf215546Sopenharmony_ci   dst = base;
1030bf215546Sopenharmony_ci} else {
1031bf215546Sopenharmony_ci   unsigned tmp = mask;
1032bf215546Sopenharmony_ci   while (!(tmp & 1)) {
1033bf215546Sopenharmony_ci      tmp >>= 1;
1034bf215546Sopenharmony_ci      insert <<= 1;
1035bf215546Sopenharmony_ci   }
1036bf215546Sopenharmony_ci   dst = (base & ~mask) | (insert & mask);
1037bf215546Sopenharmony_ci}
1038bf215546Sopenharmony_ci""")
1039bf215546Sopenharmony_ci
1040bf215546Sopenharmony_ci
1041bf215546Sopenharmony_citriop("bitfield_select", tuint, "", "(src0 & src1) | (~src0 & src2)")
1042bf215546Sopenharmony_ci
1043bf215546Sopenharmony_ci# SM5 ubfe/ibfe assembly: only the 5 least significant bits of offset and bits are used.
1044bf215546Sopenharmony_ciopcode("ubfe", 0, tuint32,
1045bf215546Sopenharmony_ci       [0, 0, 0], [tuint32, tuint32, tuint32], False, "", """
1046bf215546Sopenharmony_ciunsigned base = src0;
1047bf215546Sopenharmony_ciunsigned offset = src1 & 0x1F;
1048bf215546Sopenharmony_ciunsigned bits = src2 & 0x1F;
1049bf215546Sopenharmony_ciif (bits == 0) {
1050bf215546Sopenharmony_ci   dst = 0;
1051bf215546Sopenharmony_ci} else if (offset + bits < 32) {
1052bf215546Sopenharmony_ci   dst = (base << (32 - bits - offset)) >> (32 - bits);
1053bf215546Sopenharmony_ci} else {
1054bf215546Sopenharmony_ci   dst = base >> offset;
1055bf215546Sopenharmony_ci}
1056bf215546Sopenharmony_ci""")
1057bf215546Sopenharmony_ciopcode("ibfe", 0, tint32,
1058bf215546Sopenharmony_ci       [0, 0, 0], [tint32, tuint32, tuint32], False, "", """
1059bf215546Sopenharmony_ciint base = src0;
1060bf215546Sopenharmony_ciunsigned offset = src1 & 0x1F;
1061bf215546Sopenharmony_ciunsigned bits = src2 & 0x1F;
1062bf215546Sopenharmony_ciif (bits == 0) {
1063bf215546Sopenharmony_ci   dst = 0;
1064bf215546Sopenharmony_ci} else if (offset + bits < 32) {
1065bf215546Sopenharmony_ci   dst = (base << (32 - bits - offset)) >> (32 - bits);
1066bf215546Sopenharmony_ci} else {
1067bf215546Sopenharmony_ci   dst = base >> offset;
1068bf215546Sopenharmony_ci}
1069bf215546Sopenharmony_ci""")
1070bf215546Sopenharmony_ci
1071bf215546Sopenharmony_ci# GLSL bitfieldExtract()
1072bf215546Sopenharmony_ciopcode("ubitfield_extract", 0, tuint32,
1073bf215546Sopenharmony_ci       [0, 0, 0], [tuint32, tint32, tint32], False, "", """
1074bf215546Sopenharmony_ciunsigned base = src0;
1075bf215546Sopenharmony_ciint offset = src1, bits = src2;
1076bf215546Sopenharmony_ciif (bits == 0) {
1077bf215546Sopenharmony_ci   dst = 0;
1078bf215546Sopenharmony_ci} else if (bits < 0 || offset < 0 || offset + bits > 32) {
1079bf215546Sopenharmony_ci   dst = 0; /* undefined per the spec */
1080bf215546Sopenharmony_ci} else {
1081bf215546Sopenharmony_ci   dst = (base >> offset) & ((1ull << bits) - 1);
1082bf215546Sopenharmony_ci}
1083bf215546Sopenharmony_ci""")
1084bf215546Sopenharmony_ciopcode("ibitfield_extract", 0, tint32,
1085bf215546Sopenharmony_ci       [0, 0, 0], [tint32, tint32, tint32], False, "", """
1086bf215546Sopenharmony_ciint base = src0;
1087bf215546Sopenharmony_ciint offset = src1, bits = src2;
1088bf215546Sopenharmony_ciif (bits == 0) {
1089bf215546Sopenharmony_ci   dst = 0;
1090bf215546Sopenharmony_ci} else if (offset < 0 || bits < 0 || offset + bits > 32) {
1091bf215546Sopenharmony_ci   dst = 0;
1092bf215546Sopenharmony_ci} else {
1093bf215546Sopenharmony_ci   dst = (base << (32 - offset - bits)) >> (32 - bits); /* use sign-extending shift */
1094bf215546Sopenharmony_ci}
1095bf215546Sopenharmony_ci""")
1096bf215546Sopenharmony_ci
1097bf215546Sopenharmony_ci# Sum of absolute differences with accumulation.
1098bf215546Sopenharmony_ci# (Equivalent to AMD's v_sad_u8 instruction.)
1099bf215546Sopenharmony_ci# The first two sources contain packed 8-bit unsigned integers, the instruction
1100bf215546Sopenharmony_ci# will calculate the absolute difference of these, and then add them together.
1101bf215546Sopenharmony_ci# There is also a third source which is a 32-bit unsigned integer and added to the result.
1102bf215546Sopenharmony_citriop_horiz("sad_u8x4", 1, 1, 1, 1, """
1103bf215546Sopenharmony_ciuint8_t s0_b0 = (src0.x & 0x000000ff) >> 0;
1104bf215546Sopenharmony_ciuint8_t s0_b1 = (src0.x & 0x0000ff00) >> 8;
1105bf215546Sopenharmony_ciuint8_t s0_b2 = (src0.x & 0x00ff0000) >> 16;
1106bf215546Sopenharmony_ciuint8_t s0_b3 = (src0.x & 0xff000000) >> 24;
1107bf215546Sopenharmony_ci
1108bf215546Sopenharmony_ciuint8_t s1_b0 = (src1.x & 0x000000ff) >> 0;
1109bf215546Sopenharmony_ciuint8_t s1_b1 = (src1.x & 0x0000ff00) >> 8;
1110bf215546Sopenharmony_ciuint8_t s1_b2 = (src1.x & 0x00ff0000) >> 16;
1111bf215546Sopenharmony_ciuint8_t s1_b3 = (src1.x & 0xff000000) >> 24;
1112bf215546Sopenharmony_ci
1113bf215546Sopenharmony_cidst.x = src2.x +
1114bf215546Sopenharmony_ci        (s0_b0 > s1_b0 ? (s0_b0 - s1_b0) : (s1_b0 - s0_b0)) +
1115bf215546Sopenharmony_ci        (s0_b1 > s1_b1 ? (s0_b1 - s1_b1) : (s1_b1 - s0_b1)) +
1116bf215546Sopenharmony_ci        (s0_b2 > s1_b2 ? (s0_b2 - s1_b2) : (s1_b2 - s0_b2)) +
1117bf215546Sopenharmony_ci        (s0_b3 > s1_b3 ? (s0_b3 - s1_b3) : (s1_b3 - s0_b3));
1118bf215546Sopenharmony_ci""")
1119bf215546Sopenharmony_ci
1120bf215546Sopenharmony_ci# Combines the first component of each input to make a 3-component vector.
1121bf215546Sopenharmony_ci
1122bf215546Sopenharmony_citriop_horiz("vec3", 3, 1, 1, 1, """
1123bf215546Sopenharmony_cidst.x = src0.x;
1124bf215546Sopenharmony_cidst.y = src1.x;
1125bf215546Sopenharmony_cidst.z = src2.x;
1126bf215546Sopenharmony_ci""")
1127bf215546Sopenharmony_ci
1128bf215546Sopenharmony_cidef quadop_horiz(name, output_size, src1_size, src2_size, src3_size,
1129bf215546Sopenharmony_ci                 src4_size, const_expr):
1130bf215546Sopenharmony_ci   opcode(name, output_size, tuint,
1131bf215546Sopenharmony_ci          [src1_size, src2_size, src3_size, src4_size],
1132bf215546Sopenharmony_ci          [tuint, tuint, tuint, tuint],
1133bf215546Sopenharmony_ci          False, "", const_expr)
1134bf215546Sopenharmony_ci
1135bf215546Sopenharmony_ciopcode("bitfield_insert", 0, tuint32, [0, 0, 0, 0],
1136bf215546Sopenharmony_ci       [tuint32, tuint32, tint32, tint32], False, "", """
1137bf215546Sopenharmony_ciunsigned base = src0, insert = src1;
1138bf215546Sopenharmony_ciint offset = src2, bits = src3;
1139bf215546Sopenharmony_ciif (bits == 0) {
1140bf215546Sopenharmony_ci   dst = base;
1141bf215546Sopenharmony_ci} else if (offset < 0 || bits < 0 || bits + offset > 32) {
1142bf215546Sopenharmony_ci   dst = 0;
1143bf215546Sopenharmony_ci} else {
1144bf215546Sopenharmony_ci   unsigned mask = ((1ull << bits) - 1) << offset;
1145bf215546Sopenharmony_ci   dst = (base & ~mask) | ((insert << offset) & mask);
1146bf215546Sopenharmony_ci}
1147bf215546Sopenharmony_ci""")
1148bf215546Sopenharmony_ci
1149bf215546Sopenharmony_ciquadop_horiz("vec4", 4, 1, 1, 1, 1, """
1150bf215546Sopenharmony_cidst.x = src0.x;
1151bf215546Sopenharmony_cidst.y = src1.x;
1152bf215546Sopenharmony_cidst.z = src2.x;
1153bf215546Sopenharmony_cidst.w = src3.x;
1154bf215546Sopenharmony_ci""")
1155bf215546Sopenharmony_ci
1156bf215546Sopenharmony_ciopcode("vec5", 5, tuint,
1157bf215546Sopenharmony_ci       [1] * 5, [tuint] * 5,
1158bf215546Sopenharmony_ci       False, "", """
1159bf215546Sopenharmony_cidst.x = src0.x;
1160bf215546Sopenharmony_cidst.y = src1.x;
1161bf215546Sopenharmony_cidst.z = src2.x;
1162bf215546Sopenharmony_cidst.w = src3.x;
1163bf215546Sopenharmony_cidst.e = src4.x;
1164bf215546Sopenharmony_ci""")
1165bf215546Sopenharmony_ci
1166bf215546Sopenharmony_ciopcode("vec8", 8, tuint,
1167bf215546Sopenharmony_ci       [1] * 8, [tuint] * 8,
1168bf215546Sopenharmony_ci       False, "", """
1169bf215546Sopenharmony_cidst.x = src0.x;
1170bf215546Sopenharmony_cidst.y = src1.x;
1171bf215546Sopenharmony_cidst.z = src2.x;
1172bf215546Sopenharmony_cidst.w = src3.x;
1173bf215546Sopenharmony_cidst.e = src4.x;
1174bf215546Sopenharmony_cidst.f = src5.x;
1175bf215546Sopenharmony_cidst.g = src6.x;
1176bf215546Sopenharmony_cidst.h = src7.x;
1177bf215546Sopenharmony_ci""")
1178bf215546Sopenharmony_ci
1179bf215546Sopenharmony_ciopcode("vec16", 16, tuint,
1180bf215546Sopenharmony_ci       [1] * 16, [tuint] * 16,
1181bf215546Sopenharmony_ci       False, "", """
1182bf215546Sopenharmony_cidst.x = src0.x;
1183bf215546Sopenharmony_cidst.y = src1.x;
1184bf215546Sopenharmony_cidst.z = src2.x;
1185bf215546Sopenharmony_cidst.w = src3.x;
1186bf215546Sopenharmony_cidst.e = src4.x;
1187bf215546Sopenharmony_cidst.f = src5.x;
1188bf215546Sopenharmony_cidst.g = src6.x;
1189bf215546Sopenharmony_cidst.h = src7.x;
1190bf215546Sopenharmony_cidst.i = src8.x;
1191bf215546Sopenharmony_cidst.j = src9.x;
1192bf215546Sopenharmony_cidst.k = src10.x;
1193bf215546Sopenharmony_cidst.l = src11.x;
1194bf215546Sopenharmony_cidst.m = src12.x;
1195bf215546Sopenharmony_cidst.n = src13.x;
1196bf215546Sopenharmony_cidst.o = src14.x;
1197bf215546Sopenharmony_cidst.p = src15.x;
1198bf215546Sopenharmony_ci""")
1199bf215546Sopenharmony_ci
1200bf215546Sopenharmony_ci# An integer multiply instruction for address calculation.  This is
1201bf215546Sopenharmony_ci# similar to imul, except that the results are undefined in case of
1202bf215546Sopenharmony_ci# overflow.  Overflow is defined according to the size of the variable
1203bf215546Sopenharmony_ci# being dereferenced.
1204bf215546Sopenharmony_ci#
1205bf215546Sopenharmony_ci# This relaxed definition, compared to imul, allows an optimization
1206bf215546Sopenharmony_ci# pass to propagate bounds (ie, from an load/store intrinsic) to the
1207bf215546Sopenharmony_ci# sources, such that lower precision integer multiplies can be used.
1208bf215546Sopenharmony_ci# This is useful on hw that has 24b or perhaps 16b integer multiply
1209bf215546Sopenharmony_ci# instructions.
1210bf215546Sopenharmony_cibinop("amul", tint, _2src_commutative + associative, "src0 * src1")
1211bf215546Sopenharmony_ci
1212bf215546Sopenharmony_ci# ir3-specific instruction that maps directly to mul-add shift high mix,
1213bf215546Sopenharmony_ci# (IMADSH_MIX16 i.e. ah * bl << 16 + c). It is used for lowering integer
1214bf215546Sopenharmony_ci# multiplication (imul) on Freedreno backend..
1215bf215546Sopenharmony_ciopcode("imadsh_mix16", 0, tint32,
1216bf215546Sopenharmony_ci       [0, 0, 0], [tint32, tint32, tint32], False, "", """
1217bf215546Sopenharmony_cidst = ((((src0 & 0xffff0000) >> 16) * (src1 & 0x0000ffff)) << 16) + src2;
1218bf215546Sopenharmony_ci""")
1219bf215546Sopenharmony_ci
1220bf215546Sopenharmony_ci# ir3-specific instruction that maps directly to ir3 mad.s24.
1221bf215546Sopenharmony_ci#
1222bf215546Sopenharmony_ci# 24b multiply into 32b result (with sign extension) plus 32b int
1223bf215546Sopenharmony_citriop("imad24_ir3", tint32, _2src_commutative,
1224bf215546Sopenharmony_ci      "(((int32_t)src0 << 8) >> 8) * (((int32_t)src1 << 8) >> 8) + src2")
1225bf215546Sopenharmony_ci
1226bf215546Sopenharmony_ci# r600-specific instruction that evaluates unnormalized cube texture coordinates
1227bf215546Sopenharmony_ci# and face index
1228bf215546Sopenharmony_ci# The actual texture coordinates are evaluated from this according to
1229bf215546Sopenharmony_ci#    dst.yx / abs(dst.z) + 1.5
1230bf215546Sopenharmony_ciunop_horiz("cube_r600", 4, tfloat32, 3, tfloat32, """
1231bf215546Sopenharmony_ci   dst.x = dst.y = dst.z = 0.0;
1232bf215546Sopenharmony_ci   float absX = fabsf(src0.x);
1233bf215546Sopenharmony_ci   float absY = fabsf(src0.y);
1234bf215546Sopenharmony_ci   float absZ = fabsf(src0.z);
1235bf215546Sopenharmony_ci
1236bf215546Sopenharmony_ci   if (absX >= absY && absX >= absZ) { dst.z = 2 * src0.x; }
1237bf215546Sopenharmony_ci   if (absY >= absX && absY >= absZ) { dst.z = 2 * src0.y; }
1238bf215546Sopenharmony_ci   if (absZ >= absX && absZ >= absY) { dst.z = 2 * src0.z; }
1239bf215546Sopenharmony_ci
1240bf215546Sopenharmony_ci   if (src0.x >= 0 && absX >= absY && absX >= absZ) {
1241bf215546Sopenharmony_ci      dst.y = -src0.z; dst.x = -src0.y; dst.w = 0;
1242bf215546Sopenharmony_ci   }
1243bf215546Sopenharmony_ci   if (src0.x < 0 && absX >= absY && absX >= absZ) {
1244bf215546Sopenharmony_ci      dst.y = src0.z; dst.x = -src0.y; dst.w = 1;
1245bf215546Sopenharmony_ci   }
1246bf215546Sopenharmony_ci   if (src0.y >= 0 && absY >= absX && absY >= absZ) {
1247bf215546Sopenharmony_ci      dst.y = src0.x; dst.x = src0.z; dst.w = 2;
1248bf215546Sopenharmony_ci   }
1249bf215546Sopenharmony_ci   if (src0.y < 0 && absY >= absX && absY >= absZ) {
1250bf215546Sopenharmony_ci      dst.y = src0.x; dst.x = -src0.z; dst.w = 3;
1251bf215546Sopenharmony_ci   }
1252bf215546Sopenharmony_ci   if (src0.z >= 0 && absZ >= absX && absZ >= absY) {
1253bf215546Sopenharmony_ci      dst.y = src0.x; dst.x = -src0.y; dst.w = 4;
1254bf215546Sopenharmony_ci   }
1255bf215546Sopenharmony_ci   if (src0.z < 0 && absZ >= absX && absZ >= absY) {
1256bf215546Sopenharmony_ci      dst.y = -src0.x; dst.x = -src0.y; dst.w = 5;
1257bf215546Sopenharmony_ci   }
1258bf215546Sopenharmony_ci""")
1259bf215546Sopenharmony_ci
1260bf215546Sopenharmony_ci# r600/gcn specific sin and cos
1261bf215546Sopenharmony_ci# these trigeometric functions need some lowering because the supported
1262bf215546Sopenharmony_ci# input values are expected to be normalized by dividing by (2 * pi)
1263bf215546Sopenharmony_ciunop("fsin_amd", tfloat, "sinf(6.2831853 * src0)")
1264bf215546Sopenharmony_ciunop("fcos_amd", tfloat, "cosf(6.2831853 * src0)")
1265bf215546Sopenharmony_ci
1266bf215546Sopenharmony_ci# AGX specific sin with input expressed in quadrants. Used in the lowering for
1267bf215546Sopenharmony_ci# fsin/fcos. This corresponds to a sequence of 3 ALU ops in the backend (where
1268bf215546Sopenharmony_ci# the angle is further decomposed by quadrant, sinc is computed, and the angle
1269bf215546Sopenharmony_ci# is multiplied back for sin). Lowering fsin/fcos to fsin_agx requires some
1270bf215546Sopenharmony_ci# additional ALU that NIR may be able to optimize.
1271bf215546Sopenharmony_ciunop("fsin_agx", tfloat, "sinf(src0 * (6.2831853/4.0))")
1272bf215546Sopenharmony_ci
1273bf215546Sopenharmony_ci# 24b multiply into 32b result (with sign extension)
1274bf215546Sopenharmony_cibinop("imul24", tint32, _2src_commutative + associative,
1275bf215546Sopenharmony_ci      "(((int32_t)src0 << 8) >> 8) * (((int32_t)src1 << 8) >> 8)")
1276bf215546Sopenharmony_ci
1277bf215546Sopenharmony_ci# unsigned 24b multiply into 32b result plus 32b int
1278bf215546Sopenharmony_citriop("umad24", tuint32, _2src_commutative,
1279bf215546Sopenharmony_ci      "(((uint32_t)src0 << 8) >> 8) * (((uint32_t)src1 << 8) >> 8) + src2")
1280bf215546Sopenharmony_ci
1281bf215546Sopenharmony_ci# unsigned 24b multiply into 32b result uint
1282bf215546Sopenharmony_cibinop("umul24", tint32, _2src_commutative + associative,
1283bf215546Sopenharmony_ci      "(((uint32_t)src0 << 8) >> 8) * (((uint32_t)src1 << 8) >> 8)")
1284bf215546Sopenharmony_ci
1285bf215546Sopenharmony_ci# relaxed versions of the above, which assume input is in the 24bit range (no clamping)
1286bf215546Sopenharmony_cibinop("imul24_relaxed", tint32, _2src_commutative + associative, "src0 * src1")
1287bf215546Sopenharmony_citriop("umad24_relaxed", tuint32, _2src_commutative, "src0 * src1 + src2")
1288bf215546Sopenharmony_cibinop("umul24_relaxed", tuint32, _2src_commutative + associative, "src0 * src1")
1289bf215546Sopenharmony_ci
1290bf215546Sopenharmony_ciunop_convert("fisnormal", tbool1, tfloat, "isnormal(src0)")
1291bf215546Sopenharmony_ciunop_convert("fisfinite", tbool1, tfloat, "isfinite(src0)")
1292bf215546Sopenharmony_ciunop_convert("fisfinite32", tbool32, tfloat, "isfinite(src0)")
1293bf215546Sopenharmony_ci
1294bf215546Sopenharmony_ci# vc4-specific opcodes
1295bf215546Sopenharmony_ci
1296bf215546Sopenharmony_ci# Saturated vector add for 4 8bit ints.
1297bf215546Sopenharmony_cibinop("usadd_4x8_vc4", tint32, _2src_commutative + associative, """
1298bf215546Sopenharmony_cidst = 0;
1299bf215546Sopenharmony_cifor (int i = 0; i < 32; i += 8) {
1300bf215546Sopenharmony_ci   dst |= MIN2(((src0 >> i) & 0xff) + ((src1 >> i) & 0xff), 0xff) << i;
1301bf215546Sopenharmony_ci}
1302bf215546Sopenharmony_ci""")
1303bf215546Sopenharmony_ci
1304bf215546Sopenharmony_ci# Saturated vector subtract for 4 8bit ints.
1305bf215546Sopenharmony_cibinop("ussub_4x8_vc4", tint32, "", """
1306bf215546Sopenharmony_cidst = 0;
1307bf215546Sopenharmony_cifor (int i = 0; i < 32; i += 8) {
1308bf215546Sopenharmony_ci   int src0_chan = (src0 >> i) & 0xff;
1309bf215546Sopenharmony_ci   int src1_chan = (src1 >> i) & 0xff;
1310bf215546Sopenharmony_ci   if (src0_chan > src1_chan)
1311bf215546Sopenharmony_ci      dst |= (src0_chan - src1_chan) << i;
1312bf215546Sopenharmony_ci}
1313bf215546Sopenharmony_ci""")
1314bf215546Sopenharmony_ci
1315bf215546Sopenharmony_ci# vector min for 4 8bit ints.
1316bf215546Sopenharmony_cibinop("umin_4x8_vc4", tint32, _2src_commutative + associative, """
1317bf215546Sopenharmony_cidst = 0;
1318bf215546Sopenharmony_cifor (int i = 0; i < 32; i += 8) {
1319bf215546Sopenharmony_ci   dst |= MIN2((src0 >> i) & 0xff, (src1 >> i) & 0xff) << i;
1320bf215546Sopenharmony_ci}
1321bf215546Sopenharmony_ci""")
1322bf215546Sopenharmony_ci
1323bf215546Sopenharmony_ci# vector max for 4 8bit ints.
1324bf215546Sopenharmony_cibinop("umax_4x8_vc4", tint32, _2src_commutative + associative, """
1325bf215546Sopenharmony_cidst = 0;
1326bf215546Sopenharmony_cifor (int i = 0; i < 32; i += 8) {
1327bf215546Sopenharmony_ci   dst |= MAX2((src0 >> i) & 0xff, (src1 >> i) & 0xff) << i;
1328bf215546Sopenharmony_ci}
1329bf215546Sopenharmony_ci""")
1330bf215546Sopenharmony_ci
1331bf215546Sopenharmony_ci# unorm multiply: (a * b) / 255.
1332bf215546Sopenharmony_cibinop("umul_unorm_4x8_vc4", tint32, _2src_commutative + associative, """
1333bf215546Sopenharmony_cidst = 0;
1334bf215546Sopenharmony_cifor (int i = 0; i < 32; i += 8) {
1335bf215546Sopenharmony_ci   int src0_chan = (src0 >> i) & 0xff;
1336bf215546Sopenharmony_ci   int src1_chan = (src1 >> i) & 0xff;
1337bf215546Sopenharmony_ci   dst |= ((src0_chan * src1_chan) / 255) << i;
1338bf215546Sopenharmony_ci}
1339bf215546Sopenharmony_ci""")
1340bf215546Sopenharmony_ci
1341bf215546Sopenharmony_ci# Mali-specific opcodes
1342bf215546Sopenharmony_ciunop("fsat_signed_mali", tfloat, ("fmin(fmax(src0, -1.0), 1.0)"))
1343bf215546Sopenharmony_ciunop("fclamp_pos_mali", tfloat, ("fmax(src0, 0.0)"))
1344bf215546Sopenharmony_ci
1345bf215546Sopenharmony_ci# Magnitude equal to fddx/y, sign undefined. Derivative of a constant is zero.
1346bf215546Sopenharmony_ciunop("fddx_must_abs_mali", tfloat, "0.0")
1347bf215546Sopenharmony_ciunop("fddy_must_abs_mali", tfloat, "0.0")
1348bf215546Sopenharmony_ci
1349bf215546Sopenharmony_ci# DXIL specific double [un]pack
1350bf215546Sopenharmony_ci# DXIL doesn't support generic [un]pack instructions, so we want those
1351bf215546Sopenharmony_ci# lowered to bit ops. HLSL doesn't support 64bit bitcasts to/from
1352bf215546Sopenharmony_ci# double, only [un]pack. Technically DXIL does, but considering they
1353bf215546Sopenharmony_ci# can't be generated from HLSL, we want to match what would be coming from DXC.
1354bf215546Sopenharmony_ci# This is essentially just the standard [un]pack, except that it doesn't get
1355bf215546Sopenharmony_ci# lowered so we can handle it in the backend and turn it into MakeDouble/SplitDouble
1356bf215546Sopenharmony_ciunop_horiz("pack_double_2x32_dxil", 1, tuint64, 2, tuint32,
1357bf215546Sopenharmony_ci           "dst.x = src0.x | ((uint64_t)src0.y << 32);")
1358bf215546Sopenharmony_ciunop_horiz("unpack_double_2x32_dxil", 2, tuint32, 1, tuint64,
1359bf215546Sopenharmony_ci           "dst.x = src0.x; dst.y = src0.x >> 32;")
1360bf215546Sopenharmony_ci
1361bf215546Sopenharmony_ci# src0 and src1 are i8vec4 packed in an int32, and src2 is an int32.  The int8
1362bf215546Sopenharmony_ci# components are sign-extended to 32-bits, and a dot-product is performed on
1363bf215546Sopenharmony_ci# the resulting vectors.  src2 is added to the result of the dot-product.
1364bf215546Sopenharmony_ciopcode("sdot_4x8_iadd", 0, tint32, [0, 0, 0], [tuint32, tuint32, tint32],
1365bf215546Sopenharmony_ci       False, _2src_commutative, """
1366bf215546Sopenharmony_ci   const int32_t v0x = (int8_t)(src0      );
1367bf215546Sopenharmony_ci   const int32_t v0y = (int8_t)(src0 >>  8);
1368bf215546Sopenharmony_ci   const int32_t v0z = (int8_t)(src0 >> 16);
1369bf215546Sopenharmony_ci   const int32_t v0w = (int8_t)(src0 >> 24);
1370bf215546Sopenharmony_ci   const int32_t v1x = (int8_t)(src1      );
1371bf215546Sopenharmony_ci   const int32_t v1y = (int8_t)(src1 >>  8);
1372bf215546Sopenharmony_ci   const int32_t v1z = (int8_t)(src1 >> 16);
1373bf215546Sopenharmony_ci   const int32_t v1w = (int8_t)(src1 >> 24);
1374bf215546Sopenharmony_ci
1375bf215546Sopenharmony_ci   dst = (v0x * v1x) + (v0y * v1y) + (v0z * v1z) + (v0w * v1w) + src2;
1376bf215546Sopenharmony_ci""")
1377bf215546Sopenharmony_ci
1378bf215546Sopenharmony_ci# Like sdot_4x8_iadd, but unsigned.
1379bf215546Sopenharmony_ciopcode("udot_4x8_uadd", 0, tuint32, [0, 0, 0], [tuint32, tuint32, tuint32],
1380bf215546Sopenharmony_ci       False, _2src_commutative, """
1381bf215546Sopenharmony_ci   const uint32_t v0x = (uint8_t)(src0      );
1382bf215546Sopenharmony_ci   const uint32_t v0y = (uint8_t)(src0 >>  8);
1383bf215546Sopenharmony_ci   const uint32_t v0z = (uint8_t)(src0 >> 16);
1384bf215546Sopenharmony_ci   const uint32_t v0w = (uint8_t)(src0 >> 24);
1385bf215546Sopenharmony_ci   const uint32_t v1x = (uint8_t)(src1      );
1386bf215546Sopenharmony_ci   const uint32_t v1y = (uint8_t)(src1 >>  8);
1387bf215546Sopenharmony_ci   const uint32_t v1z = (uint8_t)(src1 >> 16);
1388bf215546Sopenharmony_ci   const uint32_t v1w = (uint8_t)(src1 >> 24);
1389bf215546Sopenharmony_ci
1390bf215546Sopenharmony_ci   dst = (v0x * v1x) + (v0y * v1y) + (v0z * v1z) + (v0w * v1w) + src2;
1391bf215546Sopenharmony_ci""")
1392bf215546Sopenharmony_ci
1393bf215546Sopenharmony_ci# src0 is i8vec4 packed in an int32, src1 is u8vec4 packed in an int32, and
1394bf215546Sopenharmony_ci# src2 is an int32.  The 8-bit components are extended to 32-bits, and a
1395bf215546Sopenharmony_ci# dot-product is performed on the resulting vectors.  src2 is added to the
1396bf215546Sopenharmony_ci# result of the dot-product.
1397bf215546Sopenharmony_ci#
1398bf215546Sopenharmony_ci# NOTE: Unlike many of the other dp4a opcodes, this mixed signs of source 0
1399bf215546Sopenharmony_ci# and source 1 mean that this opcode is not 2-source commutative
1400bf215546Sopenharmony_ciopcode("sudot_4x8_iadd", 0, tint32, [0, 0, 0], [tuint32, tuint32, tint32],
1401bf215546Sopenharmony_ci       False, "", """
1402bf215546Sopenharmony_ci   const int32_t v0x = (int8_t)(src0      );
1403bf215546Sopenharmony_ci   const int32_t v0y = (int8_t)(src0 >>  8);
1404bf215546Sopenharmony_ci   const int32_t v0z = (int8_t)(src0 >> 16);
1405bf215546Sopenharmony_ci   const int32_t v0w = (int8_t)(src0 >> 24);
1406bf215546Sopenharmony_ci   const uint32_t v1x = (uint8_t)(src1      );
1407bf215546Sopenharmony_ci   const uint32_t v1y = (uint8_t)(src1 >>  8);
1408bf215546Sopenharmony_ci   const uint32_t v1z = (uint8_t)(src1 >> 16);
1409bf215546Sopenharmony_ci   const uint32_t v1w = (uint8_t)(src1 >> 24);
1410bf215546Sopenharmony_ci
1411bf215546Sopenharmony_ci   dst = (v0x * v1x) + (v0y * v1y) + (v0z * v1z) + (v0w * v1w) + src2;
1412bf215546Sopenharmony_ci""")
1413bf215546Sopenharmony_ci
1414bf215546Sopenharmony_ci# Like sdot_4x8_iadd, but the result is clampled to the range [-0x80000000, 0x7ffffffff].
1415bf215546Sopenharmony_ciopcode("sdot_4x8_iadd_sat", 0, tint32, [0, 0, 0], [tuint32, tuint32, tint32],
1416bf215546Sopenharmony_ci       False, _2src_commutative, """
1417bf215546Sopenharmony_ci   const int64_t v0x = (int8_t)(src0      );
1418bf215546Sopenharmony_ci   const int64_t v0y = (int8_t)(src0 >>  8);
1419bf215546Sopenharmony_ci   const int64_t v0z = (int8_t)(src0 >> 16);
1420bf215546Sopenharmony_ci   const int64_t v0w = (int8_t)(src0 >> 24);
1421bf215546Sopenharmony_ci   const int64_t v1x = (int8_t)(src1      );
1422bf215546Sopenharmony_ci   const int64_t v1y = (int8_t)(src1 >>  8);
1423bf215546Sopenharmony_ci   const int64_t v1z = (int8_t)(src1 >> 16);
1424bf215546Sopenharmony_ci   const int64_t v1w = (int8_t)(src1 >> 24);
1425bf215546Sopenharmony_ci
1426bf215546Sopenharmony_ci   const int64_t tmp = (v0x * v1x) + (v0y * v1y) + (v0z * v1z) + (v0w * v1w) + src2;
1427bf215546Sopenharmony_ci
1428bf215546Sopenharmony_ci   dst = tmp >= INT32_MAX ? INT32_MAX : (tmp <= INT32_MIN ? INT32_MIN : tmp);
1429bf215546Sopenharmony_ci""")
1430bf215546Sopenharmony_ci
1431bf215546Sopenharmony_ci# Like udot_4x8_uadd, but the result is clampled to the range [0, 0xfffffffff].
1432bf215546Sopenharmony_ciopcode("udot_4x8_uadd_sat", 0, tint32, [0, 0, 0], [tuint32, tuint32, tint32],
1433bf215546Sopenharmony_ci       False, _2src_commutative, """
1434bf215546Sopenharmony_ci   const uint64_t v0x = (uint8_t)(src0      );
1435bf215546Sopenharmony_ci   const uint64_t v0y = (uint8_t)(src0 >>  8);
1436bf215546Sopenharmony_ci   const uint64_t v0z = (uint8_t)(src0 >> 16);
1437bf215546Sopenharmony_ci   const uint64_t v0w = (uint8_t)(src0 >> 24);
1438bf215546Sopenharmony_ci   const uint64_t v1x = (uint8_t)(src1      );
1439bf215546Sopenharmony_ci   const uint64_t v1y = (uint8_t)(src1 >>  8);
1440bf215546Sopenharmony_ci   const uint64_t v1z = (uint8_t)(src1 >> 16);
1441bf215546Sopenharmony_ci   const uint64_t v1w = (uint8_t)(src1 >> 24);
1442bf215546Sopenharmony_ci
1443bf215546Sopenharmony_ci   const uint64_t tmp = (v0x * v1x) + (v0y * v1y) + (v0z * v1z) + (v0w * v1w) + src2;
1444bf215546Sopenharmony_ci
1445bf215546Sopenharmony_ci   dst = tmp >= UINT32_MAX ? UINT32_MAX : tmp;
1446bf215546Sopenharmony_ci""")
1447bf215546Sopenharmony_ci
1448bf215546Sopenharmony_ci# Like sudot_4x8_iadd, but the result is clampled to the range [-0x80000000, 0x7ffffffff].
1449bf215546Sopenharmony_ci#
1450bf215546Sopenharmony_ci# NOTE: Unlike many of the other dp4a opcodes, this mixed signs of source 0
1451bf215546Sopenharmony_ci# and source 1 mean that this opcode is not 2-source commutative
1452bf215546Sopenharmony_ciopcode("sudot_4x8_iadd_sat", 0, tint32, [0, 0, 0], [tuint32, tuint32, tint32],
1453bf215546Sopenharmony_ci       False, "", """
1454bf215546Sopenharmony_ci   const int64_t v0x = (int8_t)(src0      );
1455bf215546Sopenharmony_ci   const int64_t v0y = (int8_t)(src0 >>  8);
1456bf215546Sopenharmony_ci   const int64_t v0z = (int8_t)(src0 >> 16);
1457bf215546Sopenharmony_ci   const int64_t v0w = (int8_t)(src0 >> 24);
1458bf215546Sopenharmony_ci   const uint64_t v1x = (uint8_t)(src1      );
1459bf215546Sopenharmony_ci   const uint64_t v1y = (uint8_t)(src1 >>  8);
1460bf215546Sopenharmony_ci   const uint64_t v1z = (uint8_t)(src1 >> 16);
1461bf215546Sopenharmony_ci   const uint64_t v1w = (uint8_t)(src1 >> 24);
1462bf215546Sopenharmony_ci
1463bf215546Sopenharmony_ci   const int64_t tmp = (v0x * v1x) + (v0y * v1y) + (v0z * v1z) + (v0w * v1w) + src2;
1464bf215546Sopenharmony_ci
1465bf215546Sopenharmony_ci   dst = tmp >= INT32_MAX ? INT32_MAX : (tmp <= INT32_MIN ? INT32_MIN : tmp);
1466bf215546Sopenharmony_ci""")
1467bf215546Sopenharmony_ci
1468bf215546Sopenharmony_ci# src0 and src1 are i16vec2 packed in an int32, and src2 is an int32.  The int16
1469bf215546Sopenharmony_ci# components are sign-extended to 32-bits, and a dot-product is performed on
1470bf215546Sopenharmony_ci# the resulting vectors.  src2 is added to the result of the dot-product.
1471bf215546Sopenharmony_ciopcode("sdot_2x16_iadd", 0, tint32, [0, 0, 0], [tuint32, tuint32, tint32],
1472bf215546Sopenharmony_ci       False, _2src_commutative, """
1473bf215546Sopenharmony_ci   const int32_t v0x = (int16_t)(src0      );
1474bf215546Sopenharmony_ci   const int32_t v0y = (int16_t)(src0 >> 16);
1475bf215546Sopenharmony_ci   const int32_t v1x = (int16_t)(src1      );
1476bf215546Sopenharmony_ci   const int32_t v1y = (int16_t)(src1 >> 16);
1477bf215546Sopenharmony_ci
1478bf215546Sopenharmony_ci   dst = (v0x * v1x) + (v0y * v1y) + src2;
1479bf215546Sopenharmony_ci""")
1480bf215546Sopenharmony_ci
1481bf215546Sopenharmony_ci# Like sdot_2x16_iadd, but unsigned.
1482bf215546Sopenharmony_ciopcode("udot_2x16_uadd", 0, tuint32, [0, 0, 0], [tuint32, tuint32, tuint32],
1483bf215546Sopenharmony_ci       False, _2src_commutative, """
1484bf215546Sopenharmony_ci   const uint32_t v0x = (uint16_t)(src0      );
1485bf215546Sopenharmony_ci   const uint32_t v0y = (uint16_t)(src0 >> 16);
1486bf215546Sopenharmony_ci   const uint32_t v1x = (uint16_t)(src1      );
1487bf215546Sopenharmony_ci   const uint32_t v1y = (uint16_t)(src1 >> 16);
1488bf215546Sopenharmony_ci
1489bf215546Sopenharmony_ci   dst = (v0x * v1x) + (v0y * v1y) + src2;
1490bf215546Sopenharmony_ci""")
1491bf215546Sopenharmony_ci
1492bf215546Sopenharmony_ci# Like sdot_2x16_iadd, but the result is clampled to the range [-0x80000000, 0x7ffffffff].
1493bf215546Sopenharmony_ciopcode("sdot_2x16_iadd_sat", 0, tint32, [0, 0, 0], [tuint32, tuint32, tint32],
1494bf215546Sopenharmony_ci       False, _2src_commutative, """
1495bf215546Sopenharmony_ci   const int64_t v0x = (int16_t)(src0      );
1496bf215546Sopenharmony_ci   const int64_t v0y = (int16_t)(src0 >> 16);
1497bf215546Sopenharmony_ci   const int64_t v1x = (int16_t)(src1      );
1498bf215546Sopenharmony_ci   const int64_t v1y = (int16_t)(src1 >> 16);
1499bf215546Sopenharmony_ci
1500bf215546Sopenharmony_ci   const int64_t tmp = (v0x * v1x) + (v0y * v1y) + src2;
1501bf215546Sopenharmony_ci
1502bf215546Sopenharmony_ci   dst = tmp >= INT32_MAX ? INT32_MAX : (tmp <= INT32_MIN ? INT32_MIN : tmp);
1503bf215546Sopenharmony_ci""")
1504bf215546Sopenharmony_ci
1505bf215546Sopenharmony_ci# Like udot_2x16_uadd, but the result is clampled to the range [0, 0xfffffffff].
1506bf215546Sopenharmony_ciopcode("udot_2x16_uadd_sat", 0, tint32, [0, 0, 0], [tuint32, tuint32, tint32],
1507bf215546Sopenharmony_ci       False, _2src_commutative, """
1508bf215546Sopenharmony_ci   const uint64_t v0x = (uint16_t)(src0      );
1509bf215546Sopenharmony_ci   const uint64_t v0y = (uint16_t)(src0 >> 16);
1510bf215546Sopenharmony_ci   const uint64_t v1x = (uint16_t)(src1      );
1511bf215546Sopenharmony_ci   const uint64_t v1y = (uint16_t)(src1 >> 16);
1512bf215546Sopenharmony_ci
1513bf215546Sopenharmony_ci   const uint64_t tmp = (v0x * v1x) + (v0y * v1y) + src2;
1514bf215546Sopenharmony_ci
1515bf215546Sopenharmony_ci   dst = tmp >= UINT32_MAX ? UINT32_MAX : tmp;
1516bf215546Sopenharmony_ci""")
1517