1 /*-------------------------------------------------------------------------
2  *
3  * pg_crc32c_armv8.c
4  *	  Compute CRC-32C checksum using ARMv8 CRC Extension instructions
5  *
6  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *	  src/port/pg_crc32c_armv8.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "c.h"
16 
17 #include <arm_acle.h>
18 
19 #include "port/pg_crc32c.h"
20 
21 pg_crc32c
pg_comp_crc32c_armv8(pg_crc32c crc,const void * data,size_t len)22 pg_comp_crc32c_armv8(pg_crc32c crc, const void *data, size_t len)
23 {
24 	const unsigned char *p = data;
25 	const unsigned char *pend = p + len;
26 
27 	/*
28 	 * ARMv8 doesn't require alignment, but aligned memory access is
29 	 * significantly faster. Process leading bytes so that the loop below
30 	 * starts with a pointer aligned to eight bytes.
31 	 */
32 	if (!PointerIsAligned(p, uint16) &&
33 		p + 1 <= pend)
34 	{
35 		crc = __crc32cb(crc, *p);
36 		p += 1;
37 	}
38 	if (!PointerIsAligned(p, uint32) &&
39 		p + 2 <= pend)
40 	{
41 		crc = __crc32ch(crc, *(uint16 *) p);
42 		p += 2;
43 	}
44 	if (!PointerIsAligned(p, uint64) &&
45 		p + 4 <= pend)
46 	{
47 		crc = __crc32cw(crc, *(uint32 *) p);
48 		p += 4;
49 	}
50 
51 	/* Process eight bytes at a time, as far as we can. */
52 	while (p + 8 <= pend)
53 	{
54 		crc = __crc32cd(crc, *(uint64 *) p);
55 		p += 8;
56 	}
57 
58 	/* Process remaining 0-7 bytes. */
59 	if (p + 4 <= pend)
60 	{
61 		crc = __crc32cw(crc, *(uint32 *) p);
62 		p += 4;
63 	}
64 	if (p + 2 <= pend)
65 	{
66 		crc = __crc32ch(crc, *(uint16 *) p);
67 		p += 2;
68 	}
69 	if (p < pend)
70 	{
71 		crc = __crc32cb(crc, *p);
72 	}
73 
74 	return crc;
75 }
76