Skip to content

Instantly share code, notes, and snippets.

@ParadoxV5
Created July 3, 2025 19:33
Show Gist options
  • Save ParadoxV5/9c0971c1456fe6d536e707c758417b64 to your computer and use it in GitHub Desktop.
Save ParadoxV5/9c0971c1456fe6d536e707c758417b64 to your computer and use it in GitHub Desktop.
#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;
}
@ParadoxV5
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment