Coverage Report

Created: 2024-11-21 07:03

/src/libgmp/mpn/toom_eval_pm1.c
Line
Count
Source
1
/* mpn_toom_eval_pm1 -- Evaluate a polynomial in +1 and -1
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 > 3, in the points +1 and -1. */
41
/* It returns 0 or ~0, depending on the sign of the result xm1. */
42
unsigned
43
mpn_toom_eval_pm1 (mp_ptr xp1, mp_ptr xm1, unsigned k,
44
       mp_srcptr xp, mp_size_t n, mp_size_t hn, mp_ptr tp)
45
1.72k
{
46
1.72k
  unsigned i;
47
1.72k
  unsigned neg;
48
49
1.72k
  ASSERT (k >= 4);
50
51
1.72k
  ASSERT (hn > 0);
52
1.72k
  ASSERT (hn <= n);
53
54
  /* The degree k is also the number of full-size coefficients, so
55
   * that last coefficient, of size hn, starts at xp + k*n. */
56
57
1.72k
  xp1[n] = mpn_add_n (xp1, xp, xp + 2*n, n);
58
4.45k
  for (i = 4; i < k; i += 2)
59
2.72k
    ASSERT_NOCARRY (mpn_add (xp1, xp1, n+1, xp+i*n, n));
60
61
1.72k
  tp[n] = mpn_add_n (tp, xp + n, xp + 3*n, n);
62
2.76k
  for (i = 5; i < k; i += 2)
63
1.72k
    ASSERT_NOCARRY (mpn_add (tp, tp, n+1, xp+i*n, n));
64
65
1.72k
  if (k & 1)
66
1.72k
    ASSERT_NOCARRY (mpn_add (tp, tp, n+1, xp+k*n, hn));
67
43
  else
68
1.72k
    ASSERT_NOCARRY (mpn_add (xp1, xp1, n+1, xp+k*n, hn));
69
70
1.72k
  neg = - (unsigned) (mpn_cmp (xp1, tp, n + 1) < 0);
71
72
#if HAVE_NATIVE_mpn_add_n_sub_n
73
  if (neg)
74
    mpn_add_n_sub_n (xp1, xm1, tp, xp1, n + 1);
75
  else
76
    mpn_add_n_sub_n (xp1, xm1, xp1, tp, n + 1);
77
#else
78
1.72k
  if (neg)
79
495
    mpn_sub_n (xm1, tp, xp1, n + 1);
80
1.23k
  else
81
1.23k
    mpn_sub_n (xm1, xp1, tp, n + 1);
82
83
1.72k
  mpn_add_n (xp1, xp1, tp, n + 1);
84
1.72k
#endif
85
86
1.72k
  ASSERT (xp1[n] <= k);
87
1.72k
  ASSERT (xm1[n] <= k/2 + 1);
88
89
1.72k
  return neg;
90
1.72k
}