From 397c7f2fdac494219d8c1ccf1d2ccaa009bd2301 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 21 Nov 2012 22:50:55 +0000 Subject: [PATCH] core: implement hashing functions for half, float and double. --- src/Makefile.am | 7 +-- src/core.h | 1 + src/core/hash.cpp | 104 +++++++++++++++++++++++------------- src/lol/core/array.h | 6 +-- src/lol/core/hash.h | 11 ++-- src/lol/core/string.h | 6 +-- src/lol/core/types.h | 34 ++++++++++++ src/lol/math/math.h | 6 --- src/lol/math/real.h | 5 -- src/lolcore.vcxproj | 1 + src/lolcore.vcxproj.filters | 7 ++- 11 files changed, 126 insertions(+), 62 deletions(-) create mode 100644 src/lol/core/types.h diff --git a/src/Makefile.am b/src/Makefile.am index ca75df4f..6ebef184 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -17,7 +17,7 @@ liblol_a_SOURCES = \ platform.cpp platform.h sprite.cpp sprite.h camera.cpp camera.h \ \ lol/unit.h lol/debug.h \ - lol/core/array.h lol/core/string.h lol/core/hash.h \ + lol/core/types.h lol/core/array.h lol/core/string.h lol/core/hash.h \ lol/math/vector.h lol/math/half.h lol/math/real.h lol/math/remez.h \ lol/math/math.h \ \ @@ -38,7 +38,6 @@ liblol_a_SOURCES = \ $(sdl_sources) \ $(d3d9_sources) \ $(android_sources) \ - $(bullet_sources) \ \ core/hash.cpp core/string.cpp \ \ @@ -75,7 +74,9 @@ liblol_a_SOURCES = \ \ loldebug.h \ debug/fps.cpp debug/fps.h \ - debug/record.cpp debug/record.h debug/stats.cpp debug/stats.h + debug/record.cpp debug/record.h debug/stats.cpp debug/stats.h \ + \ + $(bullet_sources) liblol_a_CPPFLAGS = @LOL_CFLAGS@ -I$(srcdir)/bullet EXTRA_DIST = easymesh/easymesh-scanner.l easymesh/easymesh-parser.y \ diff --git a/src/core.h b/src/core.h index 56117be6..8e1dc430 100644 --- a/src/core.h +++ b/src/core.h @@ -75,6 +75,7 @@ static inline int isnan(float f) // Base types #include +#include #include #include #include diff --git a/src/core/hash.cpp b/src/core/hash.cpp index 010d65b2..da382c1a 100644 --- a/src/core/hash.cpp +++ b/src/core/hash.cpp @@ -41,87 +41,117 @@ public: const data; /* - * Public Hash classes + * Helper hash functions */ -uint32_t Hash::operator ()(int8_t x) +static inline uint32_t Hash8(uint8_t x) { uint32_t ret = 0xffffffffu; ret = data.crc32_table[(uint8_t)(ret ^ x)] ^ (ret >> 8); return ret ^ 0xffffffffu; } -uint32_t Hash::operator ()(uint8_t x) +static inline uint32_t Hash16(uint16_t x) { uint32_t ret = 0xffffffffu; ret = data.crc32_table[(uint8_t)(ret ^ x)] ^ (ret >> 8); + ret = data.crc32_table[(uint8_t)(ret ^ (x >> 8))] ^ (ret >> 8); return ret ^ 0xffffffffu; } -uint32_t Hash::operator ()(int16_t x) +static inline uint32_t Hash32(uint32_t x) { uint32_t ret = 0xffffffffu; ret = data.crc32_table[(uint8_t)(ret ^ x)] ^ (ret >> 8); ret = data.crc32_table[(uint8_t)(ret ^ (x >> 8))] ^ (ret >> 8); + ret = data.crc32_table[(uint8_t)(ret ^ (x >> 16))] ^ (ret >> 8); + ret = data.crc32_table[(uint8_t)(ret ^ (x >> 24))] ^ (ret >> 8); return ret ^ 0xffffffffu; } -uint32_t Hash::operator ()(uint16_t x) +static inline uint32_t Hash64(uint64_t x) { uint32_t ret = 0xffffffffu; ret = data.crc32_table[(uint8_t)(ret ^ x)] ^ (ret >> 8); ret = data.crc32_table[(uint8_t)(ret ^ (x >> 8))] ^ (ret >> 8); + ret = data.crc32_table[(uint8_t)(ret ^ (x >> 16))] ^ (ret >> 8); + ret = data.crc32_table[(uint8_t)(ret ^ (x >> 24))] ^ (ret >> 8); + ret = data.crc32_table[(uint8_t)(ret ^ (x >> 32))] ^ (ret >> 8); + ret = data.crc32_table[(uint8_t)(ret ^ (x >> 40))] ^ (ret >> 8); + ret = data.crc32_table[(uint8_t)(ret ^ (x >> 48))] ^ (ret >> 8); + ret = data.crc32_table[(uint8_t)(ret ^ (x >> 56))] ^ (ret >> 8); return ret ^ 0xffffffffu; } +/* + * Integer hash functions + */ + +uint32_t Hash::operator ()(int8_t x) +{ + return Hash8((uint8_t)x); +} + +uint32_t Hash::operator ()(uint8_t x) +{ + return Hash8(x); +} + +uint32_t Hash::operator ()(int16_t x) +{ + return Hash16((uint16_t)x); +} + +uint32_t Hash::operator ()(uint16_t x) +{ + return Hash16(x); +} + uint32_t Hash::operator ()(int32_t x) { - uint32_t ret = 0xffffffffu; - ret = data.crc32_table[(uint8_t)(ret ^ x)] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 8))] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 16))] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 24))] ^ (ret >> 8); - return ret ^ 0xffffffffu; + return Hash32((uint32_t)x); } uint32_t Hash::operator ()(uint32_t x) { - uint32_t ret = 0xffffffffu; - ret = data.crc32_table[(uint8_t)(ret ^ x)] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 8))] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 16))] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 24))] ^ (ret >> 8); - return ret ^ 0xffffffffu; + return Hash32(x); } uint32_t Hash::operator ()(int64_t x) { - uint32_t ret = 0xffffffffu; - ret = data.crc32_table[(uint8_t)(ret ^ x)] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 8))] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 16))] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 24))] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 32))] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 40))] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 48))] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 56))] ^ (ret >> 8); - return ret ^ 0xffffffffu; + return Hash64((uint64_t)x); } uint32_t Hash::operator ()(uint64_t x) { - uint32_t ret = 0xffffffffu; - ret = data.crc32_table[(uint8_t)(ret ^ x)] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 8))] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 16))] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 24))] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 32))] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 40))] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 48))] ^ (ret >> 8); - ret = data.crc32_table[(uint8_t)(ret ^ (x >> 56))] ^ (ret >> 8); - return ret ^ 0xffffffffu; + return Hash64(x); +} + +/* + * Floating-point hash functions + */ + +uint32_t Hash::operator ()(half f) +{ + return Hash16(f.bits); } +uint32_t Hash::operator ()(float f) +{ + union { float tmp; uint32_t x; } u = { f }; + return Hash32(u.x); +} + +uint32_t Hash::operator ()(double f) +{ + union { double tmp; uint64_t x; } u = { f }; + return Hash64(u.x); +} + +/* + * String and array hash functions + */ + static uint32_t HashCharString(char const *s) { uint32_t ret = 0xffffffffu, ch; diff --git a/src/lol/core/array.h b/src/lol/core/array.h index 64f29512..fc4440ab 100644 --- a/src/lol/core/array.h +++ b/src/lol/core/array.h @@ -15,8 +15,8 @@ // additional features, eg. Array for automatic arrays of structs. // -#if !defined __LOL_ARRAY_H__ -#define __LOL_ARRAY_H__ +#if !defined __LOL_CORE_ARRAY_H__ +#define __LOL_CORE_ARRAY_H__ #include #include @@ -507,5 +507,5 @@ class Array } /* namespace lol */ -#endif // __LOL_ARRAY_H__ +#endif // __LOL_CORE_ARRAY_H__ diff --git a/src/lol/core/hash.h b/src/lol/core/hash.h index fc5634cd..f25ea3a2 100644 --- a/src/lol/core/hash.h +++ b/src/lol/core/hash.h @@ -14,8 +14,8 @@ // A very simple Hash class. // -#if !defined __LOL_HASH_H__ -#define __LOL_HASH_H__ +#if !defined __LOL_CORE_HASH_H__ +#define __LOL_CORE_HASH_H__ namespace lol { @@ -31,6 +31,11 @@ template<> class Hash { public: uint32_t operator()(uint32_t); }; template<> class Hash { public: uint32_t operator()(int64_t); }; template<> class Hash { public: uint32_t operator()(uint64_t); }; +template<> class Hash { public: uint32_t operator()(half); }; +template<> class Hash { public: uint32_t operator()(float); }; +template<> class Hash { public: uint32_t operator()(double); }; +template<> class Hash { public: uint32_t operator()(ldouble); }; + template<> class Hash { public: @@ -45,5 +50,5 @@ public: } /* namespace lol */ -#endif // __LOL_HASH_H__ +#endif // __LOL_CORE_HASH_H__ diff --git a/src/lol/core/string.h b/src/lol/core/string.h index 098fc2aa..e195f30d 100644 --- a/src/lol/core/string.h +++ b/src/lol/core/string.h @@ -14,8 +14,8 @@ // A very simple String class, based on Array. // -#if !defined __LOL_STRING_H__ -#define __LOL_STRING_H__ +#if !defined __LOL_CORE_STRING_H__ +#define __LOL_CORE_STRING_H__ #include @@ -119,5 +119,5 @@ public: } /* namespace lol */ -#endif // __LOL_STRING_H__ +#endif // __LOL_CORE_STRING_H__ diff --git a/src/lol/core/types.h b/src/lol/core/types.h new file mode 100644 index 00000000..29f92b3a --- /dev/null +++ b/src/lol/core/types.h @@ -0,0 +1,34 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2012 Sam Hocevar +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://sam.zoy.org/projects/COPYING.WTFPL for more details. +// + +#if !defined __LOL_CORE_TYPES_H__ +#define __LOL_CORE_TYPES_H__ + +namespace lol +{ + +/* There are many reasons for wanting single-word type names, the most + * important one being compilation speedups in our vector.h: we can hide + * some global methods in namespaces that contain the name of the type, + * but namespaces cannot have spaces in their names. */ +typedef long double ldouble; + +/* The “real” type used for real numbers. It’s a specialisation of the + * “Real” template class. */ +template class Real; +typedef Real<16> real; + +/* The “half” type used for 16-bit floating point numbers. */ +class half; + +} /* namespace lol */ + +#endif // __LOL_CORE_TYPES_H__ + diff --git a/src/lol/math/math.h b/src/lol/math/math.h index d1affdc7..bba6f5b3 100644 --- a/src/lol/math/math.h +++ b/src/lol/math/math.h @@ -29,12 +29,6 @@ namespace lol #undef min #undef max -/* There are many reasons for wanting single-word type names, the most - * important one being compilation speedups in our vector.h: we can hide - * some global methods in namespaces that contain the name of the type, - * but namespaces cannot have spaces in their names. */ -typedef long double ldouble; - /* Standard cmath functions */ static inline double sqrt(double const &x) { return std::sqrt(x); } static inline float sqrt(float const &x) { return std::sqrt(x); } diff --git a/src/lol/math/real.h b/src/lol/math/real.h index 7f527d55..f7b3b56e 100644 --- a/src/lol/math/real.h +++ b/src/lol/math/real.h @@ -198,11 +198,6 @@ private: uint32_t m_signexp; }; -/* - * The real type used for real numbers - */ -typedef Real<16> real; - /* * Mandatory forward declarations of template specialisations */ diff --git a/src/lolcore.vcxproj b/src/lolcore.vcxproj index e2d2fe6f..f51e4c6c 100644 --- a/src/lolcore.vcxproj +++ b/src/lolcore.vcxproj @@ -585,6 +585,7 @@ + diff --git a/src/lolcore.vcxproj.filters b/src/lolcore.vcxproj.filters index a6bfa00d..ea0efc5f 100644 --- a/src/lolcore.vcxproj.filters +++ b/src/lolcore.vcxproj.filters @@ -733,10 +733,13 @@ src\platform\d3d9 - src\... + src\core + + + src\core - src\... + src\core src\...