1 /*****************************************************************
5 | Copyright (c) 2002-2010, Axiomatic Systems, LLC.
8 | Redistribution and use in source and binary forms, with or without
9 | modification, are permitted provided that the following conditions are met:
10 | * Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 | * Redistributions in binary form must reproduce the above copyright
13 | notice, this list of conditions and the following disclaimer in the
14 | documentation and/or other materials provided with the distribution.
15 | * Neither the name of Axiomatic Systems nor the
16 | names of its contributors may be used to endorse or promote products
17 | derived from this software without specific prior written permission.
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 ****************************************************************/
32 /*----------------------------------------------------------------------
34 +---------------------------------------------------------------------*/
36 #include "NptResults.h"
39 /*----------------------------------------------------------------------
41 +---------------------------------------------------------------------*/
42 // 32 bit magic FNV-1a prime
43 const NPT_UInt32 NPT_FNV_32_PRIME
= 0x01000193;
45 /*----------------------------------------------------------------------
47 +---------------------------------------------------------------------*/
49 NPT_Fnv1aHash32(const NPT_UInt8
* data
, NPT_Size data_size
, NPT_UInt32 hash_init
)
51 const NPT_UInt8
* data_end
= data
+ data_size
;
52 NPT_UInt32 hash_value
= hash_init
;
54 while (data
< data_end
) {
55 hash_value
^= (NPT_UInt32
)*data
++;
57 #if defined(NPT_CONFIG_FNV_HASH_USE_SHIFT_MUL)
58 hash_value
+= (hash_value
<<1) + (hash_value
<<4) + (hash_value
<<7) + (hash_value
<<8) + (hash_value
<<24);
60 hash_value
*= NPT_FNV_32_PRIME
;
68 /*----------------------------------------------------------------------
70 +---------------------------------------------------------------------*/
72 NPT_Fnv1aHashStr32(const char* data
, NPT_UInt32 hash_init
)
74 NPT_UInt32 hash_value
= hash_init
;
77 hash_value
^= (NPT_UInt32
)*data
++;
79 #if defined(NPT_CONFIG_FNV_HASH_USE_SHIFT_MUL)
80 hash_value
+= (hash_value
<<1) + (hash_value
<<4) + (hash_value
<<7) + (hash_value
<<8) + (hash_value
<<24);
82 hash_value
*= NPT_FNV_32_PRIME
;
89 /*----------------------------------------------------------------------
91 +---------------------------------------------------------------------*/
92 // 64 bit magic FNV-1a prime
93 const NPT_UInt64 NPT_FNV_64_PRIME
= 0x100000001b3ULL
;
95 /*----------------------------------------------------------------------
97 +---------------------------------------------------------------------*/
99 NPT_Fnv1aHash64(const NPT_UInt8
* data
, NPT_Size data_size
, NPT_UInt64 hash_init
)
101 const NPT_UInt8
* data_end
= data
+ data_size
;
102 NPT_UInt64 hash_value
= hash_init
;
104 while (data
< data_end
) {
105 hash_value
^= (NPT_UInt64
)*data
++;
107 #if defined(NPT_CONFIG_FNV_HASH_USE_SHIFT_MUL)
108 hash_value
+= (hash_value
<< 1) + (hash_value
<< 4) + (hash_value
<< 5) + (hash_value
<< 7) + (hash_value
<< 8) + (hash_value
<< 40);
110 hash_value
*= NPT_FNV_64_PRIME
;
118 /*----------------------------------------------------------------------
120 +---------------------------------------------------------------------*/
122 NPT_Fnv1aHashStr64(const char* data
, NPT_UInt64 hash_init
)
124 NPT_UInt64 hash_value
= hash_init
;
127 hash_value
^= (NPT_UInt64
)*data
++;
129 #if defined(NPT_CONFIG_FNV_HASH_USE_SHIFT_MUL)
130 hash_value
+= (hash_value
<< 1) + (hash_value
<< 4) + (hash_value
<< 5) + (hash_value
<< 7) + (hash_value
<< 8) + (hash_value
<< 40);
132 hash_value
*= NPT_FNV_64_PRIME
;