clang 24.0.0git
hlsl_alias_intrinsics.h
Go to the documentation of this file.
1//===--- hlsl_alias_intrinsics.h - HLSL alias definitions for intrinsics --===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _HLSL_HLSL_ALIAS_INTRINSICS_H_
10#define _HLSL_HLSL_ALIAS_INTRINSICS_H_
11
12namespace hlsl {
13
14// Note: Functions in this file are sorted alphabetically, then grouped by base
15// element type, and the element types are sorted by size, then singed integer,
16// unsigned integer and floating point. Keeping this ordering consistent will
17// help keep this file manageable as it grows.
18
19#ifdef __HLSL_ENABLE_16_BIT
20#define _HLSL_16BIT_AVAILABILITY(platform, version, ...) \
21 __attribute__((availability(platform, introduced = version)))
22#define _HLSL_16BIT_AVAILABILITY_STAGE(platform, version, stage) \
23 __attribute__(( \
24 availability(platform, introduced = version, environment = stage)))
25#define _HLSL_16BIT_AVAILABILITY_SHADERMODEL_DEFAULT() \
26 _HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
27#else
28#define _HLSL_16BIT_AVAILABILITY(platform, version, ...) \
29 __VA_OPT__(_HLSL_AVAILABILITY(platform, __VA_ARGS__))
30#define _HLSL_16BIT_AVAILABILITY_STAGE(environment, version, stage)
31#define _HLSL_16BIT_AVAILABILITY_SHADERMODEL_DEFAULT()
32#endif
33
34// Generated by clang-tblgen from HLSLIntrinsics.td (alias intrinsics).
35#include "hlsl_alias_intrinsics_gen.inc"
36
37//===----------------------------------------------------------------------===//
38// mul builtins
39//===----------------------------------------------------------------------===//
40
41/// \fn R mul(X x, Y y)
42/// \brief Multiplies x and y using matrix math.
43/// \param x [in] The first input value. If x is a vector, it is treated as a
44/// row vector.
45/// \param y [in] The second input value. If y is a vector, it is treated as a
46/// column vector.
47///
48/// The inner dimension x-columns and y-rows must be equal. The result has the
49/// dimension x-rows x y-columns. When both x and y are vectors, the result is
50/// a dot product (scalar). Scalar operands are multiplied element-wise.
51///
52/// This function supports 9 overloaded forms:
53/// 1. scalar * scalar -> scalar
54/// 2. scalar * vector -> vector
55/// 3. scalar * matrix -> matrix
56/// 4. vector * scalar -> vector
57/// 5. vector * vector -> scalar (dot product)
58/// 6. vector * matrix -> vector
59/// 7. matrix * scalar -> matrix
60/// 8. matrix * vector -> vector
61/// 9. matrix * matrix -> matrix
62
63// Cases 1, 2, 3, 4, 5, and 7 of mul are defined in hlsl_intrinsics.h as
64// header-only implementations because they are elementwise operations and dot
65// products easily expressed in HLSL.
66
67// Cases 6, 8, and 9 are defined below to alias the mul builtin so that they can
68// be lowered to the llvm.matrix.multiply intrinsic which is not exposed
69// directly to HLSL.
70
71// Case 6: vector * matrix -> vector
72template <int R, int C>
74_HLSL_BUILTIN_ALIAS(__builtin_hlsl_mul) vector<half, C> mul(vector<half, R>,
75 matrix<half, R, C>);
76
77template <typename T, int R, int C>
78_HLSL_BUILTIN_ALIAS(__builtin_hlsl_mul)
79vector<T, C> mul(vector<T, R>, matrix<T, R, C>);
80
81// Case 8: matrix * vector -> vector
82template <int R, int C>
84_HLSL_BUILTIN_ALIAS(__builtin_hlsl_mul) vector<half, R> mul(matrix<half, R, C>,
85 vector<half, C>);
86
87template <typename T, int R, int C>
88_HLSL_BUILTIN_ALIAS(__builtin_hlsl_mul)
89vector<T, R> mul(matrix<T, R, C>, vector<T, C>);
90
91// Case 9: matrix * matrix -> matrix
92template <int R, int K, int C>
94_HLSL_BUILTIN_ALIAS(__builtin_hlsl_mul) matrix<half, R, C> mul(
95 matrix<half, R, K>, matrix<half, K, C>);
96
97template <typename T, int R, int K, int C>
98_HLSL_BUILTIN_ALIAS(__builtin_hlsl_mul)
99matrix<T, R, C> mul(matrix<T, R, K>, matrix<T, K, C>);
100
101//===----------------------------------------------------------------------===//
102// transpose builtins
103//===----------------------------------------------------------------------===//
104
105/// \fn matrix<T, C, R> transpose(matrix<T, R, C> x)
106/// \brief Returns the transpose of the input matrix.
107/// \param x [in] The input matrix.
108
109template <int R, int C>
110_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
111_HLSL_BUILTIN_ALIAS(__builtin_hlsl_transpose) matrix<half, C, R> transpose(
112 matrix<half, R, C>);
113
114template <typename T, int R, int C>
115_HLSL_BUILTIN_ALIAS(__builtin_hlsl_transpose)
116matrix<T, C, R> transpose(matrix<T, R, C>);
117
118//===----------------------------------------------------------------------===//
119// select builtins
120//===----------------------------------------------------------------------===//
121
122/// \fn T select(bool Cond, T TrueVal, T FalseVal)
123/// \brief ternary operator.
124/// \param Cond The Condition input value.
125/// \param TrueVal The Value returned if Cond is true.
126/// \param FalseVal The Value returned if Cond is false.
127
128template <typename T>
129_HLSL_BUILTIN_ALIAS(__builtin_hlsl_select)
130T select(bool, T, T);
131
132/// \fn vector<T,Sz> select(vector<bool,Sz> Conds, vector<T,Sz> TrueVals,
133/// vector<T,Sz> FalseVals)
134/// \brief ternary operator for vectors. All vectors must be the same size.
135/// \param Conds The Condition input values.
136/// \param TrueVals The vector values are chosen from when conditions are true.
137/// \param FalseVals The vector values are chosen from when conditions are
138/// false.
139
140template <typename T, int N>
141_HLSL_BUILTIN_ALIAS(__builtin_hlsl_select)
142vector<T, N> select(
143 __detail::enable_if_t<(N > 1), __detail::type_identity_t<vector<bool, N>>>,
144 vector<T, N>, vector<T, N>);
145
146/// \fn vector<T,Sz> select(vector<bool,Sz> Conds, T TrueVal,
147/// vector<T,Sz> FalseVals)
148/// \brief ternary operator for vectors. All vectors must be the same size.
149/// \param Conds The Condition input values.
150/// \param TrueVal The scalar value to splat from when conditions are true.
151/// \param FalseVals The vector values are chosen from when conditions are
152/// false.
153
154template <typename T, int N>
155_HLSL_BUILTIN_ALIAS(__builtin_hlsl_select)
156vector<T, N> select(
157 __detail::enable_if_t<(N > 1), __detail::type_identity_t<vector<bool, N>>>,
158 T, vector<T, N>);
159
160/// \fn vector<T,Sz> select(vector<bool,Sz> Conds, vector<T,Sz> TrueVals,
161/// T FalseVal)
162/// \brief ternary operator for vectors. All vectors must be the same size.
163/// \param Conds The Condition input values.
164/// \param TrueVals The vector values are chosen from when conditions are true.
165/// \param FalseVal The scalar value to splat from when conditions are false.
166
167template <typename T, int N>
168_HLSL_BUILTIN_ALIAS(__builtin_hlsl_select)
169vector<T, N> select(
170 __detail::enable_if_t<(N > 1), __detail::type_identity_t<vector<bool, N>>>,
171 vector<T, N>, T);
172
173/// \fn vector<T,Sz> select(vector<bool,Sz> Conds, T TrueVals,
174/// T FalseVal)
175/// \brief ternary operator for vectors. All vectors must be the same size.
176/// \param Conds The Condition input values.
177/// \param TrueVal The scalar value to splat from when conditions are true.
178/// \param FalseVal The scalar value to splat from when conditions are false.
179
180template <typename T, typename U, int N>
181__detail::enable_if_t<(N > 1 && __detail::is_arithmetic<T>::Value),
182 vector<T, N>>
183select(vector<U, N> Conds, T TrueVal, T FalseVal) {
184 return __builtin_hlsl_select((vector<bool, N>)Conds, TrueVal, FalseVal);
185}
186
187/// \fn matrix<T,R,C> select(matrix<bool,R,C> Conds, matrix<T,R,C> TrueVals,
188/// matrix<T,R,C> FalseVals)
189/// \brief ternary operator for matrices. All matrices must be the same size.
190/// \param Conds The Condition input values.
191/// \param TrueVals The matrix values are chosen from when conditions are true.
192/// \param FalseVals The matrix values are chosen from when conditions are
193/// false.
194
195template <typename T, int R, int C>
196_HLSL_BUILTIN_ALIAS(__builtin_hlsl_select)
197matrix<T, R, C> select(
198 __detail::enable_if_t<(R * C > 1),
199 __detail::type_identity_t<matrix<bool, R, C>>>,
200 matrix<T, R, C>, matrix<T, R, C>);
201
202/// \fn matrix<T,R,C> select(matrix<bool,R,C> Conds, T TrueVal,
203/// matrix<T,R,C> FalseVals)
204/// \brief ternary operator for matrices. All matrices must be the same size.
205/// \param Conds The Condition input values.
206/// \param TrueVal The scalar value to splat from when conditions are true.
207/// \param FalseVals The matrix values are chosen from when conditions are
208/// false.
209
210template <typename T, int R, int C>
211_HLSL_BUILTIN_ALIAS(__builtin_hlsl_select)
212matrix<T, R, C> select(
213 __detail::enable_if_t<(R * C > 1),
214 __detail::type_identity_t<matrix<bool, R, C>>>,
215 T, matrix<T, R, C>);
216
217/// \fn matrix<T,R,C> select(matrix<bool,R,C> Conds, matrix<T,R,C> TrueVals,
218/// T FalseVal)
219/// \brief ternary operator for matrices. All matrices must be the same size.
220/// \param Conds The Condition input values.
221/// \param TrueVals The matrix values are chosen from when conditions are true.
222/// \param FalseVal The scalar value to splat from when conditions are false.
223
224template <typename T, int R, int C>
225_HLSL_BUILTIN_ALIAS(__builtin_hlsl_select)
226matrix<T, R, C> select(
227 __detail::enable_if_t<(R * C > 1),
228 __detail::type_identity_t<matrix<bool, R, C>>>,
229 matrix<T, R, C>, T);
230
231/// \fn matrix<T,R,C> select(matrix<bool,R,C> Conds, T TrueVal, T FalseVal)
232/// \brief ternary operator for a boolean matrix condition with scalar values.
233/// \param Conds The Condition input values.
234/// \param TrueVal The scalar value to splat from when conditions are true.
235/// \param FalseVal The scalar value to splat from when conditions are false.
236
237template <typename T, int R, int C>
238_HLSL_BUILTIN_ALIAS(__builtin_hlsl_select)
239matrix<T, R, C> select(matrix<bool, R, C>, T, T);
240
241/// \fn matrix<T,R,C> select(matrix<U,R,C> Conds, T TrueVal, T FalseVal)
242/// \brief ternary operator for a non-boolean matrix condition with scalar
243/// values.
244/// \param Conds The Condition input values.
245/// \param TrueVal The scalar value to splat from when conditions are true.
246/// \param FalseVal The scalar value to splat from when conditions are false.
247
248// Forwards to the boolean overload rather than calling the builtin directly
249// because the builtin is variadic and matrices cannot pass through it.
250template <typename T, typename U, int R, int C>
251__detail::enable_if_t<(R * C > 1 && __detail::is_arithmetic<T>::Value),
252 matrix<T, R, C>>
253select(matrix<U, R, C> Conds, T TrueVal, T FalseVal) {
254 return select((matrix<bool, R, C>)Conds, TrueVal, FalseVal);
255}
256
257} // namespace hlsl
258#endif //_HLSL_HLSL_ALIAS_INTRINSICS_H_
#define _HLSL_BUILTIN_ALIAS(builtin)
Definition hlsl.h:26
#define _HLSL_16BIT_AVAILABILITY(platform, version,...)
#define _HLSL_16BIT_AVAILABILITY_SHADERMODEL_DEFAULT()
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_mul) vector< half
T select(bool, T, T)
ternary operator.
C mul(vector< half, R >, matrix< half, R, C >)
R transpose(matrix< half, R, C >)