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

Proper way to declare a const int variable? #17

Closed
sam0x17 opened this issue Feb 23, 2023 · 7 comments
Closed

Proper way to declare a const int variable? #17

sam0x17 opened this issue Feb 23, 2023 · 7 comments

Comments

@sam0x17
Copy link

sam0x17 commented Feb 23, 2023

I need to declare a const MY_NUM: I512 but I can't figure out syntax that would allow me to set the value in a const context. Is there not a way to do this yet? Would be happy to write a macro for this purpose if there is any possible way to initialize one of these in a const context.

@isaacholt100
Copy link
Owner

Hi, thanks for opening this issue! Writing a proc macro is on my todo list for this crate, but I haven't got round to it yet. Is the value that you want to initialise a single literal or is it the result of a computation? I think in theory it should be possible to create any value using the const methods defined on I512 but a macro for compile time parsing of literals would definitely be beneficial.

Currently, the from_str method in the FromStr trait implementation is not const but I could make a const equivalent, which would at least allow const parsing of decimal literals, and would be fairly quick to release as a temporary workaround.

@sam0x17
Copy link
Author

sam0x17 commented Feb 24, 2023 via email

@isaacholt100
Copy link
Owner

I think the simplest example would be using the from_digits method on U512 to convert an array of digits to a U512 and then using the from_bits method on I512 to convert that U512 to an I512. So for example (I'm using I256 here for readability), if the hexadecimal literal was 0x5501ece471b59c0ace87fdad70306960eca1623f20207afa6e316e83cb2e3 and you wanted this represented as an I256, you could use:

use bnum::types::{U256, I256};

const MY_NUM: I256 = {
    let d1 = 0xafa6e316e83cb2e3u64;
    let d2 = 0x960eca1623f20207u64;
    let d3 = 0xc0ace87fdad70306u64;
    let d4 = 0x5501ece471b59u64;
    I256::from_bits(U256::from_digits([d1, d2, d3, d4]))
};

The from_digits method takes the least significant digit first so the lower order part of the literal first is the first item in the array. Hopefully this makes sense!

@isaacholt100
Copy link
Owner

And then you could use methods like wrapping_shl, rotate_right, wrapping_add, etc. which are const to manipulate this further if needed.

@sam0x17
Copy link
Author

sam0x17 commented Feb 25, 2023

Yes thank you!

@isaacholt100
Copy link
Owner

No worries! Good news, I have just made the from_str_radix on unsigned integers const, so once I have tested it fully I will publish a new version with this change. I have also created another issue specifically for writing a macro for the purpose you described: #18.

@isaacholt100
Copy link
Owner

I have just published version 0.6.0 which provides a parse_str_radix radix method for compile time parsing of integers from string literals, so I will close this issue now.

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