forked from cmuparlay/parlaylib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkaratsuba.cpp
44 lines (37 loc) · 1.15 KB
/
karatsuba.cpp
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
#include <iostream>
#include <string>
#include <random>
#include <limits>
#include <parlay/primitives.h>
#include <parlay/random.h>
#include "karatsuba.h"
// **************************************************************
// Driver code
// **************************************************************
int main(int argc, char* argv[]) {
auto usage = "Usage: karatsuba <size>";
if (argc != 2) std::cout << usage << std::endl;
else {
long n;
try { n = std::stol(argv[1]); }
catch (...) { std::cout << usage << std::endl; return 1; }
long m = n/digit_len;
auto randnum = [] (long m, long seed) {
parlay::random_generator gen(seed);
auto maxv = std::numeric_limits<digit>::max();
std::uniform_int_distribution<digit> dis(0, maxv);
return parlay::tabulate(m, [&] (long i) {
auto r = gen[i];
if (i == m-1) return dis(r)/2; // to ensure it is not negative
else return dis(r);});
};
bigint a = randnum(m, 0);
bigint b = randnum(m, 1);
bigint result;
parlay::internal::timer t("Time");
for (int i=0; i < 5; i++) {
result = karatsuba(a, b);
t.next("karatsuba");
}
}
}