2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
2014-12-25 02:50:24 +01:00
|
|
|
* limitations under the License.
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef __UTIL_HASH_H__
|
|
|
|
|
#define __UTIL_HASH_H__
|
|
|
|
|
|
Cycles: Make all #include statements relative to cycles source directory
The idea is to make include statements more explicit and obvious where the
file is coming from, additionally reducing chance of wrong header being
picked up.
For example, it was not obvious whether bvh.h was refferring to builder
or traversal, whenter node.h is a generic graph node or a shader node
and cases like that.
Surely this might look obvious for the active developers, but after some
time of not touching the code it becomes less obvious where file is coming
from.
This was briefly mentioned in T50824 and seems @brecht is fine with such
explicitness, but need to agree with all active developers before committing
this.
Please note that this patch is lacking changes related on GPU/OpenCL
support. This will be solved if/when we all agree this is a good idea to move
forward.
Reviewers: brecht, lukasstockner97, maiself, nirved, dingto, juicyfruit, swerner
Reviewed By: lukasstockner97, maiself, nirved, dingto
Subscribers: brecht
Differential Revision: https://developer.blender.org/D2586
2017-03-28 20:39:14 +02:00
|
|
|
#include "util/util_types.h"
|
2012-05-21 12:52:28 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
2019-08-21 20:04:09 +02:00
|
|
|
/* ***** Jenkins Lookup3 Hash Functions ***** */
|
|
|
|
|
|
|
|
|
|
/* Source: http://burtleburtle.net/bob/c/lookup3.c */
|
|
|
|
|
|
2014-05-05 02:19:08 +10:00
|
|
|
#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-08-21 20:04:09 +02:00
|
|
|
#define mix(a, b, c) \
|
|
|
|
|
{ \
|
|
|
|
|
a -= c; \
|
|
|
|
|
a ^= rot(c, 4); \
|
|
|
|
|
c += b; \
|
|
|
|
|
b -= a; \
|
|
|
|
|
b ^= rot(a, 6); \
|
|
|
|
|
a += c; \
|
|
|
|
|
c -= b; \
|
|
|
|
|
c ^= rot(b, 8); \
|
|
|
|
|
b += a; \
|
|
|
|
|
a -= c; \
|
|
|
|
|
a ^= rot(c, 16); \
|
|
|
|
|
c += b; \
|
|
|
|
|
b -= a; \
|
|
|
|
|
b ^= rot(a, 19); \
|
|
|
|
|
a += c; \
|
|
|
|
|
c -= b; \
|
|
|
|
|
c ^= rot(b, 4); \
|
|
|
|
|
b += a; \
|
2019-09-21 11:34:39 +10:00
|
|
|
} \
|
|
|
|
|
((void)0)
|
2019-08-21 20:04:09 +02:00
|
|
|
|
|
|
|
|
#define final(a, b, c) \
|
|
|
|
|
{ \
|
|
|
|
|
c ^= b; \
|
|
|
|
|
c -= rot(b, 14); \
|
|
|
|
|
a ^= c; \
|
|
|
|
|
a -= rot(c, 11); \
|
|
|
|
|
b ^= a; \
|
|
|
|
|
b -= rot(a, 25); \
|
|
|
|
|
c ^= b; \
|
|
|
|
|
c -= rot(b, 16); \
|
|
|
|
|
a ^= c; \
|
|
|
|
|
a -= rot(c, 4); \
|
|
|
|
|
b ^= a; \
|
|
|
|
|
b -= rot(a, 14); \
|
|
|
|
|
c ^= b; \
|
|
|
|
|
c -= rot(b, 24); \
|
2019-09-21 11:34:39 +10:00
|
|
|
} \
|
|
|
|
|
((void)0)
|
2019-08-21 20:04:09 +02:00
|
|
|
|
|
|
|
|
ccl_device_inline uint hash_uint(uint kx)
|
|
|
|
|
{
|
2012-05-21 12:52:28 +00:00
|
|
|
uint a, b, c;
|
2019-08-21 20:04:09 +02:00
|
|
|
a = b = c = 0xdeadbeef + (1 << 2) + 13;
|
|
|
|
|
|
|
|
|
|
a += kx;
|
|
|
|
|
final(a, b, c);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-08-21 20:04:09 +02:00
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline uint hash_uint2(uint kx, uint ky)
|
|
|
|
|
{
|
|
|
|
|
uint a, b, c;
|
2011-04-27 11:58:34 +00:00
|
|
|
a = b = c = 0xdeadbeef + (2 << 2) + 13;
|
2019-08-21 20:04:09 +02:00
|
|
|
|
|
|
|
|
b += ky;
|
|
|
|
|
a += kx;
|
|
|
|
|
final(a, b, c);
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline uint hash_uint3(uint kx, uint ky, uint kz)
|
|
|
|
|
{
|
|
|
|
|
uint a, b, c;
|
|
|
|
|
a = b = c = 0xdeadbeef + (3 << 2) + 13;
|
|
|
|
|
|
|
|
|
|
c += kz;
|
|
|
|
|
b += ky;
|
|
|
|
|
a += kx;
|
|
|
|
|
final(a, b, c);
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline uint hash_uint4(uint kx, uint ky, uint kz, uint kw)
|
|
|
|
|
{
|
|
|
|
|
uint a, b, c;
|
|
|
|
|
a = b = c = 0xdeadbeef + (4 << 2) + 13;
|
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
a += kx;
|
|
|
|
|
b += ky;
|
2019-08-21 20:04:09 +02:00
|
|
|
c += kz;
|
|
|
|
|
mix(a, b, c);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-08-21 20:04:09 +02:00
|
|
|
a += kw;
|
|
|
|
|
final(a, b, c);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-09 18:56:12 +00:00
|
|
|
return c;
|
2019-08-21 20:04:09 +02:00
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2014-05-05 02:19:08 +10:00
|
|
|
#undef rot
|
2019-08-21 20:04:09 +02:00
|
|
|
#undef final
|
|
|
|
|
#undef mix
|
|
|
|
|
|
|
|
|
|
/* Hashing uint or uint[234] into a float in the range [0, 1]. */
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float hash_uint_to_float(uint kx)
|
|
|
|
|
{
|
|
|
|
|
return (float)hash_uint(kx) / (float)0xFFFFFFFFu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float hash_uint2_to_float(uint kx, uint ky)
|
|
|
|
|
{
|
|
|
|
|
return (float)hash_uint2(kx, ky) / (float)0xFFFFFFFFu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float hash_uint3_to_float(uint kx, uint ky, uint kz)
|
|
|
|
|
{
|
|
|
|
|
return (float)hash_uint3(kx, ky, kz) / (float)0xFFFFFFFFu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float hash_uint4_to_float(uint kx, uint ky, uint kz, uint kw)
|
|
|
|
|
{
|
|
|
|
|
return (float)hash_uint4(kx, ky, kz, kw) / (float)0xFFFFFFFFu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Hashing float or float[234] into a float in the range [0, 1]. */
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float hash_float_to_float(float k)
|
|
|
|
|
{
|
|
|
|
|
return hash_uint_to_float(__float_as_uint(k));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float hash_float2_to_float(float2 k)
|
|
|
|
|
{
|
|
|
|
|
return hash_uint2_to_float(__float_as_uint(k.x), __float_as_uint(k.y));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float hash_float3_to_float(float3 k)
|
|
|
|
|
{
|
|
|
|
|
return hash_uint3_to_float(__float_as_uint(k.x), __float_as_uint(k.y), __float_as_uint(k.z));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float hash_float4_to_float(float4 k)
|
|
|
|
|
{
|
|
|
|
|
return hash_uint4_to_float(
|
|
|
|
|
__float_as_uint(k.x), __float_as_uint(k.y), __float_as_uint(k.z), __float_as_uint(k.w));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Hashing float[234] into float[234] of components in the range [0, 1]. */
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float2 hash_float2_to_float2(float2 k)
|
|
|
|
|
{
|
|
|
|
|
return make_float2(hash_float2_to_float(k), hash_float3_to_float(make_float3(k.x, k.y, 1.0)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float3 hash_float3_to_float3(float3 k)
|
|
|
|
|
{
|
|
|
|
|
return make_float3(hash_float3_to_float(k),
|
|
|
|
|
hash_float4_to_float(make_float4(k.x, k.y, k.z, 1.0)),
|
|
|
|
|
hash_float4_to_float(make_float4(k.x, k.y, k.z, 2.0)));
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-21 20:04:09 +02:00
|
|
|
ccl_device_inline float4 hash_float4_to_float4(float4 k)
|
|
|
|
|
{
|
|
|
|
|
return make_float4(hash_float4_to_float(k),
|
|
|
|
|
hash_float4_to_float(make_float4(k.w, k.x, k.y, k.z)),
|
|
|
|
|
hash_float4_to_float(make_float4(k.z, k.w, k.x, k.y)),
|
|
|
|
|
hash_float4_to_float(make_float4(k.y, k.z, k.w, k.x)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Hashing float or float[234] into float3 of components in range [0, 1]. */
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float3 hash_float_to_float3(float k)
|
2011-10-29 14:27:24 +00:00
|
|
|
{
|
2019-08-21 20:04:09 +02:00
|
|
|
return make_float3(hash_float_to_float(k),
|
|
|
|
|
hash_float2_to_float(make_float2(k, 1.0)),
|
|
|
|
|
hash_float2_to_float(make_float2(k, 2.0)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float3 hash_float2_to_float3(float2 k)
|
|
|
|
|
{
|
|
|
|
|
return make_float3(hash_float2_to_float(k),
|
|
|
|
|
hash_float3_to_float(make_float3(k.x, k.y, 1.0)),
|
|
|
|
|
hash_float3_to_float(make_float3(k.x, k.y, 2.0)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline float3 hash_float4_to_float3(float4 k)
|
|
|
|
|
{
|
|
|
|
|
return make_float3(hash_float4_to_float(k),
|
|
|
|
|
hash_float4_to_float(make_float4(k.z, k.x, k.w, k.y)),
|
|
|
|
|
hash_float4_to_float(make_float4(k.w, k.z, k.y, k.x)));
|
2011-10-29 14:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
2019-09-04 17:54:32 +02:00
|
|
|
/* SSE Versions Of Jenkins Lookup3 Hash Functions */
|
|
|
|
|
|
|
|
|
|
#ifdef __KERNEL_SSE2__
|
|
|
|
|
# define rot(x, k) (((x) << (k)) | (srl(x, 32 - (k))))
|
|
|
|
|
|
|
|
|
|
# define mix(a, b, c) \
|
|
|
|
|
{ \
|
|
|
|
|
a -= c; \
|
|
|
|
|
a ^= rot(c, 4); \
|
|
|
|
|
c += b; \
|
|
|
|
|
b -= a; \
|
|
|
|
|
b ^= rot(a, 6); \
|
|
|
|
|
a += c; \
|
|
|
|
|
c -= b; \
|
|
|
|
|
c ^= rot(b, 8); \
|
|
|
|
|
b += a; \
|
|
|
|
|
a -= c; \
|
|
|
|
|
a ^= rot(c, 16); \
|
|
|
|
|
c += b; \
|
|
|
|
|
b -= a; \
|
|
|
|
|
b ^= rot(a, 19); \
|
|
|
|
|
a += c; \
|
|
|
|
|
c -= b; \
|
|
|
|
|
c ^= rot(b, 4); \
|
|
|
|
|
b += a; \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# define final(a, b, c) \
|
|
|
|
|
{ \
|
|
|
|
|
c ^= b; \
|
|
|
|
|
c -= rot(b, 14); \
|
|
|
|
|
a ^= c; \
|
|
|
|
|
a -= rot(c, 11); \
|
|
|
|
|
b ^= a; \
|
|
|
|
|
b -= rot(a, 25); \
|
|
|
|
|
c ^= b; \
|
|
|
|
|
c -= rot(b, 16); \
|
|
|
|
|
a ^= c; \
|
|
|
|
|
a -= rot(c, 4); \
|
|
|
|
|
b ^= a; \
|
|
|
|
|
b -= rot(a, 14); \
|
|
|
|
|
c ^= b; \
|
|
|
|
|
c -= rot(b, 24); \
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline ssei hash_ssei(ssei kx)
|
|
|
|
|
{
|
|
|
|
|
ssei a, b, c;
|
|
|
|
|
a = b = c = ssei(0xdeadbeef + (1 << 2) + 13);
|
|
|
|
|
|
|
|
|
|
a += kx;
|
|
|
|
|
final(a, b, c);
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline ssei hash_ssei2(ssei kx, ssei ky)
|
|
|
|
|
{
|
|
|
|
|
ssei a, b, c;
|
|
|
|
|
a = b = c = ssei(0xdeadbeef + (2 << 2) + 13);
|
|
|
|
|
|
|
|
|
|
b += ky;
|
|
|
|
|
a += kx;
|
|
|
|
|
final(a, b, c);
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline ssei hash_ssei3(ssei kx, ssei ky, ssei kz)
|
|
|
|
|
{
|
|
|
|
|
ssei a, b, c;
|
|
|
|
|
a = b = c = ssei(0xdeadbeef + (3 << 2) + 13);
|
|
|
|
|
|
|
|
|
|
c += kz;
|
|
|
|
|
b += ky;
|
|
|
|
|
a += kx;
|
|
|
|
|
final(a, b, c);
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline ssei hash_ssei4(ssei kx, ssei ky, ssei kz, ssei kw)
|
|
|
|
|
{
|
|
|
|
|
ssei a, b, c;
|
|
|
|
|
a = b = c = ssei(0xdeadbeef + (4 << 2) + 13);
|
|
|
|
|
|
|
|
|
|
a += kx;
|
|
|
|
|
b += ky;
|
|
|
|
|
c += kz;
|
|
|
|
|
mix(a, b, c);
|
|
|
|
|
|
|
|
|
|
a += kw;
|
|
|
|
|
final(a, b, c);
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 14:48:01 +02:00
|
|
|
# if defined(__KERNEL_AVX__)
|
|
|
|
|
ccl_device_inline avxi hash_avxi(avxi kx)
|
|
|
|
|
{
|
|
|
|
|
avxi a, b, c;
|
|
|
|
|
a = b = c = avxi(0xdeadbeef + (1 << 2) + 13);
|
|
|
|
|
|
|
|
|
|
a += kx;
|
|
|
|
|
final(a, b, c);
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline avxi hash_avxi2(avxi kx, avxi ky)
|
|
|
|
|
{
|
|
|
|
|
avxi a, b, c;
|
|
|
|
|
a = b = c = avxi(0xdeadbeef + (2 << 2) + 13);
|
|
|
|
|
|
|
|
|
|
b += ky;
|
|
|
|
|
a += kx;
|
|
|
|
|
final(a, b, c);
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline avxi hash_avxi3(avxi kx, avxi ky, avxi kz)
|
|
|
|
|
{
|
|
|
|
|
avxi a, b, c;
|
|
|
|
|
a = b = c = avxi(0xdeadbeef + (3 << 2) + 13);
|
|
|
|
|
|
|
|
|
|
c += kz;
|
|
|
|
|
b += ky;
|
|
|
|
|
a += kx;
|
|
|
|
|
final(a, b, c);
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ccl_device_inline avxi hash_avxi4(avxi kx, avxi ky, avxi kz, avxi kw)
|
|
|
|
|
{
|
|
|
|
|
avxi a, b, c;
|
|
|
|
|
a = b = c = avxi(0xdeadbeef + (4 << 2) + 13);
|
|
|
|
|
|
|
|
|
|
a += kx;
|
|
|
|
|
b += ky;
|
|
|
|
|
c += kz;
|
|
|
|
|
mix(a, b, c);
|
|
|
|
|
|
|
|
|
|
a += kw;
|
|
|
|
|
final(a, b, c);
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
# endif
|
|
|
|
|
|
2019-09-04 17:54:32 +02:00
|
|
|
# undef rot
|
|
|
|
|
# undef final
|
|
|
|
|
# undef mix
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-10-30 00:56:39 +02:00
|
|
|
#ifndef __KERNEL_GPU__
|
2012-05-21 12:52:28 +00:00
|
|
|
static inline uint hash_string(const char *str)
|
|
|
|
|
{
|
|
|
|
|
uint i = 0, c;
|
|
|
|
|
|
2015-03-28 00:15:15 +05:00
|
|
|
while ((c = *str++))
|
2012-05-21 12:52:28 +00:00
|
|
|
i = i * 37 + c;
|
|
|
|
|
|
|
|
|
|
return i;
|
|
|
|
|
}
|
2016-10-30 00:56:39 +02:00
|
|
|
#endif
|
2012-05-21 12:52:28 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
|
|
#endif /* __UTIL_HASH_H__ */
|