[Tarantool-patches] [PATCH v2 04/10] crc32: align memory access

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Tue Jun 9 01:33:26 MSK 2020


> diff --git a/src/cpu_feature.c b/src/cpu_feature.c
> index 9bf6223de..7b284fa98 100644
> --- a/src/cpu_feature.c
> +++ b/src/cpu_feature.c
> @@ -69,10 +69,8 @@ crc32c_hw_byte(uint32_t crc, char const *data, unsigned int length)
>  uint32_t
>  crc32c_hw(uint32_t crc, const char *buf, unsigned int len)
>  {
> -	const int align = alignof(unsigned long);
> -	unsigned long addr = (unsigned long)buf;
> -	unsigned int not_aligned_prefix =
> -		((addr - 1 + align) & ~(align - 1)) - addr;
> +	const unsigned int align = alignof(unsigned long);
> +	unsigned int not_aligned_prefix = align - (unsigned long)buf % align;

When the address is aligned, not_aligned_prefix becomes = align.
For 8 byte word it means we will do 8 operations instead of 1.

I fixed it this way:

====================
diff --git a/src/cpu_feature.c b/src/cpu_feature.c
index 7b284fa98..856f054c7 100644
--- a/src/cpu_feature.c
+++ b/src/cpu_feature.c
@@ -70,7 +70,8 @@ uint32_t
 crc32c_hw(uint32_t crc, const char *buf, unsigned int len)
 {
 	const unsigned int align = alignof(unsigned long);
-	unsigned int not_aligned_prefix = align - (unsigned long)buf % align;
+	unsigned int not_aligned_prefix =
+		(align - (unsigned long)buf % align) % align;
 	/*
 	 * Calculate CRC32 for the prefix byte-by-byte so as to
 	 * then use aligned words to calculate the rest. This is

====================
This is fast, because % align is transformed into & (align - 1)
in the assembly.


More information about the Tarantool-patches mailing list