1 /* mpn_sqr -- square natural numbers.
3 Copyright 1991, 1993, 1994, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2005, 2008, 2009 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
26 mpn_sqr (mp_ptr p, mp_srcptr a, mp_size_t n)
29 ASSERT (! MPN_OVERLAP_P (p, 2 * n, a, n));
31 if (BELOW_THRESHOLD (n, SQR_BASECASE_THRESHOLD))
32 { /* mul_basecase is faster than sqr_basecase on small sizes sometimes */
33 mpn_mul_basecase (p, a, n, a, n);
35 else if (BELOW_THRESHOLD (n, SQR_TOOM2_THRESHOLD))
37 mpn_sqr_basecase (p, a, n);
39 else if (BELOW_THRESHOLD (n, SQR_TOOM3_THRESHOLD))
41 /* Allocate workspace of fixed size on stack: fast! */
42 mp_limb_t ws[mpn_toom2_sqr_itch (SQR_TOOM3_THRESHOLD_LIMIT-1)];
43 ASSERT (SQR_TOOM3_THRESHOLD <= SQR_TOOM3_THRESHOLD_LIMIT);
44 mpn_toom2_sqr (p, a, n, ws);
46 else if (BELOW_THRESHOLD (n, SQR_TOOM4_THRESHOLD))
51 ws = TMP_SALLOC_LIMBS (mpn_toom3_sqr_itch (n));
52 mpn_toom3_sqr (p, a, n, ws);
55 else if (BELOW_THRESHOLD (n, SQR_TOOM6_THRESHOLD))
60 ws = TMP_SALLOC_LIMBS (mpn_toom4_sqr_itch (n));
61 mpn_toom4_sqr (p, a, n, ws);
64 else if (BELOW_THRESHOLD (n, SQR_TOOM8_THRESHOLD))
69 ws = TMP_SALLOC_LIMBS (mpn_toom6_sqr_itch (n));
70 mpn_toom6_sqr (p, a, n, ws);
73 else if (BELOW_THRESHOLD (n, SQR_FFT_THRESHOLD))
78 ws = TMP_ALLOC_LIMBS (mpn_toom8_sqr_itch (n));
79 mpn_toom8_sqr (p, a, n, ws);
84 /* The current FFT code allocates its own space. That should probably
86 mpn_fft_mul (p, a, n, a, n);