1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2016, assimp team
6 All rights reserved.
7 
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
11 
12 * Redistributions of source code must retain the above
13   copyright notice, this list of conditions and the
14   following disclaimer.
15 
16 * Redistributions in binary form must reproduce the above
17   copyright notice, this list of conditions and the
18   following disclaimer in the documentation and/or other
19   materials provided with the distribution.
20 
21 * Neither the name of the assimp team, nor the names of its
22   contributors may be used to endorse or promote products
23   derived from this software without specific prior
24   written permission of the assimp team.
25 
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 
38 ----------------------------------------------------------------------
39 */
40 
41 #ifndef AI_HASH_H_INCLUDED
42 #define AI_HASH_H_INCLUDED
43 
44 #include <stdint.h>
45 #include <string.h>
46 
47 // ------------------------------------------------------------------------------------------------
48 // Hashing function taken from
49 // http://www.azillionmonkeys.com/qed/hash.html
50 // (incremental version)
51 //
52 // This code is Copyright 2004-2008 by Paul Hsieh. It is used here in the belief that
53 // Assimp's license is considered compatible with Pauls's derivative license as specified
54 // on his web page.
55 //
56 // (stdint.h should have been been included here)
57 // ------------------------------------------------------------------------------------------------
58 #undef get16bits
59 #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \
60   || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__)
61 #define get16bits(d) (*((const uint16_t *) (d)))
62 #endif
63 
64 #if !defined (get16bits)
65 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
66                        +(uint32_t)(((const uint8_t *)(d))[0]) )
67 #endif
68 
69 // ------------------------------------------------------------------------------------------------
70 inline uint32_t SuperFastHash (const char * data, uint32_t len = 0, uint32_t hash = 0) {
71 uint32_t tmp;
72 int rem;
73 
74     if (!data) return 0;
75     if (!len)len = (uint32_t)::strlen(data);
76 
77     rem = len & 3;
78     len >>= 2;
79 
80     /* Main loop */
81     for (;len > 0; len--) {
82         hash  += get16bits (data);
83         tmp    = (get16bits (data+2) << 11) ^ hash;
84         hash   = (hash << 16) ^ tmp;
85         data  += 2*sizeof (uint16_t);
86         hash  += hash >> 11;
87     }
88 
89     /* Handle end cases */
90     switch (rem) {
91         case 3: hash += get16bits (data);
92                 hash ^= hash << 16;
93                 hash ^= data[sizeof (uint16_t)] << 18;
94                 hash += hash >> 11;
95                 break;
96         case 2: hash += get16bits (data);
97                 hash ^= hash << 11;
98                 hash += hash >> 17;
99                 break;
100         case 1: hash += *data;
101                 hash ^= hash << 10;
102                 hash += hash >> 1;
103     }
104 
105     /* Force "avalanching" of final 127 bits */
106     hash ^= hash << 3;
107     hash += hash >> 5;
108     hash ^= hash << 4;
109     hash += hash >> 17;
110     hash ^= hash << 25;
111     hash += hash >> 6;
112 
113     return hash;
114 }
115 
116 #endif // !! AI_HASH_H_INCLUDED
117