-
Notifications
You must be signed in to change notification settings - Fork 0
/
djb2_numbers.cpp
145 lines (113 loc) · 2.68 KB
/
djb2_numbers.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
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
#include <fstream>
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <arpa/inet.h>
#include <pthread.h>
#include "util.h"
using namespace std;
#define N ((1 << 18) - 1)
struct tdata {
uint32_t begin;
uint32_t end;
};
vector<unsigned long> results;
vector<string> results_hexa;
static string to_hex(unsigned char* md)
{
char line[9];
for (int i = 0; i < 4; i++) {
sprintf(&line[i * 2], "%02x", md[i]);
}
return string(line);
}
static void to_data(unsigned char *md, uint16_t &x, uint16_t &y)
{
x = htons(*((uint16_t *)md));
y = htons(*((uint16_t *)(&md[2])));
}
static unsigned long djb2(unsigned char *str, int count)
{
unsigned long h = 5381;
int c;
for (int i = 0; i < count; i++) {
c = *str++;
h = ((h << 5) ^ h) + c;
}
return h;
}
static void *hash(void *arg)
{
tdata *td = (tdata *)arg;
for (uint32_t i = td->begin; i < td->end; i++) {
results[i] = djb2((unsigned char *)&i, 4);
}
return NULL;
}
int main(int argc, char *argv[])
{
ofstream output;
vector<tdata> td;
vector<pthread_t> threads;
int rc, step, idx;
struct timeval t1, t2;
KICK(argc < 4, "./md5 <output_file> <data_file> <num_threads>");
int num_threads = atoi(argv[3]);
int size = N + 1;
results.resize(size);
td.resize(num_threads);
threads.resize(num_threads);
step = size / num_threads;
if (size % num_threads) {
step++;
}
gettimeofday(&t1, NULL);
idx = 0;
for (int i = 0; i < num_threads; i++) {
td[i].begin = idx;
td[i].end = min(idx + step, size);
idx = td[i].end;
rc = pthread_create(&threads[i], NULL, hash, &td[i]);
DIE(rc != 0, "pthread_create");
}
for (int i = 0; i < num_threads; i++) {
rc = pthread_join(threads[i], NULL);
DIE(rc != 0, "pthread_join");
}
gettimeofday(&t2, NULL);
uint64_t usec = diff(&t2, &t1);
cout << "djb2: " << "0 - " << N << endl;
cout << usec << " usec" << endl;
for (int i = 0; i < size; i++) {
results_hexa.push_back(to_hex((unsigned char *)&results[i]));
}
map<string, int> m;
int num_collisions = 0;
cerr << "collisions: " << endl;
for (int i = 0; i < size; i++) {
string key = results_hexa[i];
if (m.count(key)) {
cerr << "(" << m.find(key)->second << ", " << i << ")" << endl;
num_collisions++;
}
m.insert(pair<string, int>(key, i));
}
cout << num_collisions << " collisions" << endl;
output.open(argv[1]);
DIE(output.fail(), "open");
for (int i = 0; i < size; i++) {
output << results_hexa[i] << " " << i << endl;
}
output.close();
output.open(argv[2]);
DIE(output.fail(), "open");
for (int i = 0; i < size; i++) {
uint16_t x, y;
to_data((unsigned char *)&results[i], x, y);
output << x << " " << y << endl;
}
output.close();
return 0;
}