Micro-GMP
Marco Bodrato
bodrato at mail.dm.unipi.it
Fri Nov 30 14:57:24 UTC 2018
Ciao,
Il Ven, 30 Novembre 2018 12:43 pm, Vincent Lefevre ha scritto:
> On 2018-11-30 07:43:40 +0100, Marco Bodrato wrote:
>> mpz_set_si (mpz_t r, signed long int x)
>> {
>> if (x >= 0)
>> mpz_set_ui (r, x);
>> else /* (x < 0) */
>> {
>> mpz_set_ui (r, GMP_NEG_CAST (unsigned long int, x));
>> mpz_neg (r, r);
>> }
>> }
>
> What is the purpose of GMP_NEG_CAST
The purpose? Well, it's simple. When we write a piece of code for GMP, we
do not want to take care of the best way to negate and cast, that's why we
use a macro :-)
> #define GMP_NEG_CAST(T,x) (-((T)((x) + 1) - 1))
> instead of just a cast followed by a negation? i.e.
> mpz_set_ui (r, - (unsigned long int) x);
I'm sure that the compiler can understand it anyway and do the right thing
:-)
When we will agree that the way you propose is the best one, we will
change the macro :-)
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);
}
}
Following the flow Paul uses, maybe we should change the macro to
#define GMP_NEG_CAST(T,x) (((T) -((x) + 1)) - 1)
Personally, I have no opinion about that. I'll keep on using the macro in
my code, however defined :-)
Ĝis,
m
--
http://bodrato.it/
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: new.txt
URL: <https://gmplib.org/list-archives/gmp-devel/attachments/20181130/87297c0f/attachment.txt>
More information about the gmp-devel
mailing list