Skip to content

Commit 5ecbf46

Browse files
authored
Hlsl asuint16 function (#132315)
Implemented the asuint16 function and added test cases for codegen, Sema, and SPIR-V backend. fixes #99185
1 parent d6a2cca commit 5ecbf46

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

clang/lib/Headers/hlsl/hlsl_intrinsics.h

+28
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,34 @@ void asuint(double3, out uint3, out uint3);
8080
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_elementwise_splitdouble)
8181
void asuint(double4, out uint4, out uint4);
8282

83+
//===----------------------------------------------------------------------===//
84+
// asuint16 builtins
85+
//===----------------------------------------------------------------------===//
86+
87+
/// \fn uint16_t asuint16(T X)
88+
/// \brief Interprets the bit pattern of \a X as an 16-bit unsigned integer.
89+
/// \param X The input value.
90+
#ifdef __HLSL_ENABLE_16_BIT
91+
92+
template <typename T, int N>
93+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
94+
constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
95+
__detail::is_same<uint16_t, T>::value ||
96+
__detail::is_same<half, T>::value,
97+
vector<uint16_t, N>> asuint16(vector<T, N> V) {
98+
return __detail::bit_cast<uint16_t, T, N>(V);
99+
}
100+
101+
template <typename T>
102+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
103+
constexpr __detail::enable_if_t<__detail::is_same<int16_t, T>::value ||
104+
__detail::is_same<uint16_t, T>::value ||
105+
__detail::is_same<half, T>::value,
106+
uint16_t> asuint16(T F) {
107+
return __detail::bit_cast<uint16_t, T>(F);
108+
}
109+
#endif
110+
83111
//===----------------------------------------------------------------------===//
84112
// distance builtins
85113
//===----------------------------------------------------------------------===//
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.2-library %s -fnative-half-type -emit-llvm -O1 -o - | FileCheck %s
2+
3+
//CHECK-LABEL: define {{.*}}test_ints
4+
//CHECK-SAME: {{.*}}(i16 {{.*}} [[VAL:%.*]]){{.*}}
5+
//CHECK-NOT: bitcast
6+
//CHECK: entry:
7+
//CHECK: ret i16 [[VAL]]
8+
uint16_t test_int(int16_t p0)
9+
{
10+
return asuint16(p0);
11+
}
12+
13+
//CHECK-LABEL: define {{.*}}test_uint
14+
//CHECK-SAME: {{.*}}(i16 {{.*}} [[VAL:%.*]]){{.*}}
15+
//CHECK-NOT: bitcast
16+
//CHECK: entry:
17+
//CHECK-NEXT: ret i16 [[VAL]]
18+
uint16_t test_uint(uint16_t p0)
19+
{
20+
return asuint16(p0);
21+
}
22+
23+
//CHECK-LABEL: define {{.*}}test_half
24+
//CHECK-SAME: {{.*}}(half {{.*}} [[VAL:%.*]]){{.*}}
25+
//CHECK: [[RES:%.*]] = bitcast half [[VAL]] to i16
26+
//CHECK-NEXT: ret i16 [[RES]]
27+
uint16_t test_half(half p0)
28+
{
29+
return asuint16(p0);
30+
}
31+
32+
//CHECK-LABEL: define {{.*}}test_vector_int
33+
//CHECK-SAME: {{.*}}(<4 x i16> {{.*}} [[VAL:%.*]]){{.*}}
34+
//CHECK-NOT: bitcast
35+
//CHECK: entry:
36+
//CHECK-NEXT: ret <4 x i16> [[VAL]]
37+
uint16_t4 test_vector_int(int16_t4 p0)
38+
{
39+
return asuint16(p0);
40+
}
41+
42+
//CHECK-LABEL: define {{.*}}test_vector_uint
43+
//CHECK-SAME: {{.*}}(<4 x i16> {{.*}} [[VAL:%.*]]){{.*}}
44+
//CHECK-NOT: bitcast
45+
//CHECK: entry:
46+
//CHECK-NEXT: ret <4 x i16> [[VAL]]
47+
uint16_t4 test_vector_uint(uint16_t4 p0)
48+
{
49+
return asuint16(p0);
50+
}
51+
52+
//CHECK-LABEL: define {{.*}}fn
53+
//CHECK-SAME: {{.*}}(<4 x half> {{.*}} [[VAL:%.*]]){{.*}}
54+
//CHECK: [[RES:%.*]] = bitcast <4 x half> [[VAL]] to <4 x i16>
55+
//CHECK-NEXT: ret <4 x i16> [[RES]]
56+
uint16_t4 fn(half4 p1)
57+
{
58+
return asuint16(p1);
59+
}
60+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.2-library %s -fnative-half-type -verify
2+
3+
uint16_t test_asuint16_less_argument()
4+
{
5+
return asuint16();
6+
// expected-error@-1 {{no matching function for call to 'asuint16'}}
7+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'V', but no arguments were provided}}
8+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'F', but no arguments were provided}}
9+
10+
}
11+
12+
int16_t4 test_asuint16_too_many_arg(uint16_t p0, uint16_t p1)
13+
{
14+
return asuint16(p0, p1);
15+
// expected-error@-1 {{no matching function for call to 'asuint16'}}
16+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'V', but 2 arguments were provided}}
17+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'F', but 2 arguments were provided}}
18+
19+
}
20+
21+
int16_t test_asuint16_int(int p1)
22+
{
23+
return asuint16(p1);
24+
// expected-error@-1 {{no matching function for call to 'asuint16'}}
25+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: could not match 'vector<T, N>' against 'int'}}
26+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = int]: no type named 'Type'}}
27+
}
28+
29+
int16_t test_asuint16_float(float p1)
30+
{
31+
return asuint16(p1);
32+
// expected-error@-1 {{no matching function for call to 'asuint16'}}
33+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: could not match 'vector<T, N>' against 'float'}}
34+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float]: no type named 'Type'}}
35+
}
36+
37+
int16_t4 test_asuint16_vector_int(int4 p1)
38+
{
39+
return asuint16(p1);
40+
// expected-error@-1 {{no matching function for call to 'asuint16'}}
41+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = int, N = 4]: no type named 'Type'}}
42+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = int4]: no type named 'Type'}}
43+
}
44+
45+
int16_t4 test_asuint16_vector_float(float4 p1)
46+
{
47+
return asuint16(p1);
48+
// expected-error@-1 {{no matching function for call to 'asuint16'}}
49+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float, N = 4]: no type named 'Type'}}
50+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float4]: no type named 'Type'}}
51+
}
52+

0 commit comments

Comments
 (0)