2011-03-18 17:14:15 +00:00
|
|
|
// Copyright 2011 Google Inc. All Rights Reserved.
|
|
|
|
//
|
2011-03-25 16:14:41 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
2011-03-18 17:14:15 +00:00
|
|
|
//
|
2011-03-25 16:14:41 +00:00
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following disclaimer
|
|
|
|
// in the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived from
|
|
|
|
// this software without specific prior written permission.
|
2011-03-18 17:14:15 +00:00
|
|
|
//
|
2011-03-25 16:14:41 +00:00
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2011-03-18 17:14:15 +00:00
|
|
|
//
|
|
|
|
// Various stubs for the open-source version of Snappy.
|
|
|
|
|
2015-06-22 13:39:08 +00:00
|
|
|
#ifndef THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_INTERNAL_H_
|
|
|
|
#define THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_INTERNAL_H_
|
2011-03-18 17:14:15 +00:00
|
|
|
|
2021-08-16 18:22:31 +00:00
|
|
|
#if HAVE_CONFIG_H
|
2011-03-30 20:27:53 +00:00
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2020-04-29 19:32:47 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
2020-04-12 00:01:01 +00:00
|
|
|
#include <cstring>
|
2020-04-13 19:47:34 +00:00
|
|
|
#include <limits>
|
2011-03-18 17:14:15 +00:00
|
|
|
#include <string>
|
|
|
|
|
2021-08-16 18:22:31 +00:00
|
|
|
#if HAVE_SYS_MMAN_H
|
2011-03-18 17:14:15 +00:00
|
|
|
#include <sys/mman.h>
|
2011-03-24 19:12:27 +00:00
|
|
|
#endif
|
2011-03-18 17:14:15 +00:00
|
|
|
|
2021-08-16 18:22:31 +00:00
|
|
|
#if HAVE_UNISTD_H
|
2017-08-02 16:43:03 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2017-08-01 17:01:27 +00:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#include <intrin.h>
|
|
|
|
#endif // defined(_MSC_VER)
|
|
|
|
|
2018-03-27 04:55:23 +00:00
|
|
|
#ifndef __has_feature
|
|
|
|
#define __has_feature(x) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if __has_feature(memory_sanitizer)
|
|
|
|
#include <sanitizer/msan_interface.h>
|
|
|
|
#define SNAPPY_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) \
|
|
|
|
__msan_unpoison((address), (size))
|
|
|
|
#else
|
|
|
|
#define SNAPPY_ANNOTATE_MEMORY_IS_INITIALIZED(address, size) /* empty */
|
|
|
|
#endif // __has_feature(memory_sanitizer)
|
|
|
|
|
2011-03-18 17:14:15 +00:00
|
|
|
#include "snappy-stubs-public.h"
|
|
|
|
|
2020-05-06 15:55:59 +00:00
|
|
|
// Used to enable 64-bit optimized versions of some routines.
|
2020-07-15 17:34:03 +00:00
|
|
|
#if defined(__PPC64__) || defined(__powerpc64__)
|
2017-06-06 08:05:05 +00:00
|
|
|
#define ARCH_PPC 1
|
2020-05-06 15:55:59 +00:00
|
|
|
#elif defined(__aarch64__) || defined(_M_ARM64)
|
2017-08-16 19:38:06 +00:00
|
|
|
#define ARCH_ARM 1
|
2011-03-18 17:14:15 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Needed by OS X, among others.
|
|
|
|
#ifndef MAP_ANONYMOUS
|
|
|
|
#define MAP_ANONYMOUS MAP_ANON
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// The size of an array, if known at compile-time.
|
|
|
|
// Will give unexpected results if used on a pointer.
|
|
|
|
// We undefine it first, since some compilers already have a definition.
|
|
|
|
#ifdef ARRAYSIZE
|
|
|
|
#undef ARRAYSIZE
|
|
|
|
#endif
|
2020-05-05 16:13:04 +00:00
|
|
|
#define ARRAYSIZE(a) int{sizeof(a) / sizeof(*(a))}
|
2011-03-18 17:14:15 +00:00
|
|
|
|
|
|
|
// Static prediction hints.
|
2021-08-16 18:22:31 +00:00
|
|
|
#if HAVE_BUILTIN_EXPECT
|
2017-07-28 21:31:04 +00:00
|
|
|
#define SNAPPY_PREDICT_FALSE(x) (__builtin_expect(x, 0))
|
|
|
|
#define SNAPPY_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
|
2011-03-18 17:14:15 +00:00
|
|
|
#else
|
2017-07-28 21:31:04 +00:00
|
|
|
#define SNAPPY_PREDICT_FALSE(x) x
|
|
|
|
#define SNAPPY_PREDICT_TRUE(x) x
|
2021-08-16 18:22:31 +00:00
|
|
|
#endif // HAVE_BUILTIN_EXPECT
|
2011-03-18 17:14:15 +00:00
|
|
|
|
2020-04-13 19:47:34 +00:00
|
|
|
// Inlining hints.
|
2021-08-16 18:22:31 +00:00
|
|
|
#if HAVE_ATTRIBUTE_ALWAYS_INLINE
|
2020-04-13 19:47:34 +00:00
|
|
|
#define SNAPPY_ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
|
|
|
|
#else
|
|
|
|
#define SNAPPY_ATTRIBUTE_ALWAYS_INLINE
|
2021-08-16 18:22:31 +00:00
|
|
|
#endif // HAVE_ATTRIBUTE_ALWAYS_INLINE
|
2020-04-13 19:47:34 +00:00
|
|
|
|
2021-03-08 17:24:08 +00:00
|
|
|
// Stubbed version of ABSL_FLAG.
|
|
|
|
//
|
|
|
|
// In the open source version, flags can only be changed at compile time.
|
|
|
|
#define SNAPPY_FLAG(flag_type, flag_name, default_value, help) \
|
|
|
|
flag_type FLAGS_ ## flag_name = default_value
|
2011-03-18 17:14:15 +00:00
|
|
|
|
|
|
|
namespace snappy {
|
|
|
|
|
2021-03-08 17:24:08 +00:00
|
|
|
// Stubbed version of absl::GetFlag().
|
|
|
|
template <typename T>
|
|
|
|
inline T GetFlag(T flag) { return flag; }
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
static const uint32_t kuint32max = std::numeric_limits<uint32_t>::max();
|
|
|
|
static const int64_t kint64max = std::numeric_limits<int64_t>::max();
|
2011-03-18 17:14:15 +00:00
|
|
|
|
|
|
|
// Potentially unaligned loads and stores.
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
inline uint16_t UNALIGNED_LOAD16(const void *p) {
|
Remove platform-dependent code for unaligned loads/stores.
Snappy issues multi-byte (16/32/64-bit) loads and stores that are not
aligned, meaning the addresses are 16/32/64-bit multiples. This is
accomplished using two methods:
1) The portable method allocates a uint{16,32,64}_t on the stack, and
std::memcpy()s the bytes into/from the integer. This method relies on
well-defined behaviori (std::memcpy() works on all valid pointers,
fixed-width unsigned integer types use a pure binary representation and
therefore have no invalid values), and should compile to valid code on
all platforms.
2) The fast method reinterpret_casts the address to a pointer to a
uint{16,32,64}_t and dereferences the pointer. This is expected to
compile to one hardware instruction (mov on x86, ldr/str on arm). The
caveat is that the reinterpret_cast is undefined behavior (UB) unless the
address happened to be a valid uint{16,32,64}_t pointer. The UB shows up
as follows.
* On architectures that don't have hardware instructions for unaligned
loads / stores, the pointer access can trigger a hardware exceptions.
This is mitigated by #ifdef blocks that attempt to restrict the fast
method to platforms that support it.
* On architectures that have separate instructions for aligned and
unaligned access, the compiler may need an explicit hint to emit the
hardware instruction for unaligned access. This is accomplished on
Clang and GCC by wrapping the pointers into structs tagged with
__attribute__((__packed__)).
This CL removes the fast method. Fortunately, compilers have advanced
enough that the portable method gets compiled down to the same
instructions as the fast method, without the need for the caveats
explained above. Specifically, modern Clang, GCC and MSVC optimize
std::memcpy() to a single instruction (mov / ldr / str). A test case
proving this can be seen at https://godbolt.org/z/gZg2Fk
PiperOrigin-RevId: 306342728
2020-04-14 00:19:00 +00:00
|
|
|
// Compiles to a single movzx/ldrh on clang/gcc/msvc.
|
|
|
|
uint16_t v;
|
|
|
|
std::memcpy(&v, p, sizeof(v));
|
|
|
|
return v;
|
2011-03-18 17:14:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
inline uint32_t UNALIGNED_LOAD32(const void *p) {
|
Remove platform-dependent code for unaligned loads/stores.
Snappy issues multi-byte (16/32/64-bit) loads and stores that are not
aligned, meaning the addresses are 16/32/64-bit multiples. This is
accomplished using two methods:
1) The portable method allocates a uint{16,32,64}_t on the stack, and
std::memcpy()s the bytes into/from the integer. This method relies on
well-defined behaviori (std::memcpy() works on all valid pointers,
fixed-width unsigned integer types use a pure binary representation and
therefore have no invalid values), and should compile to valid code on
all platforms.
2) The fast method reinterpret_casts the address to a pointer to a
uint{16,32,64}_t and dereferences the pointer. This is expected to
compile to one hardware instruction (mov on x86, ldr/str on arm). The
caveat is that the reinterpret_cast is undefined behavior (UB) unless the
address happened to be a valid uint{16,32,64}_t pointer. The UB shows up
as follows.
* On architectures that don't have hardware instructions for unaligned
loads / stores, the pointer access can trigger a hardware exceptions.
This is mitigated by #ifdef blocks that attempt to restrict the fast
method to platforms that support it.
* On architectures that have separate instructions for aligned and
unaligned access, the compiler may need an explicit hint to emit the
hardware instruction for unaligned access. This is accomplished on
Clang and GCC by wrapping the pointers into structs tagged with
__attribute__((__packed__)).
This CL removes the fast method. Fortunately, compilers have advanced
enough that the portable method gets compiled down to the same
instructions as the fast method, without the need for the caveats
explained above. Specifically, modern Clang, GCC and MSVC optimize
std::memcpy() to a single instruction (mov / ldr / str). A test case
proving this can be seen at https://godbolt.org/z/gZg2Fk
PiperOrigin-RevId: 306342728
2020-04-14 00:19:00 +00:00
|
|
|
// Compiles to a single mov/ldr on clang/gcc/msvc.
|
|
|
|
uint32_t v;
|
|
|
|
std::memcpy(&v, p, sizeof(v));
|
|
|
|
return v;
|
2011-03-18 17:14:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
inline uint64_t UNALIGNED_LOAD64(const void *p) {
|
Remove platform-dependent code for unaligned loads/stores.
Snappy issues multi-byte (16/32/64-bit) loads and stores that are not
aligned, meaning the addresses are 16/32/64-bit multiples. This is
accomplished using two methods:
1) The portable method allocates a uint{16,32,64}_t on the stack, and
std::memcpy()s the bytes into/from the integer. This method relies on
well-defined behaviori (std::memcpy() works on all valid pointers,
fixed-width unsigned integer types use a pure binary representation and
therefore have no invalid values), and should compile to valid code on
all platforms.
2) The fast method reinterpret_casts the address to a pointer to a
uint{16,32,64}_t and dereferences the pointer. This is expected to
compile to one hardware instruction (mov on x86, ldr/str on arm). The
caveat is that the reinterpret_cast is undefined behavior (UB) unless the
address happened to be a valid uint{16,32,64}_t pointer. The UB shows up
as follows.
* On architectures that don't have hardware instructions for unaligned
loads / stores, the pointer access can trigger a hardware exceptions.
This is mitigated by #ifdef blocks that attempt to restrict the fast
method to platforms that support it.
* On architectures that have separate instructions for aligned and
unaligned access, the compiler may need an explicit hint to emit the
hardware instruction for unaligned access. This is accomplished on
Clang and GCC by wrapping the pointers into structs tagged with
__attribute__((__packed__)).
This CL removes the fast method. Fortunately, compilers have advanced
enough that the portable method gets compiled down to the same
instructions as the fast method, without the need for the caveats
explained above. Specifically, modern Clang, GCC and MSVC optimize
std::memcpy() to a single instruction (mov / ldr / str). A test case
proving this can be seen at https://godbolt.org/z/gZg2Fk
PiperOrigin-RevId: 306342728
2020-04-14 00:19:00 +00:00
|
|
|
// Compiles to a single mov/ldr on clang/gcc/msvc.
|
|
|
|
uint64_t v;
|
|
|
|
std::memcpy(&v, p, sizeof(v));
|
|
|
|
return v;
|
2011-03-18 17:14:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
inline void UNALIGNED_STORE16(void *p, uint16_t v) {
|
Remove platform-dependent code for unaligned loads/stores.
Snappy issues multi-byte (16/32/64-bit) loads and stores that are not
aligned, meaning the addresses are 16/32/64-bit multiples. This is
accomplished using two methods:
1) The portable method allocates a uint{16,32,64}_t on the stack, and
std::memcpy()s the bytes into/from the integer. This method relies on
well-defined behaviori (std::memcpy() works on all valid pointers,
fixed-width unsigned integer types use a pure binary representation and
therefore have no invalid values), and should compile to valid code on
all platforms.
2) The fast method reinterpret_casts the address to a pointer to a
uint{16,32,64}_t and dereferences the pointer. This is expected to
compile to one hardware instruction (mov on x86, ldr/str on arm). The
caveat is that the reinterpret_cast is undefined behavior (UB) unless the
address happened to be a valid uint{16,32,64}_t pointer. The UB shows up
as follows.
* On architectures that don't have hardware instructions for unaligned
loads / stores, the pointer access can trigger a hardware exceptions.
This is mitigated by #ifdef blocks that attempt to restrict the fast
method to platforms that support it.
* On architectures that have separate instructions for aligned and
unaligned access, the compiler may need an explicit hint to emit the
hardware instruction for unaligned access. This is accomplished on
Clang and GCC by wrapping the pointers into structs tagged with
__attribute__((__packed__)).
This CL removes the fast method. Fortunately, compilers have advanced
enough that the portable method gets compiled down to the same
instructions as the fast method, without the need for the caveats
explained above. Specifically, modern Clang, GCC and MSVC optimize
std::memcpy() to a single instruction (mov / ldr / str). A test case
proving this can be seen at https://godbolt.org/z/gZg2Fk
PiperOrigin-RevId: 306342728
2020-04-14 00:19:00 +00:00
|
|
|
// Compiles to a single mov/strh on clang/gcc/msvc.
|
|
|
|
std::memcpy(p, &v, sizeof(v));
|
2011-03-18 17:14:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
inline void UNALIGNED_STORE32(void *p, uint32_t v) {
|
Remove platform-dependent code for unaligned loads/stores.
Snappy issues multi-byte (16/32/64-bit) loads and stores that are not
aligned, meaning the addresses are 16/32/64-bit multiples. This is
accomplished using two methods:
1) The portable method allocates a uint{16,32,64}_t on the stack, and
std::memcpy()s the bytes into/from the integer. This method relies on
well-defined behaviori (std::memcpy() works on all valid pointers,
fixed-width unsigned integer types use a pure binary representation and
therefore have no invalid values), and should compile to valid code on
all platforms.
2) The fast method reinterpret_casts the address to a pointer to a
uint{16,32,64}_t and dereferences the pointer. This is expected to
compile to one hardware instruction (mov on x86, ldr/str on arm). The
caveat is that the reinterpret_cast is undefined behavior (UB) unless the
address happened to be a valid uint{16,32,64}_t pointer. The UB shows up
as follows.
* On architectures that don't have hardware instructions for unaligned
loads / stores, the pointer access can trigger a hardware exceptions.
This is mitigated by #ifdef blocks that attempt to restrict the fast
method to platforms that support it.
* On architectures that have separate instructions for aligned and
unaligned access, the compiler may need an explicit hint to emit the
hardware instruction for unaligned access. This is accomplished on
Clang and GCC by wrapping the pointers into structs tagged with
__attribute__((__packed__)).
This CL removes the fast method. Fortunately, compilers have advanced
enough that the portable method gets compiled down to the same
instructions as the fast method, without the need for the caveats
explained above. Specifically, modern Clang, GCC and MSVC optimize
std::memcpy() to a single instruction (mov / ldr / str). A test case
proving this can be seen at https://godbolt.org/z/gZg2Fk
PiperOrigin-RevId: 306342728
2020-04-14 00:19:00 +00:00
|
|
|
// Compiles to a single mov/str on clang/gcc/msvc.
|
|
|
|
std::memcpy(p, &v, sizeof(v));
|
2011-03-18 17:14:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
inline void UNALIGNED_STORE64(void *p, uint64_t v) {
|
Remove platform-dependent code for unaligned loads/stores.
Snappy issues multi-byte (16/32/64-bit) loads and stores that are not
aligned, meaning the addresses are 16/32/64-bit multiples. This is
accomplished using two methods:
1) The portable method allocates a uint{16,32,64}_t on the stack, and
std::memcpy()s the bytes into/from the integer. This method relies on
well-defined behaviori (std::memcpy() works on all valid pointers,
fixed-width unsigned integer types use a pure binary representation and
therefore have no invalid values), and should compile to valid code on
all platforms.
2) The fast method reinterpret_casts the address to a pointer to a
uint{16,32,64}_t and dereferences the pointer. This is expected to
compile to one hardware instruction (mov on x86, ldr/str on arm). The
caveat is that the reinterpret_cast is undefined behavior (UB) unless the
address happened to be a valid uint{16,32,64}_t pointer. The UB shows up
as follows.
* On architectures that don't have hardware instructions for unaligned
loads / stores, the pointer access can trigger a hardware exceptions.
This is mitigated by #ifdef blocks that attempt to restrict the fast
method to platforms that support it.
* On architectures that have separate instructions for aligned and
unaligned access, the compiler may need an explicit hint to emit the
hardware instruction for unaligned access. This is accomplished on
Clang and GCC by wrapping the pointers into structs tagged with
__attribute__((__packed__)).
This CL removes the fast method. Fortunately, compilers have advanced
enough that the portable method gets compiled down to the same
instructions as the fast method, without the need for the caveats
explained above. Specifically, modern Clang, GCC and MSVC optimize
std::memcpy() to a single instruction (mov / ldr / str). A test case
proving this can be seen at https://godbolt.org/z/gZg2Fk
PiperOrigin-RevId: 306342728
2020-04-14 00:19:00 +00:00
|
|
|
// Compiles to a single mov/str on clang/gcc/msvc.
|
|
|
|
std::memcpy(p, &v, sizeof(v));
|
2011-03-18 17:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert to little-endian storage, opposite of network format.
|
|
|
|
// Convert x from host to little endian: x = LittleEndian.FromHost(x);
|
|
|
|
// convert x from little endian to host: x = LittleEndian.ToHost(x);
|
|
|
|
//
|
|
|
|
// Store values into unaligned memory converting to little endian order:
|
|
|
|
// LittleEndian.Store16(p, x);
|
|
|
|
//
|
|
|
|
// Load unaligned values stored in little endian converting to host order:
|
|
|
|
// x = LittleEndian.Load16(p);
|
|
|
|
class LittleEndian {
|
|
|
|
public:
|
2020-04-13 22:30:24 +00:00
|
|
|
// 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);
|
2020-03-25 15:24:14 +00:00
|
|
|
|
2020-04-13 22:30:24 +00:00
|
|
|
// Compiles to a single mov/str on recent clang and gcc.
|
|
|
|
return (static_cast<uint16_t>(buffer[0])) |
|
|
|
|
(static_cast<uint16_t>(buffer[1]) << 8);
|
|
|
|
}
|
2011-03-18 17:14:15 +00:00
|
|
|
|
2020-04-13 22:30:24 +00:00
|
|
|
static inline uint32_t Load32(const void *ptr) {
|
|
|
|
const uint8_t* const buffer = reinterpret_cast<const uint8_t*>(ptr);
|
2011-03-18 17:14:15 +00:00
|
|
|
|
2020-04-13 22:30:24 +00:00
|
|
|
// Compiles to a single mov/str on recent clang and gcc.
|
|
|
|
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);
|
2011-03-18 17:14:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 22:30:24 +00:00
|
|
|
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.
|
|
|
|
return (static_cast<uint64_t>(buffer[0])) |
|
|
|
|
(static_cast<uint64_t>(buffer[1]) << 8) |
|
|
|
|
(static_cast<uint64_t>(buffer[2]) << 16) |
|
|
|
|
(static_cast<uint64_t>(buffer[3]) << 24) |
|
|
|
|
(static_cast<uint64_t>(buffer[4]) << 32) |
|
|
|
|
(static_cast<uint64_t>(buffer[5]) << 40) |
|
|
|
|
(static_cast<uint64_t>(buffer[6]) << 48) |
|
|
|
|
(static_cast<uint64_t>(buffer[7]) << 56);
|
2011-03-18 17:14:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 22:30:24 +00:00
|
|
|
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.
|
|
|
|
buffer[0] = static_cast<uint8_t>(value);
|
|
|
|
buffer[1] = static_cast<uint8_t>(value >> 8);
|
2011-03-18 17:14:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 22:30:24 +00:00
|
|
|
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.
|
|
|
|
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);
|
2011-03-18 17:14:15 +00:00
|
|
|
}
|
2020-03-25 15:24:14 +00:00
|
|
|
|
2020-04-13 22:30:24 +00:00
|
|
|
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.
|
|
|
|
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);
|
|
|
|
buffer[4] = static_cast<uint8_t>(value >> 32);
|
|
|
|
buffer[5] = static_cast<uint8_t>(value >> 40);
|
|
|
|
buffer[6] = static_cast<uint8_t>(value >> 48);
|
|
|
|
buffer[7] = static_cast<uint8_t>(value >> 56);
|
2020-03-25 15:24:14 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 22:30:24 +00:00
|
|
|
static inline constexpr bool IsLittleEndian() {
|
2021-08-16 18:22:31 +00:00
|
|
|
#if SNAPPY_IS_BIG_ENDIAN
|
2020-04-13 22:30:24 +00:00
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
return true;
|
2021-08-16 18:22:31 +00:00
|
|
|
#endif // SNAPPY_IS_BIG_ENDIAN
|
2020-03-25 15:24:14 +00:00
|
|
|
}
|
2011-03-18 17:14:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Some bit-manipulation functions.
|
|
|
|
class Bits {
|
|
|
|
public:
|
2019-01-06 19:48:31 +00:00
|
|
|
// Return floor(log2(n)) for positive integer n.
|
2020-04-12 20:03:50 +00:00
|
|
|
static int Log2FloorNonZero(uint32_t n);
|
2019-01-06 19:48:31 +00:00
|
|
|
|
2011-03-18 17:14:15 +00:00
|
|
|
// Return floor(log2(n)) for positive integer n. Returns -1 iff n == 0.
|
2020-04-12 20:03:50 +00:00
|
|
|
static int Log2Floor(uint32_t n);
|
2011-03-18 17:14:15 +00:00
|
|
|
|
|
|
|
// Return the first set least / most significant bit, 0-indexed. Returns an
|
|
|
|
// undefined value if n == 0. FindLSBSetNonZero() is similar to ffs() except
|
|
|
|
// that it's 0-indexed.
|
2020-04-12 20:03:50 +00:00
|
|
|
static int FindLSBSetNonZero(uint32_t n);
|
2017-08-01 17:01:27 +00:00
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
static int FindLSBSetNonZero64(uint64_t n);
|
2011-03-18 17:14:15 +00:00
|
|
|
|
|
|
|
private:
|
2017-07-26 17:08:17 +00:00
|
|
|
// No copying
|
|
|
|
Bits(const Bits&);
|
|
|
|
void operator=(const Bits&);
|
2011-03-18 17:14:15 +00:00
|
|
|
};
|
|
|
|
|
2021-08-16 18:22:31 +00:00
|
|
|
#if HAVE_BUILTIN_CTZ
|
2011-03-18 17:14:15 +00:00
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
inline int Bits::Log2FloorNonZero(uint32_t n) {
|
2019-01-06 19:48:31 +00:00
|
|
|
assert(n != 0);
|
|
|
|
// (31 ^ x) is equivalent to (31 - x) for x in [0, 31]. An easy proof
|
|
|
|
// represents subtraction in base 2 and observes that there's no carry.
|
|
|
|
//
|
2019-01-06 21:06:38 +00:00
|
|
|
// GCC and Clang represent __builtin_clz on x86 as 31 ^ _bit_scan_reverse(x).
|
2019-01-06 19:48:31 +00:00
|
|
|
// Using "31 ^" here instead of "31 -" allows the optimizer to strip the
|
|
|
|
// function body down to _bit_scan_reverse(x).
|
2019-01-06 21:06:38 +00:00
|
|
|
return 31 ^ __builtin_clz(n);
|
2019-01-06 19:48:31 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
inline int Bits::Log2Floor(uint32_t n) {
|
2019-01-06 21:06:38 +00:00
|
|
|
return (n == 0) ? -1 : Bits::Log2FloorNonZero(n);
|
2011-03-18 17:14:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
inline int Bits::FindLSBSetNonZero(uint32_t n) {
|
2019-01-06 19:48:31 +00:00
|
|
|
assert(n != 0);
|
2011-03-18 17:14:15 +00:00
|
|
|
return __builtin_ctz(n);
|
|
|
|
}
|
|
|
|
|
2017-08-01 17:01:27 +00:00
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
inline int Bits::Log2FloorNonZero(uint32_t n) {
|
2019-01-06 19:48:31 +00:00
|
|
|
assert(n != 0);
|
2020-05-05 20:17:32 +00:00
|
|
|
// NOLINTNEXTLINE(runtime/int): The MSVC intrinsic demands unsigned long.
|
2019-01-06 19:48:31 +00:00
|
|
|
unsigned long where;
|
|
|
|
_BitScanReverse(&where, n);
|
|
|
|
return static_cast<int>(where);
|
|
|
|
}
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
inline int Bits::Log2Floor(uint32_t n) {
|
2020-05-05 20:17:32 +00:00
|
|
|
// NOLINTNEXTLINE(runtime/int): The MSVC intrinsic demands unsigned long.
|
2017-08-01 17:01:27 +00:00
|
|
|
unsigned long where;
|
2019-01-06 19:48:31 +00:00
|
|
|
if (_BitScanReverse(&where, n))
|
|
|
|
return static_cast<int>(where);
|
|
|
|
return -1;
|
2017-08-01 17:01:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
inline int Bits::FindLSBSetNonZero(uint32_t n) {
|
2019-01-06 19:48:31 +00:00
|
|
|
assert(n != 0);
|
2020-05-05 20:17:32 +00:00
|
|
|
// NOLINTNEXTLINE(runtime/int): The MSVC intrinsic demands unsigned long.
|
2017-08-01 17:01:27 +00:00
|
|
|
unsigned long where;
|
2019-01-06 19:48:31 +00:00
|
|
|
if (_BitScanForward(&where, n))
|
|
|
|
return static_cast<int>(where);
|
2017-08-01 17:01:27 +00:00
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
|
2011-03-18 17:14:15 +00:00
|
|
|
#else // Portable versions.
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
inline int Bits::Log2FloorNonZero(uint32_t n) {
|
2019-01-06 19:48:31 +00:00
|
|
|
assert(n != 0);
|
|
|
|
|
2011-03-18 17:14:15 +00:00
|
|
|
int log = 0;
|
2020-04-12 20:03:50 +00:00
|
|
|
uint32_t value = n;
|
2011-03-18 17:14:15 +00:00
|
|
|
for (int i = 4; i >= 0; --i) {
|
|
|
|
int shift = (1 << i);
|
2020-04-12 20:03:50 +00:00
|
|
|
uint32_t x = value >> shift;
|
2011-03-18 17:14:15 +00:00
|
|
|
if (x != 0) {
|
|
|
|
value = x;
|
|
|
|
log += shift;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(value == 1);
|
|
|
|
return log;
|
|
|
|
}
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
inline int Bits::Log2Floor(uint32_t n) {
|
2019-04-25 15:44:08 +00:00
|
|
|
return (n == 0) ? -1 : Bits::Log2FloorNonZero(n);
|
2019-01-06 19:48:31 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
inline int Bits::FindLSBSetNonZero(uint32_t n) {
|
2019-01-06 19:48:31 +00:00
|
|
|
assert(n != 0);
|
|
|
|
|
2011-03-18 17:14:15 +00:00
|
|
|
int rc = 31;
|
|
|
|
for (int i = 4, shift = 1 << 4; i >= 0; --i) {
|
2020-04-12 20:03:50 +00:00
|
|
|
const uint32_t x = n << shift;
|
2011-03-18 17:14:15 +00:00
|
|
|
if (x != 0) {
|
|
|
|
n = x;
|
|
|
|
rc -= shift;
|
|
|
|
}
|
|
|
|
shift >>= 1;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-05-05 20:17:32 +00:00
|
|
|
#endif // End portable versions.
|
|
|
|
|
2021-08-16 18:22:31 +00:00
|
|
|
#if HAVE_BUILTIN_CTZ
|
2020-05-05 20:17:32 +00:00
|
|
|
|
|
|
|
inline int Bits::FindLSBSetNonZero64(uint64_t n) {
|
|
|
|
assert(n != 0);
|
|
|
|
return __builtin_ctzll(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(_MSC_VER) && (defined(_M_X64) || defined(_M_ARM64))
|
|
|
|
// _BitScanForward64() is only available on x64 and ARM64.
|
|
|
|
|
|
|
|
inline int Bits::FindLSBSetNonZero64(uint64_t n) {
|
|
|
|
assert(n != 0);
|
|
|
|
// NOLINTNEXTLINE(runtime/int): The MSVC intrinsic demands unsigned long.
|
|
|
|
unsigned long where;
|
|
|
|
if (_BitScanForward64(&where, n))
|
|
|
|
return static_cast<int>(where);
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // Portable version.
|
|
|
|
|
2011-03-18 17:14:15 +00:00
|
|
|
// FindLSBSetNonZero64() is defined in terms of FindLSBSetNonZero().
|
2020-04-12 20:03:50 +00:00
|
|
|
inline int Bits::FindLSBSetNonZero64(uint64_t n) {
|
2019-01-06 19:48:31 +00:00
|
|
|
assert(n != 0);
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
const uint32_t bottombits = static_cast<uint32_t>(n);
|
2011-03-18 17:14:15 +00:00
|
|
|
if (bottombits == 0) {
|
2020-05-05 20:17:32 +00:00
|
|
|
// Bottom bits are zero, so scan the top bits.
|
2020-04-12 20:03:50 +00:00
|
|
|
return 32 + FindLSBSetNonZero(static_cast<uint32_t>(n >> 32));
|
2011-03-18 17:14:15 +00:00
|
|
|
} else {
|
|
|
|
return FindLSBSetNonZero(bottombits);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-16 18:22:31 +00:00
|
|
|
#endif // HAVE_BUILTIN_CTZ
|
2011-03-18 17:14:15 +00:00
|
|
|
|
|
|
|
// Variable-length integer encoding.
|
|
|
|
class Varint {
|
|
|
|
public:
|
2020-04-12 20:03:50 +00:00
|
|
|
// Maximum lengths of varint encoding of uint32_t.
|
2011-03-18 17:14:15 +00:00
|
|
|
static const int kMax32 = 5;
|
|
|
|
|
|
|
|
// Attempts to parse a varint32 from a prefix of the bytes in [ptr,limit-1].
|
|
|
|
// Never reads a character at or beyond limit. If a valid/terminated varint32
|
|
|
|
// was found in the range, stores it in *OUTPUT and returns a pointer just
|
|
|
|
// past the last byte of the varint32. Else returns NULL. On success,
|
|
|
|
// "result <= limit".
|
|
|
|
static const char* Parse32WithLimit(const char* ptr, const char* limit,
|
2020-04-12 20:03:50 +00:00
|
|
|
uint32_t* OUTPUT);
|
2011-03-18 17:14:15 +00:00
|
|
|
|
|
|
|
// REQUIRES "ptr" points to a buffer of length sufficient to hold "v".
|
|
|
|
// EFFECTS Encodes "v" into "ptr" and returns a pointer to the
|
|
|
|
// byte just past the last encoded byte.
|
2020-04-12 20:03:50 +00:00
|
|
|
static char* Encode32(char* ptr, uint32_t v);
|
2011-03-18 17:14:15 +00:00
|
|
|
|
|
|
|
// EFFECTS Appends the varint representation of "value" to "*s".
|
2020-04-12 20:03:50 +00:00
|
|
|
static void Append32(std::string* s, uint32_t value);
|
2011-03-18 17:14:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
inline const char* Varint::Parse32WithLimit(const char* p,
|
|
|
|
const char* l,
|
2020-04-12 20:03:50 +00:00
|
|
|
uint32_t* OUTPUT) {
|
2011-03-18 17:14:15 +00:00
|
|
|
const unsigned char* ptr = reinterpret_cast<const unsigned char*>(p);
|
|
|
|
const unsigned char* limit = reinterpret_cast<const unsigned char*>(l);
|
2020-04-12 20:03:50 +00:00
|
|
|
uint32_t b, result;
|
2011-03-18 17:14:15 +00:00
|
|
|
if (ptr >= limit) return NULL;
|
|
|
|
b = *(ptr++); result = b & 127; if (b < 128) goto done;
|
|
|
|
if (ptr >= limit) return NULL;
|
|
|
|
b = *(ptr++); result |= (b & 127) << 7; if (b < 128) goto done;
|
|
|
|
if (ptr >= limit) return NULL;
|
|
|
|
b = *(ptr++); result |= (b & 127) << 14; if (b < 128) goto done;
|
|
|
|
if (ptr >= limit) return NULL;
|
|
|
|
b = *(ptr++); result |= (b & 127) << 21; if (b < 128) goto done;
|
|
|
|
if (ptr >= limit) return NULL;
|
|
|
|
b = *(ptr++); result |= (b & 127) << 28; if (b < 16) goto done;
|
|
|
|
return NULL; // Value is too long to be a varint32
|
|
|
|
done:
|
|
|
|
*OUTPUT = result;
|
|
|
|
return reinterpret_cast<const char*>(ptr);
|
|
|
|
}
|
|
|
|
|
2020-04-12 20:03:50 +00:00
|
|
|
inline char* Varint::Encode32(char* sptr, uint32_t v) {
|
2011-03-18 17:14:15 +00:00
|
|
|
// Operate on characters as unsigneds
|
2020-05-05 16:13:04 +00:00
|
|
|
uint8_t* ptr = reinterpret_cast<uint8_t*>(sptr);
|
|
|
|
static const uint8_t B = 128;
|
|
|
|
if (v < (1 << 7)) {
|
|
|
|
*(ptr++) = static_cast<uint8_t>(v);
|
|
|
|
} else if (v < (1 << 14)) {
|
|
|
|
*(ptr++) = static_cast<uint8_t>(v | B);
|
|
|
|
*(ptr++) = static_cast<uint8_t>(v >> 7);
|
|
|
|
} else if (v < (1 << 21)) {
|
|
|
|
*(ptr++) = static_cast<uint8_t>(v | B);
|
|
|
|
*(ptr++) = static_cast<uint8_t>((v >> 7) | B);
|
|
|
|
*(ptr++) = static_cast<uint8_t>(v >> 14);
|
|
|
|
} else if (v < (1 << 28)) {
|
|
|
|
*(ptr++) = static_cast<uint8_t>(v | B);
|
|
|
|
*(ptr++) = static_cast<uint8_t>((v >> 7) | B);
|
|
|
|
*(ptr++) = static_cast<uint8_t>((v >> 14) | B);
|
|
|
|
*(ptr++) = static_cast<uint8_t>(v >> 21);
|
2011-03-18 17:14:15 +00:00
|
|
|
} else {
|
2020-05-05 16:13:04 +00:00
|
|
|
*(ptr++) = static_cast<uint8_t>(v | B);
|
|
|
|
*(ptr++) = static_cast<uint8_t>((v>>7) | B);
|
|
|
|
*(ptr++) = static_cast<uint8_t>((v>>14) | B);
|
|
|
|
*(ptr++) = static_cast<uint8_t>((v>>21) | B);
|
|
|
|
*(ptr++) = static_cast<uint8_t>(v >> 28);
|
2011-03-18 17:14:15 +00:00
|
|
|
}
|
|
|
|
return reinterpret_cast<char*>(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If you know the internal layout of the std::string in use, you can
|
|
|
|
// replace this function with one that resizes the string without
|
|
|
|
// filling the new space with zeros (if applicable) --
|
|
|
|
// it will be non-portable but faster.
|
2019-09-26 17:47:14 +00:00
|
|
|
inline void STLStringResizeUninitialized(std::string* s, size_t new_size) {
|
2011-03-18 17:14:15 +00:00
|
|
|
s->resize(new_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a mutable char* pointing to a string's internal buffer,
|
|
|
|
// which may not be null-terminated. Writing through this pointer will
|
|
|
|
// modify the string.
|
|
|
|
//
|
|
|
|
// string_as_array(&str)[i] is valid for 0 <= i < str.size() until the
|
|
|
|
// next call to a string method that invalidates iterators.
|
|
|
|
//
|
|
|
|
// As of 2006-04, there is no standard-blessed way of getting a
|
|
|
|
// mutable reference to a string's internal buffer. However, issue 530
|
|
|
|
// (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-defects.html#530)
|
|
|
|
// proposes this as the method. It will officially be part of the standard
|
|
|
|
// for C++0x. This should already work on all current implementations.
|
2019-09-26 17:47:14 +00:00
|
|
|
inline char* string_as_array(std::string* str) {
|
2011-03-18 17:14:15 +00:00
|
|
|
return str->empty() ? NULL : &*str->begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace snappy
|
|
|
|
|
2015-06-22 13:39:08 +00:00
|
|
|
#endif // THIRD_PARTY_SNAPPY_OPENSOURCE_SNAPPY_STUBS_INTERNAL_H_
|