Back to index

lightning-sunbird  0.9+nobinonly
mptest-3.c
Go to the documentation of this file.
00001 /*
00002  * Simple test driver for MPI library
00003  *
00004  * Test 3: Multiplication, division, and exponentiation 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-3.c,v 1.4 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 
00053 #define  SQRT 1  /* define nonzero to get square-root test  */
00054 #define  EXPT 0  /* define nonzero to get exponentiate test */
00055 
00056 int main(int argc, char *argv[])
00057 {
00058   int      ix;
00059   mp_int   a, b, c, d;
00060   mp_digit r;
00061   mp_err   res;
00062 
00063   if(argc < 3) {
00064     fprintf(stderr, "Usage: %s <a> <b>\n", argv[0]);
00065     return 1;
00066   }
00067 
00068   printf("Test 3: Multiplication and division\n\n");
00069   srand(time(NULL));
00070 
00071   mp_init(&a);
00072   mp_init(&b);
00073 
00074   mp_read_variable_radix(&a, argv[1], 10);
00075   mp_read_variable_radix(&b, argv[2], 10);
00076   printf("a = "); mp_print(&a, stdout); fputc('\n', stdout);
00077   printf("b = "); mp_print(&b, stdout); fputc('\n', stdout);
00078   
00079   mp_init(&c);
00080   printf("\nc = a * b\n");
00081 
00082   mp_mul(&a, &b, &c);
00083   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
00084 
00085   printf("\nc = b * 32523\n");
00086 
00087   mp_mul_d(&b, 32523, &c);
00088   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
00089   
00090   mp_init(&d);
00091   printf("\nc = a / b, d = a mod b\n");
00092   
00093   mp_div(&a, &b, &c, &d);
00094   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);  
00095   printf("d = "); mp_print(&d, stdout); fputc('\n', stdout);  
00096 
00097   ix = rand() % 256;
00098   printf("\nc = a / %d, r = a mod %d\n", ix, ix);
00099   mp_div_d(&a, (mp_digit)ix, &c, &r);
00100   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);  
00101   printf("r = %04X\n", r);
00102 
00103 #if EXPT
00104   printf("\nc = a ** b\n");
00105   mp_expt(&a, &b, &c);
00106   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);  
00107 #endif
00108 
00109   ix = rand() % 256;
00110   printf("\nc = 2^%d\n", ix);
00111   mp_2expt(&c, ix);
00112   printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
00113 
00114 #if SQRT
00115   printf("\nc = sqrt(a)\n");
00116   if((res = mp_sqrt(&a, &c)) != MP_OKAY) {
00117     printf("mp_sqrt: %s\n", mp_strerror(res));
00118   } else {
00119     printf("c = "); mp_print(&c, stdout); fputc('\n', stdout);
00120     mp_sqr(&c, &c);
00121     printf("c^2 = "); mp_print(&c, stdout); fputc('\n', stdout);
00122   }
00123 #endif
00124 
00125   mp_clear(&d);
00126   mp_clear(&c);
00127   mp_clear(&b);
00128   mp_clear(&a);
00129 
00130   return 0;
00131 }