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