From 07ebcb6e292ddb30e7116726573f06bd231e5dcd Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 19 Feb 2011 21:09:31 +0000 Subject: [PATCH] Implement CRC32 in the new Hash class. Will be useful for Dict and for the new Shader cache. --- src/Makefile.am | 2 +- src/core.h | 1 + src/hash.cpp | 58 ++++++++++++++++++++++++++++++++++ src/hash.h | 26 +++++++++++++++ win32/deushax.vcxproj | 2 ++ win32/deushax.vcxproj.filters | 6 ++++ win32/editor.vcxproj | 2 ++ win32/editor.vcxproj.filters | 6 ++++ win32/monsterz.vcxproj | 2 ++ win32/monsterz.vcxproj.filters | 6 ++++ 10 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/hash.cpp create mode 100644 src/hash.h diff --git a/src/Makefile.am b/src/Makefile.am index 9f687680..12bddfcf 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,7 +8,7 @@ liblol_a_SOURCES = \ tileset.cpp tileset.h forge.cpp forge.h video.cpp video.h \ timer.cpp timer.h bitfield.h profiler.cpp profiler.h input.h input.cpp \ world.cpp world.h sample.cpp sample.h sampler.cpp sampler.h \ - text.cpp text.h emitter.cpp emitter.h numeric.h \ + text.cpp text.h emitter.cpp emitter.h numeric.h hash.cpp hash.h \ worldentity.cpp worldentity.h shader.cpp shader.h \ \ sdlinput.cpp sdlinput.h \ diff --git a/src/core.h b/src/core.h index 4a55e436..bd6a41e6 100644 --- a/src/core.h +++ b/src/core.h @@ -39,6 +39,7 @@ #include "world.h" // Other objects +#include "hash.h" #include "dict.h" #include "map.h" #include "layer.h" diff --git a/src/hash.cpp b/src/hash.cpp new file mode 100644 index 00000000..80949b67 --- /dev/null +++ b/src/hash.cpp @@ -0,0 +1,58 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2011 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 HAVE_CONFIG_H +# include "config.h" +#endif + +#include "core.h" + +/* + * Hash implementation class + */ + +static class HashData +{ + friend class Hash; + +public: + HashData() + { + /* Initialise CRC32 table */ + for (int i = 0; i < 256; i++) + { + uint32_t tmp = i; + for (int j = 8; j--; ) + tmp = (tmp >> 1) ^ ((tmp & 1) ? 0xedb88320 : 0); + crc32_table[i] = tmp; + } + } + +private: + uint32_t crc32_table[256]; +} +hashdata; + +static HashData * const data = &hashdata; + +/* + * Public Hash class + */ + +uint32_t Hash::Crc32(char const *str) +{ + uint32_t ret = 0xffffffff, ch; + + while ((ch = (uint8_t)*str++)) + ret = data->crc32_table[(uint8_t)(ret ^ ch)] ^ (ret >> 8); + + return ret ^ 0xffffffff; +} + diff --git a/src/hash.h b/src/hash.h new file mode 100644 index 00000000..72f6128e --- /dev/null +++ b/src/hash.h @@ -0,0 +1,26 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2011 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. +// + +// +// The Hash class +// -------------- +// + +#if !defined __DH_HASH_H__ +#define __DH_HASH_H__ + +class Hash +{ +public: + static uint32_t Crc32(char const *data); +}; + +#endif // __DH_HASH_H__ + diff --git a/win32/deushax.vcxproj b/win32/deushax.vcxproj index 0e9887b9..9080c437 100644 --- a/win32/deushax.vcxproj +++ b/win32/deushax.vcxproj @@ -25,6 +25,7 @@ + @@ -59,6 +60,7 @@ + diff --git a/win32/deushax.vcxproj.filters b/win32/deushax.vcxproj.filters index 1184f37f..326f36a7 100644 --- a/win32/deushax.vcxproj.filters +++ b/win32/deushax.vcxproj.filters @@ -39,6 +39,9 @@ lolengine + + lolengine + lolengine @@ -130,6 +133,9 @@ lolengine + + lolengine + lolengine diff --git a/win32/editor.vcxproj b/win32/editor.vcxproj index 350066d2..168a20f6 100644 --- a/win32/editor.vcxproj +++ b/win32/editor.vcxproj @@ -25,6 +25,7 @@ + @@ -59,6 +60,7 @@ + diff --git a/win32/editor.vcxproj.filters b/win32/editor.vcxproj.filters index 3c59c4d8..708aa665 100644 --- a/win32/editor.vcxproj.filters +++ b/win32/editor.vcxproj.filters @@ -39,6 +39,9 @@ lolengine + + lolengine + lolengine @@ -130,6 +133,9 @@ lolengine + + lolengine + lolengine diff --git a/win32/monsterz.vcxproj b/win32/monsterz.vcxproj index 977d802b..cf25073d 100644 --- a/win32/monsterz.vcxproj +++ b/win32/monsterz.vcxproj @@ -30,6 +30,7 @@ + @@ -68,6 +69,7 @@ + diff --git a/win32/monsterz.vcxproj.filters b/win32/monsterz.vcxproj.filters index ee686325..873799ff 100644 --- a/win32/monsterz.vcxproj.filters +++ b/win32/monsterz.vcxproj.filters @@ -39,6 +39,9 @@ lolengine + + lolengine + lolengine @@ -135,6 +138,9 @@ lolengine + + lolengine + lolengine