sqr.c
Upload User: qaz666999
Upload Date: 2022-08-06
Package Size: 2570k
Code Size: 3k
Category:

Algorithm

Development Platform:

Unix_Linux

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