summaryrefslogtreecommitdiff
path: root/aux
diff options
context:
space:
mode:
authorBart Massey <bart@cs.pdx.edu>2007-12-08 15:27:04 -0800
committerBart Massey <bart@cs.pdx.edu>2007-12-08 15:27:04 -0800
commit161deeb0a8cbb86b42d72afa760b12e80f1758d2 (patch)
treeb3bfc8e0658d617d87341a255afbb291778093ee /aux
parent61c711cc0a54df8840837b40447e4719be8d670d (diff)
fixed existing roundups, added rounddowns
Diffstat (limited to 'aux')
-rw-r--r--aux/xcb_bitops.h48
1 files changed, 43 insertions, 5 deletions
diff --git a/aux/xcb_bitops.h b/aux/xcb_bitops.h
index 6462af4..48c3401 100644
--- a/aux/xcb_bitops.h
+++ b/aux/xcb_bitops.h
@@ -91,14 +91,30 @@ xcb_popcount(uint32_t x)
*
* Rounds @p base up to a multiple of @p pad, where @p pad
* is a power of two. The more general case is handled by
- * xcb_roundup_2().
+ * xcb_roundup().
* @ingroup xcb__bitops
*/
_X_INLINE static uint32_t
xcb_roundup_2 (uint32_t base, uint32_t pad)
{
- uint32_t b = base + pad - 1;
- return b & ~(pad - 1);
+ return (base + pad - 1) & -pad;
+}
+
+/**
+ * Round down to the next power-of-two unit size.
+ * @param base Number to be rounded down.
+ * @param pad Multiple to be rounded to; must be a power of two.
+ * @return Rounded-down number.
+ *
+ * Rounds @p base down to a multiple of @p pad, where @p pad
+ * is a power of two. The more general case is handled by
+ * xcb_rounddown().
+ * @ingroup xcb__bitops
+ */
+_X_INLINE static uint32_t
+xcb_rounddown_2 (uint32_t base, uint32_t pad)
+{
+ return base & -pad;
}
/**
@@ -119,8 +135,30 @@ xcb_roundup (uint32_t base, uint32_t pad)
uint32_t b = base + pad - 1;
/* faster if pad is a power of two */
if (((pad - 1) & pad) == 0)
- return b & ~(pad - 1);
- return b % pad;
+ return b & -pad;
+ return b - b % pad;
+}
+
+
+/**
+ * Round down to the next unit size.
+ * @param base Number to be rounded down.
+ * @param pad Multiple to be rounded to.
+ * @return Rounded-down number.
+ *
+ * This is a general routine for rounding @p base down
+ * to a multiple of @p pad. If you know that @p pad
+ * is a power of two, you should probably call xcb_rounddown_2()
+ * instead.
+ * @ingroup xcb__bitops
+ */
+_X_INLINE static uint32_t
+xcb_rounddown (uint32_t base, uint32_t pad)
+{
+ /* faster if pad is a power of two */
+ if (((pad - 1) & pad) == 0)
+ return base & -pad;
+ return base - base % pad;
}