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

Provide pure-OCaml fall-back implementation #6

Open
keleshev opened this issue May 4, 2016 · 7 comments
Open

Provide pure-OCaml fall-back implementation #6

keleshev opened this issue May 4, 2016 · 7 comments

Comments

@keleshev
Copy link

keleshev commented May 4, 2016

It would be nice if stdint had a fall-back on pure OCaml code for targets that don't support C bindings, e.g. Js_of_ocaml.

I just open-sourced my pure OCaml integer implementation, which might be helpful for that:

https://github.com/keleshev/Integers.ml

@andrenth
Copy link
Owner

andrenth commented May 4, 2016

Hi

I agree, this and your idea of selectable behavior in #4 are nice suggestions. Currently I don't have the necessary time to work on stdint, but if you want to send a patch (or if @mwweissmann wants to do it) I'm all for it.

@mwweissmann
Copy link
Collaborator

Most of the <= int64 stuff is pure OCaml already; I'm not quite sure how to elegantly implement the int128 stuff that relies on special amd64 commands via gcc

@keleshev
Copy link
Author

@mwweissmann what do you mean by "pure OCaml"? What I meant by it is "non-C". The interest in avoiding C is to be able to work with Js_of_ocaml.

@mwweissmann
Copy link
Collaborator

All integers <= 64 Bit are stored as "int" or "int64"; we should be able to rewrite all functions on them in "pure OCaml -- without C". The > 64 Bit integers (currently only 128 Bit) use some 64 Bit stuff we cannot tap into in pure OCaml. We would need a "pure OCaml" version of the 128 Bit integer which would also be nice for 32 Bit processors.
I currently have no need for rewriting nor implementing the 128-Bit-pure-OCaml stuff but I'm not opposed to either (as long as we do use the 64-Bit CPU on 64 Bit systems).

@andrenth
Copy link
Owner

For the record, the original implementation of Uint128 in the old ocaml-uint library was in pure OCaml. The code is still up in the old repository: https://github.com/andrenth/ocaml-uint/blob/master/lib/uint128.ml

@zenhack
Copy link

zenhack commented Nov 29, 2020

I have some interest in making this happen (this library is a dependency of something I want to run in the browser). From skimming the implementation, this seems pretty doable but I have some questions:

  • For some things, it's not really clear to me why they're written in C in the first place. e.g. the min_int implementations, as I can't imagine a concrete advantage there. Was this just done for uniformity or was there a more specific reason? If the former, it seems like it might make sense to just toss the C implementations where applicable, if we're writing ones in OCaml anyway.
  • I'm a little unsure how to go about conditional compilation with dune, for the bits where we do need to keep around a C implementation; if anyone has any pointers that would be helpful.

@keleshev
Copy link
Author

I'm a little unsure how to go about conditional compilation with dune, for the bits where we do need to keep around a C implementation; if anyone has any pointers that would be helpful.

Maybe digestif could be a good example of that: https://github.com/mirage/digestif. It provides two implementations—one in C and one in OCaml.

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