summaryrefslogtreecommitdiffstats
path: root/other-licenses/snappy/src/snappy-stubs-internal.h
diff options
context:
space:
mode:
Diffstat (limited to 'other-licenses/snappy/src/snappy-stubs-internal.h')
-rw-r--r--other-licenses/snappy/src/snappy-stubs-internal.h85
1 files changed, 62 insertions, 23 deletions
diff --git a/other-licenses/snappy/src/snappy-stubs-internal.h b/other-licenses/snappy/src/snappy-stubs-internal.h
index c2a838f38f..526c38b700 100644
--- a/other-licenses/snappy/src/snappy-stubs-internal.h
+++ b/other-licenses/snappy/src/snappy-stubs-internal.h
@@ -31,7 +31,7 @@
#ifndef THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_INTERNAL_H_
#define THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_INTERNAL_H_
-#ifdef HAVE_CONFIG_H
+#if HAVE_CONFIG_H
#include "config.h"
#endif
@@ -43,11 +43,11 @@
#include <limits>
#include <string>
-#ifdef HAVE_SYS_MMAN_H
+#if HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
-#ifdef HAVE_UNISTD_H
+#if HAVE_UNISTD_H
#include <unistd.h>
#endif
@@ -90,19 +90,25 @@
#define ARRAYSIZE(a) int{sizeof(a) / sizeof(*(a))}
// Static prediction hints.
-#ifdef HAVE_BUILTIN_EXPECT
+#if HAVE_BUILTIN_EXPECT
#define SNAPPY_PREDICT_FALSE(x) (__builtin_expect(x, 0))
#define SNAPPY_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
#else
#define SNAPPY_PREDICT_FALSE(x) x
#define SNAPPY_PREDICT_TRUE(x) x
-#endif
+#endif // HAVE_BUILTIN_EXPECT
// Inlining hints.
-#ifdef HAVE_ATTRIBUTE_ALWAYS_INLINE
+#if HAVE_ATTRIBUTE_ALWAYS_INLINE
#define SNAPPY_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
#else
#define SNAPPY_ATTRIBUTE_ALWAYS_INLINE
+#endif // HAVE_ATTRIBUTE_ALWAYS_INLINE
+
+#if HAVE_BUILTIN_PREFETCH
+#define SNAPPY_PREFETCH(ptr) __builtin_prefetch(ptr, 0, 3)
+#else
+#define SNAPPY_PREFETCH(ptr) (void)(ptr)
#endif
// Stubbed version of ABSL_FLAG.
@@ -171,27 +177,42 @@ class LittleEndian {
public:
// Functions to do unaligned loads and stores in little-endian order.
static inline uint16_t Load16(const void *ptr) {
- const uint8_t* const buffer = reinterpret_cast<const uint8_t*>(ptr);
-
// Compiles to a single mov/str on recent clang and gcc.
+#if SNAPPY_IS_BIG_ENDIAN
+ const uint8_t* const buffer = reinterpret_cast<const uint8_t*>(ptr);
return (static_cast<uint16_t>(buffer[0])) |
(static_cast<uint16_t>(buffer[1]) << 8);
+#else
+ // memcpy() turns into a single instruction early in the optimization
+ // pipeline (relatively to a series of byte accesses). So, using memcpy
+ // instead of byte accesses may lead to better decisions in more stages of
+ // the optimization pipeline.
+ uint16_t value;
+ std::memcpy(&value, ptr, 2);
+ return value;
+#endif
}
static inline uint32_t Load32(const void *ptr) {
- const uint8_t* const buffer = reinterpret_cast<const uint8_t*>(ptr);
-
// Compiles to a single mov/str on recent clang and gcc.
+#if SNAPPY_IS_BIG_ENDIAN
+ const uint8_t* const buffer = reinterpret_cast<const uint8_t*>(ptr);
return (static_cast<uint32_t>(buffer[0])) |
(static_cast<uint32_t>(buffer[1]) << 8) |
(static_cast<uint32_t>(buffer[2]) << 16) |
(static_cast<uint32_t>(buffer[3]) << 24);
+#else
+ // See Load16() for the rationale of using memcpy().
+ uint32_t value;
+ std::memcpy(&value, ptr, 4);
+ return value;
+#endif
}
static inline uint64_t Load64(const void *ptr) {
- const uint8_t* const buffer = reinterpret_cast<const uint8_t*>(ptr);
-
// Compiles to a single mov/str on recent clang and gcc.
+#if SNAPPY_IS_BIG_ENDIAN
+ const uint8_t* const buffer = reinterpret_cast<const uint8_t*>(ptr);
return (static_cast<uint64_t>(buffer[0])) |
(static_cast<uint64_t>(buffer[1]) << 8) |
(static_cast<uint64_t>(buffer[2]) << 16) |
@@ -200,30 +221,44 @@ class LittleEndian {
(static_cast<uint64_t>(buffer[5]) << 40) |
(static_cast<uint64_t>(buffer[6]) << 48) |
(static_cast<uint64_t>(buffer[7]) << 56);
+#else
+ // See Load16() for the rationale of using memcpy().
+ uint64_t value;
+ std::memcpy(&value, ptr, 8);
+ return value;
+#endif
}
static inline void Store16(void *dst, uint16_t value) {
- uint8_t* const buffer = reinterpret_cast<uint8_t*>(dst);
-
// Compiles to a single mov/str on recent clang and gcc.
+#if SNAPPY_IS_BIG_ENDIAN
+ uint8_t* const buffer = reinterpret_cast<uint8_t*>(dst);
buffer[0] = static_cast<uint8_t>(value);
buffer[1] = static_cast<uint8_t>(value >> 8);
+#else
+ // See Load16() for the rationale of using memcpy().
+ std::memcpy(dst, &value, 2);
+#endif
}
static void Store32(void *dst, uint32_t value) {
- uint8_t* const buffer = reinterpret_cast<uint8_t*>(dst);
-
// Compiles to a single mov/str on recent clang and gcc.
+#if SNAPPY_IS_BIG_ENDIAN
+ uint8_t* const buffer = reinterpret_cast<uint8_t*>(dst);
buffer[0] = static_cast<uint8_t>(value);
buffer[1] = static_cast<uint8_t>(value >> 8);
buffer[2] = static_cast<uint8_t>(value >> 16);
buffer[3] = static_cast<uint8_t>(value >> 24);
+#else
+ // See Load16() for the rationale of using memcpy().
+ std::memcpy(dst, &value, 4);
+#endif
}
static void Store64(void* dst, uint64_t value) {
- uint8_t* const buffer = reinterpret_cast<uint8_t*>(dst);
-
// Compiles to a single mov/str on recent clang and gcc.
+#if SNAPPY_IS_BIG_ENDIAN
+ uint8_t* const buffer = reinterpret_cast<uint8_t*>(dst);
buffer[0] = static_cast<uint8_t>(value);
buffer[1] = static_cast<uint8_t>(value >> 8);
buffer[2] = static_cast<uint8_t>(value >> 16);
@@ -232,14 +267,18 @@ class LittleEndian {
buffer[5] = static_cast<uint8_t>(value >> 40);
buffer[6] = static_cast<uint8_t>(value >> 48);
buffer[7] = static_cast<uint8_t>(value >> 56);
+#else
+ // See Load16() for the rationale of using memcpy().
+ std::memcpy(dst, &value, 8);
+#endif
}
static inline constexpr bool IsLittleEndian() {
-#if defined(SNAPPY_IS_BIG_ENDIAN)
+#if SNAPPY_IS_BIG_ENDIAN
return false;
#else
return true;
-#endif // defined(SNAPPY_IS_BIG_ENDIAN)
+#endif // SNAPPY_IS_BIG_ENDIAN
}
};
@@ -265,7 +304,7 @@ class Bits {
void operator=(const Bits&);
};
-#if defined(HAVE_BUILTIN_CTZ)
+#if HAVE_BUILTIN_CTZ
inline int Bits::Log2FloorNonZero(uint32_t n) {
assert(n != 0);
@@ -354,7 +393,7 @@ inline int Bits::FindLSBSetNonZero(uint32_t n) {
#endif // End portable versions.
-#if defined(HAVE_BUILTIN_CTZ)
+#if HAVE_BUILTIN_CTZ
inline int Bits::FindLSBSetNonZero64(uint64_t n) {
assert(n != 0);
@@ -388,7 +427,7 @@ inline int Bits::FindLSBSetNonZero64(uint64_t n) {
}
}
-#endif // End portable version.
+#endif // HAVE_BUILTIN_CTZ
// Variable-length integer encoding.
class Varint {