aboutsummaryrefslogtreecommitdiff
path: root/vendor/gmp-6.3.0/mpn/generic/toom6_sqr.c
blob: 336eef9d63d2176b9c17b20c95f549d377e6a241 (plain) (blame)
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
/* Implementation of the squaring algorithm with Toom-Cook 6.5-way.

   Contributed to the GNU project by Marco Bodrato.

   THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE.  IT IS ONLY
   SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
   GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.

Copyright 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 "gmp-impl.h"


#if GMP_NUMB_BITS < 21
#error Not implemented.
#endif


#if TUNE_PROGRAM_BUILD
#define MAYBE_sqr_basecase 1
#define MAYBE_sqr_above_basecase   1
#define MAYBE_sqr_toom2   1
#define MAYBE_sqr_above_toom2   1
#define MAYBE_sqr_toom3   1
#define MAYBE_sqr_above_toom3   1
#define MAYBE_sqr_above_toom4   1
#else
#ifdef  SQR_TOOM8_THRESHOLD
#define SQR_TOOM6_MAX ((SQR_TOOM8_THRESHOLD+6*2-1+5)/6)
#else
#define SQR_TOOM6_MAX					\
  ((SQR_FFT_THRESHOLD <= MP_SIZE_T_MAX - (6*2-1+5)) ?	\
   ((SQR_FFT_THRESHOLD+6*2-1+5)/6)			\
   : MP_SIZE_T_MAX )
#endif
#define MAYBE_sqr_basecase					\
  (SQR_TOOM6_THRESHOLD < 6 * SQR_TOOM2_THRESHOLD)
#define MAYBE_sqr_above_basecase				\
  (SQR_TOOM6_MAX >=  SQR_TOOM2_THRESHOLD)
#define MAYBE_sqr_toom2						\
  (SQR_TOOM6_THRESHOLD < 6 * SQR_TOOM3_THRESHOLD)
#define MAYBE_sqr_above_toom2					\
  (SQR_TOOM6_MAX >= SQR_TOOM3_THRESHOLD)
#define MAYBE_sqr_toom3						\
  (SQR_TOOM6_THRESHOLD < 6 * SQR_TOOM4_THRESHOLD)
#define MAYBE_sqr_above_toom3					\
  (SQR_TOOM6_MAX >= SQR_TOOM4_THRESHOLD)
#define MAYBE_sqr_above_toom4					\
  (SQR_TOOM6_MAX >= SQR_TOOM6_THRESHOLD)
#endif

#define TOOM6_SQR_REC(p, a, n, ws)					\
  do {									\
    if (MAYBE_sqr_basecase && ( !MAYBE_sqr_above_basecase		\
	|| BELOW_THRESHOLD (n, SQR_TOOM2_THRESHOLD)))			\
      mpn_sqr_basecase (p, a, n);					\
    else if (MAYBE_sqr_toom2 && ( !MAYBE_sqr_above_toom2		\
	     || BELOW_THRESHOLD (n, SQR_TOOM3_THRESHOLD)))		\
      mpn_toom2_sqr (p, a, n, ws);					\
    else if (MAYBE_sqr_toom3 && ( !MAYBE_sqr_above_toom3		\
	     || BELOW_THRESHOLD (n, SQR_TOOM4_THRESHOLD)))		\
      mpn_toom3_sqr (p, a, n, ws);					\
    else if (! MAYBE_sqr_above_toom4					\
	     || BELOW_THRESHOLD (n, SQR_TOOM6_THRESHOLD))		\
      mpn_toom4_sqr (p, a, n, ws);					\
    else								\
      mpn_toom6_sqr (p, a, n, ws);					\
  } while (0)

void
mpn_toom6_sqr  (mp_ptr pp, mp_srcptr ap, mp_size_t an, mp_ptr scratch)
{
  mp_size_t n, s;

  /***************************** decomposition *******************************/

  ASSERT( an >= 18 );

  n = 1 + (an - 1) / (size_t) 6;

  s = an - 5 * n;

  ASSERT (0 < s && s <= n);

#define   r4    (pp + 3 * n)			/* 3n+1 */
#define   r2    (pp + 7 * n)			/* 3n+1 */
#define   r0    (pp +11 * n)			/* s+t <= 2*n */
#define   r5    (scratch)			/* 3n+1 */
#define   r3    (scratch + 3 * n + 1)		/* 3n+1 */
#define   r1    (scratch + 6 * n + 2)		/* 3n+1 */
#define   v0    (pp + 7 * n)			/* n+1 */
#define   v2    (pp + 9 * n+2)			/* n+1 */
#define   wse   (scratch + 9 * n + 3)		/* 3n+1 */

  /* Alloc also 3n+1 limbs for ws... toom_interpolate_12pts may
     need all of them, when DO_mpn_sublsh_n usea a scratch  */
/*   if (scratch== NULL) */
/*     scratch = TMP_SALLOC_LIMBS (12 * n + 6); */

  /********************** evaluation and recursive calls *********************/
  /* $\pm1/2$ */
  mpn_toom_eval_pm2rexp (v2, v0, 5, ap, n, s, 1, pp);
  TOOM6_SQR_REC(pp, v0, n + 1, wse); /* A(-1/2)*B(-1/2)*2^. */
  TOOM6_SQR_REC(r5, v2, n + 1, wse); /* A(+1/2)*B(+1/2)*2^. */
  mpn_toom_couple_handling (r5, 2 * n + 1, pp, 0, n, 1, 0);

  /* $\pm1$ */
  mpn_toom_eval_pm1 (v2, v0, 5, ap, n, s,    pp);
  TOOM6_SQR_REC(pp, v0, n + 1, wse); /* A(-1)*B(-1) */
  TOOM6_SQR_REC(r3, v2, n + 1, wse); /* A(1)*B(1) */
  mpn_toom_couple_handling (r3, 2 * n + 1, pp, 0, n, 0, 0);

  /* $\pm4$ */
  mpn_toom_eval_pm2exp (v2, v0, 5, ap, n, s, 2, pp);
  TOOM6_SQR_REC(pp, v0, n + 1, wse); /* A(-4)*B(-4) */
  TOOM6_SQR_REC(r1, v2, n + 1, wse); /* A(+4)*B(+4) */
  mpn_toom_couple_handling (r1, 2 * n + 1, pp, 0, n, 2, 4);

  /* $\pm1/4$ */
  mpn_toom_eval_pm2rexp (v2, v0, 5, ap, n, s, 2, pp);
  TOOM6_SQR_REC(pp, v0, n + 1, wse); /* A(-1/4)*B(-1/4)*4^. */
  TOOM6_SQR_REC(r4, v2, n + 1, wse); /* A(+1/4)*B(+1/4)*4^. */
  mpn_toom_couple_handling (r4, 2 * n + 1, pp, 0, n, 2, 0);

  /* $\pm2$ */
  mpn_toom_eval_pm2 (v2, v0, 5, ap, n, s, pp);
  TOOM6_SQR_REC(pp, v0, n + 1, wse); /* A(-2)*B(-2) */
  TOOM6_SQR_REC(r2, v2, n + 1, wse); /* A(+2)*B(+2) */
  mpn_toom_couple_handling (r2, 2 * n + 1, pp, 0, n, 1, 2);

#undef v0
#undef v2

  /* A(0)*B(0) */
  TOOM6_SQR_REC(pp, ap, n, wse);

  mpn_toom_interpolate_12pts (pp, r1, r3, r5, n, 2 * s, 0, wse);

#undef r0
#undef r1
#undef r2
#undef r3
#undef r4
#undef r5

}
#undef TOOM6_SQR_REC
#undef MAYBE_sqr_basecase
#undef MAYBE_sqr_above_basecase
#undef MAYBE_sqr_toom2
#undef MAYBE_sqr_above_toom2
#undef MAYBE_sqr_toom3
#undef MAYBE_sqr_above_toom3
#undef MAYBE_sqr_above_toom4