Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / freebl / mpi / mulsqr.c
blobd73ad8d69a2bd874297a082033bcfd777a678e3f
1 /*
2 * Test whether to include squaring code given the current settings
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
17 * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
19 * The Initial Developer of the Original Code is
20 * Michael J. Fromberger.
21 * Portions created by the Initial Developer are Copyright (C) 1997
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <limits.h>
43 #include <time.h>
45 #define MP_SQUARE 1 /* make sure squaring code is included */
47 #include "mpi.h"
48 #include "mpprime.h"
50 int main(int argc, char *argv[])
52 int ntests, prec, ix;
53 unsigned int seed;
54 clock_t start, stop;
55 double multime, sqrtime;
56 mp_int a, c;
58 seed = (unsigned int)time(NULL);
60 if(argc < 3) {
61 fprintf(stderr, "Usage: %s <ntests> <nbits>\n", argv[0]);
62 return 1;
65 if((ntests = abs(atoi(argv[1]))) == 0) {
66 fprintf(stderr, "%s: must request at least 1 test.\n", argv[0]);
67 return 1;
69 if((prec = abs(atoi(argv[2]))) < CHAR_BIT) {
70 fprintf(stderr, "%s: must request at least %d bits.\n", argv[0],
71 CHAR_BIT);
72 return 1;
75 prec = (prec + (DIGIT_BIT - 1)) / DIGIT_BIT;
77 mp_init_size(&a, prec);
78 mp_init_size(&c, 2 * prec);
80 /* Test multiplication by self */
81 srand(seed);
82 start = clock();
83 for(ix = 0; ix < ntests; ix++) {
84 mpp_random_size(&a, prec);
85 mp_mul(&a, &a, &c);
87 stop = clock();
89 multime = (double)(stop - start) / CLOCKS_PER_SEC;
91 /* Test squaring */
92 srand(seed);
93 start = clock();
94 for(ix = 0; ix < ntests; ix++) {
95 mpp_random_size(&a, prec);
96 mp_sqr(&a, &c);
98 stop = clock();
100 sqrtime = (double)(stop - start) / CLOCKS_PER_SEC;
102 printf("Multiply: %.4f\n", multime);
103 printf("Square: %.4f\n", sqrtime);
104 if(multime < sqrtime) {
105 printf("Speedup: %.1f%%\n", 100.0 * (1.0 - multime / sqrtime));
106 printf("Prefer: multiply\n");
107 } else {
108 printf("Speedup: %.1f%%\n", 100.0 * (1.0 - sqrtime / multime));
109 printf("Prefer: square\n");
112 mp_clear(&a); mp_clear(&c);
113 return 0;