ocaml-stdint icon indicating copy to clipboard operation
ocaml-stdint copied to clipboard

Provide pure-OCaml fall-back implementation

Open keleshev opened this issue 8 years ago • 7 comments

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

keleshev avatar May 04 '16 11:05 keleshev

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.

andrenth avatar May 04 '16 11:05 andrenth

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

mwweissmann avatar May 05 '16 19:05 mwweissmann

@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.

keleshev avatar May 12 '16 11:05 keleshev

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).

mwweissmann avatar May 12 '16 11:05 mwweissmann

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

andrenth avatar May 12 '16 11:05 andrenth

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.

zenhack avatar Nov 29 '20 00:11 zenhack

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.

keleshev avatar Nov 29 '20 22:11 keleshev