NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Hash.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "base/Hash.hpp"
29 
30 using namespace FW;
31 
32 //------------------------------------------------------------------------
33 
34 U32 FW::hashBuffer(const void* ptr, int size)
35 {
36  FW_ASSERT(size >= 0);
37  FW_ASSERT(ptr || !size);
38 
39  if ((((S32)(UPTR)ptr | size) & 3) == 0)
40  return hashBufferAlign(ptr, size);
41 
42  const U8* src = (const U8*)ptr;
43  U32 a = FW_HASH_MAGIC;
44  U32 b = FW_HASH_MAGIC;
45  U32 c = FW_HASH_MAGIC;
46 
47  while (size >= 12)
48  {
49  a += src[0] + (src[1] << 8) + (src[2] << 16) + (src[3] << 24);
50  b += src[4] + (src[5] << 8) + (src[6] << 16) + (src[7] << 24);
51  c += src[8] + (src[9] << 8) + (src[10] << 16) + (src[11] << 24);
52  FW_JENKINS_MIX(a, b, c);
53  src += 12;
54  size -= 12;
55  }
56 
57  switch (size)
58  {
59  case 11: c += src[10] << 16;
60  case 10: c += src[9] << 8;
61  case 9: c += src[8];
62  case 8: b += src[7] << 24;
63  case 7: b += src[6] << 16;
64  case 6: b += src[5] << 8;
65  case 5: b += src[4];
66  case 4: a += src[3] << 24;
67  case 3: a += src[2] << 16;
68  case 2: a += src[1] << 8;
69  case 1: a += src[0];
70  case 0: break;
71  }
72 
73  c += size;
74  FW_JENKINS_MIX(a, b, c);
75  return c;
76 }
77 
78 //------------------------------------------------------------------------
79 
80 U32 FW::hashBufferAlign(const void* ptr, int size)
81 {
82  FW_ASSERT(size >= 0);
83  FW_ASSERT(ptr || !size);
84  FW_ASSERT(((UPTR)ptr & 3) == 0);
85  FW_ASSERT((size & 3) == 0);
86 
87  const U32* src = (const U32*)ptr;
88  U32 a = FW_HASH_MAGIC;
89  U32 b = FW_HASH_MAGIC;
90  U32 c = FW_HASH_MAGIC;
91 
92  while (size >= 12)
93  {
94  a += src[0];
95  b += src[1];
96  c += src[2];
97  FW_JENKINS_MIX(a, b, c);
98  src += 3;
99  size -= 12;
100  }
101 
102  switch (size)
103  {
104  case 8: b += src[1];
105  case 4: a += src[0];
106  case 0: break;
107  }
108 
109  c += size;
110  FW_JENKINS_MIX(a, b, c);
111  return c;
112 }
113 
114 //------------------------------------------------------------------------
U32 hashBuffer(const void *ptr, int size)
Definition: Hash.cpp:34
__w64 U32 UPTR
Definition: Defs.hpp:106
void ** ptr
Definition: DLLImports.cpp:74
U32 hashBufferAlign(const void *ptr, int size)
Definition: Hash.cpp:80
#define FW_HASH_MAGIC
Definition: Hash.hpp:169
#define FW_ASSERT(X)
Definition: Defs.hpp:67
signed int S32
Definition: Defs.hpp:88
unsigned int U32
Definition: Defs.hpp:85
unsigned char U8
Definition: Defs.hpp:83
#define FW_JENKINS_MIX(a, b, c)
Definition: Hash.hpp:172
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr size
Definition: DLLImports.inl:319