Commit 9ab15578 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Compute masks in IPAddressV4

Summary: [Folly] Compute masks in `IPAddressV4`. Just like in `IPAddressV6`.

Reviewed By: WillerZ

Differential Revision: D5524197

fbshipit-source-id: ebeeab28304bff4f6150cf76216d170908e62aa4
parent efc4ca1e
...@@ -273,8 +273,10 @@ const ByteArray4 IPAddressV4::fetchMask(size_t numBits) { ...@@ -273,8 +273,10 @@ const ByteArray4 IPAddressV4::fetchMask(size_t numBits) {
throw IPAddressFormatException( throw IPAddressFormatException(
to<std::string>("IPv4 addresses are 32 bits")); to<std::string>("IPv4 addresses are 32 bits"));
} }
// masks_ is backed by an array so is zero indexed auto const val = Endian::big(uint32_t(~uint64_t(0) << (32 - numBits)));
return masks_[numBits]; ByteArray4 arr;
std::memcpy(arr.data(), &val, sizeof(val));
return arr;
} }
// public static // public static
CIDRNetworkV4 IPAddressV4::longestCommonPrefix( CIDRNetworkV4 IPAddressV4::longestCommonPrefix(
...@@ -285,41 +287,4 @@ CIDRNetworkV4 IPAddressV4::longestCommonPrefix( ...@@ -285,41 +287,4 @@ CIDRNetworkV4 IPAddressV4::longestCommonPrefix(
return {IPAddressV4(prefix.first), prefix.second}; 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 } // folly
...@@ -269,8 +269,6 @@ class IPAddressV4 { ...@@ -269,8 +269,6 @@ class IPAddressV4 {
explicit AddressStorage(const in_addr addr): inAddr_(addr) {} explicit AddressStorage(const in_addr addr): inAddr_(addr) {}
} addr_; } addr_;
static const std::array<ByteArray4, 33> masks_;
/** /**
* Set the current IPAddressV4 object to have the address specified by bytes. * Set the current IPAddressV4 object to have the address specified by bytes.
* @throws IPAddressFormatException if bytes.size() is not 4. * @throws IPAddressFormatException if bytes.size() is not 4.
......
...@@ -1272,6 +1272,28 @@ INSTANTIATE_TEST_CASE_P(IPAddress, ...@@ -1272,6 +1272,28 @@ INSTANTIATE_TEST_CASE_P(IPAddress,
IPAddressBitAccessorTest, IPAddressBitAccessorTest,
::testing::ValuesIn(validAddressProvider)); ::testing::ValuesIn(validAddressProvider));
TEST(IPAddressV4, fetchMask) {
struct X : private IPAddressV4 {
using IPAddressV4::fetchMask;
};
EXPECT_THAT(
X::fetchMask(0),
::testing::ElementsAreArray(ByteArray4{{0x00, 0x00, 0x00, 0x00}}));
EXPECT_THAT(
X::fetchMask(1),
::testing::ElementsAreArray(ByteArray4{{0x80, 0x00, 0x00, 0x00}}));
EXPECT_THAT(
X::fetchMask(31),
::testing::ElementsAreArray(ByteArray4{{0xff, 0xff, 0xff, 0xfe}}));
EXPECT_THAT(
X::fetchMask(32),
::testing::ElementsAreArray(ByteArray4{{0xff, 0xff, 0xff, 0xff}}));
}
TEST(IPAddressV6, fetchMask) { TEST(IPAddressV6, fetchMask) {
using ByteArray8 = std::array<uint8_t, 8>; using ByteArray8 = std::array<uint8_t, 8>;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment