1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
/* compute the crossover for recursive and simple multiplication */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "number.h"
#ifndef VARARGS
#include <stdarg.h>
#else
#include <varargs.h>
#endif
/* from number.c ... */
extern int mul_base_digits;
/* extern int mul_small_digits; */
extern bc_num _one_;
/* global variables */
int test_n = 1000;
long test_time = 30 * CLOCKS_PER_SEC; /* 30 seconds */
/* Other things for number.c. */
int std_only;
void
out_of_memory()
{
fprintf (stderr, "Fatal error: Out of memory for malloc.\n");
exit (1);
}
/* Runtime error will print a message and stop the machine. */
#ifndef VARARGS
#ifdef __STDC__
void
rt_error (char *mesg, ...)
#else
void
rt_error (mesg)
char *mesg;
#endif
#else
void
rt_error (mesg, va_alist)
char *mesg;
#endif
{
va_list args;
char error_mesg [255];
#ifndef VARARGS
va_start (args, mesg);
#else
va_start (args);
#endif
vsprintf (error_mesg, mesg, args);
va_end (args);
fprintf (stderr, "Runtime error: %s\n", error_mesg);
}
/* A runtime warning tells of some action taken by the processor that
may change the program execution but was not enough of a problem
to stop the execution. */
#ifndef VARARGS
#ifdef __STDC__
void
rt_warn (char *mesg, ...)
#else
void
rt_warn (mesg)
char *mesg;
#endif
#else
void
rt_warn (mesg, va_alist)
char *mesg;
#endif
{
va_list args;
char error_mesg [255];
#ifndef VARARGS
va_start (args, mesg);
#else
va_start (args);
#endif
vsprintf (error_mesg, mesg, args);
va_end (args);
fprintf (stderr, "Runtime warning: %s\n", error_mesg);
}
void
out_char (int ch)
{
putchar (ch);
}
/* Time stuff !!! */
int
timeit ( bc_num a, bc_num b, int *n)
{
clock_t first;
int i, res;
bc_num c;
bc_init_num (&c);
first = clock();
*n = 0;
do {
for (i=0; i<test_n; i++)
bc_multiply(a,b,&c,0);
*n += test_n;
res = (int) (clock() - first);
} while (res < test_time);
return res;
}
int debug = 0; /* Print debugging messages? */
int main (int argc, char **argv)
{
bc_num ten, num, expo, big;
int min, max, mid;
#if 0
int smallsize;
#endif
int n1, n2;
clock_t t1, t2;
float permul1, permul2;
/* args? */
if (argc > 1)
if (strcmp (argv[1], "-d") == 0)
debug = 1;
bc_init_numbers();
bc_init_num (&ten);
bc_init_num (&num);
bc_init_num (&expo);
bc_init_num (&big);
bc_int2num (&ten, 10);
if (debug)
fprintf (stderr, "Timings are for %d multiplies\n"
"Minimum time is %lu seconds\n", test_n,
test_time/CLOCKS_PER_SEC);
/* Two of the same size */
min = 10;
max = 500;
if (debug)
fprintf (stderr, "Testing numbers of the same length.\n");
while (min < max) {
mid = (min+max)/2;
if (debug) fprintf (stderr,"Checking %d...\n", mid);
bc_int2num (&expo, mid);
bc_raise (ten, expo, &num, 0);
bc_sub (num, _one_, &num, 0);
mul_base_digits = 2*mid+1;
t1 = timeit (num, num, &n1);
permul1 = (float)t1/(float)n1;
mul_base_digits = 2*mid-1;
t2 = timeit (num, num, &n2);
permul2 = (float)t2/(float)n2;
if (permul1 < permul2)
min = mid+1;
else
max = mid-1;
if (debug) {
fprintf (stderr, "n1 = %d :: n2 = %d\n", n1, n2);
fprintf (stderr, "p1 = %f :: p2 = %f\n", permul1, permul2);
}
}
if (debug)
fprintf (stderr, "Base digits crossover at %d digits\n", min);
printf ("#define MUL_BASE_DIGITS %d\n", 2*min);
#if 0
mul_base_digits = min;
/* Small one times a big one. */
smallsize = min/2;
bc_int2num (&expo, smallsize);
bc_raise (ten, expo, &big, 0);
bc_sub (num, _one_, &big, 0);
min = min / 2;
max = 500;
if (debug)
fprintf (stderr, "Testing numbers of the different length.\n");
while (min < max) {
mid = (min+max)/2;
if (debug) fprintf (stderr, "Checking %d...\n", mid);
bc_int2num (&expo, mid-smallsize);
bc_raise (ten, expo, &num, 0);
bc_sub (num, _one_, &num, 0);
mul_small_digits = mid+1;
t1 = timeit (big, num, &n1);
permul1 = (float)t1/(float)n1;
mul_small_digits = mid-1;
t2 = timeit (big, num, &n2);
permul2 = (float)t2/(float)n2;
if (permul1 < permul2)
min = mid+1;
else
max = mid-1;
if (debug) {
fprintf (stderr, "n1 = %d :: n2 = %d\n", n1, n2);
fprintf (stderr, "p1 = %f :: p2 = %f\n", permul1, permul2);
}
}
if (debug)
fprintf (stderr, "Non equal digits crossover at %d total digits\n", min);
printf ("#define MUL_SMALL_DIGITS = %d\n", min);
#endif
return 0;
}
|