Skip to content

Commit 53167c3

Browse files
committed
FEAT: optimize powerof2 memory allocation
1 parent a7b95f3 commit 53167c3

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/core/m-pools.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,7 @@ const REBPOOLSPEC Mem_Pool_Spec[MAX_POOLS] =
411411
#endif
412412
} else {
413413
if (powerof2) {
414-
// !!! WHO added this and why??? Just use a left shift and mask!
415-
REBCNT len=2048;
416-
while(len<length)
417-
len*=2;
418-
length=len;
414+
U32_ROUND_UP_POWER_OF_2(length);
419415
} else
420416
length = ALIGN(length, 2048);
421417
#ifdef DEBUGGING

src/include/reb-c.h

+10
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,16 @@ typedef void(*CFUNC)(void *);
306306

307307
#define ROUND_TO_INT(d) (REBINT)(floor((d) + 0.5))
308308

309+
// https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
310+
// Note: 32bit, v must be > 0
311+
#define U32_ROUND_UP_POWER_OF_2(v) \
312+
v--; \
313+
v |= v >> 1; \
314+
v |= v >> 2; \
315+
v |= v >> 4; \
316+
v |= v >> 8; \
317+
v |= v >> 16; \
318+
v++; \
309319

310320
/***********************************************************************
311321
**

0 commit comments

Comments
 (0)