Skip to content

Commit 67cdd8c

Browse files
committed
tweaked implementation to exclude vec1 & restrict to vec4, created new fmod-directx.hlsl test file, removed directx tests from fmod.hlsl
1 parent f7cee38 commit 67cdd8c

File tree

3 files changed

+84
-38
lines changed

3 files changed

+84
-38
lines changed

clang/lib/Headers/hlsl/hlsl_detail.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,25 @@ template <typename T> struct is_arithmetic {
4848
template <typename T>
4949
constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
5050
fmod_impl(T X, T Y) {
51+
#if !defined(__DirectX__)
5152
return __builtin_elementwise_fmod(X, Y);
53+
#else
54+
T div = X / Y;
55+
bool ge = div >= -div;
56+
T frc = __builtin_hlsl_elementwise_frac(__builtin_elementwise_abs(div));
57+
return __builtin_hlsl_select(ge, frc, -frc) * Y;
58+
#endif
5259
}
5360

5461
template <typename T, int N>
55-
constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
56-
fmod_vec_impl(vector<T, N> X, vector<T, N> Y) {
62+
constexpr vector<T, N> fmod_vec_impl(vector<T, N> X, vector<T, N> Y) {
5763
#if !defined(__DirectX__)
5864
return __builtin_elementwise_fmod(X, Y);
5965
#else
6066
vector<T, N> div = X / Y;
6167
vector<bool, N> ge = div >= -div;
6268
vector<T, N> frc = __builtin_hlsl_elementwise_frac(__builtin_elementwise_abs(div));
63-
vector<T, N> realFrc = __builtin_hlsl_select(ge, frc, -frc);
64-
return realFrc * Y;
69+
return __builtin_hlsl_select(ge, frc, -frc) * Y;
6570
#endif
6671
}
6772

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// DirectX target:
2+
//
3+
// ---------- Native Half support test -----------
4+
//
5+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
6+
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
7+
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
8+
// RUN: -DFNATTRS="noundef nofpclass(nan inf)" -DTYPE=half
9+
10+
//
11+
// ---------- No Native Half support test -----------
12+
//
13+
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
14+
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
15+
// RUN: -o - | FileCheck %s \
16+
// RUN: -DFNATTRS="noundef nofpclass(nan inf)" -DTYPE=float
17+
18+
19+
20+
// CHECK: define [[FNATTRS]] [[TYPE]] @
21+
// CHECK: call reassoc nnan ninf nsz arcp afn [[FNATTRS]] [[TYPE]] @{{.*}}([[TYPE]] noundef nofpclass(nan inf) %{{.*}}, [[TYPE]] noundef nofpclass(nan inf) %{{.*}}) #{{.*}}
22+
// CHECK: ret [[TYPE]] %call
23+
half test_fmod_half(half p0, half p1) { return fmod(p0, p1); }
24+
25+
// CHECK: define [[FNATTRS]] <2 x [[TYPE]]> @
26+
// CHECK: call reassoc nnan ninf nsz arcp afn [[FNATTRS]] [[TYPE]] @{{.*}}(<2 x [[TYPE]]> noundef nofpclass(nan inf) %{{.*}}, <2 x [[TYPE]]> noundef nofpclass(nan inf) %{{.*}}) #{{.*}}
27+
// CHECK: ret <2 x [[TYPE]]> %splat.splat
28+
half2 test_fmod_half2(half2 p0, half2 p1) { return fmod(p0, p1); }
29+
30+
// CHECK: define [[FNATTRS]] <3 x [[TYPE]]> @
31+
// CHECK: call reassoc nnan ninf nsz arcp afn [[FNATTRS]] [[TYPE]] @{{.*}}(<3 x [[TYPE]]> noundef nofpclass(nan inf) %{{.*}}, <3 x [[TYPE]]> noundef nofpclass(nan inf) %{{.*}} #{{.*}}
32+
// CHECK: ret <3 x [[TYPE]]> %splat.splat
33+
half3 test_fmod_half3(half3 p0, half3 p1) { return fmod(p0, p1); }
34+
35+
// CHECK: define [[FNATTRS]] <4 x [[TYPE]]> @
36+
// CHECK: call reassoc nnan ninf nsz arcp afn [[FNATTRS]] [[TYPE]] @{{.*}}(<4 x [[TYPE]]> noundef nofpclass(nan inf) %{{.*}}, <4 x [[TYPE]]> noundef nofpclass(nan inf) %{{.*}}) #{{.*}}
37+
// CHECK: ret <4 x [[TYPE]]> %splat.splat
38+
half4 test_fmod_half4(half4 p0, half4 p1) { return fmod(p0, p1); }
39+
40+
// CHECK: define [[FNATTRS]] float @
41+
// CHECK: call reassoc nnan ninf nsz arcp afn [[FNATTRS]] float @{{.*}}(float noundef nofpclass(nan inf) %{{.*}}, float noundef nofpclass(nan inf) %{{.*}}) #{{.*}}
42+
// CHECK: ret float %call
43+
float test_fmod_float(float p0, float p1) { return fmod(p0, p1); }
44+
45+
// CHECK: define [[FNATTRS]] <2 x float> @
46+
// CHECK: call reassoc nnan ninf nsz arcp afn [[FNATTRS]] float @{{.*}}(<2 x float> noundef nofpclass(nan inf) %{{.*}}, <2 x float> noundef nofpclass(nan inf) %{{.*}}) #{{.*}}
47+
// CHECK: ret <2 x float> %splat.splat
48+
float2 test_fmod_float2(float2 p0, float2 p1) { return fmod(p0, p1); }
49+
50+
// CHECK: define [[FNATTRS]] <3 x float> @
51+
// CHECK: call reassoc nnan ninf nsz arcp afn [[FNATTRS]] float @{{.*}}(<3 x float> noundef nofpclass(nan inf) %{{.*}}, <3 x float> noundef nofpclass(nan inf) %{{.*}}) #{{.*}}
52+
// CHECK: ret <3 x float> %splat.splat
53+
float3 test_fmod_float3(float3 p0, float3 p1) { return fmod(p0, p1); }
54+
55+
// CHECK: define [[FNATTRS]] <4 x float> @
56+
// CHECK: call reassoc nnan ninf nsz arcp afn [[FNATTRS]] float @{{.*}}(<4 x float> noundef nofpclass(nan inf) %{{.*}}, <4 x float> noundef nofpclass(nan inf) %{{.*}}) #{{.*}}
57+
// CHECK: ret <4 x float> %splat.splat
58+
float4 test_fmod_float4(float4 p0, float4 p1) { return fmod(p0, p1); }
59+
Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
// DirectX target:
2-
//
3-
// ---------- Native Half support test -----------
4-
//
5-
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
6-
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
7-
// RUN: -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
8-
// RUN: -DFNATTRS="noundef nofpclass(nan inf)" -DTYPE=half
9-
10-
//
11-
// ---------- No Native Half support test -----------
12-
//
13-
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
14-
// RUN: dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
15-
// RUN: -o - | FileCheck %s \
16-
// RUN: -DFNATTRS="noundef nofpclass(nan inf)" -DTYPE=float
17-
18-
191
// Spirv target:
202
//
213
// ---------- Native Half support test -----------
@@ -36,42 +18,42 @@
3618

3719

3820
// CHECK: define [[FNATTRS]] [[TYPE]] @
39-
// CHECK: call reassoc nnan ninf nsz arcp afn [[FNATTRS]] [[TYPE]] @{{.*}}([[TYPE]] noundef nofpclass(nan inf) %{{.*}}, [[TYPE]] noundef nofpclass(nan inf) %{{.*}}) #{{.*}}
40-
// CHECK: ret [[TYPE]] %call
21+
// CHECK: %fmod = frem reassoc nnan ninf nsz arcp afn [[TYPE]]
22+
// CHECK: ret [[TYPE]] %fmod
4123
half test_fmod_half(half p0, half p1) { return fmod(p0, p1); }
4224

4325
// CHECK: define [[FNATTRS]] <2 x [[TYPE]]> @
44-
// CHECK: call reassoc nnan ninf nsz arcp afn [[FNATTRS]] [[TYPE]] @{{.*}}(<2 x [[TYPE]]> noundef nofpclass(nan inf) %{{.*}}, <2 x [[TYPE]]> noundef nofpclass(nan inf) %{{.*}}) #{{.*}}
45-
// CHECK: ret <2 x [[TYPE]]> %splat.splat
26+
// CHECK: %fmod = frem reassoc nnan ninf nsz arcp afn <2 x [[TYPE]]>
27+
// CHECK: ret <2 x [[TYPE]]> %fmod
4628
half2 test_fmod_half2(half2 p0, half2 p1) { return fmod(p0, p1); }
4729

4830
// CHECK: define [[FNATTRS]] <3 x [[TYPE]]> @
49-
// CHECK: call reassoc nnan ninf nsz arcp afn [[FNATTRS]] [[TYPE]] @{{.*}}(<3 x [[TYPE]]> noundef nofpclass(nan inf) %{{.*}}, <3 x [[TYPE]]> noundef nofpclass(nan inf) %{{.*}} #{{.*}}
50-
// CHECK: ret <3 x [[TYPE]]> %splat.splat
31+
// CHECK: %fmod = frem reassoc nnan ninf nsz arcp afn <3 x [[TYPE]]>
32+
// CHECK: ret <3 x [[TYPE]]> %fmod
5133
half3 test_fmod_half3(half3 p0, half3 p1) { return fmod(p0, p1); }
5234

5335
// CHECK: define [[FNATTRS]] <4 x [[TYPE]]> @
54-
// CHECK: call reassoc nnan ninf nsz arcp afn [[FNATTRS]] [[TYPE]] @{{.*}}(<4 x [[TYPE]]> noundef nofpclass(nan inf) %{{.*}}, <4 x [[TYPE]]> noundef nofpclass(nan inf) %{{.*}}) #{{.*}}
55-
// CHECK: ret <4 x [[TYPE]]> %splat.splat
36+
// CHECK: %fmod = frem reassoc nnan ninf nsz arcp afn <4 x [[TYPE]]>
37+
// CHECK: ret <4 x [[TYPE]]> %fmod
5638
half4 test_fmod_half4(half4 p0, half4 p1) { return fmod(p0, p1); }
5739

5840
// CHECK: define [[FNATTRS]] float @
59-
// CHECK: call reassoc nnan ninf nsz arcp afn [[FNATTRS]] float @{{.*}}(float noundef nofpclass(nan inf) %{{.*}}, float noundef nofpclass(nan inf) %{{.*}}) #{{.*}}
60-
// CHECK: ret float %call
41+
// CHECK: %fmod = frem reassoc nnan ninf nsz arcp afn float
42+
// CHECK: ret float %fmod
6143
float test_fmod_float(float p0, float p1) { return fmod(p0, p1); }
6244

6345
// CHECK: define [[FNATTRS]] <2 x float> @
64-
// CHECK: call reassoc nnan ninf nsz arcp afn [[FNATTRS]] float @{{.*}}(<2 x float> noundef nofpclass(nan inf) %{{.*}}, <2 x float> noundef nofpclass(nan inf) %{{.*}}) #{{.*}}
65-
// CHECK: ret <2 x float> %splat.splat
46+
// CHECK: %fmod = frem reassoc nnan ninf nsz arcp afn <2 x float>
47+
// CHECK: ret <2 x float> %fmod
6648
float2 test_fmod_float2(float2 p0, float2 p1) { return fmod(p0, p1); }
6749

6850
// CHECK: define [[FNATTRS]] <3 x float> @
69-
// CHECK: call reassoc nnan ninf nsz arcp afn [[FNATTRS]] float @{{.*}}(<3 x float> noundef nofpclass(nan inf) %{{.*}}, <3 x float> noundef nofpclass(nan inf) %{{.*}}) #{{.*}}
70-
// CHECK: ret <3 x float> %splat.splat
51+
// CHECK: %fmod = frem reassoc nnan ninf nsz arcp afn <3 x float>
52+
// CHECK: ret <3 x float> %fmod
7153
float3 test_fmod_float3(float3 p0, float3 p1) { return fmod(p0, p1); }
7254

7355
// CHECK: define [[FNATTRS]] <4 x float> @
74-
// CHECK: call reassoc nnan ninf nsz arcp afn [[FNATTRS]] float @{{.*}}(<4 x float> noundef nofpclass(nan inf) %{{.*}}, <4 x float> noundef nofpclass(nan inf) %{{.*}}) #{{.*}}
75-
// CHECK: ret <4 x float> %splat.splat
56+
// CHECK: %fmod = frem reassoc nnan ninf nsz arcp afn <4 x float>
57+
// CHECK: ret <4 x float> %fmod
7658
float4 test_fmod_float4(float4 p0, float4 p1) { return fmod(p0, p1); }
7759

0 commit comments

Comments
 (0)