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