On Apr 22, 2007, at 5:14 PM, chromatic wrote:

On Sunday 22 April 2007 14:08, Uri Guttman wrote:

"JP" == Joerg Plate <[EMAIL PROTECTED]> writes:

const int slot = (reg_alloc + 7) >> 3; reg_alloc = slot << 3;

This is where I start not to understand.  Why reg_alloc + 7? Why
shift left and right by 3?

JP> That's just a rounding up (if necessary) to a multiple of 8 (2<<3).

and those sort of things should be macros as they are confusing
otherwise and annoying to change. and named something like
ROUND_UP_TO_BOUNDARY. as others have said it is a common idiom. and i
agree that there should be comments on WHY the rounding is used/ needed.

I figured it was a rounding, but I saw two magic numbers and didn't want to
guess what it was.

Any volunteers to macroize this?

Also, is anyone *sure* that this boundary is effective on both 32- and 64-bit
platforms?  I hate to make assumptions about alignment.

I would rather make the side effects more explicit and throw in a good comment.
How about something like this?

Index: src/gc/register.c
===================================================================
--- src/gc/register.c   (revision 18296)
+++ src/gc/register.c   (working copy)
@@ -338,9 +338,16 @@
     size_t reg_alloc = size_nip +
         sizeof (STRING*) *  n_regs_used[REGNO_STR];
-    const int slot = (reg_alloc + 7) >> 3;
-    reg_alloc = slot << 3;
+    const int slot;
+    /* round reg_alloc up to the nearest multiple of 8 */
+    reg_alloc = ((reg_alloc + 7) >> 3) << 3;
+
+    /* reg_alloc now divides evenly by 8 because of the previous
+       rounding. A granualrity of 8 is arbitratly, it could have been
+       some bigger power of 2 */
+    slot = reg_alloc / 8;
+
     if (slot >= interp->ctx_mem.n_free_slots) {
         const int n = slot + 1;
         int i;

Reply via email to