From 4e230ae8569e5536eb82d15ccdf469269d0845b8 Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 26 Aug 2025 11:18:23 +0200 Subject: [PATCH] add a utility macro for getting the bitwise ceiling. (e.i. the next power of 2) --- src/util/util.h | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/util/util.h diff --git a/src/util/util.h b/src/util/util.h new file mode 100644 index 0000000..65ecf94 --- /dev/null +++ b/src/util/util.h @@ -0,0 +1,6 @@ +#pragma once + +/* Acquires the next power of two of value `x`. + * Automatically determines the type (and therefore the width) of `x`. + * Explicitly cast `x` to a desired width, if necessary. */ +#define bit_ceil(x) (1 << (sizeof(__typeof__(x)) * 8 - __builtin_clzg(((x) - !!(x)) | 1)))