1
/* mpn_addsub_n -- Add and Subtract two limb vectors of equal, non-zero length.
3
Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
5
This file is part of the GNU MP Library.
7
The GNU MP Library is free software; you can redistribute it and/or modify
8
it under the terms of the GNU Lesser General Public License as published by
9
the Free Software Foundation; either version 2.1 of the License, or (at your
10
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.
17
You should have received a copy of the GNU Lesser General Public License
18
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20
MA 02111-1307, USA. */
26
#define L1_CACHE_SIZE 8192 /* only 68040 has less than this */
29
#define PART_SIZE (L1_CACHE_SIZE / BYTES_PER_MP_LIMB / 6)
35
All operands have n limbs.
36
In-place operations allowed. */
38
mpn_addsub_n (mp_ptr r1p, mp_ptr r2p, mp_srcptr s1p, mp_srcptr s2p, mp_size_t n)
40
mp_limb_t acyn, acyo; /* carry for add */
41
mp_limb_t scyn, scyo; /* carry for subtract */
42
mp_size_t off; /* offset in operands */
43
mp_size_t this_n; /* size of current chunk */
45
/* We alternatingly add and subtract in chunks that fit into the (L1)
46
cache. Since the chunks are several hundred limbs, the function call
47
overhead is insignificant, but we get much better locality. */
49
/* We have three variant of the inner loop, the proper loop is chosen
50
depending on whether r1 or r2 are the same operand as s1 or s2. */
52
if (r1p != s1p && r1p != s2p)
54
/* r1 is not identical to either input operand. We can therefore write
55
to r1 directly, without using temporary storage. */
58
for (off = 0; off < n; off += PART_SIZE)
60
this_n = MIN (n - off, PART_SIZE);
61
#if HAVE_NATIVE_mpn_add_nc || !HAVE_NATIVE_mpn_add_n
62
acyo = mpn_add_nc (r1p + off, s1p + off, s2p + off, this_n, acyo);
64
acyn = mpn_add_n (r1p + off, s1p + off, s2p + off, this_n);
65
acyo = acyn + mpn_add_1 (r1p + off, r1p + off, this_n, acyo);
67
#if HAVE_NATIVE_mpn_sub_nc || !HAVE_NATIVE_mpn_sub_n
68
scyo = mpn_sub_nc (r2p + off, s1p + off, s2p + off, this_n, scyo);
70
scyn = mpn_sub_n (r2p + off, s1p + off, s2p + off, this_n);
71
scyo = scyn + mpn_sub_1 (r2p + off, r2p + off, this_n, scyo);
75
else if (r2p != s1p && r2p != s2p)
77
/* r2 is not identical to either input operand. We can therefore write
78
to r2 directly, without using temporary storage. */
81
for (off = 0; off < n; off += PART_SIZE)
83
this_n = MIN (n - off, PART_SIZE);
84
#if HAVE_NATIVE_mpn_sub_nc || !HAVE_NATIVE_mpn_sub_n
85
scyo = mpn_sub_nc (r2p + off, s1p + off, s2p + off, this_n, scyo);
87
scyn = mpn_sub_n (r2p + off, s1p + off, s2p + off, this_n);
88
scyo = scyn + mpn_sub_1 (r2p + off, r2p + off, this_n, scyo);
90
#if HAVE_NATIVE_mpn_add_nc || !HAVE_NATIVE_mpn_add_n
91
acyo = mpn_add_nc (r1p + off, s1p + off, s2p + off, this_n, acyo);
93
acyn = mpn_add_n (r1p + off, s1p + off, s2p + off, this_n);
94
acyo = acyn + mpn_add_1 (r1p + off, r1p + off, this_n, acyo);
100
/* r1 and r2 are identical to s1 and s2 (r1==s1 and r2=s2 or vice versa)
101
Need temporary storage. */
102
mp_limb_t tp[PART_SIZE];
105
for (off = 0; off < n; off += PART_SIZE)
107
this_n = MIN (n - off, PART_SIZE);
108
#if HAVE_NATIVE_mpn_add_nc || !HAVE_NATIVE_mpn_add_n
109
acyo = mpn_add_nc (tp, s1p + off, s2p + off, this_n, acyo);
111
acyn = mpn_add_n (tp, s1p + off, s2p + off, this_n);
112
acyo = acyn + mpn_add_1 (tp, tp, this_n, acyo);
114
#if HAVE_NATIVE_mpn_sub_nc || !HAVE_NATIVE_mpn_sub_n
115
scyo = mpn_sub_nc (r2p + off, s1p + off, s2p + off, this_n, scyo);
117
scyn = mpn_sub_n (r2p + off, s1p + off, s2p + off, this_n);
118
scyo = scyn + mpn_sub_1 (r2p + off, r2p + off, this_n, scyo);
120
MPN_COPY (r1p + off, tp, this_n);
124
return 2 * acyo + scyo;
135
main (int argc, char **argv)
137
mp_ptr r1p, r2p, s1p, s2p;
141
n = strtol (argv[1], 0, 0);
143
r1p = malloc (n * BYTES_PER_MP_LIMB);
144
r2p = malloc (n * BYTES_PER_MP_LIMB);
145
s1p = malloc (n * BYTES_PER_MP_LIMB);
146
s2p = malloc (n * BYTES_PER_MP_LIMB);
147
TIME (t,(mpn_add_n(r1p,s1p,s2p,n),mpn_sub_n(r1p,s1p,s2p,n)));
148
printf (" separate add and sub: %.3f\n", t);
149
TIME (t,mpn_addsub_n(r1p,r2p,s1p,s2p,n));
150
printf ("combined addsub separate variables: %.3f\n", t);
151
TIME (t,mpn_addsub_n(r1p,r2p,r1p,s2p,n));
152
printf (" combined addsub r1 overlap: %.3f\n", t);
153
TIME (t,mpn_addsub_n(r1p,r2p,r1p,s2p,n));
154
printf (" combined addsub r2 overlap: %.3f\n", t);
155
TIME (t,mpn_addsub_n(r1p,r2p,r1p,r2p,n));
156
printf (" combined addsub in-place: %.3f\n", t);