e2fbf02b3eafb55859dcacd1ce224d5741bef13d
2 * Copyright © 2018 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #ifndef NIR_BUILTIN_BUILDER_H
25 #define NIR_BUILTIN_BUILDER_H
27 #include "util/u_math.h"
28 #include "nir/nir_builder.h"
35 * Functions are sorted alphabetically with removed type and "fast" prefix.
36 * Definitions for functions in the C file come first.
39 nir_ssa_def
* nir_cross3(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
);
40 nir_ssa_def
* nir_cross4(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
);
41 nir_ssa_def
* nir_length(nir_builder
*b
, nir_ssa_def
*vec
);
42 nir_ssa_def
* nir_fast_length(nir_builder
*b
, nir_ssa_def
*vec
);
43 nir_ssa_def
* nir_nextafter(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
);
44 nir_ssa_def
* nir_normalize(nir_builder
*b
, nir_ssa_def
*vec
);
45 nir_ssa_def
* nir_rotate(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
);
46 nir_ssa_def
* nir_smoothstep(nir_builder
*b
, nir_ssa_def
*edge0
,
47 nir_ssa_def
*edge1
, nir_ssa_def
*x
);
48 nir_ssa_def
* nir_upsample(nir_builder
*b
, nir_ssa_def
*hi
, nir_ssa_def
*lo
);
49 nir_ssa_def
* nir_atan(nir_builder
*b
, nir_ssa_def
*y_over_x
);
50 nir_ssa_def
* nir_atan2(nir_builder
*b
, nir_ssa_def
*y
, nir_ssa_def
*x
);
52 static inline nir_ssa_def
*
53 nir_nan_check2(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
, nir_ssa_def
*res
)
55 return nir_bcsel(b
, nir_fne(b
, x
, x
), x
, nir_bcsel(b
, nir_fne(b
, y
, y
), y
, res
));
58 static inline nir_ssa_def
*
59 nir_fmax_abs_vec_comp(nir_builder
*b
, nir_ssa_def
*vec
)
61 nir_ssa_def
*res
= nir_channel(b
, vec
, 0);
62 for (unsigned i
= 1; i
< vec
->num_components
; ++i
)
63 res
= nir_fmax(b
, res
, nir_fabs(b
, nir_channel(b
, vec
, i
)));
67 static inline nir_ssa_def
*
68 nir_iabs_diff(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
70 nir_ssa_def
*cond
= nir_ige(b
, x
, y
);
71 nir_ssa_def
*res0
= nir_isub(b
, x
, y
);
72 nir_ssa_def
*res1
= nir_isub(b
, y
, x
);
73 return nir_bcsel(b
, cond
, res0
, res1
);
76 static inline nir_ssa_def
*
77 nir_uabs_diff(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
79 nir_ssa_def
*cond
= nir_uge(b
, x
, y
);
80 nir_ssa_def
*res0
= nir_isub(b
, x
, y
);
81 nir_ssa_def
*res1
= nir_isub(b
, y
, x
);
82 return nir_bcsel(b
, cond
, res0
, res1
);
85 static inline nir_ssa_def
*
86 nir_umul24(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
88 nir_ssa_def
*mask
= nir_imm_int(b
, 0xffffff);
89 nir_ssa_def
*x_24
= nir_iand(b
, x
, mask
);
90 nir_ssa_def
*y_24
= nir_iand(b
, y
, mask
);
91 return nir_imul(b
, x_24
, y_24
);
94 static inline nir_ssa_def
*
95 nir_umad24(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
, nir_ssa_def
*z
)
97 nir_ssa_def
*temp
= nir_umul24(b
, x
, y
);
98 return nir_iadd(b
, temp
, z
);
101 static inline nir_ssa_def
*
102 nir_imad24(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
, nir_ssa_def
*z
)
104 nir_ssa_def
*temp
= nir_imul24(b
, x
, y
);
105 return nir_iadd(b
, temp
, z
);
108 static inline nir_ssa_def
*
109 nir_imad_hi(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
, nir_ssa_def
*z
)
111 nir_ssa_def
*temp
= nir_imul_high(b
, x
, y
);
112 return nir_iadd(b
, temp
, z
);
115 static inline nir_ssa_def
*
116 nir_umad_hi(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
, nir_ssa_def
*z
)
118 nir_ssa_def
*temp
= nir_umul_high(b
, x
, y
);
119 return nir_iadd(b
, temp
, z
);
122 static inline nir_ssa_def
*
123 nir_bitselect(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
, nir_ssa_def
*s
)
125 return nir_ior(b
, nir_iand(b
, nir_inot(b
, s
), x
), nir_iand(b
, s
, y
));
128 static inline nir_ssa_def
*
129 nir_fclamp(nir_builder
*b
,
130 nir_ssa_def
*x
, nir_ssa_def
*min_val
, nir_ssa_def
*max_val
)
132 return nir_fmin(b
, nir_fmax(b
, x
, min_val
), max_val
);
135 static inline nir_ssa_def
*
136 nir_iclamp(nir_builder
*b
,
137 nir_ssa_def
*x
, nir_ssa_def
*min_val
, nir_ssa_def
*max_val
)
139 return nir_imin(b
, nir_imax(b
, x
, min_val
), max_val
);
142 static inline nir_ssa_def
*
143 nir_uclamp(nir_builder
*b
,
144 nir_ssa_def
*x
, nir_ssa_def
*min_val
, nir_ssa_def
*max_val
)
146 return nir_umin(b
, nir_umax(b
, x
, min_val
), max_val
);
149 static inline nir_ssa_def
*
150 nir_copysign(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
152 uint64_t masks
= 1ull << (x
->bit_size
- 1);
153 uint64_t maskv
= ~masks
;
155 nir_ssa_def
*s
= nir_imm_intN_t(b
, masks
, x
->bit_size
);
156 nir_ssa_def
*v
= nir_imm_intN_t(b
, maskv
, x
->bit_size
);
158 return nir_ior(b
, nir_iand(b
, x
, v
), nir_iand(b
, y
, s
));
161 static inline nir_ssa_def
*
162 nir_degrees(nir_builder
*b
, nir_ssa_def
*val
)
164 return nir_fmul_imm(b
, val
, 180.0 / M_PI
);
167 static inline nir_ssa_def
*
168 nir_fdim(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
170 nir_ssa_def
*cond
= nir_flt(b
, y
, x
);
171 nir_ssa_def
*res
= nir_fsub(b
, x
, y
);
172 nir_ssa_def
*zero
= nir_imm_floatN_t(b
, 0.0, x
->bit_size
);
174 // return NaN if either x or y are NaN, else x-y if x>y, else +0.0
175 return nir_nan_check2(b
, x
, y
, nir_bcsel(b
, cond
, res
, zero
));
178 static inline nir_ssa_def
*
179 nir_distance(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
181 return nir_length(b
, nir_fsub(b
, x
, y
));
184 static inline nir_ssa_def
*
185 nir_fast_distance(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
187 return nir_fast_length(b
, nir_fsub(b
, x
, y
));
190 static inline nir_ssa_def
*
191 nir_fast_normalize(nir_builder
*b
, nir_ssa_def
*vec
)
193 return nir_fdiv(b
, vec
, nir_fast_length(b
, vec
));
196 static inline nir_ssa_def
*
197 nir_fmad(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
, nir_ssa_def
*z
)
199 return nir_fadd(b
, nir_fmul(b
, x
, y
), z
);
202 static inline nir_ssa_def
*
203 nir_maxmag(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
205 nir_ssa_def
*xabs
= nir_fabs(b
, x
);
206 nir_ssa_def
*yabs
= nir_fabs(b
, y
);
208 nir_ssa_def
*condy
= nir_flt(b
, xabs
, yabs
);
209 nir_ssa_def
*condx
= nir_flt(b
, yabs
, xabs
);
211 return nir_bcsel(b
, condy
, y
, nir_bcsel(b
, condx
, x
, nir_fmax(b
, x
, y
)));
214 static inline nir_ssa_def
*
215 nir_minmag(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
217 nir_ssa_def
*xabs
= nir_fabs(b
, x
);
218 nir_ssa_def
*yabs
= nir_fabs(b
, y
);
220 nir_ssa_def
*condx
= nir_flt(b
, xabs
, yabs
);
221 nir_ssa_def
*condy
= nir_flt(b
, yabs
, xabs
);
223 return nir_bcsel(b
, condy
, y
, nir_bcsel(b
, condx
, x
, nir_fmin(b
, x
, y
)));
226 static inline nir_ssa_def
*
227 nir_nan(nir_builder
*b
, nir_ssa_def
*x
)
229 nir_ssa_def
*nan
= nir_imm_floatN_t(b
, NAN
, x
->bit_size
);
230 if (x
->num_components
== 1)
233 nir_ssa_def
*nans
[NIR_MAX_VEC_COMPONENTS
];
234 for (unsigned i
= 0; i
< x
->num_components
; ++i
)
237 return nir_vec(b
, nans
, x
->num_components
);
240 static inline nir_ssa_def
*
241 nir_radians(nir_builder
*b
, nir_ssa_def
*val
)
243 return nir_fmul_imm(b
, val
, M_PI
/ 180.0);
246 static inline nir_ssa_def
*
247 nir_select(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
, nir_ssa_def
*s
)
249 if (s
->num_components
!= 1) {
250 uint64_t mask
= 1ull << (s
->bit_size
- 1);
251 s
= nir_iand(b
, s
, nir_imm_intN_t(b
, mask
, s
->bit_size
));
253 return nir_bcsel(b
, nir_ieq(b
, s
, nir_imm_intN_t(b
, 0, s
->bit_size
)), x
, y
);
260 #endif /* NIR_BUILTIN_BUILDER_H */