beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpn / generic / mulmid_basecase.c
blob400e9764240b9257552ef25d7a25e62a578481df
1 /* mpn_mulmid_basecase -- classical middle product algorithm
3 Contributed by David Harvey.
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.
9 Copyright 2011 Free Software Foundation, Inc.
11 This file is part of the GNU MP Library.
13 The GNU MP Library is free software; you can redistribute it and/or modify
14 it under the terms of either:
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.
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.
26 or both in parallel, as here.
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.
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/. */
38 #include "gmp.h"
39 #include "gmp-impl.h"
40 #include "longlong.h"
42 /* Middle product of {up,un} and {vp,vn}, write result to {rp,un-vn+3}.
43 Must have un >= vn >= 1.
45 Neither input buffer may overlap with the output buffer. */
47 void
48 mpn_mulmid_basecase (mp_ptr rp,
49 mp_srcptr up, mp_size_t un,
50 mp_srcptr vp, mp_size_t vn)
52 mp_limb_t lo, hi; /* last two limbs of output */
53 mp_limb_t cy;
55 ASSERT (un >= vn);
56 ASSERT (vn >= 1);
57 ASSERT (! MPN_OVERLAP_P (rp, un - vn + 3, up, un));
58 ASSERT (! MPN_OVERLAP_P (rp, un - vn + 3, vp, vn));
60 up += vn - 1;
61 un -= vn - 1;
63 /* multiply by first limb, store result */
64 lo = mpn_mul_1 (rp, up, un, vp[0]);
65 hi = 0;
67 /* accumulate remaining rows */
68 for (vn--; vn; vn--)
70 up--, vp++;
71 cy = mpn_addmul_1 (rp, up, un, vp[0]);
72 add_ssaaaa (hi, lo, hi, lo, CNST_LIMB(0), cy);
75 /* store final limbs */
76 #if GMP_NAIL_BITS != 0
77 hi = (hi << GMP_NAIL_BITS) + (lo >> GMP_NUMB_BITS);
78 lo &= GMP_NUMB_MASK;
79 #endif
81 rp[un] = lo;
82 rp[un + 1] = hi;