Skip to content

Commit f7cee38

Browse files
committed
finished implementation, working on fmod.hlsl tests
1 parent 73ec3fa commit f7cee38

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

clang/lib/Headers/hlsl/hlsl_detail.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,23 @@ template <typename T> struct is_arithmetic {
4545
static const bool Value = __is_arithmetic(T);
4646
};
4747

48+
template <typename T>
49+
constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
50+
fmod_impl(T X, T Y) {
51+
return __builtin_elementwise_fmod(X, Y);
52+
}
53+
4854
template <typename T, int N>
4955
constexpr enable_if_t<is_same<float, T>::value || is_same<half, T>::value, T>
5056
fmod_vec_impl(vector<T, N> X, vector<T, N> Y) {
5157
#if !defined(__DirectX__)
5258
return __builtin_elementwise_fmod(X, Y);
5359
#else
5460
vector<T, N> div = X / Y;
55-
vector<T, N> result = __builtin_hlsl_elementwise_frac(__builtin_elementwise_abs(div)) * Y;
56-
vector<bool, N> condition = (div >= -div);
57-
vector<T, N> realResult = __builtin_hlsl_select(condition, result, -result);
58-
return realResult;
61+
vector<bool, N> ge = div >= -div;
62+
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;
5965
#endif
6066
}
6167

clang/test/CodeGenHLSL/builtins/fmod.hlsl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,42 +36,42 @@
3636

3737

3838
// CHECK: define [[FNATTRS]] [[TYPE]] @
39-
// CHECK: %fmod = frem reassoc nnan ninf nsz arcp afn [[TYPE]]
40-
// CHECK: ret [[TYPE]] %fmod
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
4141
half test_fmod_half(half p0, half p1) { return fmod(p0, p1); }
4242

4343
// CHECK: define [[FNATTRS]] <2 x [[TYPE]]> @
44-
// CHECK: %fmod = frem reassoc nnan ninf nsz arcp afn <2 x [[TYPE]]>
45-
// CHECK: ret <2 x [[TYPE]]> %fmod
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
4646
half2 test_fmod_half2(half2 p0, half2 p1) { return fmod(p0, p1); }
4747

4848
// CHECK: define [[FNATTRS]] <3 x [[TYPE]]> @
49-
// CHECK: %fmod = frem reassoc nnan ninf nsz arcp afn <3 x [[TYPE]]>
50-
// CHECK: ret <3 x [[TYPE]]> %fmod
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
5151
half3 test_fmod_half3(half3 p0, half3 p1) { return fmod(p0, p1); }
5252

5353
// CHECK: define [[FNATTRS]] <4 x [[TYPE]]> @
54-
// CHECK: %fmod = frem reassoc nnan ninf nsz arcp afn <4 x [[TYPE]]>
55-
// CHECK: ret <4 x [[TYPE]]> %fmod
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
5656
half4 test_fmod_half4(half4 p0, half4 p1) { return fmod(p0, p1); }
5757

5858
// CHECK: define [[FNATTRS]] float @
59-
// CHECK: %fmod = frem reassoc nnan ninf nsz arcp afn float
60-
// CHECK: ret float %fmod
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
6161
float test_fmod_float(float p0, float p1) { return fmod(p0, p1); }
6262

6363
// CHECK: define [[FNATTRS]] <2 x float> @
64-
// CHECK: %fmod = frem reassoc nnan ninf nsz arcp afn <2 x float>
65-
// CHECK: ret <2 x float> %fmod
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
6666
float2 test_fmod_float2(float2 p0, float2 p1) { return fmod(p0, p1); }
6767

6868
// CHECK: define [[FNATTRS]] <3 x float> @
69-
// CHECK: %fmod = frem reassoc nnan ninf nsz arcp afn <3 x float>
70-
// CHECK: ret <3 x float> %fmod
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
7171
float3 test_fmod_float3(float3 p0, float3 p1) { return fmod(p0, p1); }
7272

7373
// CHECK: define [[FNATTRS]] <4 x float> @
74-
// CHECK: %fmod = frem reassoc nnan ninf nsz arcp afn <4 x float>
75-
// CHECK: ret <4 x float> %fmod
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
7676
float4 test_fmod_float4(float4 p0, float4 p1) { return fmod(p0, p1); }
7777

0 commit comments

Comments
 (0)