Skip to content

Commit

Permalink
🐛 svg: Prevent code with fused instructions (#464)
Browse files Browse the repository at this point in the history
Add a floating point conversion when multiplying when scaling coordinates
so that the multiplication does not get fused with an adjacent addition or
subtraction. This causes two instructions to be generated, a MUL and a
ADD/SUB, instead of a single FMADD or FMSUB. A fused instruction has
different rounding so ends up giving slightly different results to two
instructions. This was causing different values in the SVGs generated
between AMD64 and ARM64.

Revert "labs: Rework random rectangle generation" as we no longer need
to work around this by rounding in Evy code.

Remove the parens in `randrect.evy` as they are not needed any more due
to a recent change.

I've tested this on AMD64 and ARM64 and the same SVGs are now generated.

This merges the following commits:
* svg: Prevent code with fused instructions
* Revert "labs: Rework random rectangle generation"
* labs: Remove unnecessary parens in randrect

     frontend/lab/samples/ifs/img/randrect.evy |  16 +-
     frontend/lab/samples/ifs/img/randrect.svg | 420 ++++++++++++++++++----
     pkg/cli/svg/runtime.go                    |   9 +-
     3 files changed, 372 insertions(+), 73 deletions(-)

Link: https://go.dev/ref/spec#Floating_point_operators
Pull-request: #464
  • Loading branch information
camh- committed Feb 8, 2025
2 parents 4915377 + 31f9dca commit b13ec7a
Show file tree
Hide file tree
Showing 3 changed files with 372 additions and 73 deletions.
16 changes: 4 additions & 12 deletions frontend/lab/samples/ifs/img/randrect.evy
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
fill (hsl 270 100 50 10)
width 0.2
for range 60
x := roundedRand1 * 100
y := roundedRand1 * 100
s1 := roundedRand1 * 25 + 2
s2 := roundedRand1 * 25 + 2
x := rand1 * 100
y := rand1 * 100
s1 := rand1 * 25 + 2
s2 := rand1 * 25 + 2

move x-s1*0.5 y-s2*0.5
rect s1 s2
end

// roundedRand1 is a rounding rand1 function, to avoid rounding differences on
// Mac and Linux.
func roundedRand1:num
r := rand1 * 1024
r = round r
return r / 1024
end
Loading

0 comments on commit b13ec7a

Please sign in to comment.