From: Adam C. Emerson Date: Wed, 10 Jan 2018 22:06:53 +0000 (-0500) Subject: include: Add templates along side macros in intarith X-Git-Tag: v13.0.2~528^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=c06b97b3d7e36b5b3be38f1ffa121611bea61a52;p=ceph.git include: Add templates along side macros in intarith The macros can be removed later. Signed-off-by: Adam C. Emerson --- diff --git a/src/include/intarith.h b/src/include/intarith.h index 390cdc024d7..d0e33a339b8 100644 --- a/src/include/intarith.h +++ b/src/include/intarith.h @@ -1,4 +1,4 @@ -// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- // vim: ts=8 sw=2 smarttab /* * Ceph - scalable distributed file system @@ -7,14 +7,16 @@ * * This is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public - * License version 2.1, as published by the Free Software + * License version 2.1, as published by the Free Software * Foundation. See file COPYING. - * + * */ #ifndef CEPH_INTARITH_H #define CEPH_INTARITH_H +#include + #ifndef MIN #define MIN(a,b) ((a) < (b) ? (a):(b)) #endif @@ -27,19 +29,40 @@ #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) #endif +template +constexpr inline std::make_unsigned_t> div_round_up(T n, U d) { + return (n + d - 1) / d; +} + + #ifndef ROUND_UP_TO #define ROUND_UP_TO(n, d) ((n)%(d) ? ((n)+(d)-(n)%(d)) : (n)) #endif +template +constexpr inline std::make_unsigned_t> round_up_to(T n, U d) { + return (n % d ? (n + d - n % d) : n); +} + #ifndef SHIFT_ROUND_UP #define SHIFT_ROUND_UP(x,y) (((x)+(1<<(y))-1) >> (y)) #endif +template +constexpr inline std::make_unsigned_t> shift_round_up(T x, U y) { + return (x + (1 << y) - 1) >> y; +} + /* * Macro to determine if value is a power of 2 */ #define ISP2(x) (((x) & ((x) - 1)) == 0) +template +constexpr inline bool isp2(T x) { + return (x & (x - 1)) == 0; +} + /* * Macros for various sorts of alignment and rounding. The "align" must * be a power of 2. Often times it is a block, sector, or page. @@ -54,6 +77,11 @@ */ #define P2ALIGN(x, align) ((x) & -(align)) +template +constexpr inline std::make_unsigned_t> p2align(T x, U align) { + return x & -align; +} + /* * return x % (mod) align * eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align) @@ -61,6 +89,11 @@ */ #define P2PHASE(x, align) ((x) & ((align) - 1)) +template +constexpr inline std::make_unsigned_t> p2phase(T x, U align) { + return x & (align - 1); +} + /* * return how much space is left in this block (but if it's perfectly * aligned, return 0). @@ -69,6 +102,11 @@ */ #define P2NPHASE(x, align) (-(x) & ((align) - 1)) +template +constexpr inline std::make_unsigned_t> p2nphase(T x, U align) { + return -x & (align - 1); +} + /* * return x rounded up to an align boundary * eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align) @@ -76,6 +114,11 @@ */ #define P2ROUNDUP(x, align) (-(-(x) & -(align))) +template +constexpr inline std::make_unsigned_t> p2roundup(T x, U align) { + return (-(-(x) & -(align))); +} + // count trailing zeros. // NOTE: the builtin is nondeterministic on 0 input template