Skip to content

Commit

Permalink
ensure datatype alignments are powers-of-two
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Jul 21, 2016
1 parent 8a9fd10 commit df4b5d2
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 42 deletions.
4 changes: 3 additions & 1 deletion src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,9 @@ JL_DLLEXPORT jl_datatype_t *jl_new_bitstype(jl_value_t *name, jl_datatype_t *sup
jl_datatype_t *bt = jl_new_datatype((jl_sym_t*)name, super, parameters,
jl_emptysvec, jl_emptysvec, 0, 0, 0);
uint32_t nbytes = (nbits + 7) / 8;
uint32_t alignm = nbytes > MAX_ALIGN ? MAX_ALIGN : nbytes;
uint32_t alignm = next_power_of_two(nbytes);
if (alignm > MAX_ALIGN)
alignm = MAX_ALIGN;
bt->size = nbytes;
bt->layout = jl_get_layout(0, alignm, 0, NULL);
return bt;
Expand Down
13 changes: 0 additions & 13 deletions src/runtime_intrinsics.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,6 @@ JL_DLLEXPORT jl_value_t *jl_pointerset(jl_value_t *p, jl_value_t *x, jl_value_t
return p;
}

static inline unsigned int next_power_of_two(unsigned int val)
{
/* this function taken from libuv src/unix/core.c */
val -= 1;
val |= val >> 1;
val |= val >> 2;
val |= val >> 4;
val |= val >> 8;
val |= val >> 16;
val += 1;
return val;
}

static inline char signbitbyte(void *a, unsigned bytes)
{
// sign bit of an signed number of n bytes, as a byte
Expand Down
13 changes: 13 additions & 0 deletions src/support/dtypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ typedef uint32_t uint_t;
typedef int32_t int_t;
#endif

STATIC_INLINE unsigned int next_power_of_two(unsigned int val)
{
/* this function taken from libuv src/unix/core.c */
val -= 1;
val |= val >> 1;
val |= val >> 2;
val |= val >> 4;
val |= val >> 8;
val |= val >> 16;
val += 1;
return val;
}

#define LLT_ALIGN(x, sz) (((x) + (sz)-1) & -(sz))

// branch prediction annotations
Expand Down
13 changes: 0 additions & 13 deletions src/support/hashing.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@
extern "C" {
#endif

uint_t nextipow2(uint_t i)
{
if (i==0) return 1;
if ((i&(i-1))==0) return i;
if (i&TOP_BIT) return TOP_BIT;

// repeatedly clear bottom bit
while (i&(i-1))
i = i&(i-1);

return i<<1;
}

uint32_t int32hash(uint32_t a)
{
a = (a+0x7ed55d16) + (a<<12);
Expand Down
4 changes: 2 additions & 2 deletions src/support/htable.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ htable_t *htable_new(htable_t *h, size_t size)
h->table = &h->_space[0];
}
else {
size = nextipow2(size);
size = next_power_of_two(size);
size *= 2; // 2 pointers per key/value pair
size *= 2; // aim for 50% occupancy
h->size = size;
Expand All @@ -47,7 +47,7 @@ void htable_free(htable_t *h)
// empty and reduce size
void htable_reset(htable_t *h, size_t sz)
{
sz = nextipow2(sz);
sz = next_power_of_two(sz);
if (h->size > sz*4 && h->size > HT_N_INLINE) {
size_t newsz = sz*4;
void **newtab = (void**)LLT_REALLOC(h->table, newsz*sizeof(void*));
Expand Down
13 changes: 0 additions & 13 deletions src/typemap.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,6 @@ union jl_typemap_t mtcache_hash_lookup(const struct jl_ordereddict_t *a, jl_valu
return ml;
}

static inline unsigned int next_power_of_two(unsigned int val)
{
/* this function taken from libuv src/unix/core.c */
val -= 1;
val |= val >> 1;
val |= val >> 2;
val |= val >> 4;
val |= val >> 8;
val |= val >> 16;
val += 1;
return val;
}

static void mtcache_rehash(struct jl_ordereddict_t *pa, size_t newlen, jl_value_t *parent, int8_t tparam, int8_t offs)
{
size_t i, nval = jl_array_len(pa->values);
Expand Down

0 comments on commit df4b5d2

Please sign in to comment.