Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minification of number literals works differently on arm64 and 64 platforms #2383

Closed
pirxpilot opened this issue Jul 11, 2022 · 1 comment
Closed

Comments

@pirxpilot
Copy link

For arm64:

$ echo "x = 0xFFFF_FFFF_FFFF_FFFF" |  esbuild --minify
x=0xffffffffffffffff;

For 64:

$ echo "x = 0xFFFF_FFFF_FFFF_FFFF" |  esbuild --minify
x=18446744073709552e3;

Obviously not that important. Stumbled upon it while bundling mapbox/pbf
Potentially related to #2162 fix?

@evanw
Copy link
Owner

evanw commented Jul 11, 2022

Thanks for the report. Here's a simple way to reproduce the underlying problem:

package main

import (
	"fmt"
	"runtime"
	"unsafe"
)

func main() {
	var x float64 = 0xffff_ffff_ffff_ffff
	var y uint64 = uint64(x)
	fmt.Printf("GOARCH %s, x %v (0x%016x), y 0x%016x\n", runtime.GOARCH, x, *(*uint64)(unsafe.Pointer(&x)), y)
}

On arm64 that prints this:

GOARCH arm64, x 1.8446744073709552e+19 (0x43f0000000000000), y 0xffffffffffffffff

and on amd64 that prints this:

GOARCH amd64, x 1.8446744073709552e+19 (0x43f0000000000000), y 0x8000000000000000

The Go specification says this:

In all non-constant conversions involving floating-point or complex values, if the result type cannot represent the value the conversion succeeds but the result value is implementation-dependent.

The problem is that 1.8446744073709552e+19 is 18,446,744,073,709,552,000 which is 0x1_0000_0000_0000_0180 (i.e. Go rounds the 0xffff_ffff_ffff_ffff literal up instead of down when converting to float64, which then doesn't fit back into uint64).

Maybe the safest upper limit to use is 18,446,744,073,709,550,000 which is 0xffff_ffff_ffff_f800. That's the next lowest 64-bit floating-point number, and the first one that is under 0xffff_ffff_ffff_ffff and representable as uint64.

@evanw evanw closed this as completed in f730c03 Jul 11, 2022
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

No branches or pull requests

2 participants