Ruby 4.1.0dev (2026-11-24 revision 9920c0670630fd2ca853d66ca08e5848c96c0301)
memequal.c
1#include "prism/internal/memequal.h"
2
3/* Combine the differing bits so the compiler can compare the words in parallel. */
4static PRISM_INLINE bool
5pm_memequal_256(const unsigned char *a, const unsigned char *b) {
6 uint64_t left[4], right[4];
7 memcpy(left, a, sizeof(left));
8 memcpy(right, b, sizeof(right));
9
10 uint64_t difference = 0;
11 for (size_t idx = 0; idx < 4; ++idx) {
12 difference |= left[idx] ^ right[idx];
13 }
14
15 return !difference;
16}
17
18/* Compare slices longer than 16 bytes out of line. */
19bool
20pm_memequal_long(const unsigned char *a, const unsigned char *b, size_t length) {
21 size_t offset = 0;
22 while (length - offset >= 32) {
23 if (!pm_memequal_256(a + offset, b + offset)) return false;
24 offset += 32;
25 }
26
27 const size_t last = length - 8;
28 while (offset < last) {
29 if (!pm_memequal_64(a + offset, b + offset)) return false;
30 offset += 8;
31 }
32
33 return pm_memequal_64(a + last, b + last);
34}
#define PRISM_INLINE
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition inline.h:12