Coverage Report

Created: 2024-06-28 06:39

/src/gmp-6.2.1/mpn/toom_eval_pm2exp.c
Line
Count
Source (jump to first uncovered line)
1
/* mpn_toom_eval_pm2exp -- Evaluate a polynomial in +2^k and -2^k
2
3
   Contributed to the GNU project by Niels Möller
4
5
   THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE.  IT IS ONLY
6
   SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
7
   GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
8
9
Copyright 2009 Free Software Foundation, Inc.
10
11
This file is part of the GNU MP Library.
12
13
The GNU MP Library is free software; you can redistribute it and/or modify
14
it under the terms of either:
15
16
  * the GNU Lesser General Public License as published by the Free
17
    Software Foundation; either version 3 of the License, or (at your
18
    option) any later version.
19
20
or
21
22
  * the GNU General Public License as published by the Free Software
23
    Foundation; either version 2 of the License, or (at your option) any
24
    later version.
25
26
or both in parallel, as here.
27
28
The GNU MP Library is distributed in the hope that it will be useful, but
29
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
30
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
31
for more details.
32
33
You should have received copies of the GNU General Public License and the
34
GNU Lesser General Public License along with the GNU MP Library.  If not,
35
see https://www.gnu.org/licenses/.  */
36
37
38
#include "gmp-impl.h"
39
40
/* Evaluates a polynomial of degree k > 2, in the points +2^shift and -2^shift. */
41
int
42
mpn_toom_eval_pm2exp (mp_ptr xp2, mp_ptr xm2, unsigned k,
43
          mp_srcptr xp, mp_size_t n, mp_size_t hn, unsigned shift,
44
          mp_ptr tp)
45
0
{
46
0
  unsigned i;
47
0
  int neg;
48
0
#if HAVE_NATIVE_mpn_addlsh_n
49
0
  mp_limb_t cy;
50
0
#endif
51
52
0
  ASSERT (k >= 3);
53
0
  ASSERT (shift*k < GMP_NUMB_BITS);
54
55
0
  ASSERT (hn > 0);
56
0
  ASSERT (hn <= n);
57
58
  /* The degree k is also the number of full-size coefficients, so
59
   * that last coefficient, of size hn, starts at xp + k*n. */
60
61
0
#if HAVE_NATIVE_mpn_addlsh_n
62
0
  xp2[n] = mpn_addlsh_n (xp2, xp, xp + 2*n, n, 2*shift);
63
0
  for (i = 4; i < k; i += 2)
64
0
    xp2[n] += mpn_addlsh_n (xp2, xp2, xp + i*n, n, i*shift);
65
66
0
  tp[n] = mpn_lshift (tp, xp+n, n, shift);
67
0
  for (i = 3; i < k; i+= 2)
68
0
    tp[n] += mpn_addlsh_n (tp, tp, xp+i*n, n, i*shift);
69
70
0
  if (k & 1)
71
0
    {
72
0
      cy = mpn_addlsh_n (tp, tp, xp+k*n, hn, k*shift);
73
0
      MPN_INCR_U (tp + hn, n+1 - hn, cy);
74
0
    }
75
0
  else
76
0
    {
77
0
      cy = mpn_addlsh_n (xp2, xp2, xp+k*n, hn, k*shift);
78
0
      MPN_INCR_U (xp2 + hn, n+1 - hn, cy);
79
0
    }
80
81
#else /* !HAVE_NATIVE_mpn_addlsh_n */
82
  xp2[n] = mpn_lshift (tp, xp+2*n, n, 2*shift);
83
  xp2[n] += mpn_add_n (xp2, xp, tp, n);
84
  for (i = 4; i < k; i += 2)
85
    {
86
      xp2[n] += mpn_lshift (tp, xp + i*n, n, i*shift);
87
      xp2[n] += mpn_add_n (xp2, xp2, tp, n);
88
    }
89
90
  tp[n] = mpn_lshift (tp, xp+n, n, shift);
91
  for (i = 3; i < k; i+= 2)
92
    {
93
      tp[n] += mpn_lshift (xm2, xp + i*n, n, i*shift);
94
      tp[n] += mpn_add_n (tp, tp, xm2, n);
95
    }
96
97
  xm2[hn] = mpn_lshift (xm2, xp + k*n, hn, k*shift);
98
  if (k & 1)
99
    mpn_add (tp, tp, n+1, xm2, hn+1);
100
  else
101
    mpn_add (xp2, xp2, n+1, xm2, hn+1);
102
#endif /* !HAVE_NATIVE_mpn_addlsh_n */
103
104
0
  neg = (mpn_cmp (xp2, tp, n + 1) < 0) ? ~0 : 0;
105
106
#if HAVE_NATIVE_mpn_add_n_sub_n
107
  if (neg)
108
    mpn_add_n_sub_n (xp2, xm2, tp, xp2, n + 1);
109
  else
110
    mpn_add_n_sub_n (xp2, xm2, xp2, tp, n + 1);
111
#else /* !HAVE_NATIVE_mpn_add_n_sub_n */
112
0
  if (neg)
113
0
    mpn_sub_n (xm2, tp, xp2, n + 1);
114
0
  else
115
0
    mpn_sub_n (xm2, xp2, tp, n + 1);
116
117
0
  mpn_add_n (xp2, xp2, tp, n + 1);
118
0
#endif /* !HAVE_NATIVE_mpn_add_n_sub_n */
119
120
  /* FIXME: the following asserts are useless if (k+1)*shift >= GMP_LIMB_BITS */
121
0
  ASSERT ((k+1)*shift >= GMP_LIMB_BITS ||
122
0
    xp2[n] < ((CNST_LIMB(1)<<((k+1)*shift))-1)/((CNST_LIMB(1)<<shift)-1));
123
0
  ASSERT ((k+2)*shift >= GMP_LIMB_BITS ||
124
0
    xm2[n] < ((CNST_LIMB(1)<<((k+2)*shift))-((k&1)?(CNST_LIMB(1)<<shift):1))/((CNST_LIMB(1)<<(2*shift))-1));
125
126
0
  return neg;
127
0
}