-
Notifications
You must be signed in to change notification settings - Fork 70
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
Feature request: Modular (or Machine) arithmetic #103
Comments
This is interesting, but I am not considering adding this at the moment.
|
Well, in fact, it only needs a wrapper that shift right (
If think on the contrary that we want to use the GMP's sign bit plus unsigned value because for positive value, it changes nothing but for example, if you want to represent a big negative 128 bit value, e.g.
In fact, as I am very tempted to reuse some of the helper functions and constant declared in the stub c file, I thought it could be smarter to directly embed it in the same library rather than creating my own "almost copy-pasted" project. Also, as a side note, I am wondering why there is a need for long numbers to have a word reserved for |
Sorry for the delay in answering.
If I understand correctly, it is possible that you have a bitvector in compact representation that does not fit into a machine integer (bitsize between 53 and 1024 I guess). In that case, you'll do
For the modular arithmetic part, would it be possible to simply use the standard
Modifying the header of an OCaml block after its allocation seems an extremely bad idea to me. |
It is not exactly that.
This is actually what is done today. I opened some times ago the PR #90 (need to be reviewed) for adding fast path to extract operations.
The function Still I expect all 64 bits common values to fall in the compact form so, saving 1 word time to time should not really matter a lot. |
Truncating ocaml values after their creation breaks assumptions made by the flambda optimization pass of the compiler. From what I remember, with multicore (i.e. ocaml 5.0), it is also unsafe to modify headers of objects. And finally, the |
@Gbury Thank you for the precision. Of course, I would not recommend to truncate arbitrary values, but here is a special case: the value is created by an external function (so flambda is, I think, out of the discussion) and, more importantly, the header will be updated before this external function return (meaning it is the only owner of the value). Still, I do not really know how the garbage collector will work in multicore with external function calls. Can an external C primitive be interrupted by the garbage collector without calling anything related to OCaml? If yes, then I think a lot of code will become unsafe. If it is still no, then it is safe to modify the header since no one except the C function can use the value yet. |
OK, this seems more reasonable.
OK. I was not sure, given the current bitvector.ml code (linked on top of the issue).
Sorry, I meant a way to code the current fast path in OCaml instead of C, as done in the current version of most operators.
Given the discussion, it is clearly not future-proof. I am against making and exploiting such assumptions on the GC. |
It was originally for this but I updated it a time ago (#90 (comment)) to give fast path in OCaml (because small integer input restrict is a special case where the function return a small integer).
Fair enough, it is not the center of the topic anyway. |
Re: truncating an already-allocated block, this was supported in OCaml up to versions 4.xx (function In Multicore OCaml, which became OCaml 5.00 earlier today, |
Now that #90 is merged, it should be possible to write a modular arithmetic library on top of Zarith in an efficient way in 100% pure OCaml. So, closing the issue. |
Hi,
I am currently (in my spare time, so pretty slowly) reworking the implementation of binsec bitvectors.
My idea is (in the same way as
Z
) to represent small values as native OCaml integer. For this, I use 10 bits for encoding the size of the bitvector (so the compact representation can handle bitvector up to 1023 bits) and the remaining bits are for the (sign extended) value. Other cases fall through standard boxed representation.As I use
Z
as the underlying implementation, I think it often performs more check or more work than if it was directly written as a native case insidezarith
library where some shortcuts could have been taken. Additionally, I would imagine that a fast, carefully optimized modular arithmetic (aka fixed size bitvector arithmetic) may also interest some people outside ofbinsec
project if it was distributed withzarith
.So would you consider adding a module
M
(for either Modular or Machine arithmetic) alongsideZ
andQ
(possibly starting from my work when it would be ready) ?The text was updated successfully, but these errors were encountered: