cmpute / dashu

A library set of arbitrary precision numbers implemented in Rust.
Apache License 2.0
74 stars 9 forks source link

Ability to create larger constants. #38

Closed junderw closed 6 months ago

junderw commented 10 months ago

There are many constant values that people might want to create in their projects that are 256 bit, 512 bit etc.

Currently with DWord we can instantiate 128 bit values on 64 bit arch.

It would be nice to be able to instantiate a const value for up to 512 bit regardless of the arch bits.

Thoughts?

cmpute commented 10 months ago

You can instantiate an arbitrarily large number with the ubig! or ibig! macros, although not in const context. I wish to support doing that in const context, but that's limited by the Rust, because Rust doesn't allow allocating memory in rust context yet.

Due to the fact that dashu aims to support arbitrarily large numbers, which essentially means allocation is unavoidable, this won't be implemented until Rust supports const allocation. Thanks for your interest!

junderw commented 10 months ago

What about something along the lines of https://github.com/rust-num/num-bigint/pull/286 this PR (WIP) where a Cow<'static, [T]> is used instead of a required allocation?

Playing around with the above PR (for another bigint library) it wasn't really too bad to deal with.

cmpute commented 10 months ago

What about something along the lines of https://github.com/rust-num/num-bigint/pull/286 this PR (WIP) where a Cow<'static, [T]> is used instead of a required allocation?

Playing around with the above PR (for another bigint library) it wasn't really too bad to deal with.

that sounds interesting for me, however I'm not sure whether it goes well with the already pretty complex internal design of dashu integers. PR is welcome!

cmpute commented 10 months ago

After a second thought, I think the best way to handle this is to provide a function that converts a reference to a static array to a reference of (static) big integers using some trick. Direct instantiating a big integer is problematic because it conflicts with the internal design of ubig. I have to add a separate flag for the "borrowed" status, which increases the number of cases to be dealt with for unary and binary operators a lot.

Therefore, if something like fn from_words_const(arr: &'static [Word]) -> &'static UBig can be implemented, then that's perfect. Otherwise, I think the benefit of the const construction of big integers is not worth the making great changes to the code base.

cmpute commented 7 months ago

I have implemented two macros static_ubig! and static_ibig!, for creating static UBig and IBig respectively. Please let me know how you like it :) A new version will be published when I have also implemented a static_fbig macro.

junderw commented 7 months ago

What a great Christmas gift!

I'll check it out when I get back home. Thanks!

junderw commented 7 months ago

Nevermind. You were talking about the Repr itself, not the Word slice.

Looks great!

cmpute commented 7 months ago

Yep, as long as the created Repr instance is assigned to an immutable static object, it should be fine. Merry Xmas!

cmpute commented 7 months ago

Unfortunately, the addition of some const functions requires Rust 1.64. Therefore I have to bump the MSRV, which requires a major version release. The new major version will take about 1 or 2 months tho..

cmpute commented 6 months ago

I managed to implement this without increase MSRV, so a new version is published. Please check out the dashu::static_* macros.