Skip to content

Add custom functions to allow hash and marshalling - #45

Open
craff wants to merge 2 commits into
thvnx:masterfrom
craff:master
Open

Add custom functions to allow hash and marshalling#45
craff wants to merge 2 commits into
thvnx:masterfrom
craff:master

Conversation

@craff

@craff craff commented Apr 12, 2026

Copy link
Copy Markdown

Add custom functions to allow hash and marshaling

WARNING: marshaling across architecture fails if mpfr_t internal changes. (the hash changes too). An architecture independent marshaling is hard because mpfr does not provide robust float to string - string to float function. Only an import and export to file!

This is already better than nothing I think.

@thvnx

thvnx commented Jul 29, 2026

Copy link
Copy Markdown
Owner

Hi @craff and thanks for this PR! I'm sorry I missed it, having a look at it!

Comment thread src/mlmpfr_stubs.c
return sizeof(mpfr_t);
}

static intnat custom_hash(value v)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is two ways of defining the hash function. Hashing the representation (your proposal) vs. the value.

custom_compare is based on value comparison (mpfr_cmp) and both compare/hash must agree I think because Hashtbl uses = for example.

So hashing +0 and -0 should get to the same value: the sign shouldn't be taken into account during hashing. Same for the precision, and so on…

I don't know if I prefer the value or the representation method but I'll go for the value one to be coherent with compare. I'm not against modifying compare though but that's an API breakage that should be documented.

Comment thread src/mlmpfr_stubs.c
return r;
}

static void custom_serialize(value v,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Special values should be handled separately (nan/inf/zero), their limbs are uninitialized so they should not be part of the (de)serialization.

Comment thread src/mlmpfr_stubs.c
/****************************/
/* Initialization Functions */
/****************************/
CAMLprim value mlmpfr_init_custom()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename it to caml_mpfr_init_custom for consistency. It could take an argument too I guess: value unit.

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

Successfully merging this pull request may close these issues.

2 participants