Created
July 3, 2025 19:33
-
-
Save ParadoxV5/9c0971c1456fe6d536e707c758417b64 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stddef.h> | |
#include <stdint.h> | |
/** | |
Here is my Standard-compliant, SIMD-capable version of | |
https://pzemtsov.github.io/2016/11/06/bug-story-alignment-on-x86.html. | |
I wonder which one is faster in a benchmark? | |
*/ | |
_Bool check_ip_header_sum(const char *p, size_t size) | |
{ | |
uintmax_t sum_l = 0, sum_h = 0; | |
for (size_t i = 0; i < size;) | |
{ | |
// Endianness doesn’t really matter because the carry is circular. | |
sum_l += p[i++]; | |
sum_h += p[i++]; | |
} | |
sum_l += sum_h << 8; | |
do | |
sum_l = (sum_l & 0xFFFF) + (sum_l >> 16); | |
while (sum_l > 0xFFFF); | |
return sum_l == 0xFFFF; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compiled on https://godbolt.org with x86-64 gcc (trunk) and
-O3
SIMD is available for “simple” loops and produces fewer instructions with incremental iterations than decremental ones.