aboutsummaryrefslogtreecommitdiff
path: root/gmp-6.3.0/mpn/generic/fib2m.c
blob: 89d2b869b62bfbdb044ea51fd5c51b05d027f7e8 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/* mpn_fib2m -- calculate Fibonacci numbers, modulo m.

Contributed to the GNU project by Marco Bodrato, based on the previous
fib2_ui.c file.

   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, 2018 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"
#include "longlong.h"


/* Stores |{ap,n}-{bp,n}| in {rp,n},
   returns the sign of {ap,n}-{bp,n}. */
static int
abs_sub_n (mp_ptr rp, mp_srcptr ap, mp_srcptr bp, mp_size_t n)
{
  mp_limb_t  x, y;
  while (--n >= 0)
    {
      x = ap[n];
      y = bp[n];
      if (x != y)
        {
          ++n;
          if (x > y)
            {
              ASSERT_NOCARRY (mpn_sub_n (rp, ap, bp, n));
              return 1;
            }
          else
            {
              ASSERT_NOCARRY (mpn_sub_n (rp, bp, ap, n));
              return -1;
            }
        }
      rp[n] = 0;
    }
  return 0;
}

/* Store F[n] at fp and F[n-1] at f1p.  Both are computed modulo m.
   fp and f1p should have room for mn*2+1 limbs.

   The sign of one or both the values may be flipped (n-F, instead of F),
   the return value is 0 (zero) if the signs are coherent (both positive
   or both negative) and 1 (one) otherwise.

   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 F[k-1]^2 just by ORing into the
   low limb.

   TODO: Should {tp, 2 * mn} be passed as a scratch pointer?
   Should the call to mpn_fib2_ui() obtain (up to) 2*mn limbs?
*/

int
mpn_fib2m (mp_ptr fp, mp_ptr f1p, mp_srcptr np, mp_size_t nn, mp_srcptr mp, mp_size_t mn)
{
  unsigned long	nfirst;
  mp_limb_t	nh;
  mp_bitcnt_t	nbi;
  mp_size_t	sn, fn;
  int		fcnt, ncnt;

  ASSERT (! MPN_OVERLAP_P (fp, MAX(2*mn+1,5), f1p, MAX(2*mn+1,5)));
  ASSERT (nn > 0 && np[nn - 1] != 0);

  /* Estimate the maximal n such that fibonacci(n) fits in mn limbs. */
#if GMP_NUMB_BITS % 16 == 0
  if (UNLIKELY (ULONG_MAX / (23 * (GMP_NUMB_BITS / 16)) <= mn))
    nfirst = ULONG_MAX;
  else
    nfirst = mn * (23 * (GMP_NUMB_BITS / 16));
#else
  {
    mp_bitcnt_t	mbi;
    mbi = (mp_bitcnt_t) mn * GMP_NUMB_BITS;

    if (UNLIKELY (ULONG_MAX / 23 < mbi))
      {
	if (UNLIKELY (ULONG_MAX / 23 * 16 <= mbi))
	  nfirst = ULONG_MAX;
	else
	  nfirst = mbi / 16 * 23;
      }
    else
      nfirst = mbi * 23 / 16;
  }
#endif

  sn = nn - 1;
  nh = np[sn];
  count_leading_zeros (ncnt, nh);
  count_leading_zeros (fcnt, nfirst);

  if (fcnt >= ncnt)
    {
      ncnt = fcnt - ncnt;
      nh >>= ncnt;
    }
  else if (sn > 0)
    {
      ncnt -= fcnt;
      nh <<= ncnt;
      ncnt = GMP_NUMB_BITS - ncnt;
      --sn;
      nh |= np[sn] >> ncnt;
    }
  else
    ncnt = 0;

  nbi = sn * GMP_NUMB_BITS + ncnt;
  if (nh > nfirst)
    {
      nh >>= 1;
      ++nbi;
    }

  ASSERT (nh <= nfirst);
  /* Take a starting pair from mpn_fib2_ui. */
  fn = mpn_fib2_ui (fp, f1p, nh);
  MPN_ZERO (fp + fn, mn - fn);
  MPN_ZERO (f1p + fn, mn - fn);

  if (nbi == 0)
    {
      if (fn == mn)
	{
	  mp_limb_t qp[2];
	  mpn_tdiv_qr (qp, fp, 0, fp, fn, mp, mn);
	  mpn_tdiv_qr (qp, f1p, 0, f1p, fn, mp, mn);
	}

      return 0;
    }
  else
    {
      mp_ptr	tp;
      unsigned	pb = nh & 1;
      int	neg;
      TMP_DECL;

      TMP_MARK;

      tp = TMP_ALLOC_LIMBS (2 * mn + (mn < 2));

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

	     Based on the next bit of n, 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.  */

	  mpn_sqr (tp, fp,  mn);
	  mpn_sqr (fp, f1p, mn);

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

	  /* Calculate F[2k+1] = 4*F[k]^2 - F[k-1]^2 + 2*(-1)^k.
	     pb is the low bit of our implied k.  */

	  /* fp is F[k-1]^2 == 0 or 1 mod 4, like all squares. */
	  ASSERT ((fp[0] & 2) == 0);
	  ASSERT (pb == (pb & 1));
	  ASSERT ((fp[0] + (pb ? 2 : 0)) == (fp[0] | (pb << 1)));
	  fp[0] |= pb << 1;		/* possible -2 */
#if HAVE_NATIVE_mpn_rsblsh2_n
	  fp[2 * mn] = 1 + mpn_rsblsh2_n (fp, fp, tp, 2 * mn);
	  MPN_INCR_U(fp, 2 * mn + 1, (1 ^ pb) << 1);	/* possible +2 */
	  fp[2 * mn] = (fp[2 * mn] - 1) & GMP_NUMB_MAX;
#else
	  {
	    mp_limb_t  c;

	    c = mpn_lshift (tp, tp, 2 * mn, 2);
	    tp[0] |= (1 ^ pb) << 1;	/* possible +2 */
	    c -= mpn_sub_n (fp, tp, fp, 2 * mn);
	    fp[2 * mn] = c & GMP_NUMB_MAX;
	  }
#endif
	  neg = fp[2 * mn] == GMP_NUMB_MAX;

	  /* Calculate F[2k-1] = F[k]^2 + F[k-1]^2 */
	  /* Calculate F[2k+1] = 4*F[k]^2 - F[k-1]^2 + 2*(-1)^k */

	  /* Calculate F[2k] = F[2k+1] - F[2k-1], replacing the unwanted one of
	     F[2k+1] and F[2k-1].  */
	  --nbi;
	  pb = (np [nbi / GMP_NUMB_BITS] >> (nbi % GMP_NUMB_BITS)) & 1;
	  rp = pb ? f1p : fp;
	  if (neg)
	    {
	      /* Calculate -(F[2k+1] - F[2k-1]) */
	      rp[2 * mn] = f1p[2 * mn] + 1 - mpn_sub_n (rp, f1p, fp, 2 * mn);
	      neg = ! pb;
	      if (pb) /* fp not overwritten, negate it. */
		fp [2 * mn] = 1 ^ mpn_neg (fp, fp, 2 * mn);
	    }
	  else
	    {
	      neg = abs_sub_n (rp, fp, f1p, 2 * mn + 1) < 0;
	    }

	  mpn_tdiv_qr (tp, fp, 0, fp, 2 * mn + 1, mp, mn);
	  mpn_tdiv_qr (tp, f1p, 0, f1p, 2 * mn + 1, mp, mn);
	}
      while (nbi != 0);

      TMP_FREE;

      return neg;
    }
}