File: ntl-profile.c

package info (click to toggle)
libzn-poly 0.8-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 700 kB
  • sloc: ansic: 9,295; python: 162; makefile: 7; sh: 6
file content (201 lines) | stat: -rw-r--r-- 4,675 bytes parent folder | download | duplicates (2)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
   ntl-profile.c:  routines for profiling NTL
   
   Copyright (C) 2007, 2008, David Harvey
   
   This file is part of the zn_poly library (version 0.8).
   
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 2 of the License, or
   (at your option) version 3 of the License.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include <math.h>
#include "support.h"
#include "profiler.h"
#include <NTL/lzz_pX.h>
#include <NTL/ZZ_pX.h>


/*
   Profiles NTL's polynomial multiplication, either zz_pX or ZZ_pX, depending
   on modulus size.

   arg should point to a profile_mul_info_t, with algo == ALGO_MUL_NTL.

   Returns total cycle count for _count_ calls.
*/
extern "C" double profile_mul_ntl(void* arg, unsigned long count)
{
   profile_mul_info_struct* info = (profile_mul_info_struct*) arg;

   cycle_count_t t0, t1;
   
   if (info->n < (ulong) NTL_SP_BOUND)
   {
      // zz_pX version
   
      NTL::zz_pX f1, f2, g;
      NTL::zz_p::init(info->n);
      
      size_t i;
      for (i = 0; i < info->len; i++)
         SetCoeff(f1, i, random_ulong(info->n));
      for (i = 0; i < info->len; i++)
         SetCoeff(f2, i, random_ulong(info->n));

      if (info->squaring)
      {
         // warm up
         ulong j;
         for (j = 0; j < count; j++)
            sqr(g, f1);
            
         t0 = get_cycle_counter();

         for (j = 0; j < count; j++)
            sqr(g, f1);

         t1 = get_cycle_counter();
      }
      else
      {
         // warm up
         ulong j;
         for (j = 0; j < count; j++)
            mul(g, f1, f2);
            
         t0 = get_cycle_counter();

         for (j = 0; j < count; j++)
            mul(g, f1, f2);

         t1 = get_cycle_counter();
      }
   }
   else
   {
      // ZZ_pX version

      NTL::ZZ_pX f1, f2, g;
      NTL::ZZ_p::init(NTL::to_ZZ(info->n));
      
      size_t i;
      for (i = 0; i < info->len; i++)
         SetCoeff(f1, i, random_ulong(info->n));
      for (i = 0; i < info->len; i++)
         SetCoeff(f2, i, random_ulong(info->n));

      if (info->squaring)
      {
         // warm up
         ulong j;
         for (j = 0; j < count; j++)
            sqr(g, f1);
            
         t0 = get_cycle_counter();

         for (j = 0; j < count; j++)
            sqr(g, f1);

         t1 = get_cycle_counter();
      }
      else
      {
         // warm up
         ulong j;
         for (j = 0; j < count; j++)
            mul(g, f1, f2);
            
         t0 = get_cycle_counter();

         for (j = 0; j < count; j++)
            mul(g, f1, f2);

         t1 = get_cycle_counter();
      }
   }
   
   return cycle_diff(t0, t1);
}



/*
   Profiles NTL's power series inversion, either zz_pX or ZZ_pX, depending
   on modulus size.

   arg should point to a profile_invert_info_t, with algo == ALGO_INVERT_NTL.
   
   Returns total cycle count for _count_ calls.
*/
extern "C" double profile_invert_ntl(void* arg, unsigned long count)
{
   profile_invert_info_struct* info = (profile_invert_info_struct*) arg;

   cycle_count_t t0, t1;
   
   if (info->n < (ulong) NTL_SP_BOUND)
   {
      // zz_pX version
   
      NTL::zz_pX f1, f2, g;
      NTL::zz_p::init(info->n);
      
      size_t i;
      SetCoeff(f1, 0, 1);
      for (i = 1; i < info->len; i++)
         SetCoeff(f1, i, random_ulong(info->n));

      // warm up
      ulong j;
      for (j = 0; j < count; j++)
         InvTrunc(g, f1, info->len);
         
      t0 = get_cycle_counter();

      for (j = 0; j < count; j++)
         InvTrunc(g, f1, info->len);

      t1 = get_cycle_counter();
   }
   else
   {
      // ZZ_pX version

      NTL::ZZ_pX f1, f2, g;
      NTL::ZZ_p::init(NTL::to_ZZ(info->n));

      size_t i;
      SetCoeff(f1, 0, 1);
      for (i = 1; i < info->len; i++)
         SetCoeff(f1, i, random_ulong(info->n));

      // warm up
      ulong j;
      for (j = 0; j < count; j++)
         InvTrunc(g, f1, info->len);
         
      t0 = get_cycle_counter();

      for (j = 0; j < count; j++)
         InvTrunc(g, f1, info->len);

      t1 = get_cycle_counter();
   }

   return cycle_diff(t0, t1);
}


// end of file ****************************************************************