Skip to content

Commit

Permalink
Upgrade protozero to version 1.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Jun 30, 2017
1 parent bd845ac commit 3ba45c7
Show file tree
Hide file tree
Showing 13 changed files with 1,314 additions and 717 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,4 +1,9 @@
## 1.19.3

* Upgrade protozero to version 1.5.2

## 1.19.2

* Ignore UTF-8 byte order mark if present

## 1.19.1
Expand Down
81 changes: 47 additions & 34 deletions protozero/byteswap.hpp
Expand Up @@ -16,56 +16,69 @@ documentation.
* @brief Contains functions to swap bytes in values (for different endianness).
*/

#include <cstdint>
#include <cassert>
#include <cstdint>

#include <protozero/config.hpp>

namespace protozero {
namespace detail {

/**
* Swap N byte value between endianness formats. This template function must
* be specialized to actually work.
*/
template <int N>
inline void byteswap(const char* /*data*/, char* /*result*/) {
static_assert(N == 1, "Can only swap 4 or 8 byte values");
}

/**
* Swap 4 byte value (int32_t, uint32_t, float) between endianness formats.
*/
template <>
inline void byteswap<4>(const char* data, char* result) {
inline uint32_t byteswap_impl(uint32_t value) noexcept {
#ifdef PROTOZERO_USE_BUILTIN_BSWAP
*reinterpret_cast<uint32_t*>(result) = __builtin_bswap32(*reinterpret_cast<const uint32_t*>(data));
return __builtin_bswap32(value);
#else
result[3] = data[0];
result[2] = data[1];
result[1] = data[2];
result[0] = data[3];
return ((value & 0xff000000) >> 24) |
((value & 0x00ff0000) >> 8) |
((value & 0x0000ff00) << 8) |
((value & 0x000000ff) << 24);
#endif
}

/**
* Swap 8 byte value (int64_t, uint64_t, double) between endianness formats.
*/
template <>
inline void byteswap<8>(const char* data, char* result) {
inline uint64_t byteswap_impl(uint64_t value) noexcept {
#ifdef PROTOZERO_USE_BUILTIN_BSWAP
*reinterpret_cast<uint64_t*>(result) = __builtin_bswap64(*reinterpret_cast<const uint64_t*>(data));
return __builtin_bswap64(value);
#else
result[7] = data[0];
result[6] = data[1];
result[5] = data[2];
result[4] = data[3];
result[3] = data[4];
result[2] = data[5];
result[1] = data[6];
result[0] = data[7];
return ((value & 0xff00000000000000ULL) >> 56) |
((value & 0x00ff000000000000ULL) >> 40) |
((value & 0x0000ff0000000000ULL) >> 24) |
((value & 0x000000ff00000000ULL) >> 8) |
((value & 0x00000000ff000000ULL) << 8) |
((value & 0x0000000000ff0000ULL) << 24) |
((value & 0x000000000000ff00ULL) << 40) |
((value & 0x00000000000000ffULL) << 56);
#endif
}

inline void byteswap_inplace(uint32_t* ptr) noexcept {
*ptr = byteswap_impl(*ptr);
}

inline void byteswap_inplace(uint64_t* ptr) noexcept {
*ptr = byteswap_impl(*ptr);
}

inline void byteswap_inplace(int32_t* ptr) noexcept {
auto bptr = reinterpret_cast<uint32_t*>(ptr);
*bptr = byteswap_impl(*bptr);
}

inline void byteswap_inplace(int64_t* ptr) noexcept {
auto bptr = reinterpret_cast<uint64_t*>(ptr);
*bptr = byteswap_impl(*bptr);
}

inline void byteswap_inplace(float* ptr) noexcept {
auto bptr = reinterpret_cast<uint32_t*>(ptr);
*bptr = byteswap_impl(*bptr);
}

inline void byteswap_inplace(double* ptr) noexcept {
auto bptr = reinterpret_cast<uint64_t*>(ptr);
*bptr = byteswap_impl(*bptr);
}

} // end namespace detail
} // end namespace protozero

#endif // PROTOZERO_BYTESWAP_HPP
11 changes: 0 additions & 11 deletions protozero/config.hpp
Expand Up @@ -35,17 +35,6 @@ documentation.
# define PROTOZERO_BYTE_ORDER PROTOZERO_LITTLE_ENDIAN
#endif

// On some ARM machines and depending on compiler settings access to unaligned
// floating point values will result in a SIGBUS. Do not use the bare pointers
// in this case.
#if PROTOZERO_BYTE_ORDER == PROTOZERO_LITTLE_ENDIAN
# if !defined(__arm__) && !defined(_M_ARM)
# ifndef PROTOZERO_DO_NOT_USE_BARE_POINTER
# define PROTOZERO_USE_BARE_POINTER_FOR_PACKED_FIXED
# endif
# endif
#endif

// Check whether __builtin_bswap is available
#if defined(__GNUC__) || defined(__clang__)
# define PROTOZERO_USE_BUILTIN_BSWAP
Expand Down
8 changes: 4 additions & 4 deletions protozero/exception.hpp
Expand Up @@ -29,7 +29,7 @@ namespace protozero {
*/
struct exception : std::exception {
/// Returns the explanatory string.
const char *what() const noexcept override { return "pbf exception"; }
const char* what() const noexcept override { return "pbf exception"; }
};

/**
Expand All @@ -38,7 +38,7 @@ struct exception : std::exception {
*/
struct varint_too_long_exception : exception {
/// Returns the explanatory string.
const char *what() const noexcept override { return "varint too long exception"; }
const char* what() const noexcept override { return "varint too long exception"; }
};

/**
Expand All @@ -47,7 +47,7 @@ struct varint_too_long_exception : exception {
*/
struct unknown_pbf_wire_type_exception : exception {
/// Returns the explanatory string.
const char *what() const noexcept override { return "unknown pbf field type exception"; }
const char* what() const noexcept override { return "unknown pbf field type exception"; }
};

/**
Expand All @@ -60,7 +60,7 @@ struct unknown_pbf_wire_type_exception : exception {
*/
struct end_of_buffer_exception : exception {
/// Returns the explanatory string.
const char *what() const noexcept override { return "end of buffer exception"; }
const char* what() const noexcept override { return "end of buffer exception"; }
};

} // end namespace protozero
Expand Down

0 comments on commit 3ba45c7

Please sign in to comment.