Skip to content

Commit 45bccd5

Browse files
committed
Update crc32
Lets not use CRC32C when we have been using CRC32B forever. The default Garry's Mod CRC32 also uses CRC32 IEEE reflected. On Win32 if the system supports it there will be AVX512 for CRC32. This is faster than hardware CRC32C. The new CRC32 is a lot faster than the old version. Added support for Matrix even though I don't use it. Premake will now auto include the correct Garry's Mod headers so its an easy compile.
1 parent 0f63274 commit 45bccd5

9 files changed

Lines changed: 657 additions & 74 deletions

File tree

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[submodule "gmod-module-base"]
2+
path = gmod-module-base
3+
url = https://github.com/Facepunch/gmod-module-base.git
4+
branch = development

BuildProjects.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
premake5 --os=windows vs2017
1+
premake5 --os=windows vs2022
22
premake5 --os=macosx gmake
33
premake5 --os=linux gmake
44
pause

BuildProjects.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
premake5 --os=windows vs2022
2+
premake5 --os=macosx gmake
3+
premake5 --os=linux gmake

gmod-module-base

Submodule gmod-module-base added at e0156e5

premake5.exe

1.26 MB
Binary file not shown.

premake5.lua

Lines changed: 80 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,83 @@
11
workspace "gmsv_binarytable"
2-
configurations { "Debug", "Release" }
3-
location ( "projects/" .. os.get() )
4-
5-
project "gmsv_binarytable"
6-
kind "SharedLib"
7-
architecture "x86"
8-
language "C++"
9-
includedirs "../include/"
10-
targetdir "build"
11-
symbols "On"
12-
13-
if os.is( "windows" ) then targetsuffix "_win32" end
14-
if os.is( "macosx" ) then targetsuffix "_osx" end
15-
if os.is( "linux" ) then targetsuffix "_linux" end
16-
17-
configuration "Debug"
18-
optimize "Debug"
19-
20-
configuration "Release"
2+
configurations { "Release" }
3+
location ("projects/" .. os.target())
4+
5+
-- ========================
6+
-- 32-bit project
7+
-- ========================
8+
project "gmsv_binarytable_32"
9+
kind "SharedLib"
10+
language "C++"
11+
architecture "x86" -- 32-bit
12+
includedirs { "gmod-module-base/include/" }
13+
targetdir "build" -- separate output folder
14+
symbols "On"
15+
targetname "gmsv_binarytable"
16+
targetextension ".dll"
17+
targetprefix ""
18+
19+
filter "system:windows"
20+
targetsuffix "_win32"
21+
targetextension ".dll"
22+
staticruntime "On"
23+
24+
filter "system:macosx"
25+
targetsuffix "_osx32"
26+
targetextension ".dll"
27+
28+
filter "system:linux"
29+
targetsuffix "_linux32"
30+
targetextension ".dll"
31+
32+
filter { "configurations:Release", "system:windows" }
33+
optimize "Speed"
34+
linktimeoptimization "On"
35+
buildoptions { "/Ot", "/Ob2", "/Oi", "/Oy" }
36+
defines { "BUILDAVX512" }
37+
38+
filter { "configurations:Release", "system:not windows" }
39+
optimize "Speed"
40+
linktimeoptimization "On"
41+
42+
filter {}
43+
files { "source/**.*" }
44+
45+
-- ========================
46+
-- 64-bit project
47+
-- ========================
48+
project "gmsv_binarytable_64"
49+
kind "SharedLib"
50+
language "C++"
51+
architecture "x86_64" -- 64-bit
52+
includedirs { "gmod-module-base/include/" }
53+
targetdir "build" -- separate output folder
54+
symbols "On"
55+
targetname "gmsv_binarytable"
56+
targetextension ".dll"
57+
targetprefix ""
58+
59+
filter "system:windows"
60+
targetsuffix "_win64"
61+
targetextension ".dll"
62+
staticruntime "On"
63+
64+
filter "system:macosx"
65+
targetsuffix "_osx64"
66+
targetextension ".dll"
67+
68+
filter "system:linux"
69+
targetsuffix "_linux64"
70+
targetextension ".dll"
71+
72+
filter { "configurations:Release", "system:windows" }
73+
optimize "Speed"
74+
linktimeoptimization "On"
75+
buildoptions { "/Ot", "/Ob2", "/Oi", "/Oy" }
76+
defines { "BUILDAVX512" }
77+
78+
filter { "configurations:Release", "system:not windows" }
2179
optimize "Speed"
22-
flags "StaticRuntime"
80+
linktimeoptimization "On"
2381

24-
files
25-
{
26-
"source/**.*",
27-
"../include/**.*"
28-
}
82+
filter {}
83+
files { "source/**.*" }

source/crc32.h

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// new and improved
2+
3+
#ifdef BUILDAVX512
4+
#include "crc32_AVX512.h"
5+
#endif
6+
7+
#ifdef _MSC_VER
8+
#define __restrict__ __restrict
9+
#endif
10+
11+
#include <cstdint>
12+
13+
class crc32_sb16 {
14+
public:
15+
static uint32_t update( const void* data, const size_t& length ) {
16+
#ifdef BUILDAVX512
17+
static bool AVX512 = have_vpclmulqdq();
18+
if ( AVX512 ) {
19+
return crc32_avx512<true>( 0, static_cast< const char* >(data), length, refl32 );
20+
}
21+
#endif
22+
23+
// Static variables initializes once making them perfect for reducing overhead
24+
static uint32_t table[ 16 ][ 256 ];
25+
static bool init = [] {
26+
generate_table( table );
27+
return true;
28+
}();
29+
30+
const uint8_t* __restrict__ p = ( const uint8_t* )data;
31+
32+
uint32_t crc = 0xFFFFFFFFu;
33+
34+
const uint8_t* end = p + length;
35+
36+
// Process 16 bytes at a time
37+
while ( p + 16 <= end ) {
38+
crc ^= *( const uint32_t* )p;
39+
const uint32_t d1 = *( const uint32_t* )(p + 4);
40+
const uint32_t d2 = *( const uint32_t* )(p + 8);
41+
const uint32_t d3 = *( const uint32_t* )(p + 12);
42+
43+
crc =
44+
// From d0
45+
(table[ 15 ][ (crc) & 0xFF ] ^
46+
table[ 14 ][ (crc >> 8) & 0xFF ] ^
47+
table[ 13 ][ (crc >> 16) & 0xFF ] ^
48+
table[ 12 ][ (crc >> 24) & 0xFF ]) ^
49+
// From d1
50+
(table[ 11 ][ (d1) & 0xFF ] ^
51+
table[ 10 ][ (d1 >> 8) & 0xFF ] ^
52+
table[ 9 ][ (d1 >> 16) & 0xFF ] ^
53+
table[ 8 ][ (d1 >> 24) & 0xFF ]) ^
54+
// From d2
55+
(table[ 7 ][ (d2) & 0xFF ] ^
56+
table[ 6 ][ (d2 >> 8) & 0xFF ] ^
57+
table[ 5 ][ (d2 >> 16) & 0xFF ] ^
58+
table[ 4 ][ (d2 >> 24) & 0xFF ]) ^
59+
// From d3
60+
(table[ 3 ][ (d3) & 0xFF ] ^
61+
table[ 2 ][ (d3 >> 8) & 0xFF ] ^
62+
table[ 1 ][ (d3 >> 16) & 0xFF ] ^
63+
table[ 0 ][ (d3 >> 24) & 0xFF ]);
64+
65+
p += 16;
66+
}
67+
68+
if ( p + 8 <= end ) {
69+
crc ^= *( const uint32_t* )p;
70+
const uint32_t d1 = *( const uint32_t* )(p + 4);
71+
72+
crc =
73+
(table[ 7 ][ (crc) & 0xFF ] ^
74+
table[ 6 ][ (crc >> 8) & 0xFF ] ^
75+
table[ 5 ][ (crc >> 16) & 0xFF ] ^
76+
table[ 4 ][ (crc >> 24) & 0xFF ]) ^
77+
78+
(table[ 3 ][ (d1) & 0xFF ] ^
79+
table[ 2 ][ (d1 >> 8) & 0xFF ] ^
80+
table[ 1 ][ (d1 >> 16) & 0xFF ] ^
81+
table[ 0 ][ (d1 >> 24) & 0xFF ]);
82+
83+
p += 8;
84+
}
85+
86+
if ( p + 4 <= end ) {
87+
crc ^= *( const uint32_t* )p;
88+
crc =
89+
(table[ 3 ][ (crc) & 0xFF ] ^
90+
table[ 2 ][ (crc >> 8) & 0xFF ] ^
91+
table[ 1 ][ (crc >> 16) & 0xFF ] ^
92+
table[ 0 ][ (crc >> 24) & 0xFF ]);
93+
94+
p += 4;
95+
}
96+
97+
// Remaining bytes
98+
while ( p < end )
99+
crc = table[ 0 ][ (crc ^ *p++) & 0xFF ] ^ (crc >> 8);
100+
101+
return crc ^ 0xFFFFFFFFu;
102+
}
103+
private:
104+
static constexpr void generate_table( uint32_t( &table )[ 16 ][ 256 ] ) {
105+
const uint32_t poly = 0xEDB88320;
106+
107+
// Table[0]: standard CRC-32 table
108+
for ( uint32_t i = 0; i < 256; ++i ) {
109+
uint32_t c = i;
110+
for ( int j = 0; j < 8; ++j )
111+
c = (c >> 1) ^ (-( int )(c & 1) & poly);
112+
table[ 0 ][ i ] = c;
113+
}
114+
115+
// Table[1..15]
116+
for ( int t = 1; t < 16; ++t ) {
117+
for ( int i = 0; i < 256; ++i ) {
118+
uint32_t c = table[ t - 1 ][ i ];
119+
table[ t ][ i ] = table[ 0 ][ c & 0xFF ] ^ (c >> 8);
120+
}
121+
}
122+
}
123+
};

0 commit comments

Comments
 (0)