summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Sotoudeh <matthew@masot.net>2024-05-17 15:28:51 -0700
committerMatthew Sotoudeh <matthew@masot.net>2024-05-17 15:28:51 -0700
commit69038d2d0b59ef8c87a70a24f642106665030282 (patch)
tree60ba81fb5132293bf2aeb05203a8c18f2a8f7c43
parente14dc2e64357ee9415ffc784595a18f1c0affd7c (diff)
fix bug & intrinsic log
-rw-r--r--magic_buddy/magic_buddy.c13
1 files changed, 12 insertions, 1 deletions
diff --git a/magic_buddy/magic_buddy.c b/magic_buddy/magic_buddy.c
index 1eb2d8f..1029f58 100644
--- a/magic_buddy/magic_buddy.c
+++ b/magic_buddy/magic_buddy.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
+#include <math.h>
#include "magic_buddy.h"
struct free_block {
@@ -11,11 +12,21 @@ struct free_block {
};
// log2 of size rounded up (ceil) or down (!ceil) to the nearest power of 2
+// https://stackoverflow.com/questions/11376288/fast-computing-of-log2-for-64-bit-integers
static size_t size2log(size_t size, int ceil) {
+#if 1
+ size_t floor_log
+ = (8*sizeof(unsigned long long))
+ - __builtin_clzll((unsigned long long)size) - 1;
+ if (!size) floor_log = 0;
+ return (ceil && (size > (1 << floor_log))) ? (floor_log + 1) : floor_log;
+#else
size_t floor_log = 0;
for (size_t i = 0; i < (8 * sizeof(size)); i++)
- if (size & (1 << i)) floor_log = i;
+ if (size & (1ul << i)) floor_log = i;
+
return (ceil && (size > (1 << floor_log))) ? (floor_log + 1) : floor_log;
+#endif
}
static struct free_block *buddy_of(struct free_block *block, size_t logsize,
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback