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

bug in Nativeint #2668

Closed
vicuna opened this issue Jan 18, 2001 · 2 comments
Closed

bug in Nativeint #2668

vicuna opened this issue Jan 18, 2001 · 2 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Jan 18, 2001

Original bug ID: 266
Reporter: administrator
Status: closed
Resolution: not a bug
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

The implementation of logical shift is not quite right using the bytecode
compiler on Linux x86:

module N = Nativeint ;;

...

N.shift_right_logical N.minus_one 1;;

  • : nativeint = <nativeint 2147483647>

N.shift_right_logical N.minus_one 32;;

  • : nativeint = <nativeint -1>

N.shift_right_logical N.minus_one 31;;

  • : nativeint = <nativeint 1>

N.shift_right_logical (N.of_int 1) 1;;

  • : nativeint = <nativeint 0>

@vicuna
Copy link
Author

vicuna commented Jan 21, 2001

Comment author: administrator

Hi Norman,

The implementation of logical shift is not quite right using the bytecode
compiler on Linux x86:

Well, this looks OK to me. The documentation is insufficient, though:
it should say that, like the underlying C shifts, the behavior is
unspecified if the shift amount is < 0 or >= 32 (or 64 on a 64-bit
processor). Like most processors, the x86 chooses to take the shift
amount modulo 32.

module N = Nativeint ;;

...

N.shift_right_logical N.minus_one 1;;

  • : nativeint = <nativeint 2147483647>

OK: 0xFFFFFFFF >>> 1 = 0x7FFFFFFF

N.shift_right_logical N.minus_one 32;;

  • : nativeint = <nativeint -1>

Here, the processor interprets 32 as 0, as explained above, so it's OK.

N.shift_right_logical N.minus_one 31;;

  • : nativeint = <nativeint 1>

0xFFFFFFFF >>> 31 = 0x00000001, all right.

N.shift_right_logical (N.of_int 1) 1;;

  • : nativeint = <nativeint 0>

0x00000001 >>> 1 = 0x00000000, all right.

Another possibility would be to fully specify shifts the way Java does
it, i.e. enforce that the shift amount is taken modulo 32 or 64.
Since this is what popular processors do already, it would not
generate any additional code in ocamlopt; the bytecode interpreter
would have to take the modulus explicitly.

Cheers,

  • Xavier

@vicuna
Copy link
Author

vicuna commented Feb 12, 2001

Comment author: administrator

Unspecified behavior. Added documentation. (Xavier, 2001-02-12)

@vicuna vicuna closed this as completed Feb 12, 2001
@vicuna vicuna added the bug label Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant