Skip to content

Commit

Permalink
FEAT: optimize powerof2 memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Sep 9, 2021
1 parent a7b95f3 commit 53167c3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/core/m-pools.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,7 @@ const REBPOOLSPEC Mem_Pool_Spec[MAX_POOLS] =
#endif
} else {
if (powerof2) {
// !!! WHO added this and why??? Just use a left shift and mask!
REBCNT len=2048;
while(len<length)
len*=2;
length=len;
U32_ROUND_UP_POWER_OF_2(length);
} else
length = ALIGN(length, 2048);
#ifdef DEBUGGING
Expand Down
10 changes: 10 additions & 0 deletions src/include/reb-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,16 @@ typedef void(*CFUNC)(void *);

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

// https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
// Note: 32bit, v must be > 0
#define U32_ROUND_UP_POWER_OF_2(v) \
v--; \
v |= v >> 1; \
v |= v >> 2; \
v |= v >> 4; \
v |= v >> 8; \
v |= v >> 16; \
v++; \

/***********************************************************************
**
Expand Down

0 comments on commit 53167c3

Please sign in to comment.