Austin Schuh | dace2a6 | 2020-08-18 10:56:48 -0700 | [diff] [blame^] | 1 | /* mpz_lucnum2_ui -- calculate Lucas numbers. |
| 2 | |
| 3 | Copyright 2001, 2003, 2005, 2012, 2015, 2016, 2018 Free Software |
| 4 | Foundation, Inc. |
| 5 | |
| 6 | This file is part of the GNU MP Library. |
| 7 | |
| 8 | The GNU MP Library is free software; you can redistribute it and/or modify |
| 9 | it under the terms of either: |
| 10 | |
| 11 | * the GNU Lesser General Public License as published by the Free |
| 12 | Software Foundation; either version 3 of the License, or (at your |
| 13 | option) any later version. |
| 14 | |
| 15 | or |
| 16 | |
| 17 | * the GNU General Public License as published by the Free Software |
| 18 | Foundation; either version 2 of the License, or (at your option) any |
| 19 | later version. |
| 20 | |
| 21 | or both in parallel, as here. |
| 22 | |
| 23 | The GNU MP Library is distributed in the hope that it will be useful, but |
| 24 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 25 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 26 | for more details. |
| 27 | |
| 28 | You should have received copies of the GNU General Public License and the |
| 29 | GNU Lesser General Public License along with the GNU MP Library. If not, |
| 30 | see https://www.gnu.org/licenses/. */ |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include "gmp-impl.h" |
| 34 | |
| 35 | |
| 36 | void |
| 37 | mpz_lucnum2_ui (mpz_ptr ln, mpz_ptr lnsub1, unsigned long n) |
| 38 | { |
| 39 | mp_ptr lp, l1p, f1p; |
| 40 | mp_size_t size; |
| 41 | mp_limb_t c; |
| 42 | TMP_DECL; |
| 43 | |
| 44 | ASSERT (ln != lnsub1); |
| 45 | |
| 46 | /* handle small n quickly, and hide the special case for L[-1]=-1 */ |
| 47 | if (n <= FIB_TABLE_LUCNUM_LIMIT) |
| 48 | { |
| 49 | mp_limb_t f = FIB_TABLE (n); |
| 50 | mp_limb_t f1 = FIB_TABLE ((int) n - 1); |
| 51 | |
| 52 | /* L[n] = F[n] + 2F[n-1] */ |
| 53 | MPZ_NEWALLOC (ln, 1)[0] = f + 2*f1; |
| 54 | SIZ(ln) = 1; |
| 55 | |
| 56 | /* L[n-1] = 2F[n] - F[n-1], but allow for L[-1]=-1 */ |
| 57 | MPZ_NEWALLOC (lnsub1, 1)[0] = (n == 0 ? 1 : 2*f - f1); |
| 58 | SIZ(lnsub1) = (n == 0 ? -1 : 1); |
| 59 | |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | TMP_MARK; |
| 64 | size = MPN_FIB2_SIZE (n); |
| 65 | f1p = TMP_ALLOC_LIMBS (size); |
| 66 | |
| 67 | lp = MPZ_NEWALLOC (ln, size+1); |
| 68 | l1p = MPZ_NEWALLOC (lnsub1, size+1); |
| 69 | |
| 70 | size = mpn_fib2_ui (l1p, f1p, n); |
| 71 | |
| 72 | /* L[n] = F[n] + 2F[n-1] */ |
| 73 | #if HAVE_NATIVE_mpn_addlsh1_n |
| 74 | c = mpn_addlsh1_n (lp, l1p, f1p, size); |
| 75 | #else |
| 76 | c = mpn_lshift (lp, f1p, size, 1); |
| 77 | c += mpn_add_n (lp, lp, l1p, size); |
| 78 | #endif |
| 79 | lp[size] = c; |
| 80 | SIZ(ln) = size + (c != 0); |
| 81 | |
| 82 | /* L[n-1] = 2F[n] - F[n-1] */ |
| 83 | #if HAVE_NATIVE_mpn_rsblsh1_n |
| 84 | c = mpn_rsblsh1_n (l1p, f1p, l1p, size); |
| 85 | #else |
| 86 | c = mpn_lshift (l1p, l1p, size, 1); |
| 87 | c -= mpn_sub_n (l1p, l1p, f1p, size); |
| 88 | #endif |
| 89 | ASSERT ((mp_limb_signed_t) c >= 0); |
| 90 | l1p[size] = c; |
| 91 | SIZ(lnsub1) = size + (c != 0); |
| 92 | |
| 93 | TMP_FREE; |
| 94 | } |