Skip to content

Commit d11df05

Browse files
authored
[HLSL] Implement the ldexp intrinsic (#138182)
Closes #99133. Implemented `ldexp` entirely in `hlsl_intrinsics.h` and `hlsl_intrinsic_helpers.h`, added coresponding tests in `clang/test/CodeGenHLSL/builtins/ldexp.hlsl` and `clang/test/SemaHLSL/BuiltIns/ldexp-errors.hlsl`.
1 parent 79210fe commit d11df05

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h

+4
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ template <typename T> constexpr T faceforward_impl(T N, T I, T Ng) {
134134
#endif
135135
}
136136

137+
template <typename T> constexpr T ldexp_impl(T X, T Exp) {
138+
return exp2(Exp) * X;
139+
}
140+
137141
} // namespace __detail
138142
} // namespace hlsl
139143

clang/lib/Headers/hlsl/hlsl_intrinsics.h

+42
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,48 @@ fmod(__detail::HLSL_FIXED_VECTOR<float, N> X,
303303
return __detail::fmod_vec_impl(X, Y);
304304
}
305305

306+
//===----------------------------------------------------------------------===//
307+
// ldexp builtins
308+
//===----------------------------------------------------------------------===//
309+
310+
/// \fn T ldexp(T X, T Exp)
311+
/// \brief Returns the result of multiplying the specified value by two raised
312+
/// to the power of the specified exponent.
313+
/// \param X [in] The specified value.
314+
/// \param Exp [in] The specified exponent.
315+
///
316+
/// This function uses the following formula: X * 2^Exp
317+
318+
template <typename T>
319+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
320+
const inline __detail::enable_if_t<__detail::is_arithmetic<T>::Value &&
321+
__detail::is_same<half, T>::value,
322+
T> ldexp(T X, T Exp) {
323+
return __detail::ldexp_impl(X, Exp);
324+
}
325+
326+
template <typename T>
327+
const inline __detail::enable_if_t<
328+
__detail::is_arithmetic<T>::Value && __detail::is_same<float, T>::value, T>
329+
ldexp(T X, T Exp) {
330+
return __detail::ldexp_impl(X, Exp);
331+
}
332+
333+
template <int N>
334+
_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
335+
const inline __detail::HLSL_FIXED_VECTOR<half, N> ldexp(
336+
__detail::HLSL_FIXED_VECTOR<half, N> X,
337+
__detail::HLSL_FIXED_VECTOR<half, N> Exp) {
338+
return __detail::ldexp_impl(X, Exp);
339+
}
340+
341+
template <int N>
342+
const inline __detail::HLSL_FIXED_VECTOR<float, N>
343+
ldexp(__detail::HLSL_FIXED_VECTOR<float, N> X,
344+
__detail::HLSL_FIXED_VECTOR<float, N> Exp) {
345+
return __detail::ldexp_impl(X, Exp);
346+
}
347+
306348
//===----------------------------------------------------------------------===//
307349
// length builtins
308350
//===----------------------------------------------------------------------===//
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -fnative-half-type -emit-llvm -disable-llvm-passes -o - | FileCheck %s
2+
3+
// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) half @_ZN4hlsl8__detail10ldexp_implIDhEET_S2_S2_
4+
// CHECK: %elt.exp2 = call reassoc nnan ninf nsz arcp afn half @llvm.exp2.f16(half %{{.*}})
5+
// CHECK: %mul = fmul reassoc nnan ninf nsz arcp afn half %elt.exp2, %{{.*}}
6+
// CHECK: ret half %mul
7+
half test_ldexp_half(half X, half Exp) { return ldexp(X, Exp); }
8+
9+
// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <2 x half> @_ZN4hlsl8__detail10ldexp_implIDv2_DhEET_S3_S3_
10+
// CHECK: %elt.exp2 = call reassoc nnan ninf nsz arcp afn <2 x half> @llvm.exp2.v2f16(<2 x half> %{{.*}})
11+
// CHECK: %mul = fmul reassoc nnan ninf nsz arcp afn <2 x half> %elt.exp2, %{{.*}}
12+
// CHECK: ret <2 x half> %mul
13+
half2 test_ldexp_half2(half2 X, half2 Exp) { return ldexp(X, Exp); }
14+
15+
// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <3 x half> @_ZN4hlsl8__detail10ldexp_implIDv3_DhEET_S3_S3_
16+
// CHECK: %elt.exp2 = call reassoc nnan ninf nsz arcp afn <3 x half> @llvm.exp2.v3f16(<3 x half> %{{.*}})
17+
// CHECK: %mul = fmul reassoc nnan ninf nsz arcp afn <3 x half> %elt.exp2, %{{.*}}
18+
// CHECK: ret <3 x half> %mul
19+
half3 test_ldexp_half3(half3 X, half3 Exp) { return ldexp(X, Exp); }
20+
21+
// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x half> @_ZN4hlsl8__detail10ldexp_implIDv4_DhEET_S3_S3_
22+
// CHECK: %elt.exp2 = call reassoc nnan ninf nsz arcp afn <4 x half> @llvm.exp2.v4f16(<4 x half> %{{.*}})
23+
// CHECK: %mul = fmul reassoc nnan ninf nsz arcp afn <4 x half> %elt.exp2, %{{.*}}
24+
// CHECK: ret <4 x half> %mul
25+
half4 test_ldexp_half4(half4 X, half4 Exp) { return ldexp(X, Exp); }
26+
27+
// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) float @_ZN4hlsl8__detail10ldexp_implIfEET_S2_S2_
28+
// CHECK: %elt.exp2 = call reassoc nnan ninf nsz arcp afn float @llvm.exp2.f32(float %{{.*}})
29+
// CHECK: %mul = fmul reassoc nnan ninf nsz arcp afn float %elt.exp2, %{{.*}}
30+
// CHECK: ret float %mul
31+
float test_ldexp_float(float X, float Exp) { return ldexp(X, Exp); }
32+
33+
// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <2 x float> @_ZN4hlsl8__detail10ldexp_implIDv2_fEET_S3_S3_
34+
// CHECK: %elt.exp2 = call reassoc nnan ninf nsz arcp afn <2 x float> @llvm.exp2.v2f32(<2 x float> %{{.*}})
35+
// CHECK: %mul = fmul reassoc nnan ninf nsz arcp afn <2 x float> %elt.exp2, %{{.*}}
36+
// CHECK: ret <2 x float> %mul
37+
float2 test_ldexp_float2(float2 X, float2 Exp) { return ldexp(X, Exp); }
38+
39+
// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <3 x float> @_ZN4hlsl8__detail10ldexp_implIDv3_fEET_S3_S3_
40+
// CHECK: %elt.exp2 = call reassoc nnan ninf nsz arcp afn <3 x float> @llvm.exp2.v3f32(<3 x float> %{{.*}})
41+
// CHECK: %mul = fmul reassoc nnan ninf nsz arcp afn <3 x float> %elt.exp2, %{{.*}}
42+
// CHECK: ret <3 x float> %mul
43+
float3 test_ldexp_float3(float3 X, float3 Exp) { return ldexp(X, Exp); }
44+
45+
// CHECK-LABEL: define linkonce_odr noundef nofpclass(nan inf) <4 x float> @_ZN4hlsl8__detail10ldexp_implIDv4_fEET_S3_S3_
46+
// CHECK: %elt.exp2 = call reassoc nnan ninf nsz arcp afn <4 x float> @llvm.exp2.v4f32(<4 x float> %{{.*}})
47+
// CHECK: %mul = fmul reassoc nnan ninf nsz arcp afn <4 x float> %elt.exp2, %{{.*}}
48+
// CHECK: ret <4 x float> %mul
49+
float4 test_ldexp_float4(float4 X, float4 Exp) { return ldexp(X, Exp); }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify
2+
3+
float test_double_inputs(double p0, double p1) {
4+
return ldexp(p0, p1);
5+
// expected-error@-1 {{no matching function for call to 'ldexp'}}
6+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored}}
7+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored}}
8+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored}}
9+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored}}
10+
}
11+
12+
float test_int_inputs(int p0, int p1, int p2) {
13+
return ldexp(p0, p1);
14+
// expected-error@-1 {{no matching function for call to 'ldexp'}}
15+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored}}
16+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored}}
17+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored}}
18+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored}}
19+
}
20+
21+
float1 test_vec1_inputs(float1 p0, float1 p1) {
22+
return ldexp(p0, p1);
23+
// expected-error@-1 {{no matching function for call to 'ldexp'}}
24+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float1]: no type named 'Type' in 'hlsl::__detail::enable_if<false, vector<float, 1>>'}}
25+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float1]: no type named 'Type' in 'hlsl::__detail::enable_if<false, vector<float, 1>>'}}
26+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with N = 1]: no type named 'Type' in 'hlsl::__detail::enable_if<false, half>'}}
27+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with N = 1]: no type named 'Type' in 'hlsl::__detail::enable_if<false, float>'}}
28+
}
29+
30+
typedef float float5 __attribute__((ext_vector_type(5)));
31+
32+
float5 test_vec5_inputs(float5 p0, float5 p1) {
33+
return ldexp(p0, p1);
34+
// expected-error@-1 {{no matching function for call to 'ldexp'}}
35+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float5]: no type named 'Type' in 'hlsl::__detail::enable_if<false, vector<float, 5>>'}}
36+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with T = float5]: no type named 'Type' in 'hlsl::__detail::enable_if<false, vector<float, 5>>'}}
37+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with N = 5]: no type named 'Type' in 'hlsl::__detail::enable_if<false, half>'}}
38+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate template ignored: substitution failure [with N = 5]: no type named 'Type' in 'hlsl::__detail::enable_if<false, float>'}}
39+
}

0 commit comments

Comments
 (0)