-
Notifications
You must be signed in to change notification settings - Fork 18k
math/bits: different results when using a pure Go copy of Add64 #54255
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
Comments
I think your program runs into documented undefined behavior:
As to why your results are different, note that Add64 is an intrinsic on amd64, meaning that it compiles down to efficient code: https://cs.opensource.google/go/go/+/refs/tags/go1.19:src/cmd/compile/internal/ssagen/ssa.go;l=4685 I would guess that the efficient intrinsic takes advantage of the undefined inputs to be faster. |
cc @randall77 in case I got the above wrong. |
If |
@mvdan's explanation is correct. Carry is a single bit. Calling Add with carry>1 is not defined and will not give consistent results. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
The result of the
Add64
function in themath.bits
package is not the same as the result of the copy of the function.https://go.dev/play/p/K16VoyuwfvK
What did you expect to see?
Terminal output:
What did you see instead?
Terminal output:
The text was updated successfully, but these errors were encountered: