xref: /PHP-8.0/ext/standard/crc32.c (revision ddea5c08)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Rasmus Lerdorf <rasmus@php.net>                              |
14    +----------------------------------------------------------------------+
15 */
16 
17 #include "php.h"
18 #include "basic_functions.h"
19 #include "crc32.h"
20 #include "crc32_x86.h"
21 
22 #if HAVE_AARCH64_CRC32
23 # include <arm_acle.h>
24 # if defined(__linux__)
25 #  include <sys/auxv.h>
26 #  include <asm/hwcap.h>
27 # elif defined(__APPLE__)
28 #  include <sys/sysctl.h>
29 # endif
30 
has_crc32_insn()31 static inline int has_crc32_insn() {
32 	/* Only go through the runtime detection once. */
33 	static int res = -1;
34 	if (res != -1)
35 		return res;
36 # if defined(HWCAP_CRC32)
37 	res = getauxval(AT_HWCAP) & HWCAP_CRC32;
38 	return res;
39 # elif defined(HWCAP2_CRC32)
40 	res = getauxval(AT_HWCAP2) & HWCAP2_CRC32;
41 	return res;
42 # elif defined(__APPLE__)
43 	size_t reslen = sizeof(res);
44 	if (sysctlbyname("hw.optional.armv8_crc32", &res, &reslen, NULL, 0) < 0)
45 		res = 0;
46 	return res;
47 # else
48 	res = 0;
49 	return res;
50 # endif
51 }
52 
53 # if defined(__GNUC__) && !defined(__clang__)
54 #  pragma GCC push_options
55 #  pragma GCC target ("+nothing+crc")
56 # endif
crc32_aarch64(uint32_t crc,char * p,size_t nr)57 static uint32_t crc32_aarch64(uint32_t crc, char *p, size_t nr) {
58 	while (nr >= sizeof(uint64_t)) {
59 		crc = __crc32d(crc, *(uint64_t *)p);
60 		p += sizeof(uint64_t);
61 		nr -= sizeof(uint64_t);
62 	}
63 	if (nr >= sizeof(int32_t)) {
64 		crc = __crc32w(crc, *(uint32_t *)p);
65 		p += sizeof(uint32_t);
66 		nr -= sizeof(uint32_t);
67 	}
68 	if (nr >= sizeof(int16_t)) {
69 		crc = __crc32h(crc, *(uint16_t *)p);
70 		p += sizeof(uint16_t);
71 		nr -= sizeof(uint16_t);
72 	}
73 	if (nr) {
74 		crc = __crc32b(crc, *p);
75 	}
76 	return crc;
77 }
78 # if defined(__GNUC__) && !defined(__clang__)
79 #  pragma GCC pop_options
80 # endif
81 #endif
82 
83 /* {{{ Calculate the crc32 polynomial of a string */
PHP_FUNCTION(crc32)84 PHP_FUNCTION(crc32)
85 {
86 	char *p;
87 	size_t nr;
88 	uint32_t crcinit = 0;
89 	uint32_t crc;
90 
91 	ZEND_PARSE_PARAMETERS_START(1, 1)
92 		Z_PARAM_STRING(p, nr)
93 	ZEND_PARSE_PARAMETERS_END();
94 
95 	crc = crcinit^0xFFFFFFFF;
96 
97 #if HAVE_AARCH64_CRC32
98 	if (has_crc32_insn()) {
99 		crc = crc32_aarch64(crc, p, nr);
100 		RETURN_LONG(crc^0xFFFFFFFF);
101 	}
102 #endif
103 
104 #if ZEND_INTRIN_SSE4_2_PCLMUL_NATIVE || ZEND_INTRIN_SSE4_2_PCLMUL_RESOLVER
105 	size_t nr_simd = crc32_x86_simd_update(X86_CRC32B, &crc, (const unsigned char *)p, nr);
106 	nr -= nr_simd;
107 	p += nr_simd;
108 #endif
109 
110 	for (; nr--; ++p) {
111 		crc = ((crc >> 8) & 0x00FFFFFF) ^ crc32tab[(crc ^ (*p)) & 0xFF ];
112 	}
113 	RETURN_LONG(crc^0xFFFFFFFF);
114 }
115 /* }}} */
116