Skip to content

Commit

Permalink
Implementing asfloat using bit_cast (llvm#108686)
Browse files Browse the repository at this point in the history
This PR is implementing `asfloat` for HLSL.

Fixes: llvm#70098

Co-authored-by: Joao Saffran <[email protected]>
  • Loading branch information
joaosaffran and joaosaffran-zz authored Sep 16, 2024
1 parent e5271fe commit 1bfc3d0
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
17 changes: 17 additions & 0 deletions clang/lib/Headers/hlsl/hlsl_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,23 @@ bool any(double3);
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_any)
bool any(double4);

//===----------------------------------------------------------------------===//
// asfloat builtins
//===----------------------------------------------------------------------===//

/// \fn float asfloat(T Val)
/// \brief Interprets the bit pattern of x as float point number.
/// \param Val The input value.

template <typename T, int N>
_HLSL_INLINE vector<float, N> asfloat(vector<T, N> V) {
return __detail::bit_cast<float, T, N>(V);
}

template <typename T> _HLSL_INLINE float asfloat(T F) {
return __detail::bit_cast<float, T>(F);
}

//===----------------------------------------------------------------------===//
// asin builtins
//===----------------------------------------------------------------------===//
Expand Down
40 changes: 40 additions & 0 deletions clang/test/CodeGenHLSL/builtins/asfloat.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -fnative-half-type -emit-llvm -O1 -o - | FileCheck %s

// CHECK: define {{.*}}test_uint{{.*}}(i32 {{.*}} [[VAL:%.*]]){{.*}}
// CHECK: bitcast i32 [[VAL]] to float
float test_uint(uint p0) {
return asfloat(p0);
}

// CHECK: define {{.*}}test_int{{.*}}(i32 {{.*}} [[VAL:%.*]]){{.*}}
// CHECK: bitcast i32 [[VAL]] to float
float test_int(int p0) {
return asfloat(p0);
}

// CHECK: define {{.*}}test_float{{.*}}(float {{.*}} [[VAL:%.*]]){{.*}}
// CHECK-NOT: bitcast
// CHECK: ret float [[VAL]]
float test_float(float p0) {
return asfloat(p0);
}

// CHECK: define {{.*}}test_vector_uint{{.*}}(<4 x i32> {{.*}} [[VAL:%.*]]){{.*}}
// CHECK: bitcast <4 x i32> [[VAL]] to <4 x float>

float4 test_vector_uint(uint4 p0) {
return asfloat(p0);
}

// CHECK: define {{.*}}test_vector_int{{.*}}(<4 x i32> {{.*}} [[VAL:%.*]]){{.*}}
// CHECK: bitcast <4 x i32> [[VAL]] to <4 x float>
float4 test_vector_int(int4 p0) {
return asfloat(p0);
}

// CHECK: define {{.*}}test_vector_float{{.*}}(<4 x float> {{.*}} [[VAL:%.*]]){{.*}}
// CHECK-NOT: bitcast
// CHECK: ret <4 x float> [[VAL]]
float4 test_vector_float(float4 p0) {
return asfloat(p0);
}
35 changes: 35 additions & 0 deletions clang/test/SemaHLSL/BuiltIns/asfloat-errors.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -verify


float4 test_float_too_many_arg(float p0, float p1) {
return asfloat(p0, p1);
// expected-error@-1 {{no matching function for call to 'asfloat'}}
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'V', but 2 arguments were provided}}
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function template not viable: requires single argument 'F', but 2 arguments were provided}}
}


float test_float_double(double p1) {
return asfloat(p1);
// expected-error@hlsl/hlsl_intrinsics.h:* {{no matching function for call to 'bit_cast'}}
// expected-note@-2 {{in instantiation of function template specialization 'hlsl::asfloat<double>'}}
// expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: could not match 'vector<double, N>' against 'double'}}
// expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: substitution failure [with U = float, T = double]: no type named 'Type'}}
}

float test_float_half(half p1) {
return asfloat(p1);
// expected-error@hlsl/hlsl_intrinsics.h:* {{no matching function for call to 'bit_cast'}}
// expected-note@-2 {{in instantiation of function template specialization 'hlsl::asfloat<half>'}}
// expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: could not match 'vector<half, N>' against 'half'}}
// expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: substitution failure [with U = float, T = half]: no type named 'Type'}}
}


float test_float_half(bool p1) {
return asfloat(p1);
// expected-error@hlsl/hlsl_intrinsics.h:* {{no matching function for call to 'bit_cast'}}
// expected-note@-2 {{in instantiation of function template specialization 'hlsl::asfloat<bool>'}}
// expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: could not match 'vector<bool, N>' against 'bool'}}
// expected-note@hlsl/hlsl_detail.h:* {{candidate template ignored: substitution failure [with U = float, T = bool]: no type named 'Type'}}
}

0 comments on commit 1bfc3d0

Please sign in to comment.