summaryrefslogtreecommitdiff
path: root/Types.h
blob: eb2a3096fedb169ca4dc08b6bf8debfc02aa41f7 (plain)
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
#pragma once

#include "pstdint.h"
#include "Bitvec.h"

//-----------------------------------------------------------------------------
// If the optimizer detects that a value in a speed test is constant or unused,
// the optimizer may remove references to it or otherwise create code that
// would not occur in a real-world application. To prevent the optimizer from
// doing this we declare two trivial functions that either sink or source data,
// and bar the compiler from optimizing them.

void     blackhole ( uint32_t x );
uint32_t whitehole ( void );

//-----------------------------------------------------------------------------

typedef void (*pfHash) ( const void * blob, const int len, const uint32_t seed, void * out );

//-----------------------------------------------------------------------------

template < class T >
class hashfunc
{
public:

	hashfunc ( pfHash h ) : m_hash(h)
	{
	}

	inline void operator () ( const void * key, const int len, const uint32_t seed, uint32_t * out )
	{
		m_hash(key,len,seed,out);
	}

	inline operator pfHash ( void ) const
	{
		return m_hash;
	}

	inline T operator () ( const void * key, const int len, const uint32_t seed ) 
	{
		T result;

		m_hash(key,len,seed,(uint32_t*)&result);

		return result;
	}

	pfHash m_hash;
};

//-----------------------------------------------------------------------------

template < int _bits >
class Blob
{
public:

	Blob()
	{
	}

	Blob ( int x )
	{
		for(int i = 0; i < nbytes; i++)
		{
			bytes[i] = 0;
		}

		*(int*)bytes = x;
	}

	Blob ( const Blob & k )
	{
		for(int i = 0; i < nbytes; i++)
		{
			bytes[i] = k.bytes[i];
		}
	}

	Blob & operator = ( const Blob & k )
	{
		for(int i = 0; i < nbytes; i++)
		{
			bytes[i] = k.bytes[i];
		}

		return *this;
	}

	void set ( const void * blob, int len )
	{
		const uint8_t * k = (const uint8_t*)blob;

		len = len > nbytes ? nbytes : len;

		for(int i = 0; i < len; i++)
		{
			bytes[i] = k[i];
		}

		for(int i = len; i < nbytes; i++)
		{
			bytes[i] = 0;
		}
	}

	uint8_t & operator [] ( int i )
	{
		return bytes[i];
	}

	const uint8_t & operator [] ( int i ) const
	{
		return bytes[i];
	}

	//----------
	// boolean operations
	
	bool operator < ( const Blob & k ) const
	{
		for(int i = 0; i < nbytes; i++)
		{
			if(bytes[i] < k.bytes[i]) return true;
			if(bytes[i] > k.bytes[i]) return false;
		}

		return false;
	}

	bool operator == ( const Blob & k ) const
	{
		for(int i = 0; i < nbytes; i++)
		{
			if(bytes[i] != k.bytes[i]) return false;
		}

		return true;
	}

	bool operator != ( const Blob & k ) const
	{
		return !(*this == k);
	}

	//----------
	// bitwise operations

	Blob operator ^ ( const Blob & k ) const 
	{
		Blob t;

		for(int i = 0; i < nbytes; i++)
		{
			t.bytes[i] = bytes[i] ^ k.bytes[i];
		}

		return t;
	}

	Blob & operator ^= ( const Blob & k )
	{
		for(int i = 0; i < nbytes; i++)
		{
			bytes[i] ^= k.bytes[i];
		}

		return *this;
	}

	int operator & ( int x )
	{
		return (*(int*)bytes) & x;
	}

	Blob & operator &= ( const Blob & k )
	{
		for(int i = 0; i < nbytes; i++)
		{
			bytes[i] &= k.bytes[i];
		}
	}

	Blob operator << ( int c )
	{
		Blob t = *this;

		lshift(&t.bytes[0],nbytes,c);

		return t;
	}

	Blob operator >> ( int c )
	{
		Blob t = *this;

		rshift(&t.bytes[0],nbytes,c);

		return t;
	}

	Blob & operator <<= ( int c )
	{
		lshift(&bytes[0],nbytes,c);

		return *this;
	}

	Blob & operator >>= ( int c )
	{
		rshift(&bytes[0],nbytes,c);

		return *this;
	}

	//----------
	
	enum
	{
		nbits = _bits,
		nbytes = (_bits+7)/8,

		align4  = (nbytes & 2) ? 0 : 1,
		align8  = (nbytes & 3) ? 0 : 1,
		align16 = (nbytes & 4) ? 0 : 1,
	};

private:

	uint8_t bytes[nbytes];
};

typedef Blob<128> uint128_t;

//-----------------------------------------------------------------------------