miniassimp

view include/miniassimp/Hash.h @ 0:879c81d94345

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