Compute masks in IPAddressV4
[folly.git] / folly / IPAddressV4.cpp
index cd81d6d979f656fea67459e48e17c5ed83cc7cae..bcd582df89280ea5dd9d09298a36359561e8a60a 100644 (file)
@@ -273,8 +273,10 @@ const ByteArray4 IPAddressV4::fetchMask(size_t numBits) {
     throw IPAddressFormatException(
         to<std::string>("IPv4 addresses are 32 bits"));
   }
-  // masks_ is backed by an array so is zero indexed
-  return masks_[numBits];
+  auto const val = Endian::big(uint32_t(~uint64_t(0) << (32 - numBits)));
+  ByteArray4 arr;
+  std::memcpy(arr.data(), &val, sizeof(val));
+  return arr;
 }
 // public static
 CIDRNetworkV4 IPAddressV4::longestCommonPrefix(
@@ -285,41 +287,4 @@ CIDRNetworkV4 IPAddressV4::longestCommonPrefix(
   return {IPAddressV4(prefix.first), prefix.second};
 }
 
-// static private
-const std::array<ByteArray4, 33> IPAddressV4::masks_ = {{
-  {{0x00, 0x00, 0x00, 0x00}},
-  {{0x80, 0x00, 0x00, 0x00}},
-  {{0xc0, 0x00, 0x00, 0x00}},
-  {{0xe0, 0x00, 0x00, 0x00}},
-  {{0xf0, 0x00, 0x00, 0x00}},
-  {{0xf8, 0x00, 0x00, 0x00}},
-  {{0xfc, 0x00, 0x00, 0x00}},
-  {{0xfe, 0x00, 0x00, 0x00}},
-  {{0xff, 0x00, 0x00, 0x00}},
-  {{0xff, 0x80, 0x00, 0x00}},
-  {{0xff, 0xc0, 0x00, 0x00}},
-  {{0xff, 0xe0, 0x00, 0x00}},
-  {{0xff, 0xf0, 0x00, 0x00}},
-  {{0xff, 0xf8, 0x00, 0x00}},
-  {{0xff, 0xfc, 0x00, 0x00}},
-  {{0xff, 0xfe, 0x00, 0x00}},
-  {{0xff, 0xff, 0x00, 0x00}},
-  {{0xff, 0xff, 0x80, 0x00}},
-  {{0xff, 0xff, 0xc0, 0x00}},
-  {{0xff, 0xff, 0xe0, 0x00}},
-  {{0xff, 0xff, 0xf0, 0x00}},
-  {{0xff, 0xff, 0xf8, 0x00}},
-  {{0xff, 0xff, 0xfc, 0x00}},
-  {{0xff, 0xff, 0xfe, 0x00}},
-  {{0xff, 0xff, 0xff, 0x00}},
-  {{0xff, 0xff, 0xff, 0x80}},
-  {{0xff, 0xff, 0xff, 0xc0}},
-  {{0xff, 0xff, 0xff, 0xe0}},
-  {{0xff, 0xff, 0xff, 0xf0}},
-  {{0xff, 0xff, 0xff, 0xf8}},
-  {{0xff, 0xff, 0xff, 0xfc}},
-  {{0xff, 0xff, 0xff, 0xfe}},
-  {{0xff, 0xff, 0xff, 0xff}}
-}};
-
 } // folly