Repository navigation
fix(vm): keep the sign of -0.0 float results - #87
Merged
Merged
Conversation
lnumberToValue shared a preallocated value for any float equal to a small non-negative integer. -0.0 compares equal to 0, so every float result boxed through it (OP_LEN metamethod results, unary minus, arithmetic, floor division, modulo, power) turned -0.0 into 0.0, and 1/(-z) gave +inf. The cache now applies only to floats bit-identical to a cached value.
ConstIndex reused a constant that compared equal under Lua equality, so a function using both 0.0 and -0.0 shared one constant slot and returned whichever appeared first for both. Float constants are now matched bit for bit.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Defect
A float result of
-0.0loses its sign:There are two causes:
lnumberToValuereuses a cached boxed number for any float that equals a small cached value.-0.0 == 0, so every float result of-0.0came back as+0.0. This affects__lenresults, unary minus,*,/,//,%,^and underflow.ConstIndexshares constant slots by Lua equality, so0.0and-0.0in one function share a slot.Preemption fuzzing on #60 found it: a preempted
__lencall returns-0and the synchronous path returns0.Fix
fix(vm): the cache is used only when the float is bit-identical to the cached value (math.Float64bits). All 18 call sites invm.go,registry.goandnumeric_arithmetic.gogo through this one function.fix(compiler):sameConstantmatches float constants bit for bit.Tests
numeric_negative_zero_test.go:TestFloatZeroKeepsSignchecks the Lua 5.4 sign through1/v, andmath.type, for#/__len, unary minus, double unary minus,*(int and float),/, underflow,//,%,^and both constant-pool cases. 12 cases fail onmain, and all pass after both commits.TestNegativeZeroTableKeypasses before and after: a-0.0key is stored as integer key0, as in Lua 5.4.Already correct, and covered by the test:
__add/__unmmetamethod results, table values, constructors, a lone-0.0constant, varargs,tonumber('-0.0'),string.format('%g'), and-0.0 + 0(Lua gives+0).Not in this PR
These are related Lua 5.4 differences outside the boxing path:
for i = -0.0, 0starts at+0.0, because the Lua 5.1-style setup computesinit - step + step. feat(vm): tick-budget preemption of coroutines #60 changes the same numeric-for code.tostring(-0.0)gives"0"(Lua 5.4 gives"-0.0"), becauseLNumber.Stringformats integral floats as integers.math.type(tonumber('-0'))isfloat(Lua 5.4:integer), andmath.floorreturns a float.Verification
go build ./...,go vet ./...andgo test -count=1 -timeout 30m ./...: all pass.FuzzPreemptGeneratedcrash input passes.FuzzPreemptGeneratedandFuzzPreemptSourcepass, and the full preemption differential sweep shows 0 divergent programs.