X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fceph%2Fsrc%2Finclude%2Fintarith.h;fp=src%2Fceph%2Fsrc%2Finclude%2Fintarith.h;h=390cdc024d7e8cea77838ef64c81ca948d4f7a49;hb=812ff6ca9fcd3e629e49d4328905f33eee8ca3f5;hp=0000000000000000000000000000000000000000;hpb=15280273faafb77777eab341909a3f495cf248d9;p=stor4nfv.git diff --git a/src/ceph/src/include/intarith.h b/src/ceph/src/include/intarith.h new file mode 100644 index 0000000..390cdc0 --- /dev/null +++ b/src/ceph/src/include/intarith.h @@ -0,0 +1,180 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2004-2006 Sage Weil + * + * 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 + * Foundation. See file COPYING. + * + */ + +#ifndef CEPH_INTARITH_H +#define CEPH_INTARITH_H + +#ifndef MIN +#define MIN(a,b) ((a) < (b) ? (a):(b)) +#endif + +#ifndef MAX +#define MAX(a,b) ((a) > (b) ? (a):(b)) +#endif + +#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 + +#ifndef SHIFT_ROUND_UP +#define SHIFT_ROUND_UP(x,y) (((x)+(1<<(y))-1) >> (y)) +#endif + +/* + * Macro to determine if value is a power of 2 + */ +#define ISP2(x) (((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. + */ + +/* + * 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) + */ +#define P2ALIGN(x, align) ((x) & -(align)) + +/* + * return x % (mod) align + * eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align) + * eg, P2PHASE(0x5600, 0x100) == 0x00 (x-0x56*align) + */ +#define P2PHASE(x, align) ((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) + */ +#define P2NPHASE(x, align) (-(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) + */ +#define P2ROUNDUP(x, align) (-(-(x) & -(align))) + +// count trailing zeros. +// NOTE: the builtin is nondeterministic on 0 input +template + inline typename std::enable_if< + (std::is_integral::value && + sizeof(T) <= sizeof(unsigned)), + unsigned>::type ctz(T v) { + if (v == 0) + return sizeof(v) * 8; + return __builtin_ctz(v); +} + +template + inline typename std::enable_if< + (std::is_integral::value && + sizeof(T) > sizeof(unsigned int) && + sizeof(T) <= sizeof(unsigned long)), + unsigned>::type ctz(T v) { + if (v == 0) + return sizeof(v) * 8; + return __builtin_ctzl(v); +} + +template + inline typename std::enable_if< + (std::is_integral::value && + sizeof(T) > sizeof(unsigned long) && + sizeof(T) <= sizeof(unsigned long long)), + unsigned>::type ctz(T v) { + if (v == 0) + return sizeof(v) * 8; + return __builtin_ctzll(v); +} + +// count leading zeros +// NOTE: the builtin is nondeterministic on 0 input +template + inline typename std::enable_if< + (std::is_integral::value && + sizeof(T) <= sizeof(unsigned)), + unsigned>::type clz(T v) { + if (v == 0) + return sizeof(v) * 8; + return __builtin_clz(v); +} + +template + inline typename std::enable_if< + (std::is_integral::value && + sizeof(T) > sizeof(unsigned int) && + sizeof(T) <= sizeof(unsigned long)), + unsigned>::type clz(T v) { + if (v == 0) + return sizeof(v) * 8; + return __builtin_clzl(v); +} + +template + inline typename std::enable_if< + (std::is_integral::value && + sizeof(T) > sizeof(unsigned long) && + sizeof(T) <= sizeof(unsigned long long)), + unsigned>::type clz(T v) { + if (v == 0) + return sizeof(v) * 8; + return __builtin_clzll(v); +} + +// count bits (set + any 0's that follow) +template + inline typename std::enable_if< + (std::is_integral::value && + sizeof(T) <= sizeof(unsigned)), + unsigned>::type cbits(T v) { + if (v == 0) + return 0; + return (sizeof(v) * 8) - __builtin_clz(v); +} + +template + inline typename std::enable_if< + (std::is_integral::value && + sizeof(T) > sizeof(unsigned int) && + sizeof(T) <= sizeof(unsigned long)), + unsigned>::type cbits(T v) { + if (v == 0) + return 0; + return (sizeof(v) * 8) - __builtin_clzl(v); +} + +template + inline typename std::enable_if< + (std::is_integral::value && + sizeof(T) > sizeof(unsigned long) && + sizeof(T) <= sizeof(unsigned long long)), + unsigned>::type cbits(T v) { + if (v == 0) + return 0; + return (sizeof(v) * 8) - __builtin_clzll(v); +} + +#endif