Coverage Report

Created: 2025-03-18 06:55

/src/gmp/mpn/sqr.c
Line
Count
Source (jump to first uncovered line)
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-impl.h"
33
#include "longlong.h"
34
35
void
36
mpn_sqr (mp_ptr p, mp_srcptr a, mp_size_t n)
37
0
{
38
0
  ASSERT (n >= 1);
39
0
  ASSERT (! MPN_OVERLAP_P (p, 2 * n, a, n));
40
41
0
  if (BELOW_THRESHOLD (n, SQR_BASECASE_THRESHOLD))
42
0
    { /* mul_basecase is faster than sqr_basecase on small sizes sometimes */
43
0
      mpn_mul_basecase (p, a, n, a, n);
44
0
    }
45
0
  else if (BELOW_THRESHOLD (n, SQR_TOOM2_THRESHOLD))
46
0
    {
47
0
      mpn_sqr_basecase (p, a, n);
48
0
    }
49
0
  else if (BELOW_THRESHOLD (n, SQR_TOOM3_THRESHOLD))
50
0
    {
51
      /* Allocate workspace of fixed size on stack: fast! */
52
0
      mp_limb_t ws[mpn_toom2_sqr_itch (SQR_TOOM3_THRESHOLD_LIMIT-1)];
53
0
      ASSERT (SQR_TOOM3_THRESHOLD <= SQR_TOOM3_THRESHOLD_LIMIT);
54
0
      mpn_toom2_sqr (p, a, n, ws);
55
0
    }
56
0
  else if (BELOW_THRESHOLD (n, SQR_TOOM4_THRESHOLD))
57
0
    {
58
0
      mp_ptr ws;
59
0
      TMP_SDECL;
60
0
      TMP_SMARK;
61
0
      ws = TMP_SALLOC_LIMBS (mpn_toom3_sqr_itch (n));
62
0
      mpn_toom3_sqr (p, a, n, ws);
63
0
      TMP_SFREE;
64
0
    }
65
0
  else if (BELOW_THRESHOLD (n, SQR_TOOM6_THRESHOLD))
66
0
    {
67
0
      mp_ptr ws;
68
0
      TMP_SDECL;
69
0
      TMP_SMARK;
70
0
      ws = TMP_SALLOC_LIMBS (mpn_toom4_sqr_itch (n));
71
0
      mpn_toom4_sqr (p, a, n, ws);
72
0
      TMP_SFREE;
73
0
    }
74
0
  else if (BELOW_THRESHOLD (n, SQR_TOOM8_THRESHOLD))
75
0
    {
76
0
      mp_ptr ws;
77
0
      TMP_SDECL;
78
0
      TMP_SMARK;
79
0
      ws = TMP_SALLOC_LIMBS (mpn_toom6_sqr_itch (n));
80
0
      mpn_toom6_sqr (p, a, n, ws);
81
0
      TMP_SFREE;
82
0
    }
83
0
  else if (BELOW_THRESHOLD (n, SQR_FFT_THRESHOLD))
84
0
    {
85
0
      mp_ptr ws;
86
0
      TMP_DECL;
87
0
      TMP_MARK;
88
0
      ws = TMP_ALLOC_LIMBS (mpn_toom8_sqr_itch (n));
89
0
      mpn_toom8_sqr (p, a, n, ws);
90
0
      TMP_FREE;
91
0
    }
92
0
  else
93
0
    {
94
      /* The current FFT code allocates its own space.  That should probably
95
   change.  */
96
0
      mpn_fft_mul (p, a, n, a, n);
97
0
    }
98
0
}