make (array) of ArrayObject return the contents
[hiphop-php.git] / hphp / util / word-same.h
blob45e5ff41332543a13ca988fd109e4f400b48fc81
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #ifndef incl_HPHP_WORD_SAME_H_
17 #define incl_HPHP_WORD_SAME_H_
19 #include "folly/Portability.h"
21 #include "hphp/util/util.h"
23 namespace HPHP {
25 //////////////////////////////////////////////////////////////////////
27 // ASan is less precise than valgrind and believes this function overruns reads
28 #ifndef FOLLY_SANITIZE_ADDRESS
31 * Word at a time comparison for two memory regions of length
32 * `lenBytes' + 1 (for the null terminator). Returns true if the
33 * regions are the same.
35 * Assumes it can load more words than the size to compare (this is
36 * often possible in HPHP when you know you are dealing with smart
37 * allocated memory). The final word compare is adjusted to handle
38 * the slack in lenBytes so only the bytes we care about are compared.
40 ALWAYS_INLINE
41 bool wordsame(const void* mem1, const void* mem2, size_t lenBytes) {
42 assert(reinterpret_cast<const uintptr_t>(mem1) % 4 == 0);
43 auto p1 = reinterpret_cast<const uint32_t*>(mem1);
44 auto p2 = reinterpret_cast<const uint32_t*>(mem2);
45 auto constexpr W = sizeof(*p1);
46 for (auto end = p1 + lenBytes / W; p1 < end; p1++, p2++) {
47 if (*p1 != *p2) return false;
49 // let W = sizeof(*p1); now p1 and p2 point to the last 0..W-1 bytes plus
50 // the 0 terminator, ie the last 1..W bytes. Load W bytes, shift off the
51 // bytes after the 0, then compare.
52 auto shift = 8 * (W - 1) - 8 * (lenBytes % W);
53 return (*p1 << shift) == (*p2 << shift);
56 #else // FOLLY_SANITIZE_ADDRESS
58 ALWAYS_INLINE
59 bool wordsame(const void* mem1, const void* mem2, size_t lenBytes) {
60 assert(reinterpret_cast<const uintptr_t>(mem1) % 4 == 0);
61 return !memcmp(mem1, mem2, lenBytes+1);
64 #endif
66 //////////////////////////////////////////////////////////////////////
70 #endif