Skip to content

Commit df5137e

Browse files
committed
[HLSL] add pow library function
This change exposes the pow library function for HLSL, only available for floating point types. The pow function is supported for all scalar, vector, and matrix types that contain floating point types. The full documentation of the HLSL pow function is available here: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-pow Reviewed By: bogner Differential Revision: https://reviews.llvm.org/D156178
1 parent 211c975 commit df5137e

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,5 +532,36 @@ __attribute__((clang_builtin_alias(__builtin_elementwise_bitreverse)))
532532
uint64_t3 reversebits(uint64_t3);
533533
__attribute__((clang_builtin_alias(__builtin_elementwise_bitreverse)))
534534
uint64_t4 reversebits(uint64_t4);
535+
536+
// pow builtins
537+
#ifdef __HLSL_ENABLE_16_BIT
538+
__attribute__((clang_builtin_alias(__builtin_elementwise_pow)))
539+
half pow(half, half);
540+
__attribute__((clang_builtin_alias(__builtin_elementwise_pow)))
541+
half2 pow(half2, half2);
542+
__attribute__((clang_builtin_alias(__builtin_elementwise_pow)))
543+
half3 pow(half3, half3);
544+
__attribute__((clang_builtin_alias(__builtin_elementwise_pow)))
545+
half4 pow(half4, half4);
546+
#endif
547+
548+
__attribute__((clang_builtin_alias(__builtin_elementwise_pow))) float
549+
pow(float, float);
550+
__attribute__((clang_builtin_alias(__builtin_elementwise_pow)))
551+
float2 pow(float2, float2);
552+
__attribute__((clang_builtin_alias(__builtin_elementwise_pow)))
553+
float3 pow(float3, float3);
554+
__attribute__((clang_builtin_alias(__builtin_elementwise_pow)))
555+
float4 pow(float4, float4);
556+
557+
__attribute__((clang_builtin_alias(__builtin_elementwise_pow))) double
558+
pow(double, double);
559+
__attribute__((clang_builtin_alias(__builtin_elementwise_pow)))
560+
double2 pow(double2, double2);
561+
__attribute__((clang_builtin_alias(__builtin_elementwise_pow)))
562+
double3 pow(double3, double3);
563+
__attribute__((clang_builtin_alias(__builtin_elementwise_pow)))
564+
double4 pow(double4, double4);
565+
535566
} // namespace hlsl
536567
#endif //_HLSL_HLSL_INTRINSICS_H_
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
2+
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
3+
// RUN: -emit-llvm -disable-llvm-passes -O3 -o - | FileCheck %s
4+
// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
5+
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
6+
// RUN: -D__HLSL_ENABLE_16_BIT -o - | FileCheck %s --check-prefix=NO_HALF
7+
8+
// CHECK: define noundef half @
9+
// CHECK: call half @llvm.pow.f16(
10+
// NO_HALF: define noundef float @"?test_pow_half@@YA$halff@$halff@0@Z"(
11+
// NO_HALF: call float @llvm.pow.f32(
12+
half test_pow_half(half p0, half p1)
13+
{
14+
return pow(p0, p1);
15+
}
16+
// CHECK: define noundef <2 x half> @"?test_pow_half2@@YAT?$__vector@$f16@$01@__clang@@T12@0@Z"(
17+
// CHECK: call <2 x half> @llvm.pow.v2f16
18+
// NO_HALF: define noundef <2 x float> @"?test_pow_float2@@YAT?$__vector@M$01@__clang@@T12@0@Z"(
19+
// NO_HALF: call <2 x float> @llvm.pow.v2f32(
20+
half2 test_pow_half2(half2 p0, half2 p1)
21+
{
22+
return pow(p0, p1);
23+
}
24+
// CHECK: define noundef <3 x half> @"?test_pow_half3@@YAT?$__vector@$f16@$02@__clang@@T12@0@Z"(
25+
// CHECK: call <3 x half> @llvm.pow.v3f16
26+
// NO_HALF: define noundef <3 x float> @"?test_pow_float3@@YAT?$__vector@M$02@__clang@@T12@0@Z"(
27+
// NO_HALF: call <3 x float> @llvm.pow.v3f32(
28+
half3 test_pow_half3(half3 p0, half3 p1)
29+
{
30+
return pow(p0, p1);
31+
}
32+
// CHECK: define noundef <4 x half> @"?test_pow_half4@@YAT?$__vector@$f16@$03@__clang@@T12@0@Z"(
33+
// CHECK: call <4 x half> @llvm.pow.v4f16
34+
// NO_HALF: define noundef <4 x float> @"?test_pow_float4@@YAT?$__vector@M$03@__clang@@T12@0@Z"(
35+
// NO_HALF: call <4 x float> @llvm.pow.v4f32(
36+
half4 test_pow_half4(half4 p0, half4 p1)
37+
{
38+
return pow(p0, p1);
39+
}
40+
41+
// CHECK: define noundef float @"?test_pow_float@@YAMMM@Z"(
42+
// CHECK: call float @llvm.pow.f32(
43+
float test_pow_float(float p0, float p1)
44+
{
45+
return pow(p0, p1);
46+
}
47+
// CHECK: define noundef <2 x float> @"?test_pow_float2@@YAT?$__vector@M$01@__clang@@T12@0@Z"(
48+
// CHECK: call <2 x float> @llvm.pow.v2f32
49+
float2 test_pow_float2(float2 p0, float2 p1)
50+
{
51+
return pow(p0, p1);
52+
}
53+
// CHECK: define noundef <3 x float> @"?test_pow_float3@@YAT?$__vector@M$02@__clang@@T12@0@Z"(
54+
// CHECK: call <3 x float> @llvm.pow.v3f32
55+
float3 test_pow_float3(float3 p0, float3 p1)
56+
{
57+
return pow(p0, p1);
58+
}
59+
// CHECK: define noundef <4 x float> @"?test_pow_float4@@YAT?$__vector@M$03@__clang@@T12@0@Z"(
60+
// CHECK: call <4 x float> @llvm.pow.v4f32
61+
float4 test_pow_float4(float4 p0, float4 p1)
62+
{
63+
return pow(p0, p1);
64+
}
65+
66+
// CHECK: define noundef double @"?test_pow_double@@YANNN@Z"(
67+
// CHECK: call double @llvm.pow.f64(
68+
double test_pow_double(double p0, double p1)
69+
{
70+
return pow(p0, p1);
71+
}
72+
// CHECK: define noundef <2 x double> @"?test_pow_double2@@YAT?$__vector@N$01@__clang@@T12@0@Z"(
73+
// CHECK: call <2 x double> @llvm.pow.v2f64
74+
double2 test_pow_double2(double2 p0, double2 p1)
75+
{
76+
return pow(p0, p1);
77+
}
78+
// CHECK: define noundef <3 x double> @"?test_pow_double3@@YAT?$__vector@N$02@__clang@@T12@0@Z"(
79+
// CHECK: call <3 x double> @llvm.pow.v3f64
80+
double3 test_pow_double3(double3 p0, double3 p1)
81+
{
82+
return pow(p0, p1);
83+
}
84+
// CHECK: define noundef <4 x double> @"?test_pow_double4@@YAT?$__vector@N$03@__clang@@T12@0@Z"(
85+
// CHECK: call <4 x double> @llvm.pow.v4f64
86+
double4 test_pow_double4(double4 p0, double4 p1)
87+
{
88+
return pow(p0, p1);
89+
}

0 commit comments

Comments
 (0)