1 /* mpz_lucnum_ui -- calculate Lucas number.
3 Copyright 2001, 2003, 2005, 2011, 2012 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
36 /* change this to "#define TRACE(x) x" for diagnostics */
42 For the +4 in L[2k+1] when k is even, all L[4m+3] == 4, 5 or 7 mod 8, so
43 there can't be an overflow applying +4 to just the low limb (since that
44 would leave 0, 1, 2 or 3 mod 8).
46 For the -4 in L[2k+1] when k is even, it seems (no proof) that
47 L[3*2^(b-2)-3] == -4 mod 2^b, so for instance with a 32-bit limb
48 L[0xBFFFFFFD] == 0xFFFFFFFC mod 2^32, and this implies a borrow from the
49 low limb. Obviously L[0xBFFFFFFD] is a huge number, but it's at least
50 conceivable to calculate it, so it probably should be handled.
52 For the -2 in L[2k] with k even, it seems (no proof) L[2^(b-1)] == -1 mod
53 2^b, so for instance in 32-bits L[0x80000000] has a low limb of
54 0xFFFFFFFF so there would have been a borrow. Again L[0x80000000] is
55 obviously huge, but probably should be made to work. */
58 mpz_lucnum_ui (mpz_ptr ln
, unsigned long n
)
60 mp_size_t lalloc
, xalloc
, lsize
, xsize
;
66 TRACE (printf ("mpn_lucnum_ui n=%lu\n", n
));
68 if (n
<= FIB_TABLE_LUCNUM_LIMIT
)
70 /* L[n] = F[n] + 2F[n-1] */
71 PTR(ln
)[0] = FIB_TABLE(n
) + 2 * FIB_TABLE ((int) n
- 1);
76 /* +1 since L[n]=F[n]+2F[n-1] might be 1 limb bigger than F[n], further +1
77 since square or mul used below might need an extra limb over the true
79 lalloc
= MPN_FIB2_SIZE (n
) + 2;
80 lp
= MPZ_REALLOC (ln
, lalloc
);
84 xp
= TMP_ALLOC_LIMBS (xalloc
);
86 /* Strip trailing zeros from n, until either an odd number is reached
87 where the L[2k+1] formula can be used, or until n fits within the
88 FIB_TABLE data. The table is preferred of course. */
94 /* L[2k+1] = 5*F[k-1]*(2*F[k]+F[k-1]) - 4*(-1)^k */
96 mp_size_t yalloc
, ysize
;
99 TRACE (printf (" initial odd n=%lu\n", n
));
101 yalloc
= MPN_FIB2_SIZE (n
/2);
102 yp
= TMP_ALLOC_LIMBS (yalloc
);
103 ASSERT (xalloc
>= yalloc
);
105 xsize
= mpn_fib2_ui (xp
, yp
, n
/2);
107 /* possible high zero on F[k-1] */
109 ysize
-= (yp
[ysize
-1] == 0);
110 ASSERT (yp
[ysize
-1] != 0);
112 /* xp = 2*F[k] + F[k-1] */
113 #if HAVE_NATIVE_mpn_addlsh1_n
114 c
= mpn_addlsh1_n (xp
, yp
, xp
, xsize
);
116 c
= mpn_lshift (xp
, xp
, xsize
, 1);
117 c
+= mpn_add_n (xp
, xp
, yp
, xsize
);
119 ASSERT (xalloc
>= xsize
+1);
122 ASSERT (xp
[xsize
-1] != 0);
124 ASSERT (lalloc
>= xsize
+ ysize
);
125 c
= mpn_mul (lp
, xp
, xsize
, yp
, ysize
);
126 lsize
= xsize
+ ysize
;
130 #if HAVE_NATIVE_mpn_addlsh2_n
131 c
= mpn_addlsh2_n (lp
, lp
, lp
, lsize
);
133 /* FIXME: Is this faster than mpn_mul_1 ? */
134 c
= mpn_lshift (xp
, lp
, lsize
, 2);
135 c
+= mpn_add_n (lp
, lp
, xp
, lsize
);
137 ASSERT (lalloc
>= lsize
+1);
141 /* lp = lp - 4*(-1)^k */
144 /* no overflow, see comments above */
145 ASSERT (lp
[0] <= MP_LIMB_T_MAX
-4);
150 /* won't go negative */
151 MPN_DECR_U (lp
, lsize
, CNST_LIMB(4));
154 TRACE (mpn_trace (" l",lp
, lsize
));
158 MP_PTR_SWAP (xp
, lp
); /* balance the swaps wanted in the L[2k] below */
162 if (n
<= FIB_TABLE_LUCNUM_LIMIT
)
164 /* L[n] = F[n] + 2F[n-1] */
165 lp
[0] = FIB_TABLE (n
) + 2 * FIB_TABLE ((int) n
- 1);
168 TRACE (printf (" initial small n=%lu\n", n
);
169 mpn_trace (" l",lp
, lsize
));
174 for ( ; zeros
!= 0; zeros
--)
176 /* L[2k] = L[k]^2 + 2*(-1)^k */
178 TRACE (printf (" zeros=%d\n", zeros
));
180 ASSERT (xalloc
>= 2*lsize
);
181 mpn_sqr (xp
, lp
, lsize
);
183 lsize
-= (xp
[lsize
-1] == 0);
185 /* First time around the loop k==n determines (-1)^k, after that k is
186 always even and we set n=0 to indicate that. */
189 /* L[n]^2 == 0 or 1 mod 4, like all squares, so +2 gives no carry */
190 ASSERT (xp
[0] <= MP_LIMB_T_MAX
-2);
196 /* won't go negative */
197 MPN_DECR_U (xp
, lsize
, CNST_LIMB(2));
200 MP_PTR_SWAP (xp
, lp
);
201 ASSERT (lp
[lsize
-1] != 0);
204 /* should end up in the right spot after all the xp/lp swaps */
205 ASSERT (lp
== PTR(ln
));