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