New mini-gmp macro GMP_CMP?

Marco Bodrato bodrato at mail.dm.unipi.it
Wed Nov 23 21:49:16 UTC 2016


Ciao,

Il Mar, 22 Novembre 2016 9:32 am, Niels Möller ha scritto:
> I was looking at how to simplify mini-gmp's mpz_cmp_si. I considered
> using mpz_cmpabs_ui to handle the case of both inputs negative, which

Using cmpabs_ui you can reduce from four cases in cmp_si to three, correct?
Not a bad idea, IMO.

> Resulting context patch below, reducing the code size by a dozen of
> lines. Do you think this is also an improvement in clarity?

Yes it is.

> + #define GMP_CMP(a,b) (((a) > (b)) - ((a) < (b)))

> *************** mpz_cmp_ui (const mpz_t u, unsigned long
> *** 1814,1821 ****
>       return -1;
>     else
> !     {
> !       mp_limb_t ul = (usize > 0) ? u->_mp_d[0] : 0;
> !       return (ul > v) - (ul < v);
> !     }
>   }
>
> --- 1808,1812 ----
>       return -1;
>     else
> !     return GMP_CMP (mpz_get_ui (u), v);
>   }

With this changes we leave to the compiler the task of inlining mpz_get_ui
and detect the common parts... the full task of code optimisation.
If we decide for this approach, we may also
#define GMP_CMP(a,b) (((a) < (b)) ? -1 : ((a) > (b)))
It is not as elegant as, nor symmetric, but maybe it's easier to understand.

Regards,
m

-- 
http://bodrato.it/papers/



More information about the gmp-devel mailing list