Skip to content

lsr(-123, 0) not equiv. to -123 >>> 0 #7222

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

Open
leoliu opened this issue Jan 2, 2025 · 4 comments
Open

lsr(-123, 0) not equiv. to -123 >>> 0 #7222

leoliu opened this issue Jan 2, 2025 · 4 comments

Comments

@leoliu
Copy link

leoliu commented Jan 2, 2025

While converting a JS program to rescript I discovered that lsr(-123,0) is compiled to just -123 which is not equivalent to -123 >>> 0. I wonder if this is a bug?

https://rescript-lang.org/try?version=v11.1.4&module=esmodule&code=DYUwLgBAHhC8HAM4CcAUBaAjAJgMwBoAGASgChRIBPOCAUmQEMB3VAIizwgD4eJDWypAFKIAdMAD2Ac1QxY8SsSA

@shulhi shulhi added the bug label Jan 2, 2025
@jagadam97
Copy link

jagadam97 commented Jan 4, 2025

i think these #7171 enhancements will fix what ever issue we are having here.

But i found an inconstency from OCaml Lib here.

-123 lsr 0 is giving output equivalent to -123 >> 0
where as
-123 lsr 1 is giving output equivalent to -123 >>> 1

image

@shulhi shulhi removed the bug label Jan 30, 2025
@shulhi
Copy link
Member

shulhi commented Jan 30, 2025

So it's not a bug because JS converts it to an unsigned 32-bit integer before shifting.

-123 converted two's complement is 0b1111111_11111111_11111111_10000101. lsr 0 doesn't shift, so the result remains.

However, in JS, >>> forces the number to be interpreted as an unsigned 32-bit integer, thus 2³² + (-123) = 4294967173.

@cometkim if our goal is to match javascript, should we change the implementation of bitwise to match the javascript?

@cometkim
Copy link
Member

cometkim commented Feb 4, 2025

@cometkim if our goal is to match javascript, should we change the implementation of bitwise to match the javascript?

We can discuss it further at rescript-lang/rfcs#1, but as already mentioned it is not the current goal.

https://github.com/rescript-lang/rfcs/blob/int-semantic/text/0001-int.md#can-we-somehow-make-it-match-javascripts-number

One day we might be able to treat int32 like a specialized second-class citizen, but the migration path won't be simple.

@leoliu
Copy link
Author

leoliu commented Feb 5, 2025

So it's not a bug because JS converts it to an unsigned 32-bit integer before shifting.

-123 converted two's complement is 0b1111111_11111111_11111111_10000101. lsr 0 doesn't shift, so the result remains.

However, in JS, >>> forces the number to be interpreted as an unsigned 32-bit integer, thus 2³² + (-123) = 4294967173.

@cometkim if our goal is to match javascript, should we change the implementation of bitwise to match the javascript?

That explains it. Sometimes we forget how crazy JS can be. Maybe a heads up is enough when documenting lsr.

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

4 participants