aboutsummaryrefslogtreecommitdiff
path: root/src/include/port/pg_bitutils.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/include/port/pg_bitutils.h')
-rw-r--r--src/include/port/pg_bitutils.h50
1 files changed, 48 insertions, 2 deletions
diff --git a/src/include/port/pg_bitutils.h b/src/include/port/pg_bitutils.h
index f9b77ec2780..086bd08132f 100644
--- a/src/include/port/pg_bitutils.h
+++ b/src/include/port/pg_bitutils.h
@@ -137,7 +137,7 @@ pg_rightmost_one_pos64(uint64 word)
/*
* pg_nextpower2_32
- * Returns the next highest power of 2 of 'num', or 'num', if it's
+ * Returns the next higher power of 2 above 'num', or 'num' if it's
* already a power of 2.
*
* 'num' mustn't be 0 or be above PG_UINT32_MAX / 2 + 1.
@@ -160,7 +160,7 @@ pg_nextpower2_32(uint32 num)
/*
* pg_nextpower2_64
- * Returns the next highest power of 2 of 'num', or 'num', if it's
+ * Returns the next higher power of 2 above 'num', or 'num' if it's
* already a power of 2.
*
* 'num' mustn't be 0 or be above PG_UINT64_MAX / 2 + 1.
@@ -182,6 +182,52 @@ pg_nextpower2_64(uint64 num)
}
/*
+ * pg_nextpower2_size_t
+ * Returns the next higher power of 2 above 'num', for a size_t input.
+ */
+#if SIZEOF_SIZE_T == 4
+#define pg_nextpower2_size_t(num) pg_nextpower2_32(num)
+#else
+#define pg_nextpower2_size_t(num) pg_nextpower2_64(num)
+#endif
+
+/*
+ * pg_prevpower2_32
+ * Returns the next lower power of 2 below 'num', or 'num' if it's
+ * already a power of 2.
+ *
+ * 'num' mustn't be 0.
+ */
+static inline uint32
+pg_prevpower2_32(uint32 num)
+{
+ return ((uint32) 1) << pg_leftmost_one_pos32(num);
+}
+
+/*
+ * pg_prevpower2_64
+ * Returns the next lower power of 2 below 'num', or 'num' if it's
+ * already a power of 2.
+ *
+ * 'num' mustn't be 0.
+ */
+static inline uint64
+pg_prevpower2_64(uint64 num)
+{
+ return ((uint64) 1) << pg_leftmost_one_pos64(num);
+}
+
+/*
+ * pg_prevpower2_size_t
+ * Returns the next lower power of 2 below 'num', for a size_t input.
+ */
+#if SIZEOF_SIZE_T == 4
+#define pg_prevpower2_size_t(num) pg_prevpower2_32(num)
+#else
+#define pg_prevpower2_size_t(num) pg_prevpower2_64(num)
+#endif
+
+/*
* pg_ceil_log2_32
* Returns equivalent of ceil(log2(num))
*/