summaryrefslogtreecommitdiff
path: root/mpz/lucnum2_ui.c
diff options
context:
space:
mode:
authorKevin Ryde <user42@zip.com.au>2001-06-07 22:36:38 +0200
committerKevin Ryde <user42@zip.com.au>2001-06-07 22:36:38 +0200
commit9cc56a7c60b0e2392b36c0dcac5a06af7154adbb (patch)
tree87970d60ebcbfbcbdbd172ff13e4e7ea7d243047 /mpz/lucnum2_ui.c
parent0d7744a71dad2811fc9e97f4e3a8d402ca017ee8 (diff)
downloadgmp-9cc56a7c60b0e2392b36c0dcac5a06af7154adbb.tar.gz
* mpz/fib2_ui.c, mpz/lucnum_ui.c mpz/lucnum2_ui.c: New files.
Diffstat (limited to 'mpz/lucnum2_ui.c')
-rw-r--r--mpz/lucnum2_ui.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/mpz/lucnum2_ui.c b/mpz/lucnum2_ui.c
new file mode 100644
index 000000000..2e92f02df
--- /dev/null
+++ b/mpz/lucnum2_ui.c
@@ -0,0 +1,79 @@
+/* mpz_lucnum2_ui -- calculate Lucas numbers.
+
+Copyright 2001 Free Software Foundation, Inc.
+
+This file is part of the GNU MP Library.
+
+The GNU MP Library is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or (at your
+option) any later version.
+
+The GNU MP Library is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+License for more details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with the GNU MP Library; see the file COPYING.LIB. If not, write to
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+MA 02111-1307, USA. */
+
+#include <stdio.h>
+#include "gmp.h"
+#include "gmp-impl.h"
+
+
+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 (marker);
+
+ 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 (marker);
+ 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] */
+ c = mpn_lshift (lp, f1p, size, 1);
+ c += mpn_add_n (lp, lp, l1p, size);
+ lp[size] = c;
+ SIZ(ln) = size + (c != 0);
+
+ /* L[n-1] = 2F[n] - F[n-1] */
+ c = mpn_lshift (l1p, l1p, size, 1);
+ 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 (marker);
+}