Blame mpz/lucnum2_ui.c

Packit 5c3484
/* mpz_lucnum2_ui -- calculate Lucas numbers.
Packit 5c3484
Packit 5c3484
Copyright 2001, 2003, 2005, 2012 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include <stdio.h>
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
Packit 5c3484
Packit 5c3484
void
Packit 5c3484
mpz_lucnum2_ui (mpz_ptr ln, mpz_ptr lnsub1, unsigned long n)
Packit 5c3484
{
Packit 5c3484
  mp_ptr     lp, l1p, f1p;
Packit 5c3484
  mp_size_t  size;
Packit 5c3484
  mp_limb_t  c;
Packit 5c3484
  TMP_DECL;
Packit 5c3484
Packit 5c3484
  ASSERT (ln != lnsub1);
Packit 5c3484
Packit 5c3484
  /* handle small n quickly, and hide the special case for L[-1]=-1 */
Packit 5c3484
  if (n <= FIB_TABLE_LUCNUM_LIMIT)
Packit 5c3484
    {
Packit 5c3484
      mp_limb_t  f  = FIB_TABLE (n);
Packit 5c3484
      mp_limb_t  f1 = FIB_TABLE ((int) n - 1);
Packit 5c3484
Packit 5c3484
      /* L[n] = F[n] + 2F[n-1] */
Packit 5c3484
      PTR(ln)[0] = f + 2*f1;
Packit 5c3484
      SIZ(ln) = 1;
Packit 5c3484
Packit 5c3484
      /* L[n-1] = 2F[n] - F[n-1], but allow for L[-1]=-1 */
Packit 5c3484
      PTR(lnsub1)[0] = (n == 0 ? 1 : 2*f - f1);
Packit 5c3484
      SIZ(lnsub1) = (n == 0 ? -1 : 1);
Packit 5c3484
Packit 5c3484
      return;
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  TMP_MARK;
Packit 5c3484
  size = MPN_FIB2_SIZE (n);
Packit 5c3484
  f1p = TMP_ALLOC_LIMBS (size);
Packit 5c3484
Packit 5c3484
  lp  = MPZ_REALLOC (ln,     size+1);
Packit 5c3484
  l1p = MPZ_REALLOC (lnsub1, size+1);
Packit 5c3484
Packit 5c3484
  size = mpn_fib2_ui (l1p, f1p, n);
Packit 5c3484
Packit 5c3484
  /* L[n] = F[n] + 2F[n-1] */
Packit 5c3484
#if HAVE_NATIVE_mpn_addlsh1_n
Packit 5c3484
  c = mpn_addlsh1_n (lp, l1p, f1p, size);
Packit 5c3484
#else
Packit 5c3484
  c = mpn_lshift (lp, f1p, size, 1);
Packit 5c3484
  c += mpn_add_n (lp, lp, l1p, size);
Packit 5c3484
#endif
Packit 5c3484
  lp[size] = c;
Packit 5c3484
  SIZ(ln) = size + (c != 0);
Packit 5c3484
Packit 5c3484
  /* L[n-1] = 2F[n] - F[n-1] */
Packit 5c3484
  c = mpn_lshift (l1p, l1p, size, 1);
Packit 5c3484
  c -= mpn_sub_n (l1p, l1p, f1p, size);
Packit 5c3484
  ASSERT ((mp_limb_signed_t) c >= 0);
Packit 5c3484
  l1p[size] = c;
Packit 5c3484
  SIZ(lnsub1) = size + (c != 0);
Packit 5c3484
Packit 5c3484
  TMP_FREE;
Packit 5c3484
}