andrenth / ocaml-stdint

Various signed and unsigned integers for OCaml
Other
84 stars 15 forks source link

Provide pure-OCaml fall-back implementation #6

Open keleshev opened 8 years ago

keleshev commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

@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 commented 8 years ago

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 commented 8 years ago

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 commented 3 years ago

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:

keleshev commented 3 years ago

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.