Back to index

lightning-sunbird  0.9+nobinonly
mptest-3a.c
Go to the documentation of this file.
00001 /*
00002  * Simple test driver for MPI library
00003  *
00004  * Test 3a: Multiplication vs. squaring timing test
00005  *
00006  * ***** BEGIN LICENSE BLOCK *****
00007  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00008  *
00009  * The contents of this file are subject to the Mozilla Public License Version
00010  * 1.1 (the "License"); you may not use this file except in compliance with
00011  * the License. You may obtain a copy of the License at
00012  * http://www.mozilla.org/MPL/
00013  *
00014  * Software distributed under the License is distributed on an "AS IS" basis,
00015  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00016  * for the specific language governing rights and limitations under the
00017  * License.
00018  *
00019  * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
00020  *
00021  * The Initial Developer of the Original Code is
00022  * Michael J. Fromberger.
00023  * Portions created by the Initial Developer are Copyright (C) 1998
00024  * the Initial Developer. All Rights Reserved.
00025  *
00026  * Contributor(s):
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 /* $Id: mptest-3a.c,v 1.3 2004/04/27 23:04:37 gerv%gerv.net Exp $ */
00042 
00043 #include <stdio.h>
00044 #include <stdlib.h>
00045 #include <string.h>
00046 #include <ctype.h>
00047 #include <limits.h>
00048 
00049 #include <time.h>
00050 
00051 #include "mpi.h"
00052 #include "mpprime.h"
00053 
00054 int main(int argc, char *argv[])
00055 {
00056   int        ix, num, prec = 8;
00057   double     d1, d2;
00058   clock_t    start, finish;
00059   time_t     seed;
00060   mp_int     a, c, d;
00061 
00062   seed = time(NULL);
00063 
00064   if(argc < 2) {
00065     fprintf(stderr, "Usage: %s <num-tests> [<precision>]\n", argv[0]);
00066     return 1;
00067   }
00068 
00069   if((num = atoi(argv[1])) < 0)
00070     num = -num;
00071 
00072   if(!num) {
00073     fprintf(stderr, "%s: must perform at least 1 test\n", argv[0]);
00074     return 1;
00075   }
00076 
00077   if(argc > 2) {
00078     if((prec = atoi(argv[2])) <= 0)
00079       prec = 8;
00080     else
00081       prec = (prec + (DIGIT_BIT - 1)) / DIGIT_BIT;
00082   }
00083   
00084   printf("Test 3a: Multiplication vs squaring timing test\n"
00085         "Precision:  %d digits (%u bits)\n"
00086         "# of tests: %d\n\n", prec, prec * DIGIT_BIT, num);
00087 
00088   mp_init_size(&a, prec);
00089 
00090   mp_init(&c); mp_init(&d);
00091 
00092   printf("Verifying accuracy ... \n");
00093   srand((unsigned int)seed);
00094   for(ix = 0; ix < num; ix++) {
00095     mpp_random_size(&a, prec);
00096     mp_mul(&a, &a, &c);
00097     mp_sqr(&a, &d);
00098 
00099     if(mp_cmp(&c, &d) != 0) {
00100       printf("Error!  Results not accurate:\n");
00101       printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
00102       printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
00103       printf("d = "); mp_print(&d, stdout); fputc('\n', stdout);
00104       mp_sub(&c, &d, &d);
00105       printf("dif "); mp_print(&d, stdout); fputc('\n', stdout);
00106       mp_clear(&c); mp_clear(&d);
00107       mp_clear(&a);
00108       return 1;
00109     }
00110   }
00111   printf("Accuracy is confirmed for the %d test samples\n", num);
00112   mp_clear(&d);
00113 
00114   printf("Testing squaring ... \n");
00115   srand((unsigned int)seed);
00116   start = clock();
00117   for(ix = 0; ix < num; ix++) {
00118     mpp_random_size(&a, prec);
00119     mp_sqr(&a, &c);
00120   }
00121   finish = clock();
00122 
00123   d2 = (double)(finish - start) / CLOCKS_PER_SEC;
00124 
00125   printf("Testing multiplication ... \n");
00126   srand((unsigned int)seed);
00127   start = clock();
00128   for(ix = 0; ix < num; ix++) {
00129     mpp_random(&a);
00130     mp_mul(&a, &a, &c);
00131   }
00132   finish = clock();
00133 
00134   d1 = (double)(finish - start) / CLOCKS_PER_SEC;
00135 
00136   printf("Multiplication time: %.3f sec (%.3f each)\n", d1, d1 / num);
00137   printf("Squaring time:       %.3f sec (%.3f each)\n", d2, d2 / num);
00138   printf("Improvement:         %.2f%%\n", (1.0 - (d2 / d1)) * 100.0);
00139 
00140   mp_clear(&c);
00141   mp_clear(&a);
00142 
00143   return 0;
00144 }