1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
/* mpn_addsub_n -- Add and Subtract two limb vectors of equal, non-zero length.
Copyright (C) 1999, 2000 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with the GNU MP Library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */
#include "gmp.h"
#include "gmp-impl.h"
#ifndef L1_CACHE_SIZE
#define L1_CACHE_SIZE 8192 /* only 68040 has less than this */
#endif
#define PART_SIZE (L1_CACHE_SIZE / BYTES_PER_MP_LIMB / 6)
/* mpn_addsub_n.
r1[] = s1[] + s2[]
r2[] = s1[] - s2[]
All operands have n limbs.
In-place operations allowed. */
mp_limb_t
#if __STDC__
mpn_addsub_n (mp_ptr r1p, mp_ptr r2p, mp_srcptr s1p, mp_srcptr s2p, mp_size_t n)
#else
mpn_addsub_n (r1p, r2p, s1p, s2p, n)
mp_ptr r1p, r2p;
mp_srcptr s1p, s2p;
mp_size_t n;
#endif
{
mp_limb_t acyn, acyo; /* carry for add */
mp_limb_t scyn, scyo; /* carry for subtract */
mp_size_t off; /* offset in operands */
mp_size_t this_n; /* size of current chunk */
/* We alternatingly add and subtract in chunks that fit into the (L1)
cache. Since the chunks are several hundred limbs, the function call
overhead is indignificant, but we get much better locality. */
/* We have three variant of the inner loop, the proper loop is chosen
depending on whether r1 or r2 are the same operand as s1 or s2. */
if (r1p != s1p && r1p != s2p)
{
/* r1 is not identical to either input operand. We can therefore write
to r1 directly, without using temporary storage. */
acyo = 0;
scyo = 0;
for (off = 0; off < n; off += PART_SIZE)
{
this_n = MIN (n - off, PART_SIZE);
#if HAVE_NATIVE_mpn_add_nc || !HAVE_NATIVE_mpn_add_n
acyo = mpn_add_nc (r1p + off, s1p + off, s2p + off, this_n, acyo);
#else
acyn = mpn_add_n (r1p + off, s1p + off, s2p + off, this_n);
acyo = acyn + mpn_add_1 (r1p + off, r1p + off, this_n, acyo);
#endif
#if HAVE_NATIVE_mpn_sub_nc || !HAVE_NATIVE_mpn_sub_n
scyo = mpn_sub_nc (r2p + off, s1p + off, s2p + off, this_n, scyo);
#else
scyn = mpn_sub_n (r2p + off, s1p + off, s2p + off, this_n);
scyo = scyn + mpn_sub_1 (r2p + off, r2p + off, this_n, scyo);
#endif
}
}
else if (r2p != s1p && r2p != s2p)
{
/* r2 is not identical to either input operand. We can therefore write
to r2 directly, without using temporary storage. */
acyo = 0;
scyo = 0;
for (off = 0; off < n; off += PART_SIZE)
{
this_n = MIN (n - off, PART_SIZE);
#if HAVE_NATIVE_mpn_sub_nc || !HAVE_NATIVE_mpn_sub_n
scyo = mpn_sub_nc (r2p + off, s1p + off, s2p + off, this_n, scyo);
#else
scyn = mpn_sub_n (r2p + off, s1p + off, s2p + off, this_n);
scyo = scyn + mpn_sub_1 (r2p + off, r2p + off, this_n, scyo);
#endif
#if HAVE_NATIVE_mpn_add_nc || !HAVE_NATIVE_mpn_add_n
acyo = mpn_add_nc (r1p + off, s1p + off, s2p + off, this_n, acyo);
#else
acyn = mpn_add_n (r1p + off, s1p + off, s2p + off, this_n);
acyo = acyn + mpn_add_1 (r1p + off, r1p + off, this_n, acyo);
#endif
}
}
else
{
/* r1 and r2 are identical to s1 and s2 (r1==s1 and r2=s2 or vice versa)
Need temporary storage. */
mp_limb_t tp[PART_SIZE];
acyo = 0;
scyo = 0;
for (off = 0; off < n; off += PART_SIZE)
{
this_n = MIN (n - off, PART_SIZE);
#if HAVE_NATIVE_mpn_add_nc || !HAVE_NATIVE_mpn_add_n
acyo = mpn_add_nc (tp, s1p + off, s2p + off, this_n, acyo);
#else
acyn = mpn_add_n (tp, s1p + off, s2p + off, this_n);
acyo = acyn + mpn_add_1 (tp, tp, this_n, acyo);
#endif
#if HAVE_NATIVE_mpn_sub_nc || !HAVE_NATIVE_mpn_sub_n
scyo = mpn_sub_nc (r2p + off, s1p + off, s2p + off, this_n, scyo);
#else
scyn = mpn_sub_n (r2p + off, s1p + off, s2p + off, this_n);
scyo = scyn + mpn_sub_1 (r2p + off, r2p + off, this_n, scyo);
#endif
MPN_COPY (r1p + off, tp, this_n);
}
}
return 2 * acyo + scyo;
}
#ifdef MAIN
#include <stdlib.h>
#include <stdio.h>
#include "timing.h"
long cputime ();
int
main (int argc, char **argv)
{
mp_ptr r1p, r2p, s1p, s2p;
double t;
mp_size_t n;
n = strtol (argv[1], 0, 0);
r1p = malloc (n * BYTES_PER_MP_LIMB);
r2p = malloc (n * BYTES_PER_MP_LIMB);
s1p = malloc (n * BYTES_PER_MP_LIMB);
s2p = malloc (n * BYTES_PER_MP_LIMB);
TIME (t,(mpn_add_n(r1p,s1p,s2p,n),mpn_sub_n(r1p,s1p,s2p,n)));
printf (" separate add and sub: %.3f\n", t);
TIME (t,mpn_addsub_n(r1p,r2p,s1p,s2p,n));
printf ("combined addsub separate variables: %.3f\n", t);
TIME (t,mpn_addsub_n(r1p,r2p,r1p,s2p,n));
printf (" combined addsub r1 overlap: %.3f\n", t);
TIME (t,mpn_addsub_n(r1p,r2p,r1p,s2p,n));
printf (" combined addsub r2 overlap: %.3f\n", t);
TIME (t,mpn_addsub_n(r1p,r2p,r1p,r2p,n));
printf (" combined addsub in-place: %.3f\n", t);
return 0;
}
#endif
|