1/* mpn_sqr -- square natural numbers.
2
3Copyright 1991, 1993, 1994, 1996-2003, 2005, 2008, 2009 Free Software
4Foundation, Inc.
5
6This file is part of the GNU MP Library.
7
8The GNU MP Library is free software; you can redistribute it and/or modify
9it under the terms of either:
10
11  * the GNU Lesser General Public License as published by the Free
12    Software Foundation; either version 3 of the License, or (at your
13    option) any later version.
14
15or
16
17  * the GNU General Public License as published by the Free Software
18    Foundation; either version 2 of the License, or (at your option) any
19    later version.
20
21or both in parallel, as here.
22
23The GNU MP Library is distributed in the hope that it will be useful, but
24WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26for more details.
27
28You should have received copies of the GNU General Public License and the
29GNU Lesser General Public License along with the GNU MP Library.  If not,
30see https://www.gnu.org/licenses/.  */
31
32#include "gmp-impl.h"
33#include "longlong.h"
34
35void
36mpn_sqr (mp_ptr p, mp_srcptr a, mp_size_t n)
37{
38  ASSERT (n >= 1);
39  ASSERT (! MPN_OVERLAP_P (p, 2 * n, a, n));
40
41  if (BELOW_THRESHOLD (n, SQR_BASECASE_THRESHOLD))
42    { /* mul_basecase is faster than sqr_basecase on small sizes sometimes */
43      mpn_mul_basecase (p, a, n, a, n);
44    }
45  else if (BELOW_THRESHOLD (n, SQR_TOOM2_THRESHOLD))
46    {
47      mpn_sqr_basecase (p, a, n);
48    }
49  else if (BELOW_THRESHOLD (n, SQR_TOOM3_THRESHOLD))
50    {
51      /* Allocate workspace of fixed size on stack: fast! */
52      mp_limb_t ws[mpn_toom2_sqr_itch (SQR_TOOM3_THRESHOLD_LIMIT-1)];
53      ASSERT (SQR_TOOM3_THRESHOLD <= SQR_TOOM3_THRESHOLD_LIMIT);
54      mpn_toom2_sqr (p, a, n, ws);
55    }
56  else if (BELOW_THRESHOLD (n, SQR_TOOM4_THRESHOLD))
57    {
58      mp_ptr ws;
59      TMP_SDECL;
60      TMP_SMARK;
61      ws = TMP_SALLOC_LIMBS (mpn_toom3_sqr_itch (n));
62      mpn_toom3_sqr (p, a, n, ws);
63      TMP_SFREE;
64    }
65  else if (BELOW_THRESHOLD (n, SQR_TOOM6_THRESHOLD))
66    {
67      mp_ptr ws;
68      TMP_SDECL;
69      TMP_SMARK;
70      ws = TMP_SALLOC_LIMBS (mpn_toom4_sqr_itch (n));
71      mpn_toom4_sqr (p, a, n, ws);
72      TMP_SFREE;
73    }
74  else if (BELOW_THRESHOLD (n, SQR_TOOM8_THRESHOLD))
75    {
76      mp_ptr ws;
77      TMP_SDECL;
78      TMP_SMARK;
79      ws = TMP_SALLOC_LIMBS (mpn_toom6_sqr_itch (n));
80      mpn_toom6_sqr (p, a, n, ws);
81      TMP_SFREE;
82    }
83  else if (BELOW_THRESHOLD (n, SQR_FFT_THRESHOLD))
84    {
85      mp_ptr ws;
86      TMP_DECL;
87      TMP_MARK;
88      ws = TMP_ALLOC_LIMBS (mpn_toom8_sqr_itch (n));
89      mpn_toom8_sqr (p, a, n, ws);
90      TMP_FREE;
91    }
92  else
93    {
94      /* The current FFT code allocates its own space.  That should probably
95	 change.  */
96      mpn_fft_mul (p, a, n, a, n);
97    }
98}
99