sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
*
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
|
sl@0
|
18 |
|
sl@0
|
19 |
#include "tmontgomeryperformance.h"
|
sl@0
|
20 |
#include "t_input.h"
|
sl@0
|
21 |
#include "t_output.h"
|
sl@0
|
22 |
#include <bigint.h>
|
sl@0
|
23 |
#include "../../source/bigint/mont.h"
|
sl@0
|
24 |
|
sl@0
|
25 |
_LIT(KPerfConstFormat, "\tConstruction Time\t%f");
|
sl@0
|
26 |
_LIT(KPerfMultiplyFormat, "\tMultiply Time\t%f");
|
sl@0
|
27 |
_LIT(KPerfSquareFormat, "\tSquare Time\t%f");
|
sl@0
|
28 |
_LIT(KPerfExpFormat, "\tExponentiate Time\t%f");
|
sl@0
|
29 |
_LIT(KPerfReduceFormat, "\tReduce Time\t%f");
|
sl@0
|
30 |
|
sl@0
|
31 |
CTestAction* CMontgomeryPerformance::NewL(RFs& aFs, CConsoleBase& aConsole,
|
sl@0
|
32 |
Output& aOut, const TTestActionSpec& aTestActionSpec)
|
sl@0
|
33 |
{
|
sl@0
|
34 |
CTestAction* self = CMontgomeryPerformance::NewLC(aFs, aConsole,
|
sl@0
|
35 |
aOut, aTestActionSpec);
|
sl@0
|
36 |
CleanupStack::Pop();
|
sl@0
|
37 |
return self;
|
sl@0
|
38 |
}
|
sl@0
|
39 |
|
sl@0
|
40 |
CTestAction* CMontgomeryPerformance::NewLC(RFs& aFs, CConsoleBase& aConsole,
|
sl@0
|
41 |
Output& aOut, const TTestActionSpec& aTestActionSpec)
|
sl@0
|
42 |
{
|
sl@0
|
43 |
CMontgomeryPerformance* self = new(ELeave) CMontgomeryPerformance(aFs, aConsole, aOut);
|
sl@0
|
44 |
CleanupStack::PushL(self);
|
sl@0
|
45 |
self->ConstructL(aTestActionSpec);
|
sl@0
|
46 |
return self;
|
sl@0
|
47 |
}
|
sl@0
|
48 |
|
sl@0
|
49 |
CMontgomeryPerformance::~CMontgomeryPerformance()
|
sl@0
|
50 |
{
|
sl@0
|
51 |
delete iBody;
|
sl@0
|
52 |
iA.Close();
|
sl@0
|
53 |
iB.Close();
|
sl@0
|
54 |
iModulus.Close();
|
sl@0
|
55 |
}
|
sl@0
|
56 |
|
sl@0
|
57 |
CMontgomeryPerformance::CMontgomeryPerformance(RFs& aFs, CConsoleBase& aConsole,
|
sl@0
|
58 |
Output& aOut) : CTestAction(aConsole, aOut), iFs(aFs)
|
sl@0
|
59 |
{
|
sl@0
|
60 |
}
|
sl@0
|
61 |
|
sl@0
|
62 |
void CMontgomeryPerformance::ConstructL(const TTestActionSpec& aTestActionSpec)
|
sl@0
|
63 |
{
|
sl@0
|
64 |
CTestAction::ConstructL(aTestActionSpec);
|
sl@0
|
65 |
iBody = HBufC8::NewL(aTestActionSpec.iActionBody.Length());
|
sl@0
|
66 |
iBody->Des().Copy(aTestActionSpec.iActionBody);
|
sl@0
|
67 |
|
sl@0
|
68 |
iIterations = Input::ParseIntElement(*iBody, _L8("<iterations>"), _L8("</iterations>"));
|
sl@0
|
69 |
|
sl@0
|
70 |
TUint bits = Input::ParseIntElement(*iBody, _L8("<bits>"), _L8("</bits>"));
|
sl@0
|
71 |
|
sl@0
|
72 |
do
|
sl@0
|
73 |
{
|
sl@0
|
74 |
if(*((TUint*)&iModulus)) //if the size is zero, ie iModulus hasn't been initialised so don't clean it up
|
sl@0
|
75 |
{
|
sl@0
|
76 |
iModulus.Close();
|
sl@0
|
77 |
}
|
sl@0
|
78 |
iModulus = RInteger::NewRandomL(bits);
|
sl@0
|
79 |
}
|
sl@0
|
80 |
while(iModulus.IsEven());
|
sl@0
|
81 |
|
sl@0
|
82 |
iA = RInteger::NewRandomL(TInteger::One(), iModulus);
|
sl@0
|
83 |
iB = RInteger::NewRandomL(TInteger::One(), iModulus);
|
sl@0
|
84 |
}
|
sl@0
|
85 |
|
sl@0
|
86 |
void CMontgomeryPerformance::DoPerformPrerequisite(TRequestStatus& aStatus)
|
sl@0
|
87 |
{
|
sl@0
|
88 |
TRequestStatus* status = &aStatus;
|
sl@0
|
89 |
User::RequestComplete(status, KErrNone);
|
sl@0
|
90 |
iActionState = CTestAction::EAction;
|
sl@0
|
91 |
}
|
sl@0
|
92 |
|
sl@0
|
93 |
void CMontgomeryPerformance::DoPerformPostrequisite(TRequestStatus& aStatus)
|
sl@0
|
94 |
{
|
sl@0
|
95 |
TRequestStatus* status = &aStatus;
|
sl@0
|
96 |
iFinished = ETrue;
|
sl@0
|
97 |
User::RequestComplete(status, KErrNone);
|
sl@0
|
98 |
}
|
sl@0
|
99 |
|
sl@0
|
100 |
void CMontgomeryPerformance::DoReportAction(void)
|
sl@0
|
101 |
{
|
sl@0
|
102 |
}
|
sl@0
|
103 |
|
sl@0
|
104 |
void CMontgomeryPerformance::DoCheckResult(TInt)
|
sl@0
|
105 |
{
|
sl@0
|
106 |
}
|
sl@0
|
107 |
|
sl@0
|
108 |
void CMontgomeryPerformance::PerformAction(TRequestStatus& aStatus)
|
sl@0
|
109 |
{
|
sl@0
|
110 |
TRequestStatus* status = &aStatus;
|
sl@0
|
111 |
iResult = ETrue;
|
sl@0
|
112 |
|
sl@0
|
113 |
CMontgomeryStructure* montConst;
|
sl@0
|
114 |
|
sl@0
|
115 |
TUint iterations = 0;
|
sl@0
|
116 |
|
sl@0
|
117 |
TTime start, end;
|
sl@0
|
118 |
TTimeIntervalSeconds diff(0);
|
sl@0
|
119 |
const TTimeIntervalSeconds iterationTime(iIterations);
|
sl@0
|
120 |
|
sl@0
|
121 |
start.UniversalTime();
|
sl@0
|
122 |
while (diff < iterationTime)
|
sl@0
|
123 |
{
|
sl@0
|
124 |
montConst = CMontgomeryStructure::NewL(iModulus);
|
sl@0
|
125 |
delete montConst;
|
sl@0
|
126 |
iterations++;
|
sl@0
|
127 |
end.UniversalTime();
|
sl@0
|
128 |
end.SecondsFrom(start, diff);
|
sl@0
|
129 |
}
|
sl@0
|
130 |
end.UniversalTime();
|
sl@0
|
131 |
|
sl@0
|
132 |
TTimeIntervalMicroSeconds time = end.MicroSecondsFrom(start);
|
sl@0
|
133 |
TReal rate = I64REAL(time.Int64()) / (iterations);
|
sl@0
|
134 |
TReal rtime = I64REAL(time.Int64());
|
sl@0
|
135 |
HBufC* realbuf = HBufC::NewLC(128);
|
sl@0
|
136 |
TPtr buf = realbuf->Des();
|
sl@0
|
137 |
buf.Format(KPerfConstFormat, rate, iterations, rtime);
|
sl@0
|
138 |
iOut.writeString(buf);
|
sl@0
|
139 |
iConsole.Printf(_L("."));
|
sl@0
|
140 |
|
sl@0
|
141 |
CMontgomeryStructure* mont = CMontgomeryStructure::NewLC(iModulus);
|
sl@0
|
142 |
|
sl@0
|
143 |
//Multiply
|
sl@0
|
144 |
diff = 0;
|
sl@0
|
145 |
iterations = 0;
|
sl@0
|
146 |
start.UniversalTime();
|
sl@0
|
147 |
while (diff < iterationTime)
|
sl@0
|
148 |
{
|
sl@0
|
149 |
mont->MultiplyL(iA, iB);
|
sl@0
|
150 |
iterations++;
|
sl@0
|
151 |
end.UniversalTime();
|
sl@0
|
152 |
end.SecondsFrom(start, diff);
|
sl@0
|
153 |
}
|
sl@0
|
154 |
end.UniversalTime();
|
sl@0
|
155 |
|
sl@0
|
156 |
time = end.MicroSecondsFrom(start);
|
sl@0
|
157 |
rate = I64REAL(time.Int64()) / iterations;
|
sl@0
|
158 |
rtime = I64REAL(time.Int64());
|
sl@0
|
159 |
buf.Zero();
|
sl@0
|
160 |
buf.Format(KPerfMultiplyFormat, rate, iterations, rtime);
|
sl@0
|
161 |
iOut.writeString(buf);
|
sl@0
|
162 |
iConsole.Printf(_L("."));
|
sl@0
|
163 |
|
sl@0
|
164 |
//Square
|
sl@0
|
165 |
diff = 0;
|
sl@0
|
166 |
iterations = 0;
|
sl@0
|
167 |
start.UniversalTime();
|
sl@0
|
168 |
while (diff < iterationTime)
|
sl@0
|
169 |
{
|
sl@0
|
170 |
mont->SquareL(iA);
|
sl@0
|
171 |
iterations++;
|
sl@0
|
172 |
end.UniversalTime();
|
sl@0
|
173 |
end.SecondsFrom(start, diff);
|
sl@0
|
174 |
}
|
sl@0
|
175 |
end.UniversalTime();
|
sl@0
|
176 |
|
sl@0
|
177 |
time = end.MicroSecondsFrom(start);
|
sl@0
|
178 |
rate = I64REAL(time.Int64()) / iterations;
|
sl@0
|
179 |
rtime = I64REAL(time.Int64());
|
sl@0
|
180 |
buf.Zero();
|
sl@0
|
181 |
buf.Format(KPerfSquareFormat, rate, iterations, rtime);
|
sl@0
|
182 |
iOut.writeString(buf);
|
sl@0
|
183 |
iConsole.Printf(_L("."));
|
sl@0
|
184 |
|
sl@0
|
185 |
//Exponentiate
|
sl@0
|
186 |
diff = 0;
|
sl@0
|
187 |
iterations = 0;
|
sl@0
|
188 |
start.UniversalTime();
|
sl@0
|
189 |
while (diff < iterationTime)
|
sl@0
|
190 |
{
|
sl@0
|
191 |
mont->ExponentiateL(iA, iB);
|
sl@0
|
192 |
iterations++;
|
sl@0
|
193 |
end.UniversalTime();
|
sl@0
|
194 |
end.SecondsFrom(start, diff);
|
sl@0
|
195 |
}
|
sl@0
|
196 |
end.UniversalTime();
|
sl@0
|
197 |
|
sl@0
|
198 |
time = end.MicroSecondsFrom(start);
|
sl@0
|
199 |
rate = I64REAL(time.Int64()) / iterations;
|
sl@0
|
200 |
rtime = I64REAL(time.Int64());
|
sl@0
|
201 |
|
sl@0
|
202 |
buf.Zero();
|
sl@0
|
203 |
buf.Format(KPerfExpFormat, rate, iterations, rtime);
|
sl@0
|
204 |
iOut.writeString(buf);
|
sl@0
|
205 |
iConsole.Printf(_L("."));
|
sl@0
|
206 |
|
sl@0
|
207 |
//Reduce
|
sl@0
|
208 |
RInteger top = iA.TimesL(iB);
|
sl@0
|
209 |
CleanupStack::PushL(top);
|
sl@0
|
210 |
|
sl@0
|
211 |
diff = 0;
|
sl@0
|
212 |
iterations = 0;
|
sl@0
|
213 |
start.UniversalTime();
|
sl@0
|
214 |
while (diff < iterationTime)
|
sl@0
|
215 |
{
|
sl@0
|
216 |
mont->ReduceL(top);
|
sl@0
|
217 |
iterations++;
|
sl@0
|
218 |
end.UniversalTime();
|
sl@0
|
219 |
end.SecondsFrom(start, diff);
|
sl@0
|
220 |
}
|
sl@0
|
221 |
end.UniversalTime();
|
sl@0
|
222 |
|
sl@0
|
223 |
CleanupStack::PopAndDestroy(&top);
|
sl@0
|
224 |
|
sl@0
|
225 |
time = end.MicroSecondsFrom(start);
|
sl@0
|
226 |
rate = I64REAL(time.Int64()) / iterations;
|
sl@0
|
227 |
rtime = I64REAL(time.Int64());
|
sl@0
|
228 |
buf.Zero();
|
sl@0
|
229 |
buf.Format(KPerfReduceFormat, rate, iterations, rtime);
|
sl@0
|
230 |
iOut.writeString(buf);
|
sl@0
|
231 |
iConsole.Printf(_L("."));
|
sl@0
|
232 |
CleanupStack::PopAndDestroy(mont);
|
sl@0
|
233 |
CleanupStack::PopAndDestroy(realbuf);
|
sl@0
|
234 |
|
sl@0
|
235 |
User::RequestComplete(status, KErrNone);
|
sl@0
|
236 |
iActionState = CTestAction::EPostrequisite;
|
sl@0
|
237 |
}
|
sl@0
|
238 |
|