Equivalently I'd like to load x % 2^n
into another mpz_t. It's not clear to me whether mpz_mod
(manual) will be faster than something like:
mpz_t setSuffix(int n,mpz_t x){
mpz_t y;
mpz_init_set_ui(y,0);
for(int i=0;i<n;i++){
if(mpz_tstbit(x,i)){
mpz_setbit(y,i);
}
}
return y;
}
Can I do better here with some built in function, or is this the speed limit? It might be faster to run the loop backwards so allocation would only be done once.
Additional optimization (instead of 3. above):
It might still be faster that 4.-6. above if:
From here, do some benchmarking and improve as needed.
Note: I assumed you have a 64-bit system. For older systems, adjust as needed. Also, some of the math above might need some adjustments (in steps 4.-6.).