Skip to content

Commit

Permalink
intarith: kill P2* macros
Browse files Browse the repository at this point in the history
These macros are originally introduced in ceph#10128
and get replaced by ceph#19913.

Signed-off-by: xie xingguo <xie.xingguo@zte.com.cn>
  • Loading branch information
xiexingguo authored and votdev committed Apr 3, 2018
1 parent c237107 commit 75510f7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
34 changes: 12 additions & 22 deletions src/include/intarith.h
Expand Up @@ -46,41 +46,35 @@ constexpr inline std::make_unsigned_t<std::common_type_t<T, U>> shift_round_up(T
}

/*
* Macro to determine if value is a power of 2
* Wrapper to determine if value is a power of 2
*/
#define ISP2(x) (((x) & ((x) - 1)) == 0)

template<typename T>
constexpr inline bool isp2(T x) {
return (x & (x - 1)) == 0;
}

/*
* Macros for various sorts of alignment and rounding. The "align" must
* Wrappers for various sorts of alignment and rounding. The "align" must
* be a power of 2. Often times it is a block, sector, or page.
*/

/*
* return x rounded down to an align boundary
* eg, P2ALIGN(1200, 1024) == 1024 (1*align)
* eg, P2ALIGN(1024, 1024) == 1024 (1*align)
* eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
* eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
* eg, p2align(1200, 1024) == 1024 (1*align)
* eg, p2align(1024, 1024) == 1024 (1*align)
* eg, p2align(0x1234, 0x100) == 0x1200 (0x12*align)
* eg, p2align(0x5600, 0x100) == 0x5600 (0x56*align)
*/
#define P2ALIGN(x, align) ((x) & -(align))

template<typename T, typename U>
constexpr inline std::make_unsigned_t<std::common_type_t<T, U>> p2align(T x, U align) {
return x & -align;
}

/*
* return x % (mod) align
* eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align)
* eg, P2PHASE(0x5600, 0x100) == 0x00 (x-0x56*align)
* eg, p2phase(0x1234, 0x100) == 0x34 (x-0x12*align)
* eg, p2phase(0x5600, 0x100) == 0x00 (x-0x56*align)
*/
#define P2PHASE(x, align) ((x) & ((align) - 1))

template<typename T, typename U>
constexpr inline std::make_unsigned_t<std::common_type_t<T, U>> p2phase(T x, U align) {
return x & (align - 1);
Expand All @@ -89,23 +83,19 @@ constexpr inline std::make_unsigned_t<std::common_type_t<T, U>> p2phase(T x, U a
/*
* return how much space is left in this block (but if it's perfectly
* aligned, return 0).
* eg, P2NPHASE(0x1234, 0x100) == 0xcc (0x13*align-x)
* eg, P2NPHASE(0x5600, 0x100) == 0x00 (0x56*align-x)
* eg, p2nphase(0x1234, 0x100) == 0xcc (0x13*align-x)
* eg, p2nphase(0x5600, 0x100) == 0x00 (0x56*align-x)
*/
#define P2NPHASE(x, align) (-(x) & ((align) - 1))

template<typename T, typename U>
constexpr inline std::make_unsigned_t<std::common_type_t<T, U>> p2nphase(T x, U align) {
return -x & (align - 1);
}

/*
* return x rounded up to an align boundary
* eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align)
* eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align)
* eg, p2roundup(0x1234, 0x100) == 0x1300 (0x13*align)
* eg, p2roundup(0x5600, 0x100) == 0x5600 (0x56*align)
*/
#define P2ROUNDUP(x, align) (-(-(x) & -(align)))

template<typename T, typename U>
constexpr inline std::make_unsigned_t<std::common_type_t<T, U>> p2roundup(T x, U align) {
return (-(-(x) & -(align)));
Expand Down
2 changes: 1 addition & 1 deletion src/os/bluestore/BlueStore.h
Expand Up @@ -2716,7 +2716,7 @@ class BlueStoreRepairer
granularity = ROUND_UP_TO(granularity, min_alloc_size);
}

uint64_t entries = P2ROUNDUP(total, granularity) / granularity;
uint64_t entries = p2roundup(total, granularity) / granularity;
collections_bfs.resize(entries,
bloom_filter(BLOOM_FILTER_SALT_COUNT,
BLOOM_FILTER_TABLE_SIZE,
Expand Down
24 changes: 12 additions & 12 deletions src/test/test_intarith.cc
Expand Up @@ -63,20 +63,20 @@ TEST(intarith, ctz) {
}

TEST(intarith, p2family) {
ASSERT_TRUE(ISP2(0x100));
ASSERT_FALSE(ISP2(0x1234));
ASSERT_TRUE(isp2(0x100));
ASSERT_FALSE(isp2(0x1234));

ASSERT_EQ(1024, P2ALIGN(1200, 1024));
ASSERT_EQ(1024, P2ALIGN(1024, 1024));
ASSERT_EQ(0x1200, P2ALIGN(0x1234, 0x100));
ASSERT_EQ(0x5600, P2ALIGN(0x5600, 0x100));
ASSERT_EQ(1024, p2align(1200, 1024));
ASSERT_EQ(1024, p2align(1024, 1024));
ASSERT_EQ(0x1200, p2align(0x1234, 0x100));
ASSERT_EQ(0x5600, p2align(0x5600, 0x100));

ASSERT_EQ(0x34, P2PHASE(0x1234, 0x100));
ASSERT_EQ(0x00, P2PHASE(0x5600, 0x100));
ASSERT_EQ(0x34, p2phase(0x1234, 0x100));
ASSERT_EQ(0x00, p2phase(0x5600, 0x100));

ASSERT_EQ(0xcc, P2NPHASE(0x1234, 0x100));
ASSERT_EQ(0x00, P2NPHASE(0x5600, 0x100));
ASSERT_EQ(0xcc, p2nphase(0x1234, 0x100));
ASSERT_EQ(0x00, p2nphase(0x5600, 0x100));

ASSERT_EQ(0x1300, P2ROUNDUP(0x1234, 0x100));
ASSERT_EQ(0x5600, P2ROUNDUP(0x5600, 0x100));
ASSERT_EQ(0x1300, p2roundup(0x1234, 0x100));
ASSERT_EQ(0x5600, p2roundup(0x5600, 0x100));
}

0 comments on commit 75510f7

Please sign in to comment.