toom_eval_pm2.c
上传用户:qaz666999
上传日期:2022-08-06
资源大小:2570k
文件大小:3k
源码类别:

数学计算

开发平台:

Unix_Linux

  1. /* mpn_toom_eval_pm2 -- Evaluate a polynomial in +2 and -2
  2.    Contributed to the GNU project by Niels M鰈ler and Marco Bodrato
  3.    THE FUNCTION IN THIS FILE IS INTERNAL WITH A MUTABLE INTERFACE.  IT IS ONLY
  4.    SAFE TO REACH IT THROUGH DOCUMENTED INTERFACES.  IN FACT, IT IS ALMOST
  5.    GUARANTEED THAT IT WILL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
  6. Copyright 2009 Free Software Foundation, Inc.
  7. This file is part of the GNU MP Library.
  8. The GNU MP Library is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU Lesser General Public License as published by
  10. the Free Software Foundation; either version 3 of the License, or (at your
  11. option) any later version.
  12. The GNU MP Library is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  15. License for more details.
  16. You should have received a copy of the GNU Lesser General Public License
  17. along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
  18. #include "gmp.h"
  19. #include "gmp-impl.h"
  20. /* DO_addlsh2(d,a,b,n,cy) computes cy,{d,n} <- {a,n} + 4*(cy,{b,n}), it
  21.    can be used as DO_addlsh2(d,a,d,n,d[n]), for accumulation on {d,n+1}. */
  22. #if HAVE_NATIVE_mpn_addlsh2_n
  23. #define DO_addlsh2(d, a, b, n, cy)
  24. do {
  25.   (cy) <<= 2;
  26.   (cy) += mpn_addlsh2_n(d, a, b, n);
  27. } while (0)
  28. #else
  29. #if HAVE_NATIVE_mpn_addlsh_n
  30. #define DO_addlsh2(d, a, b, n, cy)
  31. do {
  32.   (cy) <<= 2;
  33.   (cy) += mpn_addlsh_n(d, a, b, n, 2);
  34. } while (0)
  35. #else
  36. /* The following is not a general substitute for addlsh2.
  37.    It is correct if d == b, but it is not if d == a. */
  38. #define DO_addlsh2(d, a, b, n, cy)
  39. do {
  40.   (cy) <<= 2;
  41.   (cy) += mpn_lshift(d, b, n, 2);
  42.   (cy) += mpn_add_n(d, d, a, n);
  43. } while (0)
  44. #endif
  45. #endif
  46. /* Evaluates a polynomial of degree 2 < k < GMP_NUMB_BITS, in the
  47.    points +2 and -2. */
  48. int
  49. mpn_toom_eval_pm2 (mp_ptr xp2, mp_ptr xm2, unsigned k,
  50.    mp_srcptr xp, mp_size_t n, mp_size_t hn, mp_ptr tp)
  51. {
  52.   int i;
  53.   int neg;
  54.   mp_limb_t cy;
  55.   ASSERT (k >= 3);
  56.   ASSERT (k < GMP_NUMB_BITS);
  57.   ASSERT (hn > 0);
  58.   ASSERT (hn <= n);
  59.   /* The degree k is also the number of full-size coefficients, so
  60.    * that last coefficient, of size hn, starts at xp + k*n. */
  61.   cy = 0;
  62.   DO_addlsh2 (xp2, xp + (k-2) * n, xp + k * n, hn, cy);
  63.   if (hn != n)
  64.     cy = mpn_add_1 (xp2 + hn, xp + (k-2) * n + hn, n - hn, cy);
  65.   for (i = k - 4; i >= 0; i -= 2)
  66.     DO_addlsh2 (xp2, xp + i * n, xp2, n, cy);
  67.   xp2[n] = cy;
  68.   k--;
  69.   cy = 0;
  70.   DO_addlsh2 (tp, xp + (k-2) * n, xp + k * n, n, cy);
  71.   for (i = k - 4; i >= 0; i -= 2)
  72.     DO_addlsh2 (tp, xp + i * n, tp, n, cy);
  73.   tp[n] = cy;
  74.   if (k & 1)
  75.     ASSERT_NOCARRY(mpn_lshift (tp , tp , n + 1, 1));
  76.   else
  77.     ASSERT_NOCARRY(mpn_lshift (xp2, xp2, n + 1, 1));
  78.   neg = (mpn_cmp (xp2, tp, n + 1) < 0) ? ~0 : 0;
  79. #if HAVE_NATIVE_mpn_add_n_sub_n
  80.   if (neg)
  81.     mpn_add_n_sub_n (xp2, xm2, tp, xp2, n + 1);
  82.   else
  83.     mpn_add_n_sub_n (xp2, xm2, xp2, tp, n + 1);
  84. #else /* !HAVE_NATIVE_mpn_add_n_sub_n */
  85.   if (neg)
  86.     mpn_sub_n (xm2, tp, xp2, n + 1);
  87.   else
  88.     mpn_sub_n (xm2, xp2, tp, n + 1);
  89.   mpn_add_n (xp2, xp2, tp, n + 1);
  90. #endif /* !HAVE_NATIVE_mpn_add_n_sub_n */
  91.   ASSERT (xp2[n] < (1<<(k+2))-1);
  92.   ASSERT (xm2[n] < ((1<<(k+3))-1 - (1^k&1))/3);
  93.   neg ^= ((k & 1) - 1);
  94.   return neg;
  95. }
  96. #undef DO_addlsh2