-
Notifications
You must be signed in to change notification settings - Fork 188
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
Add to_slice and to_vec functions #12
base: master
Are you sure you want to change the base?
Conversation
I hesitate to expose the internal representation so much, for the borrowed slice especially. How would you feel about some kind of iterator instead? We could make it return bytes or even larger units, without having to allocate the full conversion. |
That would not solve the problem fully due to |
Perhaps we could frame this in a way that doesn't commit to a particular representation? Like: impl BigUint {
// Returns `(count, order, size, endian, nails, ptr)` in the style of `mpz_import`
fn export_raw<F, T>(&self, f: F) -> (usize, c_int, usize, c_int, usize, *const c_void);
} Obviously that's tailored directly for GMP, but I'm not sure how to generalize this idea. |
If what is required is conversion to use gmp_mpfr_sys::gmp;
use std::os::raw::c_int;
use std::slice;
fn copy_to_gmp(src: &BigUint, dst: &mut gmp::mpz_t) {
assert_eq!(gmp::NAIL_BITS, 0, "nails not supported");
let bits = src.bits();
dst.size = 0;
if bits == 0 {
return;
}
let limb_bits = gmp::LIMB_BITS as usize;
let limb_count = (bits + limb_bits - 1) / limb_bits;
let limbs = unsafe {
gmp::_mpz_realloc(mpz_t, limb_count);
slice::from_raw_parts_mut(dst.d, limb_count)
};
// limb_t is either u32 or u64
let mut digits = src.digits::<gmp::limb_t>();
for i in limbs.iter_mut() {
*i = digits.next().expect("not enough digits");
}
assert!(digits.next().is_none(), "too many digits");
dst.size = limb_count as c_int;
assert!(dst.size > 0 && dst.size as usize == limb_count, "overflow");
} |
Another data point: it would be great to have the limbs exposed as an iterator |
I've the same issue, I need to access the internal &[u32] slice. For performance, it would be nice to have an iterable version of it or a non documented version (like |
I'd like either a function that returned the underlying slice or an iterator too. |
I needed to convert to/from mpz_t. While converting into is relativly simple due to from_slice function the invert requires allocation of array with base 28 making it less efficient (requiring 2 copies and 2 convertions from/to 8 bit instead of 1 or 0). Those functions would allow inspecting representation of function without need of allocating and creating additional copies.