mpq_cmp_z

Marco Bodrato bodrato at mail.dm.unipi.it
Thu Aug 20 06:07:42 UTC 2015


Ciao,

On Sun, August 16, 2015 11:50 pm, Torbjörn Granlund wrote:
>   On Sat, August 15, 2015 12:37 pm, Vincent Delecroix wrote:
>   > it would be convenient to have a GMP function
>   >
>   >     int mpq_cmp_z(mpq_t, mpz_t)

> We might consider some code sharing between the mpq_cmp and this
> function, they look very similar.

Maybe I've found a good sharing strategy...

static int
mpq_cmp_maybe_z (mpq_srcptr op1, mpq_srcptr op2, int op2_is_mpz)
{
...
}

int
mpq_cmp (mpq_srcptr op1, mpq_srcptr op2)
{
  ASSERT (SIZ(DEN(op2) != 0));
  return mpq_cmp_maybe_z (op1, op2,
         (SIZ(DEN(op2)) | PTR(DEN(op2))[0]) == CNST_LIMB(1));
}

int
mpq_cmp_z (mpq_srcptr op1, mpz_srcptr op2)
{
  return mpq_cmp_maybe_z (op1, (mpq_srcptr) op2, 1);
}

This way gcc compiles the mpq_cmp_z function (on amd64) as:
        movl    $1, %edx
        jmp     mpq_cmp_maybe_z

Is casting an mpz to an mpq, then accessing only the NUM() part of it,
portable?

I attach the proposed code, it adds four branches to the typical mpq_cmp
flow. One of them (checking if op1 is an integer) is not needed for
correctness. Please review!

Regards,
m

-- 
http://bodrato.it/papers/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cmp.c
Type: text/x-csrc
Size: 5077 bytes
Desc: not available
URL: <https://gmplib.org/list-archives/gmp-devel/attachments/20150820/be156ed5/attachment.bin>


More information about the gmp-devel mailing list