]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/include/intarith.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / include / intarith.h
index 390cdc024d7e8cea77838ef64c81ca948d4f7a49..e912cbe7b3c44f242921f298dcd8dd0b4dc11e16 100644 (file)
@@ -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,74 +7,87 @@
  *
  * 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
 
-#ifndef MIN
-#define MIN(a,b) ((a) < (b) ? (a):(b))
-#endif
+#include <type_traits>
 
-#ifndef MAX
-#define MAX(a,b) ((a) > (b) ? (a):(b))
-#endif
+template<typename T, typename U>
+constexpr inline std::make_unsigned_t<std::common_type_t<T, U>> div_round_up(T n, U d) {
+  return (n + d - 1) / d;
+}
 
-#ifndef DIV_ROUND_UP
-#define DIV_ROUND_UP(n, d)  (((n) + (d) - 1) / (d))
-#endif
 
-#ifndef ROUND_UP_TO
-#define ROUND_UP_TO(n, d) ((n)%(d) ? ((n)+(d)-(n)%(d)) : (n))
-#endif
+template<typename T, typename U>
+constexpr inline std::make_unsigned_t<std::common_type_t<T, U>> 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<typename T, typename U>
+constexpr inline std::make_unsigned_t<std::common_type_t<T, U>> shift_round_up(T x, U y) {
+  return (x + (1 << y) - 1) >> y;
+}
 
 /*
- * 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>
+constexpr inline T p2align(T x, T 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>
+constexpr inline T p2phase(T x, T align) {
+  return x & (align - 1);
+}
 
 /*
  * 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>
+constexpr inline T p2nphase(T x, T 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>
+constexpr inline T p2roundup(T x, T align) {
+  return -(-x & -align);
+}
 
 // count trailing zeros.
 // NOTE: the builtin is nondeterministic on 0 input