clang 23.0.0git
hlsl_intrinsics.h
Go to the documentation of this file.
1//===----- hlsl_intrinsics.h - HLSL 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_INTRINSICS_H_
10#define _HLSL_HLSL_INTRINSICS_H_
11
13
14namespace hlsl {
15
16// Generated by clang-tblgen from HLSLIntrinsics.td (detail/inline intrinsics).
17#include "hlsl_inline_intrinsics_gen.inc"
18
19//===----------------------------------------------------------------------===//
20// asfloat builtins
21//===----------------------------------------------------------------------===//
22
23/// \fn float asfloat(T Val)
24/// \brief Interprets the bit pattern of x as float point number.
25/// \param Val The input value.
26
27template <typename T, int R, int C>
28constexpr matrix<float, R, C> asfloat(matrix<T, R, C> V) {
30}
31
32template <typename T, int N>
33constexpr vector<float, N> asfloat(vector<T, N> V) {
35}
36
37template <typename T> constexpr float asfloat(T F) {
39}
40
41//===----------------------------------------------------------------------===//
42// asint builtins
43//===----------------------------------------------------------------------===//
44
45/// \fn int asint(T Val)
46/// \brief Interprets the bit pattern of x as an integer.
47/// \param Val The input value.
48
49template <typename T, int R, int C>
50constexpr matrix<int, R, C> asint(matrix<T, R, C> V) {
52}
53
54template <typename T, int N> constexpr vector<int, N> asint(vector<T, N> V) {
56}
57
58template <typename T> constexpr int asint(T F) {
60}
61
62//===----------------------------------------------------------------------===//
63// asint16 builtins
64//===----------------------------------------------------------------------===//
65
66/// \fn int16_t asint16(T X)
67/// \brief Interprets the bit pattern of \a X as an 16-bit integer.
68/// \param X The input value.
69
70#ifdef __HLSL_ENABLE_16_BIT
71
72template <typename T, int N>
73_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
74constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
75 __detail::is_same<uint16_t, T>::value ||
76 __detail::is_same<half, T>::value,
77 vector<int16_t, N>> asint16(vector<T, N> V) {
79}
80
81template <typename T>
82_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
83constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
84 __detail::is_same<uint16_t, T>::value ||
85 __detail::is_same<half, T>::value,
86 int16_t> asint16(T F) {
88}
89#endif
90
91//===----------------------------------------------------------------------===//
92// asuint builtins
93//===----------------------------------------------------------------------===//
94
95/// \fn uint asuint(T Val)
96/// \brief Interprets the bit pattern of x as an unsigned integer.
97/// \param Val The input value.
98
99template <typename T, int R, int C>
100constexpr matrix<uint, R, C> asuint(matrix<T, R, C> V) {
102}
103
104template <typename T, int N> constexpr vector<uint, N> asuint(vector<T, N> V) {
106}
107
108template <typename T> constexpr uint asuint(T F) {
110}
111
112//===----------------------------------------------------------------------===//
113// asuint splitdouble builtins
114//===----------------------------------------------------------------------===//
115
116/// \fn void asuint(double D, out uint lowbits, out int highbits)
117/// \brief Split and interprets the lowbits and highbits of double D into uints.
118/// \param D The input double.
119/// \param lowbits The output lowbits of D.
120/// \param highbits The output highbits of D.
121_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_splitdouble)
122void asuint(double, out uint, out uint);
123_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_splitdouble)
124void asuint(double2, out uint2, out uint2);
125_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_splitdouble)
126void asuint(double3, out uint3, out uint3);
127_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_splitdouble)
128void asuint(double4, out uint4, out uint4);
129
130//===----------------------------------------------------------------------===//
131// asuint16 builtins
132//===----------------------------------------------------------------------===//
133
134/// \fn uint16_t asuint16(T X)
135/// \brief Interprets the bit pattern of \a X as an 16-bit unsigned integer.
136/// \param X The input value.
137
138#ifdef __HLSL_ENABLE_16_BIT
139
140template <typename T, int N>
141_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
142constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
143 __detail::is_same<uint16_t, T>::value ||
144 __detail::is_same<half, T>::value,
145 vector<uint16_t, N>> asuint16(vector<T, N> V) {
147}
148
149template <typename T>
150_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
151constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
152 __detail::is_same<uint16_t, T>::value ||
153 __detail::is_same<half, T>::value,
154 uint16_t> asuint16(T F) {
156}
157#endif
158
159//===----------------------------------------------------------------------===//
160// firstbithigh builtins
161//===----------------------------------------------------------------------===//
162
163/// \fn T firstbithigh(T Val)
164/// \brief Returns the location of the first set bit starting from the lowest
165/// order bit and working upward, per component.
166/// \param Val the input value.
167
168#ifdef __HLSL_ENABLE_16_BIT
169
170template <typename T>
171_HLSL_AVAILABILITY(shadermodel, 6.2)
172const inline __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
173 __detail::is_same<uint16_t, T>::value,
174 uint> firstbithigh(T X) {
176}
177
178template <typename T, int N>
179_HLSL_AVAILABILITY(shadermodel, 6.2)
180const
181 inline __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
182 __detail::is_same<uint16_t, T>::value,
183 vector<uint, N>> firstbithigh(vector<T, N> X) {
184 return __detail::firstbithigh_impl<vector<uint, N>, vector<T, N>, 16>(X);
185}
186
187#endif
188
189template <typename T>
190const inline __detail::enable_if_t<
195
196template <typename T, int N>
197const inline __detail::enable_if_t<__detail::is_same<int, T>::value ||
199 vector<uint, N>>
200firstbithigh(vector<T, N> X) {
201 return __detail::firstbithigh_impl<vector<uint, N>, vector<T, N>, 32>(X);
202}
203
204template <typename T>
205const inline __detail::enable_if_t<__detail::is_same<int64_t, T>::value ||
207 uint>
211
212template <typename T, int N>
213const inline __detail::enable_if_t<__detail::is_same<int64_t, T>::value ||
215 vector<uint, N>>
216firstbithigh(vector<T, N> X) {
217 return __detail::firstbithigh_impl<vector<uint, N>, vector<T, N>, 64>(X);
218}
219
220//===----------------------------------------------------------------------===//
221// mul builtins
222//===----------------------------------------------------------------------===//
223
224/// \fn R mul(X x, Y y)
225/// \brief Multiplies x and y using matrix math.
226/// \param x [in] The first input value. If x is a vector, it is treated as a
227/// row vector.
228/// \param y [in] The second input value. If y is a vector, it is treated as a
229/// column vector.
230///
231/// The inner dimension x-columns and y-rows must be equal. The result has the
232/// dimension x-rows x y-columns. When both x and y are vectors, the result is
233/// a dot product (scalar). Scalar operands are multiplied element-wise.
234///
235/// This function supports 9 overloaded forms:
236/// 1. scalar * scalar -> scalar
237/// 2. scalar * vector -> vector
238/// 3. scalar * matrix -> matrix
239/// 4. vector * scalar -> vector
240/// 5. vector * vector -> scalar (dot product)
241/// 6. vector * matrix -> vector
242/// 7. matrix * scalar -> matrix
243/// 8. matrix * vector -> vector
244/// 9. matrix * matrix -> matrix
245
246// Cases 1, 2, 3, 4, 5, and 7 of mul are defined below as
247// header-only implementations because they are elementwise operations and dot
248// products easily expressed in HLSL.
249
250// Cases 6, 8, and 9 are defined in hlsl_alias_intrinsics.h to alias the mul
251// builtin so that they can be lowered to the llvm.matrix.multiply intrinsic
252// which is not exposed directly to HLSL.
253
254// Case 1: scalar * scalar -> scalar
255template <typename T>
256_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
257constexpr __detail::enable_if_t<__detail::is_arithmetic<T>::Value &&
258 __detail::is_same<half, T>::value,
259 T> mul(T x, T y) {
260 return x * y;
261}
262
263template <typename T>
264constexpr __detail::enable_if_t<
266mul(T x, T y) {
267 return x * y;
268}
269
270// Case 2: scalar * vector -> vector
271template <int N>
272_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
273constexpr vector<half, N> mul(half x, vector<half, N> y) {
274 return x * y;
275}
276
277template <typename T, int N> constexpr vector<T, N> mul(T x, vector<T, N> y) {
278 return x * y;
279}
280
281// Case 3: scalar * matrix -> matrix
282template <int R, int C>
283_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
284constexpr matrix<half, R, C> mul(half x, matrix<half, R, C> y) {
285 return x * y;
286}
287
288template <typename T, int R, int C>
289constexpr matrix<T, R, C> mul(T x, matrix<T, R, C> y) {
290 return x * y;
291}
292
293// Case 4: vector * scalar -> vector
294template <int N>
295_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
296constexpr vector<half, N> mul(vector<half, N> x, half y) {
297 return x * y;
298}
299
300template <typename T, int N> constexpr vector<T, N> mul(vector<T, N> x, T y) {
301 return x * y;
302}
303
304// Case 5: vector * vector -> scalar (dot product)
305template <int N>
306_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
307half mul(vector<half, N> x, vector<half, N> y) {
308 return __detail::mul_vec_impl(x, y);
309}
310
311template <typename T, int N> T mul(vector<T, N> x, vector<T, N> y) {
312 return __detail::mul_vec_impl(x, y);
313}
314
315// Case 7: matrix * scalar -> matrix
316template <int R, int C>
317_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
318constexpr matrix<half, R, C> mul(matrix<half, R, C> x, half y) {
319 return x * y;
320}
321
322template <typename T, int R, int C>
323constexpr matrix<T, R, C> mul(matrix<T, R, C> x, T y) {
324 return x * y;
325}
326
327} // namespace hlsl
328#endif //_HLSL_HLSL_INTRINSICS_H_
#define V(N, I)
#define X(type, name)
Definition Value.h:97
#define _HLSL_BUILTIN_ALIAS(builtin)
#define _HLSL_AVAILABILITY(platform, version)
#define _HLSL_16BIT_AVAILABILITY(platform, version,...)
constexpr K firstbithigh_impl(T X)
typename enable_if< B, T >::Type enable_if_t
Definition hlsl_detail.h:31
constexpr enable_if_t< sizeof(U)==sizeof(T), matrix< U, R, C > > bit_cast(matrix< T, R, C > M)
Definition hlsl_detail.h:35
constexpr enable_if_t<!is_same< double, T >::value, T > mul_vec_impl(vector< T, N > x, vector< T, N > y)
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_mul) vector< half
unsigned int uint
An unsigned 32-bit integer.
vector< uint, 2 > uint2
C mul(vector< half, R >, matrix< half, R, C >)
const __detail::enable_if_t< __detail::is_same< int, T >::value||__detail::is_same< uint, T >::value, uint > firstbithigh(T X)
Returns the location of the first set bit starting from the lowest order bit and working upward,...
vector< double, 3 > double3
constexpr matrix< uint, R, C > asuint(matrix< T, R, C > V)
vector< uint, 3 > uint3
vector< double, 4 > double4
constexpr matrix< float, R, C > asfloat(matrix< T, R, C > V)
vector< double, 2 > double2
vector< uint, 4 > uint4
constexpr matrix< int, R, C > asint(matrix< T, R, C > V)
static const bool value
Definition hlsl_detail.h:17