-
Notifications
You must be signed in to change notification settings - Fork 463
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
int32, int64, int operations #107
Comments
here is a nice summary how different languages handle overflow: |
here is my proposal:
type t = {lo : int (* signed 32 bit *) ; high: int} (* which will be [lo, hi] in js *)
|
I've never seen correct code which relies on the overflow semantics of plain ints. If it can simplify the generated code to drop strict compatibility with the OCaml semantics, this seems a very reasonable choice to me. |
You mean
What is the reason for this? I don't see an issue with a signed representation and it would make use from JavaScript easier.
I think this is reasonable to keep the code uncluttered, although browsers are able to generate better code (integer operations) if the range of numbers is restricted to integers. Note that for 32 bit multiplication you need to generate 64 bit multiplication and division are rather complicated, have a look here: https://closure-library.googlecode.com/git-history/docs/local_closure_goog_math_long.js.source.html and here: https://github.com/kripken/emscripten/blob/master/src/fastLong.js |
Yes, sorry for the typo
since
Thanks for the link to Google Closure Library |
about |
I am strongly opposed to this. If people want floating point semantics, they can use
I don't see the need for unsigned integers in the standard library. After all, OCaml (and many other languages) also don't have them.
I am also opposed to this, I don't think we should introduce undefined behaviour in OCaml. |
but if you want int behave exactly as c like int, you can always use Int32.t. I would imagine people use int (js context) mostly just like plain numbers. Having int behave like js number would allow people write some low level js code in ocaml, for example, if I want implement polyfill Math.imul, I would really like it behave like js number |
If you want a number to behave like JavaScript's numbers, you can use |
Note that I did take a look at signed int in the c standard, it seems that signed int overflow is undefined behavior, so techinally I think what we are doing here is correct. |
I wasn't aware of that. I'd still prefer to keep numbers in the integer range, at least for performance (V8 tries to keep numbers in 32 bit registers and deoptimizes if they overflow). Can we make this a compiler flag?
Why not use |
if I remember correctly, v8 will optimize 31 ints(same as ocaml), which means you can get deoptimized even if it is a purely 32 bit number |
@copy we will add |
Nice! |
Co-authored-by: EduardoRFS <theeduardorfs@gmail.com>
we need make sure int32 int64 semantics correct for sure.
for int (ocaml int is 31/63 bits), do we really need wrap those int everywhere, like
a+b
-->a+b|0
, the chance of overflow in ocaml often result in incorrect programsThe text was updated successfully, but these errors were encountered: