mulmid_basecase.c revision 1.1.1.3
1/* mpn_mulmid_basecase -- classical middle product algorithm
2
3   Contributed by David Harvey.
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'LL CHANGE OR DISAPPEAR IN A FUTURE GNU MP RELEASE.
8
9Copyright 2011 Free Software Foundation, Inc.
10
11This file is part of the GNU MP Library.
12
13The GNU MP Library is free software; you can redistribute it and/or modify
14it 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
20or
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
26or both in parallel, as here.
27
28The GNU MP Library is distributed in the hope that it will be useful, but
29WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
30or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
31for more details.
32
33You should have received copies of the GNU General Public License and the
34GNU Lesser General Public License along with the GNU MP Library.  If not,
35see https://www.gnu.org/licenses/.  */
36
37
38#include "gmp-impl.h"
39#include "longlong.h"
40
41/* Middle product of {up,un} and {vp,vn}, write result to {rp,un-vn+3}.
42   Must have un >= vn >= 1.
43
44   Neither input buffer may overlap with the output buffer. */
45
46void
47mpn_mulmid_basecase (mp_ptr rp,
48                     mp_srcptr up, mp_size_t un,
49                     mp_srcptr vp, mp_size_t vn)
50{
51  mp_limb_t lo, hi;  /* last two limbs of output */
52  mp_limb_t cy;
53
54  ASSERT (un >= vn);
55  ASSERT (vn >= 1);
56  ASSERT (! MPN_OVERLAP_P (rp, un - vn + 3, up, un));
57  ASSERT (! MPN_OVERLAP_P (rp, un - vn + 3, vp, vn));
58
59  up += vn - 1;
60  un -= vn - 1;
61
62  /* multiply by first limb, store result */
63  lo = mpn_mul_1 (rp, up, un, vp[0]);
64  hi = 0;
65
66  /* accumulate remaining rows */
67  for (vn--; vn; vn--)
68    {
69      up--, vp++;
70      cy = mpn_addmul_1 (rp, up, un, vp[0]);
71      add_ssaaaa (hi, lo, hi, lo, CNST_LIMB(0), cy);
72    }
73
74  /* store final limbs */
75#if GMP_NAIL_BITS != 0
76  hi = (hi << GMP_NAIL_BITS) + (lo >> GMP_NUMB_BITS);
77  lo &= GMP_NUMB_MASK;
78#endif
79
80  rp[un] = lo;
81  rp[un + 1] = hi;
82}
83