diff options
author | Billy Donahue <billy.donahue@mongodb.com> | 2019-10-02 14:19:43 +0000 |
---|---|---|
committer | evergreen <evergreen@mongodb.com> | 2019-10-02 14:19:43 +0000 |
commit | 3175a30264d26b31309e9a4abfb69d9f14136702 (patch) | |
tree | 8864b8f41072071eb4603323cdbf1c76ba54a618 /src/mongo/db/geo | |
parent | dadd0a797c334219b5980e7ac2dc9e38288bdc38 (diff) | |
download | mongo-3175a30264d26b31309e9a4abfb69d9f14136702.tar.gz |
SERVER-43175 platform/endian.h refresh
This is a very low-level bare metal header, and should ideally
#include only std headers.
- Remove Decimal128 dependency from endian.h. Decimal128 doesn't
need endian conversions, and makes endian.h transitively include
several higher-level mongo specifics. Current conversions are
underspecified and mathematically incorrect or at least ambiguous.
They swap the order within each of the low64 and high64 fields,
but don't swap them with each other. This is behavior needed only
by one spot in db/pipeline/value.cpp to deserialize NumberDecimal,
so we can just inline the behavior there and remove it from
endian.h.
- Remove MONGO_CONFIG_BYTE_ORDER from config.h. The running compiler
holds the ultimate truth on what the target endianness is
(available via _BYTE_ORDER_). We should not read it from a config
header. The names exported into C++ code for the endian
possibilities can be changed to line up with those in C++20's
std::endian {big,little,native} enum. This eliminates the
Scons<=>C++ bridge protocol of "1234" and "4321" magic numbers.
Scons will talk to the compiler, not directly to the code.
- Use enum expressions (including if constexpr) rather than #if for
branching on endianness. This makes bit-rot of unexecuted paths
less likely, and is just cleaner C++.
- Remove bswap_slow variants. All supported compilers have builtin
bswap operations. Can reduce to a simple MSVC vs GCC branching.
All compilers have a builtin, so remove the bswap_slow"
implementation.
- Don't need all the push_macro / pop_macro stuff, or really any
macros at all. Just rely on inline C++ functions. These optimize to
the same thing.
- Don't need ByteOrderConverter or IntegralTypeMap traits either.
Simpler metaprogramming based only on sizeof will work fine.
All in all we can remove about 400 lines of old code here and
shave some low-level edges off of the dependency graph.
- benchmark
Diffstat (limited to 'src/mongo/db/geo')
-rw-r--r-- | src/mongo/db/geo/hash.cpp | 59 |
1 files changed, 28 insertions, 31 deletions
diff --git a/src/mongo/db/geo/hash.cpp b/src/mongo/db/geo/hash.cpp index 6d870ece872..41c6801bf63 100644 --- a/src/mongo/db/geo/hash.cpp +++ b/src/mongo/db/geo/hash.cpp @@ -41,6 +41,8 @@ namespace mongo { using std::stringstream; +static constexpr bool kNativeLittle = (endian::Order::kNative == endian::Order::kLittle); + std::ostream& operator<<(std::ostream& s, const GeoHash& h) { return s << h.toString(); } @@ -237,12 +239,7 @@ void GeoHash::unhash_fast(unsigned* x, unsigned* y) const { // 0x55 in binary is "01010101", // it's an odd bitmask that we use to turn off all the even bits unsigned t = (unsigned)(c[i]) & 0x55; - int leftShift; -#if MONGO_CONFIG_BYTE_ORDER == MONGO_LITTLE_ENDIAN - leftShift = 4 * i; -#else - leftShift = 28 - (4 * i); -#endif + const int leftShift = 4 * (kNativeLittle ? i : (7 - i)); *y |= geoBitSets.hashedToNormal[t] << leftShift; t = ((unsigned)(c[i]) >> 1) & 0x55; @@ -262,11 +259,11 @@ void GeoHash::unhash_slow(unsigned* x, unsigned* y) const { } void GeoHash::unhash(unsigned* x, unsigned* y) const { -#if MONGO_CONFIG_BYTE_ORDER == MONGO_LITTLE_ENDIAN - unhash_fast(x, y); -#else - unhash_slow(x, y); -#endif + if constexpr (kNativeLittle) { + unhash_fast(x, y); + } else { + unhash_slow(x, y); + } } /** Is the 'bit'-th most significant bit set? (NOT the least significant) */ @@ -485,31 +482,31 @@ void GeoHash::clearUnusedBits() { static void appendHashToBuilder(long long hash, BSONObjBuilder* builder, const char* fieldName) { char buf[8]; -#if MONGO_CONFIG_BYTE_ORDER == MONGO_LITTLE_ENDIAN - // Reverse the order of bytes when copying between BinData and GeoHash. - // GeoHashes are meant to be compared from MSB to LSB, where the first 2 MSB indicate the - // quadrant. - // In BinData, the GeoHash of a 2D index is compared from LSB to MSB, so the bytes should be - // reversed on little-endian systems - copyAndReverse(buf, (char*)&hash); -#else - std::memcpy(buf, reinterpret_cast<char*>(&hash), 8); -#endif + if constexpr (kNativeLittle) { + // Reverse the order of bytes when copying between BinData and GeoHash. + // GeoHashes are meant to be compared from MSB to LSB, where the first 2 MSB indicate the + // quadrant. + // In BinData, the GeoHash of a 2D index is compared from LSB to MSB, so the bytes should be + // reversed on little-endian systems + copyAndReverse(buf, (char*)&hash); + } else { + std::memcpy(buf, reinterpret_cast<char*>(&hash), 8); + } builder->appendBinData(fieldName, 8, bdtCustom, buf); } static void appendHashToKeyString(long long hash, KeyString::Builder* ks) { char buf[8]; -#if MONGO_CONFIG_BYTE_ORDER == MONGO_LITTLE_ENDIAN - // Reverse the order of bytes when copying between BinData and GeoHash. - // GeoHashes are meant to be compared from MSB to LSB, where the first 2 MSB indicate the - // quadrant. - // In BinData, the GeoHash of a 2D index is compared from LSB to MSB, so the bytes should be - // reversed on little-endian systems - copyAndReverse(buf, (char*)&hash); -#else - std::memcpy(buf, reinterpret_cast<char*>(&hash), 8); -#endif + if constexpr (kNativeLittle) { + // Reverse the order of bytes when copying between BinData and GeoHash. + // GeoHashes are meant to be compared from MSB to LSB, where the first 2 MSB indicate the + // quadrant. + // In BinData, the GeoHash of a 2D index is compared from LSB to MSB, so the bytes should be + // reversed on little-endian systems + copyAndReverse(buf, (char*)&hash); + } else { + std::memcpy(buf, reinterpret_cast<char*>(&hash), 8); + } ks->appendBinData(BSONBinData(buf, 8, bdtCustom)); } |