1/* mpn_sqr -- square natural numbers. 2 3Copyright 1991, 1993, 1994, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 42005, 2008, 2009 Free Software Foundation, 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 the GNU Lesser General Public License as published by 10the Free Software Foundation; either version 3 of the License, or (at your 11option) any later version. 12 13The GNU MP Library is distributed in the hope that it will be useful, but 14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 15or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 16License for more details. 17 18You should have received a copy of the GNU Lesser General Public License 19along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */ 20 21#include "gmp.h" 22#include "gmp-impl.h" 23#include "longlong.h" 24 25void 26mpn_sqr (mp_ptr p, mp_srcptr a, mp_size_t n) 27{ 28 ASSERT (n >= 1); 29 ASSERT (! MPN_OVERLAP_P (p, 2 * n, a, n)); 30 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); 34 } 35 else if (BELOW_THRESHOLD (n, SQR_TOOM2_THRESHOLD)) 36 { 37 mpn_sqr_basecase (p, a, n); 38 } 39 else if (BELOW_THRESHOLD (n, SQR_TOOM3_THRESHOLD)) 40 { 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); 45 } 46 else if (BELOW_THRESHOLD (n, SQR_TOOM4_THRESHOLD)) 47 { 48 mp_ptr ws; 49 TMP_SDECL; 50 TMP_SMARK; 51 ws = TMP_SALLOC_LIMBS (mpn_toom3_sqr_itch (n)); 52 mpn_toom3_sqr (p, a, n, ws); 53 TMP_SFREE; 54 } 55 else if (BELOW_THRESHOLD (n, SQR_TOOM6_THRESHOLD)) 56 { 57 mp_ptr ws; 58 TMP_SDECL; 59 TMP_SMARK; 60 ws = TMP_SALLOC_LIMBS (mpn_toom4_sqr_itch (n)); 61 mpn_toom4_sqr (p, a, n, ws); 62 TMP_SFREE; 63 } 64 else if (BELOW_THRESHOLD (n, SQR_TOOM8_THRESHOLD)) 65 { 66 mp_ptr ws; 67 TMP_SDECL; 68 TMP_SMARK; 69 ws = TMP_SALLOC_LIMBS (mpn_toom6_sqr_itch (n)); 70 mpn_toom6_sqr (p, a, n, ws); 71 TMP_SFREE; 72 } 73 else if (BELOW_THRESHOLD (n, SQR_FFT_THRESHOLD)) 74 { 75 mp_ptr ws; 76 TMP_DECL; 77 TMP_MARK; 78 ws = TMP_ALLOC_LIMBS (mpn_toom8_sqr_itch (n)); 79 mpn_toom8_sqr (p, a, n, ws); 80 TMP_FREE; 81 } 82 else 83 { 84 /* The current FFT code allocates its own space. That should probably 85 change. */ 86 mpn_fft_mul (p, a, n, a, n); 87 } 88} 89