Micro-GMP

Vincent Lefevre vincent at vinc17.net
Fri Nov 30 15:24:46 UTC 2018


On 2018-11-30 15:57:24 +0100, Marco Bodrato wrote:
> By the way, the code Paul wrote and I propose to simplify is the following.
> 
> /* MPZ assignment and basic conversions. */
> /* function changed by PZ */
> void
> mpz_set_si (mpz_t r, signed long int x)
> {
>   if (x >= 0)
>     mpz_set_ui (r, x);
>   else /* (x < 0) */
>     {
>       if (x == LONG_MIN) /* special case for 2^-k */
>         {
>           mpz_set_ui (r, -(x + 1));
>           mpz_add_ui (r, r, 1);
>         }
>       else
>         mpz_set_ui (r, -x);
>       mpz_neg (r, r);
>     }
> }

I don't see why Paul does this. It seems that Paul was assuming
that mpz_set_ui accepted an integer between 0 and LONG_MAX,
instead of between 0 and ULONG_MAX.

> Following the flow Paul uses, maybe we should change the macro to
> 
> #define GMP_NEG_CAST(T,x) (((T) -((x) + 1)) - 1)

With the correct signs:

#define GMP_NEG_CAST(T,x) (((T) -((x) + 1)) + 1)

but I still don't see any advantage.

-- 
Vincent Lefèvre <vincent at vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


More information about the gmp-devel mailing list