lib/string.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/string.c b/lib/string.c index 77bd0b1d3296..3eb390fc4f73 100644 --- a/lib/string.c +++ b/lib/string.c @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -935,6 +936,31 @@ __visible int memcmp(const void *cs, const void *ct, size_t count) const unsigned char *su1, *su2; int res = 0; + if (count >= sizeof(unsigned long)) { + const unsigned long *u1 = cs; + const unsigned long *u2 = ct; + unsigned long offset; + + if (get_unaligned(u1) != get_unaligned(u2)) + goto bytewise; + + /* Align 'u1' up */ + offset = sizeof(*u1) - ((sizeof(*u1)-1) & (unsigned long)(u1)); + u1 = cs + offset; + u2 = ct + offset; + count -= offset; + + while (count >= sizeof(unsigned long)) { + if (*u1 != get_unaligned(u2)) + break; + u1++; + u2++; + count -= sizeof(unsigned long); + } + cs = u1; + ct = u2; + } +bytewise: for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) if ((res = *su1 - *su2) != 0) break;