Back to index

lightning-sunbird  0.9+nobinonly
mulsqr.c
Go to the documentation of this file.
00001 /*
00002  * Test whether to include squaring code given the current settings
00003  *
00004  * ***** BEGIN LICENSE BLOCK *****
00005  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006  *
00007  * The contents of this file are subject to the Mozilla Public License Version
00008  * 1.1 (the "License"); you may not use this file except in compliance with
00009  * the License. You may obtain a copy of the License at
00010  * http://www.mozilla.org/MPL/
00011  *
00012  * Software distributed under the License is distributed on an "AS IS" basis,
00013  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00014  * for the specific language governing rights and limitations under the
00015  * License.
00016  *
00017  * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Michael J. Fromberger.
00021  * Portions created by the Initial Developer are Copyright (C) 1997
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 #include <limits.h>
00043 #include <time.h>
00044 
00045 #define MP_SQUARE 1  /* make sure squaring code is included */
00046 
00047 #include "mpi.h"
00048 #include "mpprime.h"
00049 
00050 int main(int argc, char *argv[])
00051 {
00052   int           ntests, prec, ix;
00053   unsigned int  seed;
00054   clock_t       start, stop;
00055   double        multime, sqrtime;
00056   mp_int        a, c;
00057 
00058   seed = (unsigned int)time(NULL);
00059 
00060   if(argc < 3) {
00061     fprintf(stderr, "Usage: %s <ntests> <nbits>\n", argv[0]);
00062     return 1;
00063   }
00064 
00065   if((ntests = abs(atoi(argv[1]))) == 0) {
00066     fprintf(stderr, "%s: must request at least 1 test.\n", argv[0]);
00067     return 1;
00068   }
00069   if((prec = abs(atoi(argv[2]))) < CHAR_BIT) {
00070     fprintf(stderr, "%s: must request at least %d bits.\n", argv[0],
00071            CHAR_BIT);
00072     return 1;
00073   }
00074 
00075   prec = (prec + (DIGIT_BIT - 1)) / DIGIT_BIT;
00076 
00077   mp_init_size(&a, prec);
00078   mp_init_size(&c, 2 * prec);
00079 
00080   /* Test multiplication by self */
00081   srand(seed);
00082   start = clock();
00083   for(ix = 0; ix < ntests; ix++) {
00084     mpp_random_size(&a, prec);
00085     mp_mul(&a, &a, &c);
00086   }
00087   stop = clock();
00088 
00089   multime = (double)(stop - start) / CLOCKS_PER_SEC;
00090 
00091   /* Test squaring */
00092   srand(seed);
00093   start = clock();
00094   for(ix = 0; ix < ntests; ix++) {
00095     mpp_random_size(&a, prec);
00096     mp_sqr(&a, &c);
00097   }
00098   stop = clock();
00099 
00100   sqrtime = (double)(stop - start) / CLOCKS_PER_SEC;
00101 
00102   printf("Multiply: %.4f\n", multime);
00103   printf("Square:   %.4f\n", sqrtime);
00104   if(multime < sqrtime) {
00105     printf("Speedup:  %.1f%%\n", 100.0 * (1.0 - multime / sqrtime));
00106     printf("Prefer:   multiply\n");
00107   } else {
00108     printf("Speedup:  %.1f%%\n", 100.0 * (1.0 - sqrtime / multime));
00109     printf("Prefer:   square\n");
00110   }
00111 
00112   mp_clear(&a); mp_clear(&c);
00113   return 0;
00114 
00115 }