summaryrefslogtreecommitdiff
path: root/mpn/generic/fib2_ui.c
blob: b6ff651f87bb0c753b8b44abf3f9d2749d7fe31b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* mpn_fib2_ui -- calculate Fibonacci numbers.

   THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
   CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
   FUTURE GNU MP RELEASES.

Copyright 2001, 2002, 2005, 2009 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 either:

  * the GNU Lesser General Public License as published by the Free
    Software Foundation; either version 3 of the License, or (at your
    option) any later version.

or

  * the GNU General Public License as published by the Free Software
    Foundation; either version 2 of the License, or (at your option) any
    later version.

or both in parallel, as here.

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 General Public License
for more details.

You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library.  If not,
see https://www.gnu.org/licenses/.  */

#include <stdio.h>
#include "gmp-impl.h"

/* change this to "#define TRACE(x) x" for diagnostics */
#define TRACE(x)


/* Store F[n] at fp and F[n-1] at f1p.  fp and f1p should have room for
   MPN_FIB2_SIZE(n) limbs.

   The return value is the actual number of limbs stored, this will be at
   least 1.  fp[size-1] will be non-zero, except when n==0, in which case
   fp[0] is 0 and f1p[0] is 1.  f1p[size-1] can be zero, since F[n-1]<F[n]
   (for n>0).

   Notes:

   In F[2k+1] with k even, +2 is applied to 4*F[k]^2 just by ORing into the
   low limb.

   In F[2k+1] with k odd, -2 is applied to the low limb of 4*F[k]^2 -
   F[k-1]^2.  This F[2k+1] is an F[4m+3] and such numbers are congruent to
   1, 2 or 5 mod 8, which means no underflow reaching it with a -2 (since
   that would leave 6 or 7 mod 8).

   This property of F[4m+3] can be verified by induction on F[4m+3] =
   7*F[4m-1] - F[4m-5], that formula being a standard lucas sequence
   identity U[i+j] = U[i]*V[j] - U[i-j]*Q^j.
*/

mp_size_t
mpn_fib2_ui (mp_ptr fp, mp_ptr f1p, unsigned long int n)
{
  mp_size_t      size;
  unsigned long  nfirst, mask;

  TRACE (printf ("mpn_fib2_ui n=%lu\n", n));

  ASSERT (! MPN_OVERLAP_P (fp, MPN_FIB2_SIZE(n), f1p, MPN_FIB2_SIZE(n)));

  /* Take a starting pair from the table. */
  mask = 1;
  for (nfirst = n; nfirst > FIB_TABLE_LIMIT; nfirst /= 2)
    mask <<= 1;
  TRACE (printf ("nfirst=%lu mask=0x%lX\n", nfirst, mask));

  f1p[0] = FIB_TABLE ((int) nfirst - 1);
  fp[0]  = FIB_TABLE (nfirst);
  size = 1;

  /* Skip to the end if the table lookup gives the final answer. */
  if (mask != 1)
    {
      mp_size_t  alloc;
      mp_ptr        xp;
      TMP_DECL;

      TMP_MARK;
      alloc = MPN_FIB2_SIZE (n);
      xp = TMP_ALLOC_LIMBS (alloc);

      do
	{
	  /* Here fp==F[k] and f1p==F[k-1], with k being the bits of n from
	     n&mask upwards.

	     The next bit of n is n&(mask>>1) and we'll double to the pair
	     fp==F[2k],f1p==F[2k-1] or fp==F[2k+1],f1p==F[2k], according as
	     that bit is 0 or 1 respectively.  */

	  TRACE (printf ("k=%lu mask=0x%lX size=%ld alloc=%ld\n",
			 n >> refmpn_count_trailing_zeros(mask),
			 mask, size, alloc);
		 mpn_trace ("fp ", fp, size);
		 mpn_trace ("f1p", f1p, size));

	  /* fp normalized, f1p at most one high zero */
	  ASSERT (fp[size-1] != 0);
	  ASSERT (f1p[size-1] != 0 || f1p[size-2] != 0);

	  /* f1p[size-1] might be zero, but this occurs rarely, so it's not
	     worth bothering checking for it */
	  ASSERT (alloc >= 2*size);
	  mpn_sqr (xp, fp,  size);
	  mpn_sqr (fp, f1p, size);
	  size *= 2;

	  /* Shrink if possible.  Since fp was normalized there'll be at
	     most one high zero on xp (and if there is then there's one on
	     yp too).  */
	  ASSERT (xp[size-1] != 0 || fp[size-1] == 0);
	  size -= (xp[size-1] == 0);
	  ASSERT (xp[size-1] != 0);  /* only one xp high zero */

	  /* Calculate F[2k-1] = F[k]^2 + F[k-1]^2. */
	  f1p[size] = mpn_add_n (f1p, xp, fp, size);

	  /* Calculate F[2k+1] = 4*F[k]^2 - F[k-1]^2 + 2*(-1)^k.
	     n&mask is the low bit of our implied k.  */
#if HAVE_NATIVE_mpn_rsblsh2_n
	  fp[size] = mpn_rsblsh2_n (fp, fp, xp, size);
	  if ((n & mask) == 0)
	    MPN_INCR_U(fp, size + 1, 2);	/* possible +2 */
	  else
	  {
	    ASSERT (fp[0] >= 2);
	    fp[0] -= 2;				/* possible -2 */
	  }
#else
	  {
	    mp_limb_t  c;

	    c = mpn_lshift (xp, xp, size, 2);
	    xp[0] |= (n & mask ? 0 : 2);	/* possible +2 */
	    c -= mpn_sub_n (fp, xp, fp, size);
	    ASSERT (n & mask ? fp[0] != 0 && fp[0] != 1 : 1);
	    fp[0] -= (n & mask ? 2 : 0);	/* possible -2 */
	    fp[size] = c;
	  }
#endif
	  ASSERT (alloc >= size+1);
	  size += (fp[size] != 0);

	  /* now n&mask is the new bit of n being considered */
	  mask >>= 1;

	  /* Calculate F[2k] = F[2k+1] - F[2k-1], replacing the unwanted one of
	     F[2k+1] and F[2k-1].  */
	  if (n & mask)
	    ASSERT_NOCARRY (mpn_sub_n (f1p, fp, f1p, size));
	  else {
	    ASSERT_NOCARRY (mpn_sub_n ( fp, fp, f1p, size));

	    /* Can have a high zero after replacing F[2k+1] with F[2k].
	       f1p will have a high zero if fp does. */
	    ASSERT (fp[size-1] != 0 || f1p[size-1] == 0);
	    size -= (fp[size-1] == 0);
	  }
	}
      while (mask != 1);

      TMP_FREE;
    }

  TRACE (printf ("done size=%ld\n", size);
	 mpn_trace ("fp ", fp, size);
	 mpn_trace ("f1p", f1p, size));

  return size;
}