logging: fix compilation error on older C++ compilers
[folly.git] / folly / GroupVarint.h
index f91191cea139921e4ae5dd779329d9d0a6aaf621..ed7dae63192456a486232054511aaef2949ea1df 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 #pragma once
 
+#include <cstdint>
+#include <limits>
+
+#include <glog/logging.h>
+
 #if !defined(__GNUC__) && !defined(_MSC_VER)
 #error GroupVarint.h requires GCC or MSVC
 #endif
 
 #include <folly/Portability.h>
 
-#if FOLLY_X64 || defined(__i386__) || FOLLY_PPC64
+#if FOLLY_X64 || defined(__i386__) || FOLLY_PPC64 || FOLLY_AARCH64
 #define HAVE_GROUP_VARINT 1
 
-#include <cstdint>
-#include <limits>
-#include <folly/detail/GroupVarintDetail.h>
 #include <folly/Bits.h>
 #include <folly/Range.h>
-#include <glog/logging.h>
+#include <folly/detail/GroupVarintDetail.h>
+#include <folly/portability/Builtins.h>
 
 #if FOLLY_SSE >= 3
-#include <x86intrin.h>
+#include <nmmintrin.h>
 namespace folly {
 namespace detail {
-extern const __m128i groupVarintSSEMasks[];
-}  // namespace detail
-}  // namespace folly
+alignas(16) extern const uint64_t groupVarintSSEMasks[];
+} // namespace detail
+} // namespace folly
 #endif
 
 namespace folly {
 namespace detail {
 extern const uint8_t groupVarintLengths[];
-}  // namespace detail
-}  // namespace folly
+} // namespace detail
+} // namespace folly
 
 namespace folly {
 
@@ -101,16 +104,24 @@ class GroupVarint<uint32_t> : public detail::GroupVarintBase<uint32_t> {
    * buffer of size bytes.
    */
   static size_t partialCount(const char* p, size_t size) {
-    char v = *p;
+    uint8_t v = uint8_t(*p);
     size_t s = kHeaderSize;
     s += 1 + b0key(v);
-    if (s > size) return 0;
+    if (s > size) {
+      return 0;
+    }
     s += 1 + b1key(v);
-    if (s > size) return 1;
+    if (s > size) {
+      return 1;
+    }
     s += 1 + b2key(v);
-    if (s > size) return 2;
+    if (s > size) {
+      return 2;
+    }
     s += 1 + b3key(v);
-    if (s > size) return 3;
+    if (s > size) {
+      return 3;
+    }
     return 4;
   }
 
@@ -119,8 +130,9 @@ class GroupVarint<uint32_t> : public detail::GroupVarintBase<uint32_t> {
    * return the number of bytes used by the encoding.
    */
   static size_t encodedSize(const char* p) {
-    return (kHeaderSize + kGroupSize +
-            b0key(*p) + b1key(*p) + b2key(*p) + b3key(*p));
+    return kHeaderSize + kGroupSize +
+           b0key(uint8_t(*p)) + b1key(uint8_t(*p)) +
+           b2key(uint8_t(*p)) + b3key(uint8_t(*p));
   }
 
   /**
@@ -193,9 +205,10 @@ class GroupVarint<uint32_t> : public detail::GroupVarintBase<uint32_t> {
    * that we must be able to read at least 17 bytes from the input pointer, p.
    */
   static const char* decode(const char* p, uint32_t* dest) {
-    uint8_t key = p[0];
+    uint8_t key = uint8_t(p[0]);
     __m128i val = _mm_loadu_si128((const __m128i*)(p+1));
-    __m128i mask = detail::groupVarintSSEMasks[key];
+    __m128i mask =
+        _mm_load_si128((const __m128i*)&detail::groupVarintSSEMasks[key * 2]);
     __m128i r = _mm_shuffle_epi8(val, mask);
     _mm_storeu_si128((__m128i*)dest, r);
     return p + detail::groupVarintLengths[key];
@@ -207,17 +220,18 @@ class GroupVarint<uint32_t> : public detail::GroupVarintBase<uint32_t> {
    */
   static const char* decode(const char* p, uint32_t* a, uint32_t* b,
                             uint32_t* c, uint32_t* d) {
-    uint8_t key = p[0];
+    uint8_t key = uint8_t(p[0]);
     __m128i val = _mm_loadu_si128((const __m128i*)(p+1));
-    __m128i mask = detail::groupVarintSSEMasks[key];
+    __m128i mask =
+        _mm_load_si128((const __m128i*)&detail::groupVarintSSEMasks[key * 2]);
     __m128i r = _mm_shuffle_epi8(val, mask);
 
     // Extracting 32 bits at a time out of an XMM register is a SSE4 feature
 #if FOLLY_SSE >= 4
-    *a = _mm_extract_epi32(r, 0);
-    *b = _mm_extract_epi32(r, 1);
-    *c = _mm_extract_epi32(r, 2);
-    *d = _mm_extract_epi32(r, 3);
+    *a = uint32_t(_mm_extract_epi32(r, 0));
+    *b = uint32_t(_mm_extract_epi32(r, 1));
+    *c = uint32_t(_mm_extract_epi32(r, 2));
+    *d = uint32_t(_mm_extract_epi32(r, 3));
 #else  /* !__SSE4__ */
     *a = _mm_extract_epi16(r, 0) + (_mm_extract_epi16(r, 1) << 16);
     *b = _mm_extract_epi16(r, 2) + (_mm_extract_epi16(r, 3) << 16);
@@ -242,7 +256,7 @@ class GroupVarint<uint32_t> : public detail::GroupVarintBase<uint32_t> {
  private:
   static uint8_t key(uint32_t x) {
     // __builtin_clz is undefined for the x==0 case
-    return 3 - (__builtin_clz(x|1) / 8);
+    return uint8_t(3 - (__builtin_clz(x | 1) / 8));
   }
   static size_t b0key(size_t x) { return x & 3; }
   static size_t b1key(size_t x) { return (x >> 2) & 3; }
@@ -271,8 +285,8 @@ class GroupVarint<uint64_t> : public detail::GroupVarintBase<uint64_t> {
    */
   static size_t size(uint64_t a, uint64_t b, uint64_t c, uint64_t d,
                      uint64_t e) {
-    return (kHeaderSize + kGroupSize +
-            key(a) + key(b) + key(c) + key(d) + key(e));
+    return kHeaderSize + kGroupSize +
+           key(a) + key(b) + key(c) + key(d) + key(e);
   }
 
   /**
@@ -306,15 +320,25 @@ class GroupVarint<uint64_t> : public detail::GroupVarintBase<uint64_t> {
     uint16_t v = loadUnaligned<uint16_t>(p);
     size_t s = kHeaderSize;
     s += 1 + b0key(v);
-    if (s > size) return 0;
+    if (s > size) {
+      return 0;
+    }
     s += 1 + b1key(v);
-    if (s > size) return 1;
+    if (s > size) {
+      return 1;
+    }
     s += 1 + b2key(v);
-    if (s > size) return 2;
+    if (s > size) {
+      return 2;
+    }
     s += 1 + b3key(v);
-    if (s > size) return 3;
+    if (s > size) {
+      return 3;
+    }
     s += 1 + b4key(v);
-    if (s > size) return 4;
+    if (s > size) {
+      return 4;
+    }
     return 5;
   }
 
@@ -324,8 +348,8 @@ class GroupVarint<uint64_t> : public detail::GroupVarintBase<uint64_t> {
    */
   static size_t encodedSize(const char* p) {
     uint16_t n = loadUnaligned<uint16_t>(p);
-    return (kHeaderSize + kGroupSize +
-            b0key(n) + b1key(n) + b2key(n) + b3key(n) + b4key(n));
+    return kHeaderSize + kGroupSize +
+            b0key(n) + b1key(n) + b2key(n) + b3key(n) + b4key(n);
   }
 
   /**
@@ -335,14 +359,19 @@ class GroupVarint<uint64_t> : public detail::GroupVarintBase<uint64_t> {
    */
   static char* encode(char* p, uint64_t a, uint64_t b, uint64_t c,
                       uint64_t d, uint64_t e) {
-    uint8_t b0key = key(a);
-    uint8_t b1key = key(b);
-    uint8_t b2key = key(c);
-    uint8_t b3key = key(d);
-    uint8_t b4key = key(e);
+    uint16_t b0key = key(a);
+    uint16_t b1key = key(b);
+    uint16_t b2key = key(c);
+    uint16_t b3key = key(d);
+    uint16_t b4key = key(e);
     storeUnaligned<uint16_t>(
         p,
-        (b4key << 12) | (b3key << 9) | (b2key << 6) | (b1key << 3) | b0key);
+        uint16_t(
+            (b4key << 12) |
+            (b3key << 9) |
+            (b2key << 6) |
+            (b1key << 3) |
+            b0key));
     p += 2;
     storeUnaligned(p, a);
     p += b0key+1;
@@ -406,14 +435,14 @@ class GroupVarint<uint64_t> : public detail::GroupVarintBase<uint64_t> {
 
   static uint8_t key(uint64_t x) {
     // __builtin_clzll is undefined for the x==0 case
-    return 7 - (__builtin_clzll(x|1) / 8);
+    return uint8_t(7 - (__builtin_clzll(x | 1) / 8));
   }
 
-  static uint8_t b0key(uint16_t x) { return x & 7; }
-  static uint8_t b1key(uint16_t x) { return (x >> 3) & 7; }
-  static uint8_t b2key(uint16_t x) { return (x >> 6) & 7; }
-  static uint8_t b3key(uint16_t x) { return (x >> 9) & 7; }
-  static uint8_t b4key(uint16_t x) { return (x >> 12) & 7; }
+  static uint8_t b0key(uint16_t x) { return x & 7u; }
+  static uint8_t b1key(uint16_t x) { return (x >> 3) & 7u; }
+  static uint8_t b2key(uint16_t x) { return (x >> 6) & 7u; }
+  static uint8_t b3key(uint16_t x) { return (x >> 9) & 7u; }
+  static uint8_t b4key(uint16_t x) { return (x >> 12) & 7u; }
 
   static const uint64_t kMask[];
 };
@@ -540,7 +569,7 @@ class GroupVarintDecoder {
   bool next(type* val) {
     if (pos_ == count_) {
       // refill
-      size_t rem = end_ - p_;
+      size_t rem = size_t(end_ - p_);
       if (rem == 0 || remaining_ == 0) {
         return false;
       }
@@ -572,7 +601,7 @@ class GroupVarintDecoder {
         }
       } else {
         // Can't decode a full group
-        count_ = Base::partialCount(p_, end_ - p_);
+        count_ = Base::partialCount(p_, size_t(end_ - p_));
         if (remaining_ >= count_) {
           remaining_ -= count_;
           p_ = end_;
@@ -595,7 +624,7 @@ class GroupVarintDecoder {
     CHECK(pos_ == count_ && (p_ == end_ || remaining_ == 0));
     // p_ may point to the internal buffer (tmp_), but we want
     // to return subpiece of the original data
-    size_t size = end_ - p_;
+    size_t size = size_t(end_ - p_);
     return StringPiece(rrest_ - size, rrest_);
   }
 
@@ -614,6 +643,6 @@ class GroupVarintDecoder {
 typedef GroupVarintDecoder<uint32_t> GroupVarint32Decoder;
 typedef GroupVarintDecoder<uint64_t> GroupVarint64Decoder;
 
-}  // namespace folly
+} // namespace folly
 
 #endif /* FOLLY_X64 || defined(__i386__) || FOLLY_PPC64 */