mpz_limbs interface

bodrato at mail.dm.unipi.it bodrato at mail.dm.unipi.it
Sat Feb 8 07:00:19 UTC 2014


Ciao,

Il Gio, 6 Febbraio 2014 9:56 am, Niels ha scritto:
> What do you think?

>   void
>   mpz_set_d (mpz_ptr r, double d)
>   {
>     int negative;
> !   mp_limb_t tp[MPN_SET_D_SIZE];
> !   mpz_t t = MPZ_ROINIT_N (tp, MPN_SET_D_SIZE);

Uhm, in tests/mpz/t-limbs.c we test MPZ_ROINIT_N only
#if __STDC_VERSION__ >= 199901

Should we use it unconditionally in the library?

I'd suggest:

void
mpz_set_d (mpz_ptr r, double d)
{
  int negative;
  long exp;

  DOUBLE_NAN_INF_ACTION (d,
			  __gmp_invalid_operation (),
			  __gmp_invalid_operation ());

  negative = d < 0;
  d = ABS (d);

  if (d < 1.0)
    {
      SIZ(r) = 0;
      return;
    }

  exp = mpn_set_d (mpz_limbs_write (r, MPN_SET_D_SIZE), d);

  if (exp >= 0)
    mpz_mul_2exp (r, r, exp);
  else
    mpz_fdiv_q_2exp (r, r, -exp);

  if (negative)
    SIZ (r) = -SIZ (r);
}

Regards,
m

-- 
http://bodrato.it/software/



More information about the gmp-devel mailing list