Skip to content
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

Ability to create larger constants. #38

Closed
junderw opened this issue Sep 29, 2023 · 10 comments
Closed

Ability to create larger constants. #38

junderw opened this issue Sep 29, 2023 · 10 comments

Comments

@junderw
Copy link

junderw commented Sep 29, 2023

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
Copy link
Owner

cmpute commented Sep 29, 2023

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!

@cmpute cmpute closed this as not planned Won't fix, can't repro, duplicate, stale Sep 29, 2023
@junderw
Copy link
Author

junderw commented Sep 29, 2023

What about something along the lines of rust-num/num-bigint#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
Copy link
Owner

cmpute commented Sep 29, 2023

What about something along the lines of rust-num/num-bigint#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 cmpute reopened this Sep 29, 2023
@cmpute
Copy link
Owner

cmpute commented Sep 30, 2023

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
Copy link
Owner

cmpute commented Dec 25, 2023

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
Copy link
Author

junderw commented Dec 25, 2023

What a great Christmas gift!

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

@junderw
Copy link
Author

junderw commented Dec 25, 2023

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

Looks great!

@cmpute
Copy link
Owner

cmpute commented Dec 25, 2023

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

@cmpute
Copy link
Owner

cmpute commented Dec 29, 2023

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
Copy link
Owner

cmpute commented Jan 12, 2024

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

@cmpute cmpute closed this as completed Jan 12, 2024
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

No branches or pull requests

2 participants