Skip to content

fix(vm): keep int64 arithmetic when int is 32 bits - #1043

Open
tonycoder-hub wants to merge 1 commit into
expr-lang:masterfrom
tonycoder-hub:gb/int64-arith-386-v3
Open

tonycoder-hub wants to merge 1 commit into
expr-lang:masterfrom
tonycoder-hub:gb/int64-arith-386-v3

Conversation

@tonycoder-hub

Copy link
Copy Markdown

Signed arithmetic that includes an int64 was evaluated as int, so on GOARCH=386 I64 + 0 with I64 = int64(MaxInt32+1) returned -2147483648.

The helper generator uses an int64 operation only inside if strconv.IntSize == 32 for +, -, *, and %. On amd64 that arm compiles out and the result stays int, so results and types on 64-bit match master. Comparisons of those signed pairs are int64(x) op int64(y) with no arch branch; when int is 64 bits that is the same boolean as comparing as int. The checker uses the same result rule.

On 386, the compile error for an int64 argument applies only to a real Go function invoked by reflection or OpCallTyped, where the VM would assert .(int). Builtins and expr.Function still receive ...any and convert with runtime.ToInt, so repeat, bitand, bitshl, split, and a double function declared as func(int) int with a ToInt body keep working.

runtime.Modulo still returns int. New exported runtime.ModuloWide handles only the signed int64 cases and falls back to Modulo. The VM calls it only when int is 32 bits.

uniq keeps the original int-keyed index on amd64. On 386 it uses a separate index, so signed int64 values that do not fit in int are not collapsed. An amd64 bench of uniq stayed within noise (78.49µ vs 78.45µ, same 58 allocs/op).

Behavior changes on 386:

  • An int64 passed to a Go func(int) parameter, or used as a map[int] key, is now a compile error (it used to truncate silently). Wrap it in int(...).
  • FnInt(Any + 1) and MI[Any + 1] fail at runtime when Any's dynamic value is an int64, because the static type is any.

Still limited on 386:

  • Mixed signed/unsigned operands (for example int64 with any uint type, or uint64 with an int literal) still compute as int.
  • a..b range bounds are int.
  • sum([]uint64) and sum([]int32) accumulate in int.
  • Integer literals above MaxInt32 are rejected by the parser.

A fully green GOARCH=386 go test ./... still depends on #1039. With those three test edits applied locally, that command passed. #1039 and this branch both add an import to the same expr_test.go block, so whichever lands second needs a trivial rebase.

Fixes #804

Signed operations that include an int64 were evaluated as int, so on
GOARCH=386 values above MaxInt32 wrapped. The generated helpers take an
int64 path only when strconv.IntSize == 32; on amd64 that arm compiles
out and the result stays int. Comparisons of those pairs use int64 with
no arch branch, which matches int comparison when int is 64 bits.

The checker uses the same result rule. On 386, a real Go func invoked
by reflection or OpCallTyped rejects an int64 argument where it would
assert .(int). Builtins and expr.Function still take ...any and convert
with ToInt. runtime.Modulo still returns int. The new runtime.ModuloWide
handles only the int64 cases and falls back to Modulo; the VM calls it
only when int is 32 bits. uniq's wider index is used only on 386.

uint64 arithmetic is unchanged.

Fixes expr-lang#804

Signed-off-by: Tony Coder <407243179@qq.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Arithmetic operations choose arch-dependant type and overflow in 32bit

1 participant