~ubuntu-branches/ubuntu/wily/libzn-poly/wily

« back to all changes in this revision

Viewing changes to array-profile.c

  • Committer: Bazaar Package Importer
  • Author(s): Tim Abbott
  • Date: 2008-05-27 20:23:43 UTC
  • Revision ID: james.westby@ubuntu.com-20080527202343-ufcb3fwj2as0edoz
Tags: upstream-0.8
ImportĀ upstreamĀ versionĀ 0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   array-profile.c:  routines for profiling simple array operations
 
3
   
 
4
   Copyright (C) 2007, 2008, David Harvey
 
5
   
 
6
   This file is part of the zn_poly library (version 0.8).
 
7
   
 
8
   This program is free software: you can redistribute it and/or modify
 
9
   it under the terms of the GNU General Public License as published by
 
10
   the Free Software Foundation, either version 2 of the License, or
 
11
   (at your option) version 3 of the License.
 
12
 
 
13
   This program is distributed in the hope that it will be useful,
 
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
   GNU General Public License for more details.
 
17
 
 
18
   You should have received a copy of the GNU General Public License
 
19
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 
 
21
*/
 
22
 
 
23
 
 
24
#include <math.h>
 
25
#include "support.h"
 
26
#include "profiler.h"
 
27
#include "zn_poly_internal.h"
 
28
#include "zn_poly.h"
 
29
 
 
30
 
 
31
typedef void (*bfly_func)(ulong*, ulong*, ulong, const zn_mod_t);
 
32
 
 
33
 
 
34
/*
 
35
   Profiles one of the butterfly routines.
 
36
 
 
37
   arg points to an array of ulongs:
 
38
      * First is 0 for add, 1 for subtract, 2 for inplace butterfly.
 
39
      * Second is 0 for safe version, 1 for slim version.
 
40
   
 
41
   Returns total cycle count for _count_ calls to butterfly of length 1000.
 
42
*/
 
43
double profile_bfly(void* arg, unsigned long count)
 
44
{
 
45
   ulong type = ((ulong*) arg)[0];
 
46
   ulong speed = ((ulong*) arg)[1];
 
47
   ulong n = 123 +
 
48
             (speed ? (1UL << (ULONG_BITS - 2)) : (1UL << (ULONG_BITS - 1)));
 
49
   
 
50
   zn_mod_t mod;
 
51
   zn_mod_init(mod, n);
 
52
   
 
53
   const ulong len = 1000;
 
54
   
 
55
   ulong* buf1 = (ulong*) malloc(sizeof(ulong) * len);
 
56
   ulong* buf2 = (ulong*) malloc(sizeof(ulong) * len);
 
57
 
 
58
   // generate random inputs
 
59
   size_t i;
 
60
   for (i = 0; i < len; i++)
 
61
      buf1[i] = random_ulong(n);
 
62
   for (i = 0; i < len; i++)
 
63
      buf2[i] = random_ulong(n);
 
64
 
 
65
   bfly_func target;
 
66
 
 
67
   if (type == 0)
 
68
      target = (bfly_func) zn_array_add_inplace;
 
69
   else if (type == 1)
 
70
      target = (bfly_func) zn_array_sub_inplace;
 
71
   else   // type == 2
 
72
      target = zn_array_bfly_inplace;
 
73
 
 
74
   // warm up
 
75
   ulong j;
 
76
   for (j = 0; j < count; j++)
 
77
      target(buf1, buf2, len, mod);
 
78
 
 
79
   // do the actual profile
 
80
   cycle_count_t t0 = get_cycle_counter();
 
81
 
 
82
   for (j = 0; j < count; j++)
 
83
      target(buf1, buf2, len, mod);
 
84
 
 
85
   cycle_count_t t1 = get_cycle_counter();
 
86
   
 
87
   free(buf2);
 
88
   free(buf1);
 
89
   
 
90
   zn_mod_clear(mod);
 
91
 
 
92
   return cycle_diff(t0, t1);
 
93
}
 
94
 
 
95
 
 
96
 
 
97
/*
 
98
   Profiles mpn_add_n or mpn_sub_n.
 
99
 
 
100
   arg points to a single ulong: 0 for mpn_add_n, 1 for mpn_sub_n.
 
101
   
 
102
   Returns total cycle count for _count_ calls to length 1000 call.
 
103
*/
 
104
double profile_mpn_aors(void* arg, unsigned long count)
 
105
{
 
106
   ulong type = ((ulong*) arg)[0];
 
107
 
 
108
   const ulong len = 1000;
 
109
   
 
110
   mp_limb_t* buf1 = (mp_limb_t*) malloc(sizeof(mp_limb_t) * len);
 
111
   mp_limb_t* buf2 = (mp_limb_t*) malloc(sizeof(mp_limb_t) * len);
 
112
 
 
113
   mp_limb_t (*target)(mp_limb_t*, const mp_limb_t*, const mp_limb_t*,
 
114
                       mp_size_t len);
 
115
   
 
116
   target = type ? mpn_sub_n : mpn_add_n;
 
117
 
 
118
   // generate random inputs
 
119
   size_t i;
 
120
   for (i = 0; i < len; i++)
 
121
      buf1[i] = random_ulong(1UL << (ULONG_BITS - 1));
 
122
   for (i = 0; i < len; i++)
 
123
      buf2[i] = random_ulong(1UL << (ULONG_BITS - 1));
 
124
 
 
125
   // warm up
 
126
   ulong j;
 
127
   for (j = 0; j < count; j++)
 
128
      target(buf1, buf1, buf2, len);
 
129
 
 
130
   // do the actual profile
 
131
   cycle_count_t t0 = get_cycle_counter();
 
132
 
 
133
   for (j = 0; j < count; j++)
 
134
      target(buf1, buf1, buf2, len);
 
135
 
 
136
   cycle_count_t t1 = get_cycle_counter();
 
137
 
 
138
   free(buf2);
 
139
   free(buf1);
 
140
 
 
141
   return cycle_diff(t0, t1);
 
142
}
 
143
 
 
144
 
 
145
/*
 
146
   Profiles scalar multiplication.
 
147
 
 
148
   arg points to an array of ulongs:
 
149
      * First is modulus size in bits.
 
150
      * Second is 0 for regular multiply, 1 for REDC multiply
 
151
   
 
152
   Returns total cycle count for _count_ calls to zn_array_scalar_mul
 
153
   of length 1000.
 
154
*/
 
155
double profile_scalar_mul(void* arg, unsigned long count)
 
156
{
 
157
   int bits = ((ulong*) arg)[0];
 
158
   int algo = ((ulong*) arg)[1];
 
159
   
 
160
   zn_mod_t mod;
 
161
   ulong n = random_modulus(bits, 1);
 
162
   zn_mod_init(mod, n);
 
163
   
 
164
   ulong scalar = random_ulong(n);
 
165
   const ulong len = 1000;
 
166
 
 
167
   // generate random input
 
168
   ulong* buf = (ulong*) malloc(sizeof(ulong) * len);
 
169
   size_t i;
 
170
   for (i = 0; i < len; i++)
 
171
      buf[i] = random_ulong(n);
 
172
 
 
173
   cycle_count_t t0, t1;
 
174
 
 
175
   // warm up
 
176
   ulong j;
 
177
   for (j = 0; j < count; j++)
 
178
      _zn_array_scalar_mul(buf, buf, len, scalar, algo, mod);
 
179
 
 
180
   // do the actual profile
 
181
   t0 = get_cycle_counter();
 
182
 
 
183
   for (j = 0; j < count; j++)
 
184
      _zn_array_scalar_mul(buf, buf, len, scalar, algo, mod);
 
185
 
 
186
   t1 = get_cycle_counter();
 
187
 
 
188
   free(buf);
 
189
   zn_mod_clear(mod);
 
190
 
 
191
   return cycle_diff(t0, t1);
 
192
}
 
193
 
 
194
 
 
195
 
 
196
// end of file ****************************************************************