Skip to content

Commit 32e71ea

Browse files
implementing asfloat using bit_cast
1 parent 947374c commit 32e71ea

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,23 @@ bool any(double3);
361361
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_any)
362362
bool any(double4);
363363

364+
//===----------------------------------------------------------------------===//
365+
// asfloat builtins
366+
//===----------------------------------------------------------------------===//
367+
368+
/// \fn float asfloat(T Val)
369+
/// \brief Interprets the bit pattern of x as float point number.
370+
/// \param Val The input value.
371+
372+
template <typename T, int N>
373+
_HLSL_INLINE vector<float, N> asfloat(vector<T, N> V) {
374+
return __detail::bit_cast<float, T, N>(V);
375+
}
376+
377+
template <typename T> _HLSL_INLINE float asfloat(T F) {
378+
return __detail::bit_cast<float, T>(F);
379+
}
380+
364381
//===----------------------------------------------------------------------===//
365382
// asin builtins
366383
//===----------------------------------------------------------------------===//
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -fnative-half-type -emit-llvm -O1 -o - | FileCheck %s
2+
3+
// CHECK: define {{.*}}test_uint{{.*}}(i32 {{.*}} [[VAL:%.*]]){{.*}}
4+
// CHECK: bitcast i32 [[VAL]] to float
5+
float test_uint(uint p0) {
6+
return asfloat(p0);
7+
}
8+
9+
// CHECK: define {{.*}}test_int{{.*}}(i32 {{.*}} [[VAL:%.*]]){{.*}}
10+
// CHECK: bitcast i32 [[VAL]] to float
11+
float test_int(int p0) {
12+
return asfloat(p0);
13+
}
14+
15+
// CHECK: define {{.*}}test_float{{.*}}(float {{.*}} [[VAL:%.*]]){{.*}}
16+
// CHECK-NOT: bitcast
17+
// CHECK: ret float [[VAL]]
18+
float test_float(float p0) {
19+
return asfloat(p0);
20+
}
21+
22+
// CHECK: define {{.*}}test_vector_uint{{.*}}(<4 x i32> {{.*}} [[VAL:%.*]]){{.*}}
23+
// CHECK: bitcast <4 x i32> [[VAL]] to <4 x float>
24+
25+
float4 test_vector_uint(uint4 p0) {
26+
return asfloat(p0);
27+
}
28+
29+
// CHECK: define {{.*}}test_vector_int{{.*}}(<4 x i32> {{.*}} [[VAL:%.*]]){{.*}}
30+
// CHECK: bitcast <4 x i32> [[VAL]] to <4 x float>
31+
float4 test_vector_int(int4 p0) {
32+
return asfloat(p0);
33+
}
34+
35+
// CHECK: define {{.*}}test_vector_float{{.*}}(<4 x float> {{.*}} [[VAL:%.*]]){{.*}}
36+
// CHECK-NOT: bitcast
37+
// CHECK: ret <4 x float> [[VAL]]
38+
float4 test_vector_float(float4 p0) {
39+
return asfloat(p0);
40+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -verify
2+
3+
4+
float4 test_float_too_many_arg(float p0, float p1) {
5+
return asfloat(p0, p1);
6+
// expected-error@-1 {{no matching function for call to 'asfloat'}}
7+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'V', but 2 arguments were provided}}
8+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'F', but 2 arguments were provided}}
9+
}
10+
11+
12+
float test_float_double(double p1) {
13+
return asfloat(p1);
14+
// expected-error@hlsl/hlsl_intrinsics.h:* {{no matching function for call to 'bit_cast'}}
15+
// expected-note@-2 {{in instantiation of function template specialization 'hlsl::asfloat<double>'}}
16+
// expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: could not match 'vector<double, N>' against 'double'}}
17+
// expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: substitution failure [with U = float, T = double]: no type named 'Type'}}
18+
}
19+
20+
float test_float_half(half p1) {
21+
return asfloat(p1);
22+
// expected-error@hlsl/hlsl_intrinsics.h:* {{no matching function for call to 'bit_cast'}}
23+
// expected-note@-2 {{in instantiation of function template specialization 'hlsl::asfloat<half>'}}
24+
// expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: could not match 'vector<half, N>' against 'half'}}
25+
// expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: substitution failure [with U = float, T = half]: no type named 'Type'}}
26+
}
27+
28+
29+
float test_float_half(bool p1) {
30+
return asfloat(p1);
31+
// expected-error@hlsl/hlsl_intrinsics.h:* {{no matching function for call to 'bit_cast'}}
32+
// expected-note@-2 {{in instantiation of function template specialization 'hlsl::asfloat<bool>'}}
33+
// expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: could not match 'vector<bool, N>' against 'bool'}}
34+
// expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: substitution failure [with U = float, T = bool]: no type named 'Type'}}
35+
}

0 commit comments

Comments
 (0)