Skip to content

Commit 74a523f

Browse files
committed
Fix constant folding of frac for negative values
The DXIL constant folder evaluated Frc as fabs(modf(x)), which is |x - trunc(x)|, so frac(-1.75) folded to 0.75 instead of 0.25. Scalars are folded earlier with x - floor(x), but matrix arguments reach the DXIL folder. Use x - floor(x) there as well. The existing frac_literal.hlsl expectations for the negative matrix elements encoded the old result; update them and add the case from the issue. Fixes #8937
1 parent 5cf9f59 commit 74a523f

3 files changed

Lines changed: 12 additions & 5 deletions

File tree

‎docs/ReleaseNotes.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ line upon naming the release. Refer to previous for appropriate section names.
6767

6868
- Fixed an optimizer crash when scalarizing an out-of-bounds vector access
6969
[#8940](https://github.com/microsoft/DirectXShaderCompiler/issues/8940).
70+
- Fixed constant folding of `frac` returning `abs(x - trunc(x))` instead of
71+
`x - floor(x)` for negative values, such as matrix arguments
72+
[#8937](https://github.com/microsoft/DirectXShaderCompiler/issues/8937).
7073

7174
### Upcoming Preview Release
7275

‎lib/Analysis/DxilConstantFolding.cpp‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,9 @@ static Constant *ConstantFoldUnaryFPIntrinsic(OP::OpCode opcode, Type *Ty,
192192
case OP::OpCode::Exp:
193193
return DxilConstantFoldFP(exp2, Op, Ty);
194194
case OP::OpCode::Frc: {
195-
NativeFPUnaryOp f = [](double x) {
196-
double unused;
197-
return fabs(modf(x, &unused));
198-
};
195+
// frac(x) = x - floor(x), so negative inputs also give a result in
196+
// [+0, 1), the same as the front end's folding of the frac intrinsic
197+
NativeFPUnaryOp f = [](double x) { return x - floor(x); };
199198
return DxilConstantFoldFP(f, Op, Ty);
200199
}
201200
case OP::OpCode::Log:

‎tools/clang/test/HLSLFileCheck/hlsl/intrinsics/basic/frac_literal.hlsl‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ void main() {
5656
f4buf[2] = float4(fmat_splat[0][0], fmat_splat[0][1], fmat_splat[1][0], fmat_splat[1][1]) ;
5757

5858
// matrix non-splat
59-
// CHECK: float 0x3F2A400000000000, float 0x3F53A92A40000000, float 0x3FCD710000000000, float 0.000000e+00
59+
// CHECK: float 0x3FEFFE5C00000000, float 0x3FEFF62B60000000, float 0x3FCD710000000000, float 0.000000e+00
6060
float2x2 fmat = { -21.0002, -0.0012, 421.23, 1 };
6161
fmat = frac(fmat);
6262
f4buf[3] = float4(fmat[0][0], fmat[0][1], fmat[1][0], fmat[1][1]) ;
63+
64+
// matrix with negative values: frac(x) = x - floor(x)
65+
// CHECK: float 2.500000e-01, float 7.500000e-01, float 7.500000e-01, float 2.500000e-01
66+
float2x2 fmat_neg = frac(float2x2(-1.75, -0.25, 0.75, 1.25));
67+
f4buf[4] = float4(fmat_neg[0][0], fmat_neg[0][1], fmat_neg[1][0], fmat_neg[1][1]);
6368
}

0 commit comments

Comments
 (0)