[BACK]Return to lucnum2_ui.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpz

Annotation of OpenXM_contrib/gmp/mpz/lucnum2_ui.c, Revision 1.1.1.1

1.1       ohara       1: /* mpz_lucnum2_ui -- calculate Lucas numbers.
                      2:
                      3: Copyright 2001 Free Software Foundation, Inc.
                      4:
                      5: This file is part of the GNU MP Library.
                      6:
                      7: The GNU MP Library is free software; you can redistribute it and/or modify
                      8: it under the terms of the GNU Lesser General Public License as published by
                      9: the Free Software Foundation; either version 2.1 of the License, or (at your
                     10: option) any later version.
                     11:
                     12: The GNU MP Library is distributed in the hope that it will be useful, but
                     13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
                     15: License for more details.
                     16:
                     17: You should have received a copy of the GNU Lesser General Public License
                     18: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
                     19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     20: MA 02111-1307, USA. */
                     21:
                     22: #include <stdio.h>
                     23: #include "gmp.h"
                     24: #include "gmp-impl.h"
                     25:
                     26:
                     27: void
                     28: mpz_lucnum2_ui (mpz_ptr ln, mpz_ptr lnsub1, unsigned long n)
                     29: {
                     30:   mp_ptr     lp, l1p, f1p;
                     31:   mp_size_t  size;
                     32:   mp_limb_t  c;
                     33:   TMP_DECL (marker);
                     34:
                     35:   ASSERT (ln != lnsub1);
                     36:
                     37:   /* handle small n quickly, and hide the special case for L[-1]=-1 */
                     38:   if (n <= FIB_TABLE_LUCNUM_LIMIT)
                     39:     {
                     40:       mp_limb_t  f  = FIB_TABLE (n);
                     41:       mp_limb_t  f1 = FIB_TABLE ((int) n - 1);
                     42:
                     43:       /* L[n] = F[n] + 2F[n-1] */
                     44:       PTR(ln)[0] = f + 2*f1;
                     45:       SIZ(ln) = 1;
                     46:
                     47:       /* L[n-1] = 2F[n] - F[n-1], but allow for L[-1]=-1 */
                     48:       PTR(lnsub1)[0] = (n == 0 ? 1 : 2*f - f1);
                     49:       SIZ(lnsub1) = (n == 0 ? -1 : 1);
                     50:
                     51:       return;
                     52:     }
                     53:
                     54:   TMP_MARK (marker);
                     55:   size = MPN_FIB2_SIZE (n);
                     56:   f1p = TMP_ALLOC_LIMBS (size);
                     57:
                     58:   MPZ_REALLOC (ln,     size+1);
                     59:   MPZ_REALLOC (lnsub1, size+1);
                     60:   lp  = PTR(ln);
                     61:   l1p = PTR(lnsub1);
                     62:
                     63:   size = mpn_fib2_ui (l1p, f1p, n);
                     64:
                     65:   /* L[n] = F[n] + 2F[n-1] */
                     66:   c = mpn_lshift (lp, f1p, size, 1);
                     67:   c += mpn_add_n (lp, lp, l1p, size);
                     68:   lp[size] = c;
                     69:   SIZ(ln) = size + (c != 0);
                     70:
                     71:   /* L[n-1] = 2F[n] - F[n-1] */
                     72:   c = mpn_lshift (l1p, l1p, size, 1);
                     73:   c -= mpn_sub_n (l1p, l1p, f1p, size);
                     74:   ASSERT ((mp_limb_signed_t) c >= 0);
                     75:   l1p[size] = c;
                     76:   SIZ(lnsub1) = size + (c != 0);
                     77:
                     78:   TMP_FREE (marker);
                     79: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>