Commit 6dbaa206 authored by Russ Cox's avatar Russ Cox

runtime: replace runtime·rnd function with ROUND macro

It's sad to introduce a new macro, but rnd shows up consistently
in profiles, and the function call overwhelms the two arithmetic
instructions it performs.

R=r
CC=golang-dev
https://golang.org/cl/6260051
parent 90d59c58
......@@ -559,8 +559,16 @@ dcommontype(Sym *s, int ot, Type *t)
ot = duintptr(s, ot, t->width);
ot = duint32(s, ot, typehash(t));
ot = duint8(s, ot, 0); // unused
// runtime (and common sense) expects alignment to be a power of two.
i = t->align;
if(i == 0)
i = 1;
if((i&(i-1)) != 0)
fatal("invalid alignment %d for %T", t->align, t);
ot = duint8(s, ot, t->align); // align
ot = duint8(s, ot, t->align); // fieldAlign
i = kinds[t->etype];
if(t->etype == TARRAY && t->bound < 0)
i = KindSlice;
......
......@@ -446,7 +446,7 @@ runtime·selectnbsend(ChanType *t, Hchan *c, ...)
byte *ae, *ap;
ae = (byte*)(&c + 1);
ap = ae + runtime·rnd(t->elem->size, Structrnd);
ap = ae + ROUND(t->elem->size, Structrnd);
runtime·chansend(t, c, ae, ap);
}
......@@ -571,7 +571,7 @@ runtime·newselect(int32 size, ...)
int32 o;
Select **selp;
o = runtime·rnd(sizeof(size), Structrnd);
o = ROUND(sizeof(size), Structrnd);
selp = (Select**)((byte*)&size + o);
newselect(size, selp);
}
......
......@@ -115,7 +115,7 @@ hash_init (Hmap *h, int32 datasize, int64 hint)
if(datasize < sizeof (void *))
datasize = sizeof (void *);
datasize = runtime·rnd(datasize, sizeof (void *));
datasize = ROUND(datasize, sizeof (void *));
init_sizes (hint, &init_power);
h->datasize = datasize;
assert (h->datasize == datasize);
......@@ -744,8 +744,8 @@ runtime·makemap_c(MapType *typ, int64 hint)
h = runtime·mal(sizeof(*h));
h->flag |= CanFreeTable; /* until reflect gets involved, free is okay */
ksize = runtime·rnd(key->size, sizeof(void*));
vsize = runtime·rnd(val->size, sizeof(void*));
ksize = ROUND(key->size, sizeof(void*));
vsize = ROUND(val->size, sizeof(void*));
if(ksize > MaxData || vsize > MaxData || ksize+vsize > MaxData) {
// Either key is too big, or value is, or combined they are.
// Prefer to keep the key if possible, because we look at
......@@ -829,7 +829,7 @@ runtime·mapaccess1(MapType *t, Hmap *h, ...)
bool pres;
ak = (byte*)(&h + 1);
av = ak + runtime·rnd(t->key->size, Structrnd);
av = ak + ROUND(t->key->size, Structrnd);
runtime·mapaccess(t, h, ak, av, &pres);
......@@ -854,7 +854,7 @@ runtime·mapaccess2(MapType *t, Hmap *h, ...)
byte *ak, *av, *ap;
ak = (byte*)(&h + 1);
av = ak + runtime·rnd(t->key->size, Structrnd);
av = ak + ROUND(t->key->size, Structrnd);
ap = av + t->elem->size;
runtime·mapaccess(t, h, ak, av, ap);
......@@ -952,7 +952,7 @@ runtime·mapassign1(MapType *t, Hmap *h, ...)
runtime·panicstring("assignment to entry in nil map");
ak = (byte*)(&h + 1);
av = ak + runtime·rnd(t->key->size, t->elem->align);
av = ak + ROUND(t->key->size, t->elem->align);
runtime·mapassign(t, h, ak, av);
}
......@@ -1171,7 +1171,7 @@ runtime·mapiter2(struct hash_iter *it, ...)
t = it->t;
ak = (byte*)(&it + 1);
av = ak + runtime·rnd(t->key->size, t->elem->align);
av = ak + ROUND(t->key->size, t->elem->align);
res = it->data;
if(res == nil)
......
......@@ -193,7 +193,7 @@ runtime·convT2I(Type *t, InterfaceType *inter, ...)
elem = (byte*)(&inter+1);
wid = t->size;
ret = (Iface*)(elem + runtime·rnd(wid, Structrnd));
ret = (Iface*)(elem + ROUND(wid, Structrnd));
ret->tab = itab(inter, t, 0);
copyin(t, elem, &ret->data);
}
......@@ -209,7 +209,7 @@ runtime·convT2E(Type *t, ...)
elem = (byte*)(&t+1);
wid = t->size;
ret = (Eface*)(elem + runtime·rnd(wid, Structrnd));
ret = (Eface*)(elem + ROUND(wid, Structrnd));
ret->type = t;
copyin(t, elem, &ret->data);
}
......
......@@ -88,36 +88,36 @@ vprintf(int8 *s, byte *base)
break;
case 'd': // 32-bit
case 'x':
arg = runtime·rnd(arg, 4);
arg = ROUND(arg, 4);
narg = arg + 4;
break;
case 'D': // 64-bit
case 'U':
case 'X':
case 'f':
arg = runtime·rnd(arg, sizeof(uintptr));
arg = ROUND(arg, sizeof(uintptr));
narg = arg + 8;
break;
case 'C':
arg = runtime·rnd(arg, sizeof(uintptr));
arg = ROUND(arg, sizeof(uintptr));
narg = arg + 16;
break;
case 'p': // pointer-sized
case 's':
arg = runtime·rnd(arg, sizeof(uintptr));
arg = ROUND(arg, sizeof(uintptr));
narg = arg + sizeof(uintptr);
break;
case 'S': // pointer-aligned but bigger
arg = runtime·rnd(arg, sizeof(uintptr));
arg = ROUND(arg, sizeof(uintptr));
narg = arg + sizeof(String);
break;
case 'a': // pointer-aligned but bigger
arg = runtime·rnd(arg, sizeof(uintptr));
arg = ROUND(arg, sizeof(uintptr));
narg = arg + sizeof(Slice);
break;
case 'i': // pointer-aligned but bigger
case 'e':
arg = runtime·rnd(arg, sizeof(uintptr));
arg = ROUND(arg, sizeof(uintptr));
narg = arg + sizeof(Eface);
break;
}
......
......@@ -156,19 +156,6 @@ runtime·mchr(byte *p, byte c, byte *ep)
return nil;
}
uint32
runtime·rnd(uint32 n, uint32 m)
{
uint32 r;
if(m > maxround)
m = maxround;
r = n % m;
if(r)
n += m-r;
return n;
}
static int32 argc;
static uint8** argv;
......
......@@ -390,6 +390,7 @@ struct ParFor
#define nelem(x) (sizeof(x)/sizeof((x)[0]))
#define nil ((void*)0)
#define offsetof(s,m) (uint32)(&(((s*)0)->m))
#define ROUND(x, n) (((x)+(n)-1)&~((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
/*
* known to compiler
......@@ -533,7 +534,6 @@ void runtime·goenvs_unix(void);
void* runtime·getu(void);
void runtime·throw(int8*);
void runtime·panicstring(int8*);
uint32 runtime·rnd(uint32, uint32);
void runtime·prints(int8*);
void runtime·printf(int8*, ...);
byte* runtime·mchr(byte*, byte, byte*);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment