Coverage Report

Created: 2023-02-22 06:39

/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
12.5k
{
38
12.5k
  ASSERT (n >= 1);
39
12.5k
  ASSERT (! MPN_OVERLAP_P (p, 2 * n, a, n));
40
41
12.5k
  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
12.5k
  else if (BELOW_THRESHOLD (n, SQR_TOOM2_THRESHOLD))
46
11.3k
    {
47
11.3k
      mpn_sqr_basecase (p, a, n);
48
11.3k
    }
49
1.17k
  else if (BELOW_THRESHOLD (n, SQR_TOOM3_THRESHOLD))
50
936
    {
51
      /* Allocate workspace of fixed size on stack: fast! */
52
936
      mp_limb_t ws[mpn_toom2_sqr_itch (SQR_TOOM3_THRESHOLD_LIMIT-1)];
53
936
      ASSERT (SQR_TOOM3_THRESHOLD <= SQR_TOOM3_THRESHOLD_LIMIT);
54
936
      mpn_toom2_sqr (p, a, n, ws);
55
936
    }
56
235
  else if (BELOW_THRESHOLD (n, SQR_TOOM4_THRESHOLD))
57
235
    {
58
235
      mp_ptr ws;
59
235
      TMP_SDECL;
60
235
      TMP_SMARK;
61
235
      ws = TMP_SALLOC_LIMBS (mpn_toom3_sqr_itch (n));
62
235
      mpn_toom3_sqr (p, a, n, ws);
63
235
      TMP_SFREE;
64
235
    }
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
12.5k
}