mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
Unroll iterate loops.
name old speed new speed delta puffs_adler32_10k/clang 2.12GB/s ± 1% 2.42GB/s ± 1% +14.24% (p=0.008 n=5+5) puffs_adler32_100k/clang 2.12GB/s ± 3% 2.43GB/s ± 0% +14.29% (p=0.008 n=5+5) mimic_adler32_10k/clang 2.93GB/s ± 2% 2.96GB/s ± 0% ~ (p=0.310 n=5+5) mimic_adler32_100k/clang 2.89GB/s ± 2% 2.91GB/s ± 0% ~ (p=1.000 n=5+5) puffs_adler32_10k/gcc 2.17GB/s ± 1% 3.23GB/s ± 0% +48.59% (p=0.008 n=5+5) puffs_adler32_100k/gcc 2.18GB/s ± 1% 3.23GB/s ± 0% +48.34% (p=0.008 n=5+5) mimic_adler32_10k/gcc 2.97GB/s ± 0% 2.96GB/s ± 1% ~ (p=1.000 n=5+5) mimic_adler32_100k/gcc 2.91GB/s ± 1% 2.91GB/s ± 1% ~ (p=0.548 n=5+5)
This commit is contained in:
1 parent
b024cb1d8c
commit
245e3ec7b9
6 files changed
+94
-19
No files matched your search
@@ -256,17 +256,34 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
|
||||
}
|
||||
b.writes(";\n")
|
||||
b.printf("uint8_t* %s%s = %sslice_%s.ptr;\n", vPrefix, name, iPrefix, name)
|
||||
b.printf("uint8_t* %send_%s = %sslice_%s.ptr + %sslice_%s.len;\n",
|
||||
// TODO: look at n.HasContinue() and n.HasBreak().
|
||||
|
||||
unrollCount := int(n.UnrollCount().ConstValue().Int64())
|
||||
if unrollCount != 1 {
|
||||
b.printf("uint8_t* %send0_%s = %sslice_%s.ptr + (%sslice_%s.len / %d) * %d;\n",
|
||||
iPrefix, name, iPrefix, name, iPrefix, name, unrollCount, unrollCount)
|
||||
b.printf("while (%s%s < %send0_%s) {\n", vPrefix, name, iPrefix, name)
|
||||
for i := 0; i < unrollCount; i++ {
|
||||
for _, o := range n.Body() {
|
||||
if err := g.writeStatement(b, o, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
b.printf("%s%s++;\n", vPrefix, name)
|
||||
}
|
||||
b.writes("}\n")
|
||||
}
|
||||
|
||||
b.printf("uint8_t* %send1_%s = %sslice_%s.ptr + %sslice_%s.len;\n",
|
||||
iPrefix, name, iPrefix, name, iPrefix, name)
|
||||
// TODO: look at n.HasContinue().
|
||||
b.printf("while (%s%s < %send_%s) {\n", vPrefix, name, iPrefix, name)
|
||||
b.printf("while (%s%s < %send1_%s) {\n", vPrefix, name, iPrefix, name)
|
||||
for _, o := range n.Body() {
|
||||
if err := g.writeStatement(b, o, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
b.printf("%s%s++;\n}\n", vPrefix, name)
|
||||
// TODO: look at n.HasBreak().
|
||||
b.printf("%s%s++;\n", vPrefix, name)
|
||||
b.writes("}\n")
|
||||
|
||||
b.writes("}\n")
|
||||
return nil
|
||||
|
||||
+29
-2
@@ -2437,8 +2437,35 @@ uint32_t puffs_flate_adler32_update(puffs_flate_adler32* self,
|
||||
{
|
||||
puffs_base_slice_u8 i_slice_p = a_x;
|
||||
uint8_t* v_p = i_slice_p.ptr;
|
||||
uint8_t* i_end_p = i_slice_p.ptr + i_slice_p.len;
|
||||
while (v_p < i_end_p) {
|
||||
uint8_t* i_end0_p = i_slice_p.ptr + (i_slice_p.len / 8) * 8;
|
||||
while (v_p < i_end0_p) {
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
}
|
||||
uint8_t* i_end1_p = i_slice_p.ptr + i_slice_p.len;
|
||||
while (v_p < i_end1_p) {
|
||||
v_s1 += ((uint32_t)(*v_p));
|
||||
v_s2 += v_s1;
|
||||
v_p++;
|
||||
|
||||
+14
-10
@@ -393,7 +393,7 @@ func NewField(name t.ID, xType *TypeExpr, defaultValue *Expr) *Field {
|
||||
}
|
||||
}
|
||||
|
||||
// Iterate is "iterate:ID1 (vars), List1 { List2 }":
|
||||
// Iterate is "iterate.LHS:ID1 (vars), List1 { List2 }":
|
||||
// - FlagsHasBreak is the iterate has an explicit break
|
||||
// - FlagsHasContinue is the iterate has an explicit continue
|
||||
// - ID1: <0|label>
|
||||
@@ -406,6 +406,7 @@ func (n *Iterate) Node() *Node { return (*Node)(n) }
|
||||
func (n *Iterate) HasBreak() bool { return n.flags&FlagsHasBreak != 0 }
|
||||
func (n *Iterate) HasContinue() bool { return n.flags&FlagsHasContinue != 0 }
|
||||
func (n *Iterate) Label() t.ID { return n.id1 }
|
||||
func (n *Iterate) UnrollCount() *Expr { return n.lhs.Expr() }
|
||||
func (n *Iterate) Variables() []*Node { return n.list0 }
|
||||
func (n *Iterate) Asserts() []*Node { return n.list1 }
|
||||
func (n *Iterate) Body() []*Node { return n.list2 }
|
||||
@@ -413,30 +414,33 @@ func (n *Iterate) Body() []*Node { return n.list2 }
|
||||
func (n *Iterate) SetHasBreak() { n.flags |= FlagsHasBreak }
|
||||
func (n *Iterate) SetHasContinue() { n.flags |= FlagsHasContinue }
|
||||
|
||||
func NewIterate(label t.ID, variables []*Node, asserts []*Node, body []*Node) *Iterate {
|
||||
func NewIterate(label t.ID, unrollCount *Expr, variables []*Node, asserts []*Node, body []*Node) *Iterate {
|
||||
return &Iterate{
|
||||
kind: KIterate,
|
||||
id1: label,
|
||||
lhs: unrollCount.Node(),
|
||||
list0: variables,
|
||||
list1: asserts,
|
||||
list2: body,
|
||||
}
|
||||
}
|
||||
|
||||
// While is "while:ID1 LHS, List1 { List2 }":
|
||||
// While is "while:ID1 MHS, List1 { List2 }":
|
||||
// - FlagsHasBreak is the while has an explicit break
|
||||
// - FlagsHasContinue is the while has an explicit continue
|
||||
// - ID1: <0|label>
|
||||
// - LHS: <Expr>
|
||||
// - MHS: <Expr>
|
||||
// - List1: <Assert> asserts
|
||||
// - List2: <Statement> loop body
|
||||
//
|
||||
// TODO: should we be able to unroll while loops too?
|
||||
type While Node
|
||||
|
||||
func (n *While) Node() *Node { return (*Node)(n) }
|
||||
func (n *While) HasBreak() bool { return n.flags&FlagsHasBreak != 0 }
|
||||
func (n *While) HasContinue() bool { return n.flags&FlagsHasContinue != 0 }
|
||||
func (n *While) Label() t.ID { return n.id1 }
|
||||
func (n *While) Condition() *Expr { return n.lhs.Expr() }
|
||||
func (n *While) Condition() *Expr { return n.mhs.Expr() }
|
||||
func (n *While) Asserts() []*Node { return n.list1 }
|
||||
func (n *While) Body() []*Node { return n.list2 }
|
||||
|
||||
@@ -447,21 +451,21 @@ func NewWhile(label t.ID, condition *Expr, asserts []*Node, body []*Node) *While
|
||||
return &While{
|
||||
kind: KWhile,
|
||||
id1: label,
|
||||
lhs: condition.Node(),
|
||||
mhs: condition.Node(),
|
||||
list1: asserts,
|
||||
list2: body,
|
||||
}
|
||||
}
|
||||
|
||||
// If is "if LHS { List0 } else RHS" or "if LHS { List0 } else { List1 }":
|
||||
// - LHS: <Expr>
|
||||
// If is "if MHS { List0 } else RHS" or "if MHS { List0 } else { List1 }":
|
||||
// - MHS: <Expr>
|
||||
// - RHS: <nil|If>
|
||||
// - List0: <Statement> if-true body
|
||||
// - List1: <Statement> if-false body
|
||||
type If Node
|
||||
|
||||
func (n *If) Node() *Node { return (*Node)(n) }
|
||||
func (n *If) Condition() *Expr { return n.lhs.Expr() }
|
||||
func (n *If) Condition() *Expr { return n.mhs.Expr() }
|
||||
func (n *If) ElseIf() *If { return n.rhs.If() }
|
||||
func (n *If) BodyIfTrue() []*Node { return n.list0 }
|
||||
func (n *If) BodyIfFalse() []*Node { return n.list1 }
|
||||
@@ -469,7 +473,7 @@ func (n *If) BodyIfFalse() []*Node { return n.list1 }
|
||||
func NewIf(condition *Expr, elseIf *If, bodyIfTrue []*Node, bodyIfFalse []*Node) *If {
|
||||
return &If{
|
||||
kind: KIf,
|
||||
lhs: condition.Node(),
|
||||
mhs: condition.Node(),
|
||||
rhs: elseIf.Node(),
|
||||
list0: bodyIfTrue,
|
||||
list1: bodyIfFalse,
|
||||
|
||||
@@ -203,6 +203,13 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
||||
|
||||
case a.KIterate:
|
||||
n := n.Iterate()
|
||||
unroll := n.UnrollCount()
|
||||
if err := q.tcheckExpr(unroll, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if cv := unroll.ConstValue(); cv == nil {
|
||||
return fmt.Errorf("check: unroll count %q is not constant", unroll.String(q.tm))
|
||||
}
|
||||
for _, o := range n.Variables() {
|
||||
if err := q.tcheckStatement(o); err != nil {
|
||||
return err
|
||||
|
||||
+21
-1
@@ -570,6 +570,26 @@ func (p *parser) parseStatement1() (*a.Node, error) {
|
||||
|
||||
case t.KeyIterate:
|
||||
p.src = p.src[1:]
|
||||
if x := p.peek1().Key(); x != t.KeyDot {
|
||||
got := p.tm.ByKey(x)
|
||||
return nil, fmt.Errorf(`parse: expected ".", got %q at %s:%d`, got, p.filename, p.line())
|
||||
}
|
||||
p.src = p.src[1:]
|
||||
unrollID := p.peek1()
|
||||
unrollStr := p.tm.ByKey(unrollID.Key())
|
||||
if !unrollID.IsLiteral() {
|
||||
return nil, fmt.Errorf(`parse: expected literal unroll count, got %q at %s:%d`,
|
||||
unrollStr, p.filename, p.line())
|
||||
}
|
||||
p.src = p.src[1:]
|
||||
switch unrollStr {
|
||||
default:
|
||||
return nil, fmt.Errorf(`parse: expected power-of-2 unroll count in [1..256], got %q at %s:%d`,
|
||||
unrollStr, p.filename, p.line())
|
||||
case "1", "2", "4", "8", "16", "32", "64", "128", "256":
|
||||
}
|
||||
unroll := a.NewExpr(0, 0, unrollID, nil, nil, nil, nil)
|
||||
|
||||
label, err := p.parseLabel()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -586,7 +606,7 @@ func (p *parser) parseStatement1() (*a.Node, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return a.NewIterate(label, vars, asserts, body).Node(), nil
|
||||
return a.NewIterate(label, unroll, vars, asserts, body).Node(), nil
|
||||
|
||||
case t.KeyWhile:
|
||||
p.src = p.src[1:]
|
||||
|
||||
@@ -61,7 +61,7 @@ pri func adler32.update(x[] u8)(checksum u32) {
|
||||
in.x = in.x[:5552]
|
||||
}
|
||||
|
||||
iterate (p ptr u8:in.x) {
|
||||
iterate.8(p ptr u8:in.x) {
|
||||
s1 ~+= deref p as u32
|
||||
s2 ~+= s1
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user