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 of the macro? Well, it's simple. When we write a piece of code for GMP, we do not want to take care of the best sequence to negate a negative number and cast it to unsigned, 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 :-) PS: are we sure that LONG_MIN has the form -2^k? Does it depend on the one-complement or two-complement representation? Ĝis, m -- http://bodrato.it/