Coverage Report

Created: 2025-03-09 06:52

/src/gmp-6.2.1/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
4.80M
{
38
4.80M
  ASSERT (n >= 1);
39
4.80M
  ASSERT (! MPN_OVERLAP_P (p, 2 * n, a, n));
40
41
4.80M
  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
4.80M
  else if (BELOW_THRESHOLD (n, SQR_TOOM2_THRESHOLD))
46
1.37M
    {
47
1.37M
      mpn_sqr_basecase (p, a, n);
48
1.37M
    }
49
3.42M
  else if (BELOW_THRESHOLD (n, SQR_TOOM3_THRESHOLD))
50
885k
    {
51
      /* Allocate workspace of fixed size on stack: fast! */
52
885k
      mp_limb_t ws[mpn_toom2_sqr_itch (SQR_TOOM3_THRESHOLD_LIMIT-1)];
53
885k
      ASSERT (SQR_TOOM3_THRESHOLD <= SQR_TOOM3_THRESHOLD_LIMIT);
54
885k
      mpn_toom2_sqr (p, a, n, ws);
55
885k
    }
56
2.54M
  else if (BELOW_THRESHOLD (n, SQR_TOOM4_THRESHOLD))
57
2.53M
    {
58
2.53M
      mp_ptr ws;
59
2.53M
      TMP_SDECL;
60
2.53M
      TMP_SMARK;
61
2.53M
      ws = TMP_SALLOC_LIMBS (mpn_toom3_sqr_itch (n));
62
2.53M
      mpn_toom3_sqr (p, a, n, ws);
63
2.53M
      TMP_SFREE;
64
2.53M
    }
65
2.32k
  else if (BELOW_THRESHOLD (n, SQR_TOOM6_THRESHOLD))
66
316
    {
67
316
      mp_ptr ws;
68
316
      TMP_SDECL;
69
316
      TMP_SMARK;
70
316
      ws = TMP_SALLOC_LIMBS (mpn_toom4_sqr_itch (n));
71
316
      mpn_toom4_sqr (p, a, n, ws);
72
316
      TMP_SFREE;
73
316
    }
74
2.00k
  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
2.00k
  else if (BELOW_THRESHOLD (n, SQR_FFT_THRESHOLD))
84
2.00k
    {
85
2.00k
      mp_ptr ws;
86
2.00k
      TMP_DECL;
87
2.00k
      TMP_MARK;
88
2.00k
      ws = TMP_ALLOC_LIMBS (mpn_toom8_sqr_itch (n));
89
2.00k
      mpn_toom8_sqr (p, a, n, ws);
90
2.00k
      TMP_FREE;
91
2.00k
    }
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
4.80M
}