From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org> To: Timur Safin <tsafin@tarantool.org>, tarantool-patches@dev.tarantool.org, alyapunov@tarantool.org, korablev@tarantool.org Subject: Re: [Tarantool-patches] [PATCH v2 04/10] crc32: align memory access Date: Fri, 29 May 2020 01:23:29 +0200 [thread overview] Message-ID: <05c14f7d-6f18-05ba-19ad-b5aa4dc91d17@tarantool.org> (raw) In-Reply-To: <048601d6352c$3cdb81a0$b69284e0$@tarantool.org> Thanks for the review! On 28/05/2020 22:11, Timur Safin wrote: > > > : -----Original Message----- > : From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org> > : Subject: [PATCH v2 04/10] crc32: align memory access > : > : > : diff --git a/src/cpu_feature.c b/src/cpu_feature.c > : index 98567ccb3..9bf6223de 100644 > : --- a/src/cpu_feature.c > : +++ b/src/cpu_feature.c > : @@ -50,7 +51,7 @@ > : > : > : static uint32_t > : -crc32c_hw_byte(uint32_t crc, unsigned char const *data, unsigned int > : length) > : +crc32c_hw_byte(uint32_t crc, char const *data, unsigned int length) > : { > : while (length--) { > : __asm__ __volatile__( > : @@ -68,6 +69,26 @@ crc32c_hw_byte(uint32_t crc, unsigned 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; > > Hmm, hmm... > > Isn't it simple `addr % align`? Or even `addr & (align - 1)` ? Consider the example: addr = 14, align = 8. Then not_aligned_prefix = 2. Need to read first 2 bytes one by one to get to 16, the closest aligned address. addr % align = 14 % 8 = 6 != 2 addr & (align - 1) = 14 & 7 = 1110 & 0111 = 110 = 6 != 2 But yeah, this could be done simpler: align - addr % align. This will give how many bytes are needed to the next aligned address. I wrote the solution above by blindly using 'aligned - not aligned' and the same schema as in small_align(). Here is the diff: ==================== 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; /* * Calculate CRC32 for the prefix byte-by-byte so as to * then use aligned words to calculate the rest. This is
next prev parent reply other threads:[~2020-05-28 23:23 UTC|newest] Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-05-27 23:32 [Tarantool-patches] [PATCH v2 00/10] Sanitize unaligned access Vladislav Shpilevoy 2020-05-27 23:32 ` [Tarantool-patches] [PATCH v2 01/10] small: sanitized rlist and new region API Vladislav Shpilevoy 2020-05-28 20:41 ` Timur Safin 2020-05-28 22:56 ` Vladislav Shpilevoy 2020-06-08 23:01 ` Vladislav Shpilevoy 2020-05-27 23:32 ` [Tarantool-patches] [PATCH v2 10/10] xrow: use unaligned store operation in xrow_to_iovec() Vladislav Shpilevoy 2020-05-28 20:20 ` Timur Safin 2020-05-27 23:32 ` [Tarantool-patches] [PATCH v2 02/10] cmake: ignore warnings on alignof() and offsetof() Vladislav Shpilevoy 2020-05-28 20:18 ` Timur Safin 2020-05-29 6:24 ` Kirill Yukhin 2020-05-29 22:34 ` Vladislav Shpilevoy 2020-05-27 23:32 ` [Tarantool-patches] [PATCH v2 03/10] cmake: add option ENABLE_UB_SANITIZER Vladislav Shpilevoy 2020-05-28 20:42 ` Timur Safin 2020-05-29 8:53 ` Sergey Bronnikov 2020-05-29 22:36 ` Vladislav Shpilevoy 2020-05-27 23:32 ` [Tarantool-patches] [PATCH v2 04/10] crc32: align memory access Vladislav Shpilevoy 2020-05-28 20:11 ` Timur Safin 2020-05-28 23:23 ` Vladislav Shpilevoy [this message] 2020-05-28 23:32 ` Timur Safin 2020-06-08 22:33 ` Vladislav Shpilevoy 2020-05-27 23:32 ` [Tarantool-patches] [PATCH v2 05/10] sql: make BtCursor's memory aligned Vladislav Shpilevoy 2020-05-28 20:20 ` Timur Safin 2020-05-27 23:32 ` [Tarantool-patches] [PATCH v2 06/10] region: use aligned allocations where necessary Vladislav Shpilevoy 2020-05-28 20:35 ` Timur Safin 2020-05-28 23:07 ` Vladislav Shpilevoy 2020-05-27 23:32 ` [Tarantool-patches] [PATCH v2 07/10] vinyl: align statements and bps tree extents Vladislav Shpilevoy 2020-05-28 20:38 ` Timur Safin 2020-05-27 23:32 ` [Tarantool-patches] [PATCH v2 08/10] tuple: use unaligned store-load for field map Vladislav Shpilevoy 2020-05-28 20:22 ` Timur Safin 2020-05-27 23:32 ` [Tarantool-patches] [PATCH v2 09/10] port: make port_c_entry not PACKED Vladislav Shpilevoy 2020-05-28 20:42 ` Timur Safin 2020-06-03 21:27 ` [Tarantool-patches] [PATCH v2 00/10] Sanitize unaligned access Vladislav Shpilevoy 2020-06-08 22:33 ` Vladislav Shpilevoy
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=05c14f7d-6f18-05ba-19ad-b5aa4dc91d17@tarantool.org \ --to=v.shpilevoy@tarantool.org \ --cc=alyapunov@tarantool.org \ --cc=korablev@tarantool.org \ --cc=tarantool-patches@dev.tarantool.org \ --cc=tsafin@tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH v2 04/10] crc32: align memory access' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox