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);
129template <int R, int C>
130_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_splitdouble)
131void asuint(matrix<double, R, C>, out matrix<uint, R, C>,
132 out matrix<uint, R, C>);
133//===----------------------------------------------------------------------===//
134// asuint16 builtins
135//===----------------------------------------------------------------------===//
136
137/// \fn uint16_t asuint16(T X)
138/// \brief Interprets the bit pattern of \a X as an 16-bit unsigned integer.
139/// \param X The input value.
140
141#ifdef __HLSL_ENABLE_16_BIT
142
143template <typename T, int N>
144_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
145constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
146 __detail::is_same<uint16_t, T>::value ||
147 __detail::is_same<half, T>::value,
148 vector<uint16_t, N>> asuint16(vector<T, N> V) {
150}
151
152template <typename T>
153_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
154constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
155 __detail::is_same<uint16_t, T>::value ||
156 __detail::is_same<half, T>::value,
157 uint16_t> asuint16(T F) {
159}
160#endif
161
162//===----------------------------------------------------------------------===//
163// firstbithigh builtins
164//===----------------------------------------------------------------------===//
165
166/// \fn T firstbithigh(T Val)
167/// \brief Returns the location of the first set bit starting from the lowest
168/// order bit and working upward, per component.
169/// \param Val the input value.
170
171#ifdef __HLSL_ENABLE_16_BIT
172
173template <typename T>
174_HLSL_AVAILABILITY(shadermodel, 6.2)
175const inline __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
176 __detail::is_same<uint16_t, T>::value,
177 uint> firstbithigh(T X) {
179}
180
181template <typename T, int N>
182_HLSL_AVAILABILITY(shadermodel, 6.2)
183const
184 inline __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
185 __detail::is_same<uint16_t, T>::value,
186 vector<uint, N>> firstbithigh(vector<T, N> X) {
187 return __detail::firstbithigh_impl<vector<uint, N>, vector<T, N>, 16>(X);
188}
189
190#endif
191
192template <typename T>
193const inline __detail::enable_if_t<
198
199template <typename T, int N>
200const inline __detail::enable_if_t<__detail::is_same<int, T>::value ||
202 vector<uint, N>>
203firstbithigh(vector<T, N> X) {
204 return __detail::firstbithigh_impl<vector<uint, N>, vector<T, N>, 32>(X);
205}
206
207template <typename T>
208const inline __detail::enable_if_t<__detail::is_same<int64_t, T>::value ||
210 uint>
214
215template <typename T, int N>
216const inline __detail::enable_if_t<__detail::is_same<int64_t, T>::value ||
218 vector<uint, N>>
219firstbithigh(vector<T, N> X) {
220 return __detail::firstbithigh_impl<vector<uint, N>, vector<T, N>, 64>(X);
221}
222
223//===----------------------------------------------------------------------===//
224// mul builtins
225//===----------------------------------------------------------------------===//
226
227/// \fn R mul(X x, Y y)
228/// \brief Multiplies x and y using matrix math.
229/// \param x [in] The first input value. If x is a vector, it is treated as a
230/// row vector.
231/// \param y [in] The second input value. If y is a vector, it is treated as a
232/// column vector.
233///
234/// The inner dimension x-columns and y-rows must be equal. The result has the
235/// dimension x-rows x y-columns. When both x and y are vectors, the result is
236/// a dot product (scalar). Scalar operands are multiplied element-wise.
237///
238/// This function supports 9 overloaded forms:
239/// 1. scalar * scalar -> scalar
240/// 2. scalar * vector -> vector
241/// 3. scalar * matrix -> matrix
242/// 4. vector * scalar -> vector
243/// 5. vector * vector -> scalar (dot product)
244/// 6. vector * matrix -> vector
245/// 7. matrix * scalar -> matrix
246/// 8. matrix * vector -> vector
247/// 9. matrix * matrix -> matrix
248
249// Cases 1, 2, 3, 4, 5, and 7 of mul are defined below as
250// header-only implementations because they are elementwise operations and dot
251// products easily expressed in HLSL.
252
253// Cases 6, 8, and 9 are defined in hlsl_alias_intrinsics.h to alias the mul
254// builtin so that they can be lowered to the llvm.matrix.multiply intrinsic
255// which is not exposed directly to HLSL.
256
257// Case 1: scalar * scalar -> scalar
258template <typename T>
259_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
260constexpr __detail::enable_if_t<__detail::is_arithmetic<T>::Value &&
261 __detail::is_same<half, T>::value,
262 T> mul(T x, T y) {
263 return x * y;
264}
265
266template <typename T>
267constexpr __detail::enable_if_t<
269mul(T x, T y) {
270 return x * y;
271}
272
273// Case 2: scalar * vector -> vector
274template <int N>
275_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
276constexpr vector<half, N> mul(half x, vector<half, N> y) {
277 return x * y;
278}
279
280template <typename T, int N> constexpr vector<T, N> mul(T x, vector<T, N> y) {
281 return x * y;
282}
283
284// Case 3: scalar * matrix -> matrix
285template <int R, int C>
286_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
287constexpr matrix<half, R, C> mul(half x, matrix<half, R, C> y) {
288 return x * y;
289}
290
291template <typename T, int R, int C>
292constexpr matrix<T, R, C> mul(T x, matrix<T, R, C> y) {
293 return x * y;
294}
295
296// Case 4: vector * scalar -> vector
297template <int N>
298_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
299constexpr vector<half, N> mul(vector<half, N> x, half y) {
300 return x * y;
301}
302
303template <typename T, int N> constexpr vector<T, N> mul(vector<T, N> x, T y) {
304 return x * y;
305}
306
307// Case 5: vector * vector -> scalar (dot product)
308template <int N>
309_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
310half mul(vector<half, N> x, vector<half, N> y) {
311 return __detail::mul_vec_impl(x, y);
312}
313
314template <typename T, int N> T mul(vector<T, N> x, vector<T, N> y) {
315 return __detail::mul_vec_impl(x, y);
316}
317
318// Case 7: matrix * scalar -> matrix
319template <int R, int C>
320_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
321constexpr matrix<half, R, C> mul(matrix<half, R, C> x, half y) {
322 return x * y;
323}
324
325template <typename T, int R, int C>
326constexpr matrix<T, R, C> mul(matrix<T, R, C> x, T y) {
327 return x * y;
328}
329
330} // namespace hlsl
331#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)
__packed_splat4 int16_t
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 uint16_t
static const bool value
Definition hlsl_detail.h:17