[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / polly / lib / External / isl / imath / imtimer.c
blobfde49a7ce51aa81e4dc4b1c5aa34516cbc55c932
1 /*
2 Name: imtimer.c
3 Purpose: Timing tests for the imath library.
4 Author: M. J. Fromberger
6 Copyright (C) 2002-2008 Michael J. Fromberger, All Rights Reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
27 #include <limits.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
33 #include <getopt.h>
34 #include <unistd.h>
36 #include "imath.h"
38 double clocks_to_seconds(clock_t start, clock_t end);
39 double get_multiply_time(int nt, int prec);
40 double get_exptmod_time(int nt, int prec);
41 mp_int alloc_values(int nt, int prec);
42 void randomize_values(mp_int values, int nt, int prec);
43 void release_values(mp_int values, int nt);
44 void mp_int_random(mp_int z, int prec);
46 const int g_mul_factor = 1000;
48 int main(int argc, char *argv[]) {
49 int do_mul = 0, do_exp = 0, do_header = 1;
50 int num_tests, precision = 0, opt;
51 mp_size threshold = 0;
52 unsigned int seed = (unsigned int)time(NULL);
54 while ((opt = getopt(argc, argv, "ehmnp:s:t:")) != EOF) {
55 switch (opt) {
56 case 'e':
57 do_exp = 1;
58 break;
59 case 'm':
60 do_mul = 1;
61 break;
62 case 'n':
63 do_header = 0;
64 break;
65 case 'p':
66 precision = atoi(optarg);
67 break;
68 case 's':
69 seed = atoi(optarg);
70 break;
71 case 't':
72 threshold = (mp_size)atoi(optarg);
73 break;
74 default:
75 fprintf(stderr,
76 "Usage: imtimer [options] <num-tests>\n\n"
77 "Options understood:\n"
78 " -e -- test modular exponentiation speed.\n"
79 " -h -- display this help message.\n"
80 " -m -- test multiplication speed.\n"
81 " -n -- no header line.\n"
82 " -p <dig> -- use values with <dig> digits.\n"
83 " -s <rnd> -- set random seed to <rnd>.\n"
84 " -t <dig> -- set recursion threshold to <dig> digits.\n\n");
85 return (opt != 'h');
89 if (optind >= argc) {
90 fprintf(stderr,
91 "Usage: imtimer [options] <num-tests>\n"
92 "[use \"imtimer -h\" for help with options]\n\n");
93 return 1;
94 } else
95 num_tests = atoi(argv[optind]);
97 srand(seed);
99 if (num_tests <= 0) {
100 fprintf(stderr, "You must request at least one test.\n");
101 return 1;
103 if (precision < 0) {
104 fprintf(stderr, "Precision must be non-negative (0 means default).\n");
105 return 1;
107 mp_int_multiply_threshold(threshold);
109 if (do_header) printf("NUM\tPREC\tBITS\tREC\tRESULT\n");
110 printf("%d\t%d\t%d\t%u", num_tests, precision,
111 (int)(precision * MP_DIGIT_BIT), threshold);
113 if (do_mul) {
114 double m_time = get_multiply_time(num_tests, precision);
116 printf("\tMUL %.3f %.3f", m_time, m_time / num_tests);
119 if (do_exp) {
120 double e_time = get_exptmod_time(num_tests, precision);
122 printf("\tEXP %.3f %.3f", e_time, e_time / num_tests);
124 fputc('\n', stdout);
125 fflush(stdout);
127 return 0;
130 double clocks_to_seconds(clock_t start, clock_t end) {
131 return (double)(end - start) / CLOCKS_PER_SEC;
134 mp_int alloc_values(int nt, int prec) {
135 mp_int out = malloc(nt * sizeof(mpz_t));
136 int i;
138 if (out == NULL) return NULL;
140 for (i = 0; i < nt; ++i) {
141 if (mp_int_init_size(out + i, prec) != MP_OK) {
142 while (--i >= 0) mp_int_clear(out + i);
143 return NULL;
147 return out;
150 void randomize_values(mp_int values, int nt, int prec) {
151 int i;
153 for (i = 0; i < nt; ++i) mp_int_random(values + i, prec);
156 void release_values(mp_int values, int nt) {
157 int i;
159 for (i = 0; i < nt; ++i) mp_int_clear(values + i);
161 free(values);
164 double get_multiply_time(int nt, int prec) {
165 clock_t start, end;
166 mp_int values;
167 int i;
169 if ((values = alloc_values(3, prec)) == NULL) return 0.0;
170 randomize_values(values, 2, prec);
172 start = clock();
173 for (i = 0; i < nt; ++i) mp_int_mul(values, values + 1, values + 2);
174 end = clock();
176 release_values(values, 3);
178 return clocks_to_seconds(start, end);
181 double get_exptmod_time(int nt, int prec) {
182 clock_t start, end;
183 mp_int values;
184 int i;
186 if ((values = alloc_values(4, prec)) == NULL) return 0.0;
187 randomize_values(values, 3, prec);
189 start = clock();
190 for (i = 0; i < nt; ++i)
191 mp_int_exptmod(values, values + 1, values + 2, values + 3);
192 end = clock();
194 release_values(values, 4);
196 return clocks_to_seconds(start, end);
199 void mp_int_random(mp_int z, int prec) {
200 int i;
202 if (prec > (int)MP_ALLOC(z)) prec = (int)MP_ALLOC(z);
204 for (i = 0; i < prec; ++i) {
205 mp_digit d = 0;
206 int j;
208 for (j = 0; j < (int)sizeof(d); ++j) {
209 d = (d << CHAR_BIT) | (rand() & UCHAR_MAX);
212 z->digits[i] = d;
214 z->used = prec;