/src/gmp/mpn/mullo_basecase.c
Line | Count | Source |
1 | | /* mpn_mullo_basecase -- Internal routine to multiply two natural |
2 | | numbers of length n and return the low part. |
3 | | |
4 | | THIS IS AN INTERNAL FUNCTION WITH A MUTABLE INTERFACE. IT IS ONLY |
5 | | SAFE TO REACH THIS FUNCTION THROUGH DOCUMENTED INTERFACES. |
6 | | |
7 | | |
8 | | Copyright (C) 2000, 2002, 2004, 2015 Free Software Foundation, Inc. |
9 | | |
10 | | This file is part of the GNU MP Library. |
11 | | |
12 | | The GNU MP Library is free software; you can redistribute it and/or modify |
13 | | it under the terms of either: |
14 | | |
15 | | * the GNU Lesser General Public License as published by the Free |
16 | | Software Foundation; either version 3 of the License, or (at your |
17 | | option) any later version. |
18 | | |
19 | | or |
20 | | |
21 | | * the GNU General Public License as published by the Free Software |
22 | | Foundation; either version 2 of the License, or (at your option) any |
23 | | later version. |
24 | | |
25 | | or both in parallel, as here. |
26 | | |
27 | | The GNU MP Library is distributed in the hope that it will be useful, but |
28 | | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
29 | | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
30 | | for more details. |
31 | | |
32 | | You should have received copies of the GNU General Public License and the |
33 | | GNU Lesser General Public License along with the GNU MP Library. If not, |
34 | | see https://www.gnu.org/licenses/. */ |
35 | | |
36 | | #include "gmp-impl.h" |
37 | | |
38 | | /* FIXME: Should optionally use mpn_mul_2/mpn_addmul_2. */ |
39 | | |
40 | | #ifndef MULLO_VARIANT |
41 | | #define MULLO_VARIANT 2 |
42 | | #endif |
43 | | |
44 | | |
45 | | #if MULLO_VARIANT == 1 |
46 | | void |
47 | | mpn_mullo_basecase (mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n) |
48 | | { |
49 | | mp_size_t i; |
50 | | |
51 | | mpn_mul_1 (rp, up, n, vp[0]); |
52 | | |
53 | | for (i = n - 1; i > 0; i--) |
54 | | { |
55 | | vp++; |
56 | | rp++; |
57 | | mpn_addmul_1 (rp, up, i, vp[0]); |
58 | | } |
59 | | } |
60 | | #endif |
61 | | |
62 | | |
63 | | #if MULLO_VARIANT == 2 |
64 | | void |
65 | | mpn_mullo_basecase (mp_ptr rp, mp_srcptr up, mp_srcptr vp, mp_size_t n) |
66 | 1.80M | { |
67 | 1.80M | mp_limb_t h; |
68 | | |
69 | 1.80M | h = up[0] * vp[n - 1]; |
70 | | |
71 | 1.80M | if (n != 1) |
72 | 1.80M | { |
73 | 1.80M | mp_size_t i; |
74 | 1.80M | mp_limb_t v0; |
75 | | |
76 | 1.80M | v0 = *vp++; |
77 | 1.80M | h += up[n - 1] * v0 + mpn_mul_1 (rp, up, n - 1, v0); |
78 | 1.80M | rp++; |
79 | | |
80 | 68.6M | for (i = n - 2; i > 0; i--) |
81 | 66.8M | { |
82 | 66.8M | v0 = *vp++; |
83 | 66.8M | h += up[i] * v0 + mpn_addmul_1 (rp, up, i, v0); |
84 | 66.8M | rp++; |
85 | 66.8M | } |
86 | 1.80M | } |
87 | | |
88 | 1.80M | rp[0] = h; |
89 | 1.80M | } |
90 | | #endif |