-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
implement int64,uint64,int,uint for JS backend #4714
Comments
Problem 1: template ordLimitDecl(typ: typedesc): untyped =
proc min(t = typ): typ = low(typ)
proc max(t = typ): typ = high(typ)
int64.ordLimitDecl
echo int64.min C version prints |
Problem 2: let x = 0'u64
echo x Produces:
|
Problem 3: let x = 0'u32
echo x Produces:
|
Problem 4: import parseutils
var x: int64
discard parseBiggestInt($int64.high, x)
echo x Fails at runtime:
|
Yes, please fix these. :-) |
Part of the issue here is that the Javascript standard mandates that all Numbers in Javascript are represented as IEEE-754 floating point numbers. Which means that you get 53 bits of precision (Number.MAX_SAFE_INTEGER + 1), and then after a number becomes larger than that, the engine will start representing the number in approximations. So that's why int64 is broken. uint64 would have the same issue. So as far as problem 1, max(int64) and min(int64) on the JS platform should take this limitation into account and evaluate to the limits of the Javascript engine's absolute precision. There could also be a flag "-d:emulate64bitNumbersSlow", which could force all 64 bit integers to use a software big number implementation supplied by the compiler. The other problems are valid :) |
So copy their solution. |
This is an attempt to copy the kotlin solution. Kotlin only has code for signed int64. I don't know how something like this could be better integrated in Nim. |
Not an issue for v1. You can use emscripten for low level code and high level code is about targeting the DOM which in JS and has no need for int64 and friends. |
#16411 was closed as duplicate for this issue so I'm adding its test case here: problem 5echo uint64.high js: -1 |
This issue is currently blocking JS support for nim-lang/bigints, see nim-lang/bigints#59. |
If someone is inclined to integrate the Scala.js implementation, I can provide guidance about it, having written it. (I know nothing about Nim nor its compiler, though.) To the best of our knowledge, the Scala.js implementation is the fastest implementation of 64 bit integers for JavaScript. It also features the unsigned operations (unsigned division, modulo, toString and fromString; the others are the same whether they're signed or not). The latest version is here: By the way, we also have correct implementations of |
@sjrd Thanks for your help! Your implementation looks great.
May I have the link? |
Sure. It's a bit more "all over the place", but here are the relevant moving parts: (in Scala,
As for tests, here are links as well:
You may also be interested in our polyfills to read/write the bits of float{32,64} to int{32,64}, here: That should cover the essentials. If there's anything else you'd like to know, feel free to ask. ;) |
Thanks for the elaborate explanation. I will keep these in mind. |
We need the implementation of int64/uint64 types for js backend. Now it's broken for int64 and absent for uint64.
For example, here is how they do it in ScalaJS: https://github.com/scala-js/scala-js/blob/10d67c9e479b40714a9e134522a181829b8f64bc/library/src/main/scala/scala/scalajs/runtime/RuntimeLong.scala
The text was updated successfully, but these errors were encountered: