Implement a logical shift for mpz_t

Jan Claußen jan.claussen10 at web.de
Thu May 6 16:12:44 UTC 2021


> So the functions exist, you are just confused why they are not called shift?

Yes, I was searching for shift functions in the documentation and only found the low-level ones, which I did not understand. So yes, I think they should be renamed or listed in the integer bit-fiddling section.
> Do you assume that your numbers are non-negative?
Yes, I know they are.
> Are you (badly) trying to emulate fixed size numbers?
Yes, the fixed size of the registers is important for my application, so I am emulating the usual shift behavior.
On Mai 6 2021, at 4:59 pm, Marc Glisse <marc.glisse at inria.fr> wrote:
> On Thu, 6 May 2021, Jan Claußen wrote:
>
> > I am just fiddling around with mpz_t variables and it took me quite a long time to come up with a bit shifting method with some help of the Stackexchange hive mind.
> Might as well give the link https://stackoverflow.com/q/67388748/1918193
> Do you assume that your numbers are non-negative?
> > These are the functions I have now:
> > inline void mpz_lshift(mpz_t rop, mpz_t op1, mp_bitcnt_t op2) {
> > mpz_mul_2exp(rop,op1,op2);
> > mpz_tdiv_r_2exp(rop, rop, mpz_popcount(op1));
> > }
>
> No idea what that weird operation is supposed to be. mpz_mul_2exp shifts
> left, and that's that. Are you (badly) trying to emulate fixed size
> numbers?
>
> > inline void mpz_rshift(mpz_t rop, mpz_t op1, mp_bitcnt_t op2) {
> > mpz_fdiv_q_2exp(rop, op1, op2);
> > }
> >
> > Is this a practical solution? There is probably a better way to do this in assembly, but it works for now. I just wonder why no functions like this exist yet. Is there a good reason?
>
> So the functions exist, you are just confused why they are not called
> shift?
>
> --
> Marc Glisse
>



More information about the gmp-discuss mailing list