Exemplo n.º 1
0
void
mpz_lucnum2_ui (mpz_ptr ln, mpz_ptr lnsub1, unsigned long n)
{
  mp_ptr     lp, l1p, f1p;
  mp_size_t  size;
  mp_limb_t  c;
  TMP_DECL;

  ASSERT (ln != lnsub1);

  /* handle small n quickly, and hide the special case for L[-1]=-1 */
  if (n <= FIB_TABLE_LUCNUM_LIMIT)
    {
      mp_limb_t  f  = FIB_TABLE (n);
      mp_limb_t  f1 = FIB_TABLE ((int) n - 1);

      /* L[n] = F[n] + 2F[n-1] */
      PTR(ln)[0] = f + 2*f1;
      SIZ(ln) = 1;

      /* L[n-1] = 2F[n] - F[n-1], but allow for L[-1]=-1 */
      PTR(lnsub1)[0] = (n == 0 ? 1 : 2*f - f1);
      SIZ(lnsub1) = (n == 0 ? -1 : 1);

      return;
    }

  TMP_MARK;
  size = MPN_FIB2_SIZE (n);
  f1p = TMP_ALLOC_LIMBS (size);

  MPZ_REALLOC (ln,     size+1);
  MPZ_REALLOC (lnsub1, size+1);
  lp  = PTR(ln);
  l1p = PTR(lnsub1);

  size = mpn_fib2_ui (l1p, f1p, n);

  /* L[n] = F[n] + 2F[n-1] */
#if HAVE_NATIVE_mpn_addlsh1_n
  c = mpn_addlsh1_n (lp, l1p, f1p, size);
#else
  c = mpn_lshift1 (lp, f1p, size);
  c += mpn_add_n (lp, lp, l1p, size);
#endif
  lp[size] = c;
  SIZ(ln) = size + (c != 0);

  /* L[n-1] = 2F[n] - F[n-1] */
  c = mpn_double (l1p, size);
  c -= mpn_sub_n (l1p, l1p, f1p, size);
  ASSERT ((mp_limb_signed_t) c >= 0);
  l1p[size] = c;
  SIZ(lnsub1) = size + (c != 0);

  TMP_FREE;
}
Exemplo n.º 2
0
void
mpz_fib2_ui (mpz_ptr fn, mpz_ptr fnsub1, mpir_ui n)
{
  mp_ptr     fp, f1p;
  mp_size_t  size;

  size = MPN_FIB2_SIZE (n);
  MPZ_REALLOC (fn,     size);
  MPZ_REALLOC (fnsub1, size);
  fp = PTR (fn);
  f1p = PTR (fnsub1);

  size = mpn_fib2_ui (fp, f1p, n);

  SIZ(fn)     = size - (n == 0);
  SIZ(fnsub1) = size - (f1p[size-1] == 0);
}
Exemplo n.º 3
0
void
mpz_fib2_ui (mpz_ptr fn, mpz_ptr fnsub1, unsigned long n)
{
  mp_ptr     fp, f1p;
  mp_size_t  size;

  if (n <= FIB_TABLE_LIMIT)
    {
      MPZ_NEWALLOC (fn, 1)[0] = FIB_TABLE (n);
      SIZ(fn) = (n != 0);      /* F[0]==0, others are !=0 */
      MPZ_NEWALLOC (fnsub1, 1)[0] = FIB_TABLE ((int) n - 1);
      SIZ(fnsub1) = (n != 1);  /* F[1-1]==0, others are !=0 */
      return;
    }

  size = MPN_FIB2_SIZE (n);
  fp =  MPZ_NEWALLOC (fn,     size);
  f1p = MPZ_NEWALLOC (fnsub1, size);

  size = mpn_fib2_ui (fp, f1p, n);

  SIZ(fn)     = size;
  SIZ(fnsub1) = size - (f1p[size-1] == 0);
}
Exemplo n.º 4
0
int
main (int argc, char *argv[])
{
  unsigned long  n;
  unsigned long  limit = 100 * BITS_PER_MP_LIMB;
  mpz_t          want_fn, want_fn1, got_fn, got_fn1;

  tests_start ();
  mp_trace_base = -16;
  if (argc > 1 && argv[1][0] == 'x')
    limit = ULONG_MAX;
  else if (argc > 1)
    limit = atoi (argv[1]);

  check_fib_table ();

  /* start at n==0 */
  mpz_init_set_ui (want_fn1, 1);  /* F[-1] */
  mpz_init_set_ui (want_fn,  0);  /* F[0]   */
  mpz_init (got_fn);
  mpz_init (got_fn1);

  for (n = 0; n < limit; n++)
    {
      /* check our float formula seems right */
      if (MPZ_FIB_SIZE_FLOAT (n) < SIZ(want_fn))
        {
          printf ("MPZ_FIB_SIZE_FLOAT wrong at n=%lu\n", n);
          printf ("  MPZ_FIB_SIZE_FLOAT  %ld\n", MPZ_FIB_SIZE_FLOAT (n));
          printf ("  SIZ(want_fn)        %d\n", SIZ(want_fn));
          abort ();
        }

      /* check MPN_FIB2_SIZE seems right, compared to actual size and
         compared to our float formula */
      if (MPN_FIB2_SIZE (n) < MPZ_FIB_SIZE_FLOAT (n))
        {
          printf ("MPN_FIB2_SIZE wrong at n=%lu\n", n);
          printf ("  MPN_FIB2_SIZE       %ld\n", MPN_FIB2_SIZE (n));
          printf ("  MPZ_FIB_SIZE_FLOAT  %ld\n", MPZ_FIB_SIZE_FLOAT (n));
          abort ();
        }
      if (MPN_FIB2_SIZE (n) < SIZ(want_fn))
        {
          printf ("MPN_FIB2_SIZE wrong at n=%lu\n", n);
          printf ("  MPN_FIB2_SIZE  %ld\n", MPN_FIB2_SIZE (n));
          printf ("  SIZ(want_fn)   %d\n", SIZ(want_fn));
          abort ();
        }

      mpz_fib2_ui (got_fn, got_fn1, n);
      MPZ_CHECK_FORMAT (got_fn);
      MPZ_CHECK_FORMAT (got_fn1);
      if (mpz_cmp (got_fn, want_fn) != 0 || mpz_cmp (got_fn1, want_fn1) != 0)
        {
          printf ("mpz_fib2_ui(%lu) wrong\n", n);
          mpz_trace ("want fn ", want_fn);
          mpz_trace ("got  fn ",  got_fn);
          mpz_trace ("want fn1", want_fn1);
          mpz_trace ("got  fn1",  got_fn1);
          abort ();
        }

      mpz_fib_ui (got_fn, n);
      MPZ_CHECK_FORMAT (got_fn);
      if (mpz_cmp (got_fn, want_fn) != 0)
        {
          printf ("mpz_fib_ui(%lu) wrong\n", n);
          mpz_trace ("want fn", want_fn);
          mpz_trace ("got  fn", got_fn);
          abort ();
        }

      mpz_add (want_fn1, want_fn1, want_fn);  /* F[n+1] = F[n] + F[n-1] */
      mpz_swap (want_fn1, want_fn);
    }

  mpz_clear (want_fn);
  mpz_clear (want_fn1);
  mpz_clear (got_fn);
  mpz_clear (got_fn1);

  tests_end ();
  exit (0);
}