Tweak size handling in Endian
authorYedidya Feldblum <yfeldblum@fb.com>
Fri, 3 Mar 2017 05:32:09 +0000 (21:32 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Fri, 3 Mar 2017 05:34:58 +0000 (21:34 -0800)
Summary:
[Folly] Tweak size handling in `Endian`.

Either multiply by 8 in one place, or divide by 8 in another place. Go with the latter, because `sizeof` in C++ emits byte sizes, not bit sizes.

Reviewed By: simpkins

Differential Revision: D4645020

fbshipit-source-id: cb78600ba4c20bebc66aed506d4b5d6c378fc998

folly/Bits.h

index 6c3c30ece1d027a5fd6018dedfbe270ca260b1f5..b818655e2a347c6d301a7723fa0bf823c0ed1970 100644 (file)
@@ -241,7 +241,7 @@ struct uint_types_by_size;
     return fn(v);                                           \
   }                                                         \
   template <>                                               \
     return fn(v);                                           \
   }                                                         \
   template <>                                               \
-  struct uint_types_by_size<sz> {                           \
+  struct uint_types_by_size<sz / 8> {                       \
     using type = uint##sz##_t;                              \
   };
 
     using type = uint##sz##_t;                              \
   };
 
@@ -268,7 +268,7 @@ struct EndianInt {
     // we implement this with memcpy because that is defined behavior in C++
     // we rely on compilers to optimize away the memcpy calls
     constexpr auto s = sizeof(T);
     // we implement this with memcpy because that is defined behavior in C++
     // we rely on compilers to optimize away the memcpy calls
     constexpr auto s = sizeof(T);
-    using B = typename uint_types_by_size<8 * s>::type;
+    using B = typename uint_types_by_size<s>::type;
     B b;
     std::memcpy(&b, &x, s);
     b = byteswap_gen(b);
     B b;
     std::memcpy(&b, &x, s);
     b = byteswap_gen(b);