Skip to content

Commit 1b3319a

Browse files
committed
x86: add SSE4_2 PCLMUL version for crc32
Based on: "Fast CRC Computation for Generic Polynomials Using PCLMULQDQ Instruction" V. Gopal, E. Ozturk, et al., 2009, http://intel.ly/2ySEwL0 Signed-off-by: Frank Du <[email protected]>
1 parent 9b81ce0 commit 1b3319a

File tree

7 files changed

+425
-7
lines changed

7 files changed

+425
-7
lines changed

ext/hash/hash_crc32.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "php_hash.h"
1919
#include "php_hash_crc32.h"
2020
#include "php_hash_crc32_tables.h"
21+
#include "ext/standard/crc32_x86.h"
2122

2223
PHP_HASH_API void PHP_CRC32Init(PHP_CRC32_CTX *context)
2324
{
@@ -28,7 +29,9 @@ PHP_HASH_API void PHP_CRC32Update(PHP_CRC32_CTX *context, const unsigned char *i
2829
{
2930
size_t i;
3031

31-
for (i = 0; i < len; ++i) {
32+
i = crc32_x86_simd_update(X86_CRC32, &context->state, input, len);
33+
34+
for (; i < len; ++i) {
3235
context->state = (context->state << 8) ^ crc32_table[(context->state >> 24) ^ (input[i] & 0xff)];
3336
}
3437
}
@@ -37,7 +40,9 @@ PHP_HASH_API void PHP_CRC32BUpdate(PHP_CRC32_CTX *context, const unsigned char *
3740
{
3841
size_t i;
3942

40-
for (i = 0; i < len; ++i) {
43+
i = crc32_x86_simd_update(X86_CRC32B, &context->state, input, len);
44+
45+
for (; i < len; ++i) {
4146
context->state = (context->state >> 8) ^ crc32b_table[(context->state ^ input[i]) & 0xff];
4247
}
4348
}
@@ -46,7 +51,9 @@ PHP_HASH_API void PHP_CRC32CUpdate(PHP_CRC32_CTX *context, const unsigned char *
4651
{
4752
size_t i;
4853

49-
for (i = 0; i < len; ++i) {
54+
i = crc32_x86_simd_update(X86_CRC32C, &context->state, input, len);
55+
56+
for (; i < len; ++i) {
5057
context->state = (context->state >> 8) ^ crc32c_table[(context->state ^ input[i]) & 0xff];
5158
}
5259
}

ext/standard/basic_functions.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "ext/standard/php_dns.h"
3434
#include "ext/standard/php_uuencode.h"
3535
#include "ext/standard/php_mt_rand.h"
36+
#include "ext/standard/crc32_x86.h"
3637

3738
#ifdef PHP_WIN32
3839
#include "win32/php_win32_globals.h"
@@ -366,6 +367,10 @@ PHP_MINIT_FUNCTION(basic) /* {{{ */
366367
BASIC_MINIT_SUBMODULE(string_intrin)
367368
#endif
368369

370+
#if ZEND_INTRIN_SSE4_2_PCLMUL_FUNC_PTR
371+
BASIC_MINIT_SUBMODULE(crc32_x86_intrin)
372+
#endif
373+
369374
#if ZEND_INTRIN_AVX2_FUNC_PTR || ZEND_INTRIN_SSSE3_FUNC_PTR
370375
BASIC_MINIT_SUBMODULE(base64_intrin)
371376
#endif

ext/standard/config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ PHP_NEW_EXTENSION(standard, array.c base64.c basic_functions.c browscap.c crc32.
449449
http_fopen_wrapper.c php_fopen_wrapper.c credits.c css.c \
450450
var_unserializer.c ftok.c sha1.c user_filters.c uuencode.c \
451451
filters.c proc_open.c streamsfuncs.c http.c password.c \
452-
random.c net.c hrtime.c,,,
452+
random.c net.c hrtime.c crc32_x86.c,,,
453453
-DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
454454

455455
PHP_ADD_MAKEFILE_FRAGMENT

ext/standard/config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ADD_FLAG("LIBS_STANDARD", "iphlpapi.lib");
2525

2626
EXTENSION("standard", "array.c base64.c basic_functions.c browscap.c \
2727
crc32.c crypt.c crypt_freesec.c crypt_blowfish.c crypt_sha256.c \
28-
crypt_sha512.c php_crypt_r.c \
28+
crypt_sha512.c php_crypt_r.c crc32_x86.c \
2929
datetime.c dir.c dl.c dns.c dns_win32.c exec.c \
3030
file.c filestat.c formatted_print.c fsock.c head.c html.c image.c \
3131
info.c iptc.c lcg.c link.c mail.c math.c md5.c metaphone.c microtime.c \

ext/standard/crc32.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "php.h"
1818
#include "basic_functions.h"
1919
#include "crc32.h"
20+
#include "crc32_x86.h"
2021

2122
#if HAVE_AARCH64_CRC32
2223
# include <arm_acle.h>
@@ -72,9 +73,9 @@ static uint32_t crc32_aarch64(uint32_t crc, char *p, size_t nr) {
7273
PHP_FUNCTION(crc32)
7374
{
7475
char *p;
75-
size_t nr;
76+
size_t nr, nr_simd;
7677
uint32_t crcinit = 0;
77-
register uint32_t crc;
78+
uint32_t crc;
7879

7980
ZEND_PARSE_PARAMETERS_START(1, 1)
8081
Z_PARAM_STRING(p, nr)
@@ -89,6 +90,10 @@ PHP_FUNCTION(crc32)
8990
}
9091
#endif
9192

93+
nr_simd = crc32_x86_simd_update(X86_CRC32B, &crc, (const unsigned char *)p, nr);
94+
nr -= nr_simd;
95+
p += nr_simd;
96+
9297
for (; nr--; ++p) {
9398
crc = ((crc >> 8) & 0x00FFFFFF) ^ crc32tab[(crc ^ (*p)) & 0xFF ];
9499
}

0 commit comments

Comments
 (0)