|
1 /* |
|
2 * Simple test driver for MPI library |
|
3 * |
|
4 * Test 3: Multiplication, division, and exponentiation test |
|
5 * |
|
6 * This Source Code Form is subject to the terms of the Mozilla Public |
|
7 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
9 |
|
10 #include <stdio.h> |
|
11 #include <stdlib.h> |
|
12 #include <string.h> |
|
13 #include <ctype.h> |
|
14 #include <limits.h> |
|
15 |
|
16 #include <time.h> |
|
17 |
|
18 #include "mpi.h" |
|
19 |
|
20 #define SQRT 1 /* define nonzero to get square-root test */ |
|
21 #define EXPT 0 /* define nonzero to get exponentiate test */ |
|
22 |
|
23 int main(int argc, char *argv[]) |
|
24 { |
|
25 int ix; |
|
26 mp_int a, b, c, d; |
|
27 mp_digit r; |
|
28 mp_err res; |
|
29 |
|
30 if(argc < 3) { |
|
31 fprintf(stderr, "Usage: %s <a> <b>\n", argv[0]); |
|
32 return 1; |
|
33 } |
|
34 |
|
35 printf("Test 3: Multiplication and division\n\n"); |
|
36 srand(time(NULL)); |
|
37 |
|
38 mp_init(&a); |
|
39 mp_init(&b); |
|
40 |
|
41 mp_read_variable_radix(&a, argv[1], 10); |
|
42 mp_read_variable_radix(&b, argv[2], 10); |
|
43 printf("a = "); mp_print(&a, stdout); fputc('\n', stdout); |
|
44 printf("b = "); mp_print(&b, stdout); fputc('\n', stdout); |
|
45 |
|
46 mp_init(&c); |
|
47 printf("\nc = a * b\n"); |
|
48 |
|
49 mp_mul(&a, &b, &c); |
|
50 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
51 |
|
52 printf("\nc = b * 32523\n"); |
|
53 |
|
54 mp_mul_d(&b, 32523, &c); |
|
55 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
56 |
|
57 mp_init(&d); |
|
58 printf("\nc = a / b, d = a mod b\n"); |
|
59 |
|
60 mp_div(&a, &b, &c, &d); |
|
61 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
62 printf("d = "); mp_print(&d, stdout); fputc('\n', stdout); |
|
63 |
|
64 ix = rand() % 256; |
|
65 printf("\nc = a / %d, r = a mod %d\n", ix, ix); |
|
66 mp_div_d(&a, (mp_digit)ix, &c, &r); |
|
67 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
68 printf("r = %04X\n", r); |
|
69 |
|
70 #if EXPT |
|
71 printf("\nc = a ** b\n"); |
|
72 mp_expt(&a, &b, &c); |
|
73 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
74 #endif |
|
75 |
|
76 ix = rand() % 256; |
|
77 printf("\nc = 2^%d\n", ix); |
|
78 mp_2expt(&c, ix); |
|
79 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
80 |
|
81 #if SQRT |
|
82 printf("\nc = sqrt(a)\n"); |
|
83 if((res = mp_sqrt(&a, &c)) != MP_OKAY) { |
|
84 printf("mp_sqrt: %s\n", mp_strerror(res)); |
|
85 } else { |
|
86 printf("c = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
87 mp_sqr(&c, &c); |
|
88 printf("c^2 = "); mp_print(&c, stdout); fputc('\n', stdout); |
|
89 } |
|
90 #endif |
|
91 |
|
92 mp_clear(&d); |
|
93 mp_clear(&c); |
|
94 mp_clear(&b); |
|
95 mp_clear(&a); |
|
96 |
|
97 return 0; |
|
98 } |