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 "nir/nir_builder.h"
30 * Functions are sorted alphabetically with removed type and "fast" prefix.
31 * Definitions for functions in the C file come first.
34 nir_ssa_def
* nir_cross3(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
);
35 nir_ssa_def
* nir_cross4(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
);
36 nir_ssa_def
* nir_length(nir_builder
*b
, nir_ssa_def
*vec
);
37 nir_ssa_def
* nir_fast_length(nir_builder
*b
, nir_ssa_def
*vec
);
38 nir_ssa_def
* nir_nextafter(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
);
39 nir_ssa_def
* nir_normalize(nir_builder
*b
, nir_ssa_def
*vec
);
40 nir_ssa_def
* nir_rotate(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
);
41 nir_ssa_def
* nir_smoothstep(nir_builder
*b
, nir_ssa_def
*edge0
,
42 nir_ssa_def
*edge1
, nir_ssa_def
*x
);
43 nir_ssa_def
* nir_upsample(nir_builder
*b
, nir_ssa_def
*hi
, nir_ssa_def
*lo
);
45 static inline nir_ssa_def
*
46 nir_nan_check2(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
, nir_ssa_def
*res
)
48 return nir_bcsel(b
, nir_fne(b
, x
, x
), x
, nir_bcsel(b
, nir_fne(b
, y
, y
), y
, res
));
51 static inline nir_ssa_def
*
52 nir_fmax_abs_vec_comp(nir_builder
*b
, nir_ssa_def
*vec
)
54 nir_ssa_def
*res
= nir_channel(b
, vec
, 0);
55 for (unsigned i
= 1; i
< vec
->num_components
; ++i
)
56 res
= nir_fmax(b
, res
, nir_fabs(b
, nir_channel(b
, vec
, i
)));
60 static inline nir_ssa_def
*
61 nir_iabs_diff(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
63 nir_ssa_def
*cond
= nir_ige(b
, x
, y
);
64 nir_ssa_def
*res0
= nir_isub(b
, x
, y
);
65 nir_ssa_def
*res1
= nir_isub(b
, y
, x
);
66 return nir_bcsel(b
, cond
, res0
, res1
);
69 static inline nir_ssa_def
*
70 nir_uabs_diff(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
72 nir_ssa_def
*cond
= nir_uge(b
, x
, y
);
73 nir_ssa_def
*res0
= nir_isub(b
, x
, y
);
74 nir_ssa_def
*res1
= nir_isub(b
, y
, x
);
75 return nir_bcsel(b
, cond
, res0
, res1
);
78 static inline nir_ssa_def
*
79 nir_bitselect(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
, nir_ssa_def
*s
)
81 return nir_ior(b
, nir_iand(b
, nir_inot(b
, s
), x
), nir_iand(b
, s
, y
));
84 static inline nir_ssa_def
*
85 nir_fclamp(nir_builder
*b
,
86 nir_ssa_def
*x
, nir_ssa_def
*min_val
, nir_ssa_def
*max_val
)
88 return nir_fmin(b
, nir_fmax(b
, x
, min_val
), max_val
);
91 static inline nir_ssa_def
*
92 nir_iclamp(nir_builder
*b
,
93 nir_ssa_def
*x
, nir_ssa_def
*min_val
, nir_ssa_def
*max_val
)
95 return nir_imin(b
, nir_imax(b
, x
, min_val
), max_val
);
98 static inline nir_ssa_def
*
99 nir_uclamp(nir_builder
*b
,
100 nir_ssa_def
*x
, nir_ssa_def
*min_val
, nir_ssa_def
*max_val
)
102 return nir_umin(b
, nir_umax(b
, x
, min_val
), max_val
);
105 static inline nir_ssa_def
*
106 nir_copysign(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
108 uint64_t masks
= 1ull << (x
->bit_size
- 1);
109 uint64_t maskv
= ~masks
;
111 nir_ssa_def
*s
= nir_imm_intN_t(b
, masks
, x
->bit_size
);
112 nir_ssa_def
*v
= nir_imm_intN_t(b
, maskv
, x
->bit_size
);
114 return nir_ior(b
, nir_iand(b
, x
, v
), nir_iand(b
, y
, s
));
117 static inline nir_ssa_def
*
118 nir_degrees(nir_builder
*b
, nir_ssa_def
*val
)
120 return nir_fmul_imm(b
, val
, 180.0 / M_PI
);
123 static inline nir_ssa_def
*
124 nir_fdim(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
126 nir_ssa_def
*cond
= nir_flt(b
, y
, x
);
127 nir_ssa_def
*res
= nir_fsub(b
, x
, y
);
128 nir_ssa_def
*zero
= nir_imm_floatN_t(b
, 0.0, x
->bit_size
);
130 // return NaN if either x or y are NaN, else x-y if x>y, else +0.0
131 return nir_nan_check2(b
, x
, y
, nir_bcsel(b
, cond
, res
, zero
));
134 static inline nir_ssa_def
*
135 nir_distance(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
137 return nir_length(b
, nir_fsub(b
, x
, y
));
140 static inline nir_ssa_def
*
141 nir_fast_distance(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
143 return nir_fast_length(b
, nir_fsub(b
, x
, y
));
146 static inline nir_ssa_def
*
147 nir_fast_normalize(nir_builder
*b
, nir_ssa_def
*vec
)
149 return nir_fdiv(b
, vec
, nir_fast_length(b
, vec
));
152 static inline nir_ssa_def
*
153 nir_fmad(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
, nir_ssa_def
*z
)
155 return nir_fadd(b
, nir_fmul(b
, x
, y
), z
);
158 static inline nir_ssa_def
*
159 nir_maxmag(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
161 nir_ssa_def
*xabs
= nir_fabs(b
, x
);
162 nir_ssa_def
*yabs
= nir_fabs(b
, y
);
164 nir_ssa_def
*condy
= nir_flt(b
, xabs
, yabs
);
165 nir_ssa_def
*condx
= nir_flt(b
, yabs
, xabs
);
167 return nir_bcsel(b
, condy
, y
, nir_bcsel(b
, condx
, x
, nir_fmax(b
, x
, y
)));
170 static inline nir_ssa_def
*
171 nir_minmag(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
)
173 nir_ssa_def
*xabs
= nir_fabs(b
, x
);
174 nir_ssa_def
*yabs
= nir_fabs(b
, y
);
176 nir_ssa_def
*condx
= nir_flt(b
, xabs
, yabs
);
177 nir_ssa_def
*condy
= nir_flt(b
, yabs
, xabs
);
179 return nir_bcsel(b
, condy
, y
, nir_bcsel(b
, condx
, x
, nir_fmin(b
, x
, y
)));
182 static inline nir_ssa_def
*
183 nir_nan(nir_builder
*b
, nir_ssa_def
*x
)
185 nir_ssa_def
*nan
= nir_imm_floatN_t(b
, NAN
, x
->bit_size
);
186 if (x
->num_components
== 1)
189 nir_ssa_def
*nans
[NIR_MAX_VEC_COMPONENTS
];
190 for (unsigned i
= 0; i
< x
->num_components
; ++i
)
193 return nir_vec(b
, nans
, x
->num_components
);
196 static inline nir_ssa_def
*
197 nir_radians(nir_builder
*b
, nir_ssa_def
*val
)
199 return nir_fmul_imm(b
, val
, M_PI
/ 180.0);
202 static inline nir_ssa_def
*
203 nir_select(nir_builder
*b
, nir_ssa_def
*x
, nir_ssa_def
*y
, nir_ssa_def
*s
)
205 if (s
->num_components
!= 1) {
206 uint64_t mask
= 1ull << (s
->bit_size
- 1);
207 s
= nir_iand(b
, s
, nir_imm_intN_t(b
, mask
, s
->bit_size
));
209 return nir_bcsel(b
, nir_ieq(b
, s
, nir_imm_intN_t(b
, 0, s
->bit_size
)), x
, y
);
212 #endif /* NIR_BUILTIN_BUILDER_H */