Skip to content

Commit e037f4b

Browse files
authored
[naga wgsl-in] Implement unary negation during const evaluation for Literal::F64 (#7431)
This not being implemented made it impossible to express negative f64 literals. This is now fixed, and we've added a test.
1 parent 7e0e1e2 commit e037f4b

File tree

6 files changed

+29
-22
lines changed

6 files changed

+29
-22
lines changed

naga/src/proc/constant_evaluator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,7 @@ impl<'a> ConstantEvaluator<'a> {
19451945
Literal::I64(v) => Literal::I64(v.wrapping_neg()),
19461946
Literal::F32(v) => Literal::F32(-v),
19471947
Literal::F16(v) => Literal::F16(-v),
1948+
Literal::F64(v) => Literal::F64(-v),
19481949
Literal::AbstractInt(v) => Literal::AbstractInt(v.wrapping_neg()),
19491950
Literal::AbstractFloat(v) => Literal::AbstractFloat(-v),
19501951
_ => return Err(ConstantEvaluatorError::InvalidUnaryOpArg),

naga/tests/in/wgsl/f64.wgsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const k: f64 = 2.0lf;
44
fn f(x: f64) -> f64 {
55
let y: f64 = 3e1lf + 4.0e2lf;
66
var z = y + f64(5);
7+
var w = -1.0lf;
78
return x + y + k + 5.0lf;
89
}
910

naga/tests/out/glsl/f64.main.Compute.glsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const double k = 2.0LF;
77

88
double f(double x) {
99
double z = 0.0;
10+
double w = -1.0LF;
1011
double y = (30.0LF + 400.0LF);
1112
z = (y + 5.0LF);
1213
return (((x + y) + k) + 5.0LF);

naga/tests/out/hlsl/f64.hlsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ static double v = 1.0L;
55
double f(double x)
66
{
77
double z = (double)0;
8+
double w = -1.0L;
89

910
double y = (30.0L + 400.0L);
1011
z = (y + 5.0L);

naga/tests/out/spv/f64.spvasm

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
; SPIR-V
22
; Version: 1.0
33
; Generator: rspirv
4-
; Bound: 30
4+
; Bound: 32
55
OpCapability Shader
66
OpCapability Float64
77
%1 = OpExtInstImport "GLSL.std.450"
88
OpMemoryModel Logical GLSL450
9-
OpEntryPoint GLCompute %25 "main"
10-
OpExecutionMode %25 LocalSize 1 1 1
9+
OpEntryPoint GLCompute %27 "main"
10+
OpExecutionMode %27 LocalSize 1 1 1
1111
%2 = OpTypeVoid
1212
%3 = OpTypeFloat 64
1313
%4 = OpConstant %3 1.0
@@ -18,28 +18,30 @@ OpExecutionMode %25 LocalSize 1 1 1
1818
%12 = OpConstant %3 30.0
1919
%13 = OpConstant %3 400.0
2020
%14 = OpConstant %3 5.0
21-
%16 = OpTypePointer Function %3
22-
%17 = OpConstantNull %3
23-
%26 = OpTypeFunction %2
24-
%27 = OpConstant %3 6.0
21+
%15 = OpConstant %3 -1.0
22+
%17 = OpTypePointer Function %3
23+
%18 = OpConstantNull %3
24+
%28 = OpTypeFunction %2
25+
%29 = OpConstant %3 6.0
2526
%10 = OpFunction %3 None %11
2627
%9 = OpFunctionParameter %3
2728
%8 = OpLabel
28-
%15 = OpVariable %16 Function %17
29-
OpBranch %18
30-
%18 = OpLabel
31-
%19 = OpFAdd %3 %12 %13
32-
%20 = OpFAdd %3 %19 %14
33-
OpStore %15 %20
34-
%21 = OpFAdd %3 %9 %19
35-
%22 = OpFAdd %3 %21 %5
36-
%23 = OpFAdd %3 %22 %14
37-
OpReturnValue %23
29+
%16 = OpVariable %17 Function %18
30+
%19 = OpVariable %17 Function %15
31+
OpBranch %20
32+
%20 = OpLabel
33+
%21 = OpFAdd %3 %12 %13
34+
%22 = OpFAdd %3 %21 %14
35+
OpStore %16 %22
36+
%23 = OpFAdd %3 %9 %21
37+
%24 = OpFAdd %3 %23 %5
38+
%25 = OpFAdd %3 %24 %14
39+
OpReturnValue %25
3840
OpFunctionEnd
39-
%25 = OpFunction %2 None %26
40-
%24 = OpLabel
41-
OpBranch %28
42-
%28 = OpLabel
43-
%29 = OpFunctionCall %3 %10 %27
41+
%27 = OpFunction %2 None %28
42+
%26 = OpLabel
43+
OpBranch %30
44+
%30 = OpLabel
45+
%31 = OpFunctionCall %3 %10 %29
4446
OpReturn
4547
OpFunctionEnd

naga/tests/out/wgsl/f64.wgsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var<private> v: f64 = 1.0lf;
44

55
fn f(x: f64) -> f64 {
66
var z: f64;
7+
var w: f64 = -1.0lf;
78

89
let y = (30.0lf + 400.0lf);
910
z = (y + 5.0lf);

0 commit comments

Comments
 (0)