CFx SDK Documentation 2024 SP0
Loading...
Searching...
No Matches
OdFNVHash.h
Go to the documentation of this file.
1
2// Copyright (C) 2002-2022, Open Design Alliance (the "Alliance").
3// All rights reserved.
4//
5// This software and its documentation and related materials are owned by
6// the Alliance. The software may only be incorporated into application
7// programs owned by members of the Alliance, subject to a signed
8// Membership Agreement and Supplemental Software License Agreement with the
9// Alliance. The structure and organization of this software are the valuable
10// trade secrets of the Alliance and its suppliers. The software is also
11// protected by copyright law and international treaty provisions. Application
12// programs incorporating this software must include the following statement
13// with their copyright notices:
14//
15// This application incorporates Open Design Alliance software pursuant to a license
16// agreement with Open Design Alliance.
17// Open Design Alliance Copyright (C) 2002-2022 by Open Design Alliance.
18// All rights reserved.
19//
20// By use of this software, its documentation or related materials, you
21// acknowledge and accept the above terms.
23
24/*
25 * Fowler/Noll/Vo hash
26 *
27 * The basis of this hash algorithm was taken from an idea sent
28 * as reviewer comments to the IEEE POSIX P1003.2 committee by:
29 *
30 * Phong Vo (http://www.research.att.com/info/kpv/)
31 * Glenn Fowler (http://www.research.att.com/~gsf/)
32 *
33 * In a subsequent ballot round:
34 *
35 * Landon Curt Noll (http://www.isthe.com/chongo/)
36 *
37 * improved on their algorithm. Some people tried this hash
38 * and found that it worked rather well. In an EMail message
39 * to Landon, they named it the ``Fowler/Noll/Vo'' or FNV hash.
40 *
41 * FNV hashes are designed to be fast while maintaining a low
42 * collision rate. The FNV speed allows one to quickly hash lots
43 * of data while maintaining a reasonable collision rate. See:
44 *
45 * http://www.isthe.com/chongo/tech/comp/fnv/index.html
46 *
47 * for more details as well as other forms of the FNV hash.
48 *
49 * Please do not copyright this code. This code is in the public domain.
50 *
51 * LANDON CURT NOLL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
52 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO
53 * EVENT SHALL LANDON CURT NOLL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
54 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
55 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
56 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
57 * PERFORMANCE OF THIS SOFTWARE.
58 *
59 * By:
60 * chongo <Landon Curt Noll> /\oo/\
61 * http://www.isthe.com/chongo/
62 *
63 * Share and Enjoy! :-)
64 */
65
66#ifndef _OD_FNV1HASH_H_
67#define _OD_FNV1HASH_H_
68
69#include "OdString.h"
70
71#include "TD_PackPush.h"
72
87inline OdUInt32 odFNV32HashBuf(const void *buf, size_t len, OdUInt32 hval = 0x811c9dc5)
88{
89 const OdUInt8 *bp = (const OdUInt8*)buf; // Start of buffer
90 const OdUInt8 *be = bp + len; // End of buffer
91 while (bp < be) // FNV-1 hash each octet in the buffer
92 {
93 // Multiply by the 32 bit FNV magic prime mod 2^32
94#if defined(NO_FNV_GCC_OPTIMIZATION)
95 hval *= 0x01000193;
96#else
97 hval += (hval << 1) + (hval << 4) + (hval << 7) +
98 (hval << 8) + (hval << 24);
99#endif
100 // Xor the bottom with the current octet
101 hval ^= (OdUInt32)*bp++;
102 }
103 // Return our new hash value
104 return hval;
105}
106
120inline OdUInt32 odFNV32HashStr(const char *str, OdUInt32 hval = 0x811c9dc5)
121{
122 const OdUInt8 *s = (const OdUInt8*)str; // Unsigned string
123 while (*s) // FNV-1 hash each octet in the buffer
124 {
125 // Multiply by the 32 bit FNV magic prime mod 2^32
126#if defined(NO_FNV_GCC_OPTIMIZATION)
127 hval *= 0x01000193;
128#else
129 hval += (hval << 1) + (hval << 4) + (hval << 7) +
130 (hval << 8) + (hval << 24);
131#endif
132 // Xor the bottom with the current octet
133 hval ^= (OdUInt32)*s++;
134 }
135 // Return our new hash value
136 return hval;
137}
138
152inline OdUInt32 odFNV32aHashBuf(const void *buf, size_t len, OdUInt32 hval = 0x811c9dc5)
153{
154 const OdUInt8 *bp = (const OdUInt8*)buf; // Start of buffer
155 const OdUInt8 *be = bp + len; // End of buffer
156 while (bp < be) // FNV-1a hash each octet in the buffer
157 {
158 // Xor the bottom with the current octet
159 hval ^= (OdUInt32)*bp++;
160 // Multiply by the 32 bit FNV magic prime mod 2^32
161#if defined(NO_FNV_GCC_OPTIMIZATION)
162 hval *= 0x01000193;
163#else
164 hval += (hval << 1) + (hval << 4) + (hval << 7) +
165 (hval << 8) + (hval << 24);
166#endif
167 }
168 // Return our new hash value
169 return hval;
170}
171
184inline OdUInt32 odFNV32aHashStr(const char *str, OdUInt32 hval = 0x811c9dc5)
185{
186 const OdUInt8 *s = (const OdUInt8*)str; // Unsigned string
187 while (*s) // FNV-1a hash each octet in the buffer
188 {
189 // Xor the bottom with the current octet
190 hval ^= (OdUInt32)*s++;
191 // Multiply by the 32 bit FNV magic prime mod 2^32
192#if defined(NO_FNV_GCC_OPTIMIZATION)
193 hval *= 0x01000193;
194#else
195 hval += (hval << 1) + (hval << 4) + (hval << 7) +
196 (hval << 8) + (hval << 24);
197#endif
198 }
199 // Return our new hash value
200 return hval;
201}
202
217inline OdUInt64 odFNV64HashBuf(const void *buf, size_t len, OdUInt64 hval = 0xcbf29ce484222325ULL)
218{
219 const OdUInt8 *bp = (const OdUInt8*)buf; // Start of buffer
220 const OdUInt8 *be = bp + len; // Beyond end of buffer
221 while (bp < be) // FNV-1 hash each octet in the buffer
222 {
223 // Multiply by the 64 bit FNV magic prime mod 2^32
224#if defined(NO_FNV_GCC_OPTIMIZATION)
225 hval *= 0x100000001b3;
226#else
227 hval += (hval << 1) + (hval << 4) + (hval << 5) +
228 (hval << 7) + (hval << 8) + (hval << 40);
229#endif
230 // Xor the bottom with the current octet
231 hval ^= (OdUInt64)*bp++;
232 }
233 // Return our new hash value
234 return hval;
235}
236
250inline OdUInt64 odFNV64HashStr(const char *str, OdUInt64 hval = 0xcbf29ce484222325ULL)
251{
252 const OdUInt8 *s = (const OdUInt8*)str; // Unsigned string
253 while (*s) // FNV-1 hash each octet in the buffer
254 {
255 // Multiply by the 64 bit FNV magic prime mod 2^32
256#if defined(NO_FNV_GCC_OPTIMIZATION)
257 hval *= 0x100000001b3;
258#else
259 hval += (hval << 1) + (hval << 4) + (hval << 5) +
260 (hval << 7) + (hval << 8) + (hval << 40);
261#endif
262 // Xor the bottom with the current octet
263 hval ^= (OdUInt64)*s++;
264 }
265 // Return our new hash value
266 return hval;
267}
268
282inline OdUInt64 odFNV64aHashBuf(const void *buf, size_t len, OdUInt64 hval = 0xcbf29ce484222325ULL)
283{
284 const OdUInt8 *bp = (const OdUInt8*)buf; // Start of buffer
285 const OdUInt8 *be = bp + len; // Beyond end of buffer
286 while (bp < be) // FNV-1a hash each octet in the buffer
287 {
288 // Xor the bottom with the current octet
289 hval ^= (OdUInt64)*bp++;
290 // Multiply by the 64 bit FNV magic prime mod 2^32
291#if defined(NO_FNV_GCC_OPTIMIZATION)
292 hval *= 0x100000001b3;
293#else
294 hval += (hval << 1) + (hval << 4) + (hval << 5) +
295 (hval << 7) + (hval << 8) + (hval << 40);
296#endif
297 }
298 // Return our new hash value
299 return hval;
300}
301
314inline OdUInt64 odFNV64aHashStr(const char *str, OdUInt64 hval = 0xcbf29ce484222325ULL)
315{
316 const OdUInt8 *s = (const OdUInt8*)str; // Unsigned string
317 while (*s) // FNV-1a hash each octet in the buffer
318 {
319 // Xor the bottom with the current octet
320 hval ^= (OdUInt64)*s++;
321 // Multiply by the 64 bit FNV magic prime mod 2^32
322#if defined(NO_FNV_GCC_OPTIMIZATION)
323 hval *= 0x100000001b3;
324#else
325 hval += (hval << 1) + (hval << 4) + (hval << 5) +
326 (hval << 7) + (hval << 8) + (hval << 40);
327#endif
328 }
329 // Return our new hash value
330 return hval;
331}
332
333// C++ wrappers for hash functions
334
335template <typename Type, size_t szf = sizeof(Type)>
337template <typename Type>
338class OdFNVHashWrap<Type, 4>
339{
340 public:
341 // For buffer
342 static inline Type buf(const void *buf, size_t len) { return (Type)::odFNV32HashBuf(buf, len); };
343 static inline Type buf(const void *buf, size_t len, Type hval) { return (Type)::odFNV32HashBuf(buf, len, (OdUInt32)hval); };
344 // For strings
345 static inline Type str(const char *str) { return (Type)::odFNV32HashStr(str); };
346 static inline Type str(const char *str, Type hval) { return (Type)::odFNV32HashStr(str, (OdUInt32)hval); };
347 static inline Type str(const OdChar *str) { return buf(str, odStrLen(str) * sizeof(OdChar)); };
348 static inline Type str(const OdChar *str, Type hval) { return buf(str, odStrLen(str) * sizeof(OdChar), hval); };
349 static inline Type str(const OdString &str) { return buf(str.c_str(), str.getLength() * sizeof(OdChar)); };
350 static inline Type str(const OdString &str, Type hval) { return buf(str.c_str(), str.getLength() * sizeof(OdChar), hval); };
351 // For other types
352 template <typename DataType>
353 static inline Type type(DataType data) { return buf(&data, sizeof(DataType)); }
354 template <typename DataType>
355 static inline Type type(DataType data, Type hval) { return buf(&data, sizeof(DataType), hval); }
356 // For typed arrays
357 template <typename DataType>
358 static inline Type tArray(const DataType *pData, size_t nVals) { return buf(pData, sizeof(DataType) * nVals); }
359 template <typename DataType>
360 static inline Type tArray(const DataType *pData, size_t nVals, Type hval) { return buf(pData, sizeof(DataType) * nVals, hval); }
361 // For ODA arrays and vectors
362 template <typename ArrayType>
363 static inline Type odArray(const ArrayType &arry) { return tArray(arry.getPtr(), arry.size()); }
364 template <typename ArrayType>
365 static inline Type odArray(const ArrayType &arry, Type hval) { return tArray(arry.getPtr(), arry.size(), hval); }
366};
367template <typename Type>
368class OdFNVHashWrap<Type, 8>
369{
370 public:
371 // For buffer
372 static inline Type buf(const void *buf, size_t len) { return (Type)::odFNV64HashBuf(buf, len); };
373 static inline Type buf(const void *buf, size_t len, Type hval) { return (Type)::odFNV64HashBuf(buf, len, (OdUInt64)hval); };
374 // For strings
375 static inline Type str(const char *str) { return (Type)::odFNV64HashStr(str); };
376 static inline Type str(const char *str, Type hval) { return (Type)::odFNV64HashStr(str, (OdUInt32)hval); };
377 static inline Type str(const OdChar *str) { return buf(str, odStrLen(str) * sizeof(OdChar)); };
378 static inline Type str(const OdChar *str, Type hval) { return buf(str, odStrLen(str) * sizeof(OdChar), hval); };
379 static inline Type str(const OdString &str) { return buf(str.c_str(), str.getLength() * sizeof(OdChar)); };
380 static inline Type str(const OdString &str, Type hval) { return buf(str.c_str(), str.getLength() * sizeof(OdChar), hval); };
381 // For other types
382 template <typename DataType>
383 static inline Type type(DataType data) { return buf(&data, sizeof(DataType)); }
384 template <typename DataType>
385 static inline Type type(DataType data, Type hval) { return buf(&data, sizeof(DataType), hval); }
386 // For typed arrays
387 template <typename DataType>
388 static inline Type tArray(const DataType *pData, size_t nVals) { return buf(pData, sizeof(DataType) * nVals); }
389 template <typename DataType>
390 static inline Type tArray(const DataType *pData, size_t nVals, Type hval) { return buf(pData, sizeof(DataType) * nVals, hval); }
391 // For ODA arrays and vectors
392 template <typename ArrayType>
393 static inline Type odArray(const ArrayType &arry) { return tArray(arry.getPtr(), arry.size()); }
394 template <typename ArrayType>
395 static inline Type odArray(const ArrayType &arry, Type hval) { return tArray(arry.getPtr(), arry.size(), hval); }
396};
397
398template <typename Type, size_t szf = sizeof(Type)>
400template <typename Type>
401class OdFNVaHashWrap<Type, 4>
402{
403 public:
404 // For buffer
405 static inline Type buf(const void *buf, size_t len) { return (Type)::odFNV32aHashBuf(buf, len); };
406 static inline Type buf(const void *buf, size_t len, Type hval) { return (Type)::odFNV32aHashBuf(buf, len, (OdUInt32)hval); };
407 // For strings
408 static inline Type str(const char *str) { return (Type)::odFNV32aHashStr(str); };
409 static inline Type str(const char *str, Type hval) { return (Type)::odFNV32aHashStr(str, (OdUInt32)hval); };
410 static inline Type str(const OdChar *str) { return buf(str, odStrLen(str) * sizeof(OdChar)); };
411 static inline Type str(const OdChar *str, Type hval) { return buf(str, odStrLen(str) * sizeof(OdChar), hval); };
412 static inline Type str(const OdString &str) { return buf(str.c_str(), str.getLength() * sizeof(OdChar)); };
413 static inline Type str(const OdString &str, Type hval) { return buf(str.c_str(), str.getLength() * sizeof(OdChar), hval); };
414 // For other types
415 template <typename DataType>
416 static inline Type type(DataType data) { return buf(&data, sizeof(DataType)); }
417 template <typename DataType>
418 static inline Type type(DataType data, Type hval) { return buf(&data, sizeof(DataType), hval); }
419 // For typed arrays
420 template <typename DataType>
421 static inline Type tArray(const DataType *pData, size_t nVals) { return buf(pData, sizeof(DataType) * nVals); }
422 template <typename DataType>
423 static inline Type tArray(const DataType *pData, size_t nVals, Type hval) { return buf(pData, sizeof(DataType) * nVals, hval); }
424 // For ODA arrays and vectors
425 template <typename ArrayType>
426 static inline Type odArray(const ArrayType &arry) { return tArray(arry.getPtr(), arry.size()); }
427 template <typename ArrayType>
428 static inline Type odArray(const ArrayType &arry, Type hval) { return tArray(arry.getPtr(), arry.size(), hval); }
429};
430template <typename Type>
431class OdFNVaHashWrap<Type, 8>
432{
433 public:
434 // For buffer
435 static inline Type buf(const void *buf, size_t len) { return (Type)::odFNV64aHashBuf(buf, len); };
436 static inline Type buf(const void *buf, size_t len, Type hval) { return (Type)::odFNV64aHashBuf(buf, len, (OdUInt64)hval); };
437 // For strings
438 static inline Type str(const char *str) { return (Type)::odFNV64aHashStr(str); };
439 static inline Type str(const char *str, Type hval) { return (Type)::odFNV64aHashStr(str, (OdUInt32)hval); };
440 static inline Type str(const OdChar *str) { return buf(str, odStrLen(str) * sizeof(OdChar)); };
441 static inline Type str(const OdChar *str, Type hval) { return buf(str, odStrLen(str) * sizeof(OdChar), hval); };
442 static inline Type str(const OdString &str) { return buf(str.c_str(), str.getLength() * sizeof(OdChar)); };
443 static inline Type str(const OdString &str, Type hval) { return buf(str.c_str(), str.getLength() * sizeof(OdChar), hval); };
444 // For other types
445 template <typename DataType>
446 static inline Type type(DataType data) { return buf(&data, sizeof(DataType)); }
447 template <typename DataType>
448 static inline Type type(DataType data, Type hval) { return buf(&data, sizeof(DataType), hval); }
449 // For typed arrays
450 template <typename DataType>
451 static inline Type tArray(const DataType *pData, size_t nVals) { return buf(pData, sizeof(DataType) * nVals); }
452 template <typename DataType>
453 static inline Type tArray(const DataType *pData, size_t nVals, Type hval) { return buf(pData, sizeof(DataType) * nVals, hval); }
454 // For ODA arrays and vectors
455 template <typename ArrayType>
456 static inline Type odArray(const ArrayType &arry) { return tArray(arry.getPtr(), arry.size()); }
457 template <typename ArrayType>
458 static inline Type odArray(const ArrayType &arry, Type hval) { return tArray(arry.getPtr(), arry.size(), hval); }
459};
460
461#include "TD_PackPop.h"
462
463#endif //_OD_FNV1HASH_H_
OdUInt64 odFNV64aHashStr(const char *str, OdUInt64 hval=0xcbf29ce484222325ULL)
Definition: OdFNVHash.h:314
OdUInt32 odFNV32aHashBuf(const void *buf, size_t len, OdUInt32 hval=0x811c9dc5)
Definition: OdFNVHash.h:152
OdUInt32 odFNV32aHashStr(const char *str, OdUInt32 hval=0x811c9dc5)
Definition: OdFNVHash.h:184
OdUInt64 odFNV64HashBuf(const void *buf, size_t len, OdUInt64 hval=0xcbf29ce484222325ULL)
Definition: OdFNVHash.h:217
OdUInt64 odFNV64HashStr(const char *str, OdUInt64 hval=0xcbf29ce484222325ULL)
Definition: OdFNVHash.h:250
OdUInt64 odFNV64aHashBuf(const void *buf, size_t len, OdUInt64 hval=0xcbf29ce484222325ULL)
Definition: OdFNVHash.h:282
OdUInt32 odFNV32HashBuf(const void *buf, size_t len, OdUInt32 hval=0x811c9dc5)
Definition: OdFNVHash.h:87
OdUInt32 odFNV32HashStr(const char *str, OdUInt32 hval=0x811c9dc5)
Definition: OdFNVHash.h:120
#define odStrLen(str)
Definition: OdPlatform.h:272
unsigned int OdUInt32
unsigned char OdUInt8
wchar_t OdChar
static Type buf(const void *buf, size_t len)
Definition: OdFNVHash.h:342
static Type str(const char *str)
Definition: OdFNVHash.h:345
static Type tArray(const DataType *pData, size_t nVals)
Definition: OdFNVHash.h:358
static Type str(const OdChar *str)
Definition: OdFNVHash.h:347
static Type str(const OdString &str, Type hval)
Definition: OdFNVHash.h:350
static Type odArray(const ArrayType &arry)
Definition: OdFNVHash.h:363
static Type str(const OdString &str)
Definition: OdFNVHash.h:349
static Type str(const OdChar *str, Type hval)
Definition: OdFNVHash.h:348
static Type buf(const void *buf, size_t len, Type hval)
Definition: OdFNVHash.h:343
static Type tArray(const DataType *pData, size_t nVals, Type hval)
Definition: OdFNVHash.h:360
static Type str(const char *str, Type hval)
Definition: OdFNVHash.h:346
static Type type(DataType data)
Definition: OdFNVHash.h:353
static Type type(DataType data, Type hval)
Definition: OdFNVHash.h:355
static Type odArray(const ArrayType &arry, Type hval)
Definition: OdFNVHash.h:365
static Type tArray(const DataType *pData, size_t nVals, Type hval)
Definition: OdFNVHash.h:390
static Type str(const OdString &str, Type hval)
Definition: OdFNVHash.h:380
static Type str(const OdChar *str, Type hval)
Definition: OdFNVHash.h:378
static Type odArray(const ArrayType &arry, Type hval)
Definition: OdFNVHash.h:395
static Type str(const OdString &str)
Definition: OdFNVHash.h:379
static Type tArray(const DataType *pData, size_t nVals)
Definition: OdFNVHash.h:388
static Type str(const char *str, Type hval)
Definition: OdFNVHash.h:376
static Type str(const char *str)
Definition: OdFNVHash.h:375
static Type buf(const void *buf, size_t len, Type hval)
Definition: OdFNVHash.h:373
static Type odArray(const ArrayType &arry)
Definition: OdFNVHash.h:393
static Type buf(const void *buf, size_t len)
Definition: OdFNVHash.h:372
static Type str(const OdChar *str)
Definition: OdFNVHash.h:377
static Type type(DataType data)
Definition: OdFNVHash.h:383
static Type type(DataType data, Type hval)
Definition: OdFNVHash.h:385
static Type buf(const void *buf, size_t len)
Definition: OdFNVHash.h:405
static Type buf(const void *buf, size_t len, Type hval)
Definition: OdFNVHash.h:406
static Type type(DataType data, Type hval)
Definition: OdFNVHash.h:418
static Type type(DataType data)
Definition: OdFNVHash.h:416
static Type str(const char *str, Type hval)
Definition: OdFNVHash.h:409
static Type str(const OdString &str, Type hval)
Definition: OdFNVHash.h:413
static Type odArray(const ArrayType &arry)
Definition: OdFNVHash.h:426
static Type odArray(const ArrayType &arry, Type hval)
Definition: OdFNVHash.h:428
static Type str(const char *str)
Definition: OdFNVHash.h:408
static Type str(const OdChar *str, Type hval)
Definition: OdFNVHash.h:411
static Type str(const OdChar *str)
Definition: OdFNVHash.h:410
static Type tArray(const DataType *pData, size_t nVals, Type hval)
Definition: OdFNVHash.h:423
static Type tArray(const DataType *pData, size_t nVals)
Definition: OdFNVHash.h:421
static Type str(const OdString &str)
Definition: OdFNVHash.h:412
static Type str(const OdChar *str)
Definition: OdFNVHash.h:440
static Type str(const OdString &str)
Definition: OdFNVHash.h:442
static Type str(const OdString &str, Type hval)
Definition: OdFNVHash.h:443
static Type str(const char *str, Type hval)
Definition: OdFNVHash.h:439
static Type tArray(const DataType *pData, size_t nVals, Type hval)
Definition: OdFNVHash.h:453
static Type buf(const void *buf, size_t len, Type hval)
Definition: OdFNVHash.h:436
static Type buf(const void *buf, size_t len)
Definition: OdFNVHash.h:435
static Type tArray(const DataType *pData, size_t nVals)
Definition: OdFNVHash.h:451
static Type str(const OdChar *str, Type hval)
Definition: OdFNVHash.h:441
static Type str(const char *str)
Definition: OdFNVHash.h:438
static Type odArray(const ArrayType &arry)
Definition: OdFNVHash.h:456
static Type type(DataType data)
Definition: OdFNVHash.h:446
static Type odArray(const ArrayType &arry, Type hval)
Definition: OdFNVHash.h:458
static Type type(DataType data, Type hval)
Definition: OdFNVHash.h:448
const OdChar * c_str() const
Definition: OdString.h:203
int getLength() const
Definition: OdString.h:133
GLint GLenum GLsizei GLsizei GLint GLsizei const void * data
Definition: gles2_ext.h:110