aboutsummaryrefslogtreecommitdiff
path: root/vendor/gmp-6.3.0/mpn/generic/sqrlo_basecase.c
blob: 3148609da0f8e8f59d8739ce20a22b12e581bebf (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
182
183
184
185
186
187
188
189
190
191
192
193
194
/* mpn_sqrlo_basecase -- Internal routine to square a natural number
   of length n.

   THIS IS AN INTERNAL FUNCTION WITH A MUTABLE INTERFACE.  IT IS ONLY
   SAFE TO REACH THIS FUNCTION THROUGH DOCUMENTED INTERFACES.


Copyright 1991-1994, 1996, 1997, 2000-2005, 2008, 2010, 2011, 2015,
2016 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"
#include "longlong.h"

#ifndef SQRLO_SHORTCUT_MULTIPLICATIONS
#if HAVE_NATIVE_mpn_addmul_1
#define SQRLO_SHORTCUT_MULTIPLICATIONS 0
#else
#define SQRLO_SHORTCUT_MULTIPLICATIONS 1
#endif
#endif

#if HAVE_NATIVE_mpn_sqr_diagonal
#define MPN_SQR_DIAGONAL(rp, up, n)					\
  mpn_sqr_diagonal (rp, up, n)
#else
#define MPN_SQR_DIAGONAL(rp, up, n)					\
  do {									\
    mp_size_t _i;							\
    for (_i = 0; _i < (n); _i++)					\
      {									\
	mp_limb_t ul, lpl;						\
	ul = (up)[_i];							\
	umul_ppmm ((rp)[2 * _i + 1], lpl, ul, ul << GMP_NAIL_BITS);	\
	(rp)[2 * _i] = lpl >> GMP_NAIL_BITS;				\
      }									\
  } while (0)
#endif

#define MPN_SQRLO_DIAGONAL(rp, up, n)					\
  do {									\
    mp_size_t nhalf;							\
    nhalf = (n) >> 1;							\
    MPN_SQR_DIAGONAL ((rp), (up), nhalf);				\
    if (((n) & 1) != 0)							\
      {									\
	mp_limb_t op;							\
	op = (up)[nhalf];						\
	(rp)[(n) - 1] = (op * op) & GMP_NUMB_MASK;			\
      }									\
  } while (0)

#if HAVE_NATIVE_mpn_addlsh1_n_ip1
#define MPN_SQRLO_DIAG_ADDLSH1(rp, tp, up, n)				\
  do {									\
    MPN_SQRLO_DIAGONAL((rp), (up), (n));				\
    mpn_addlsh1_n_ip1 ((rp) + 1, (tp), (n) - 1);			\
  } while (0)
#else
#define MPN_SQRLO_DIAG_ADDLSH1(rp, tp, up, n)				\
  do {									\
    MPN_SQRLO_DIAGONAL((rp), (up), (n));				\
    mpn_lshift ((tp), (tp), (n) - 1, 1);				\
    mpn_add_n ((rp) + 1, (rp) + 1, (tp), (n) - 1);			\
  } while (0)
#endif

/* Avoid zero allocations when SQRLO_LO_THRESHOLD is 0 (this code not used). */
#define SQRLO_BASECASE_ALLOC						\
  (SQRLO_DC_THRESHOLD_LIMIT < 2 ? 1 : SQRLO_DC_THRESHOLD_LIMIT - 1)

/* Default mpn_sqrlo_basecase using mpn_addmul_1.  */
#ifndef SQRLO_SPECIAL_CASES
#define SQRLO_SPECIAL_CASES 2
#endif

#if TUNE_PROGRAM_BUILD || WANT_FAT_BINARY
#define MAYBE_special_cases 1
#else
#define MAYBE_special_cases \
  ((SQRLO_BASECASE_THRESHOLD <= SQRLO_SPECIAL_CASES) && (SQRLO_DC_THRESHOLD != 0))
#endif

void
mpn_sqrlo_basecase (mp_ptr rp, mp_srcptr up, mp_size_t n)
{
  mp_limb_t ul;

  ASSERT (n >= 1);
  ASSERT (! MPN_OVERLAP_P (rp, n, up, n));

  ul = up[0];

  if (MAYBE_special_cases && n <= SQRLO_SPECIAL_CASES)
    {
#if SQRLO_SPECIAL_CASES == 1
      rp[0] = (ul * ul) & GMP_NUMB_MASK;
#else
      if (n == 1)
	rp[0] = (ul * ul) & GMP_NUMB_MASK;
      else
	{
	  mp_limb_t hi, lo, ul1;
	  umul_ppmm (hi, lo, ul, ul << GMP_NAIL_BITS);
	  rp[0] = lo >> GMP_NAIL_BITS;
	  ul1 = up[1];
#if SQRLO_SPECIAL_CASES == 2
	  rp[1] = (hi + ul * ul1 * 2) & GMP_NUMB_MASK;
#else
	  if (n == 2)
	    rp[1] = (hi + ul * ul1 * 2) & GMP_NUMB_MASK;
	  else
	    {
	      mp_limb_t hi1;
#if GMP_NAIL_BITS != 0
	      ul <<= 1;
#endif
	      umul_ppmm (hi1, lo, ul1 << GMP_NAIL_BITS, ul);
	      hi1 += ul * up[2];
#if GMP_NAIL_BITS == 0
	      hi1 = (hi1 << 1) | (lo >> (GMP_LIMB_BITS - 1));
	      add_ssaaaa(rp[2], rp[1], hi1, lo << 1, ul1 * ul1, hi);
#else
	      hi += lo >> GMP_NAIL_BITS;
	      rp[1] = hi & GMP_NUMB_MASK;
	      rp[2] = (hi1 + ul1 * ul1 + (hi >> GMP_NUMB_BITS)) & GMP_NUMB_MASK;
#endif
	    }
#endif
	}
#endif
    }
  else
    {
      mp_limb_t tp[SQRLO_BASECASE_ALLOC];
      mp_size_t i;

      /* must fit n-1 limbs in tp */
      ASSERT (n <= SQRLO_DC_THRESHOLD_LIMIT);

      --n;
#if SQRLO_SHORTCUT_MULTIPLICATIONS
      {
	mp_limb_t cy;

	cy = ul * up[n] + mpn_mul_1 (tp, up + 1, n - 1, ul);
	for (i = 1; 2 * i + 1 < n; ++i)
	  {
	    ul = up[i];
	    cy += ul * up[n - i] + mpn_addmul_1 (tp + 2 * i, up + i + 1, n - 2 * i - 1, ul);
	  }
	tp [n-1] = (cy + ((n & 1)?up[i] * up[i + 1]:0)) & GMP_NUMB_MASK;
      }
#else
      mpn_mul_1 (tp, up + 1, n, ul);
      for (i = 1; 2 * i < n; ++i)
	mpn_addmul_1 (tp + 2 * i, up + i + 1, n - 2 * i, up[i]);
#endif

      MPN_SQRLO_DIAG_ADDLSH1 (rp, tp, up, n + 1);
    }
}
#undef SQRLO_SPECIAL_CASES
#undef MAYBE_special_cases
#undef SQRLO_BASECASE_ALLOC
#undef SQRLO_SHORTCUT_MULTIPLICATIONS
#undef MPN_SQR_DIAGONAL
#undef MPN_SQRLO_DIAGONAL
#undef MPN_SQRLO_DIAG_ADDLSH1