From 58cd282138334159a264f4ea9d0a328521415d05 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 4 May 2018 14:00:04 +1000 Subject: [PATCH] Unroll the std/crc32 loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old speed new speed delta wuffs_crc32_ieee_10k/clang 2.30GB/s ± 1% 2.37GB/s ± 0% +3.31% (p=0.016 n=5+4) wuffs_crc32_ieee_100k/clang 2.30GB/s ± 0% 2.35GB/s ± 0% +2.39% (p=0.008 n=5+5) wuffs_crc32_ieee_10k/gcc 2.49GB/s ± 1% 2.72GB/s ± 0% +9.19% (p=0.008 n=5+5) wuffs_crc32_ieee_100k/gcc 2.50GB/s ± 0% 2.70GB/s ± 1% +8.18% (p=0.008 n=5+5) wuffs_gzip_decode_10k/clang 206MB/s ± 2% 207MB/s ± 0% ~ (p=1.000 n=5+5) wuffs_gzip_decode_100k/clang 231MB/s ± 2% 235MB/s ± 0% +1.57% (p=0.008 n=5+5) wuffs_gzip_decode_10k/gcc 234MB/s ± 0% 235MB/s ± 1% ~ (p=0.095 n=5+5) wuffs_gzip_decode_100k/gcc 267MB/s ± 0% 266MB/s ± 2% ~ (p=0.690 n=5+5) --- cmd/wuffs-c/internal/cgen/statement.go | 18 ++++- gen/c/std/crc32.c | 102 ++++++++++++++++++++++++- std/crc32/common_crc32.wuffs | 2 +- 3 files changed, 118 insertions(+), 4 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/statement.go b/cmd/wuffs-c/internal/cgen/statement.go index 9837bfb1..fcd73657 100644 --- a/cmd/wuffs-c/internal/cgen/statement.go +++ b/cmd/wuffs-c/internal/cgen/statement.go @@ -244,10 +244,10 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { unroll := n.Unroll().SmallPowerOf2Value() step := n.Step().SmallPowerOf2Value() - if uStep := unroll * step; uStep > 1 { + if unroll > 1 || step > 1 { b.printf("%s%s.len = %d;\n", vPrefix, name, step) b.printf("uint8_t* %send0_%s = %sslice_%s.ptr + (%sslice_%s.len / %d) * %d;\n", - iPrefix, name, iPrefix, name, iPrefix, name, uStep, uStep) + iPrefix, name, iPrefix, name, iPrefix, name, unroll*step, unroll*step) b.printf("while (%s%s.ptr < %send0_%s) {\n", vPrefix, name, iPrefix, name) for i := 0; i < unroll; i++ { for _, o := range n.Body() { @@ -260,6 +260,20 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { b.writes("}\n") } + if unroll > 1 && step > 1 { + b.printf("%s%s.len = %d;\n", vPrefix, name, step) + b.printf("uint8_t* %send1_%s = %sslice_%s.ptr + (%sslice_%s.len / %d) * %d;\n", + iPrefix, name, iPrefix, name, iPrefix, name, step, step) + b.printf("while (%s%s.ptr < %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.ptr += %d;\n", vPrefix, name, step) + b.writes("}\n") + } + { b.printf("%s%s.len = %d;\n", vPrefix, name, 1) b.printf("uint8_t* %send2_%s = %sslice_%s.ptr + %sslice_%s.len;\n", diff --git a/gen/c/std/crc32.c b/gen/c/std/crc32.c index 6e7d7428..681faa55 100644 --- a/gen/c/std/crc32.c +++ b/gen/c/std/crc32.c @@ -2017,7 +2017,7 @@ uint32_t wuffs_crc32__ieee_hasher__update(wuffs_crc32__ieee_hasher* self, wuffs_base__slice_u8 i_slice_p = a_x; wuffs_base__slice_u8 v_p = i_slice_p; v_p.len = 8; - uint8_t* i_end0_p = i_slice_p.ptr + (i_slice_p.len / 8) * 8; + uint8_t* i_end0_p = i_slice_p.ptr + (i_slice_p.len / 64) * 64; while (v_p.ptr < i_end0_p) { v_s ^= ((((uint32_t)(v_p.ptr[0])) << 0) | (((uint32_t)(v_p.ptr[1])) << 8) | @@ -2031,6 +2031,106 @@ uint32_t wuffs_crc32__ieee_hasher__update(wuffs_crc32__ieee_hasher* self, wuffs_crc32__ieee_table[6][255 & (v_s >> 8)] ^ wuffs_crc32__ieee_table[7][255 & (v_s >> 0)]); v_p.ptr += 8; + v_s ^= + ((((uint32_t)(v_p.ptr[0])) << 0) | (((uint32_t)(v_p.ptr[1])) << 8) | + (((uint32_t)(v_p.ptr[2])) << 16) | (((uint32_t)(v_p.ptr[3])) << 24)); + v_s = (wuffs_crc32__ieee_table[0][v_p.ptr[7]] ^ + wuffs_crc32__ieee_table[1][v_p.ptr[6]] ^ + wuffs_crc32__ieee_table[2][v_p.ptr[5]] ^ + wuffs_crc32__ieee_table[3][v_p.ptr[4]] ^ + wuffs_crc32__ieee_table[4][255 & (v_s >> 24)] ^ + wuffs_crc32__ieee_table[5][255 & (v_s >> 16)] ^ + wuffs_crc32__ieee_table[6][255 & (v_s >> 8)] ^ + wuffs_crc32__ieee_table[7][255 & (v_s >> 0)]); + v_p.ptr += 8; + v_s ^= + ((((uint32_t)(v_p.ptr[0])) << 0) | (((uint32_t)(v_p.ptr[1])) << 8) | + (((uint32_t)(v_p.ptr[2])) << 16) | (((uint32_t)(v_p.ptr[3])) << 24)); + v_s = (wuffs_crc32__ieee_table[0][v_p.ptr[7]] ^ + wuffs_crc32__ieee_table[1][v_p.ptr[6]] ^ + wuffs_crc32__ieee_table[2][v_p.ptr[5]] ^ + wuffs_crc32__ieee_table[3][v_p.ptr[4]] ^ + wuffs_crc32__ieee_table[4][255 & (v_s >> 24)] ^ + wuffs_crc32__ieee_table[5][255 & (v_s >> 16)] ^ + wuffs_crc32__ieee_table[6][255 & (v_s >> 8)] ^ + wuffs_crc32__ieee_table[7][255 & (v_s >> 0)]); + v_p.ptr += 8; + v_s ^= + ((((uint32_t)(v_p.ptr[0])) << 0) | (((uint32_t)(v_p.ptr[1])) << 8) | + (((uint32_t)(v_p.ptr[2])) << 16) | (((uint32_t)(v_p.ptr[3])) << 24)); + v_s = (wuffs_crc32__ieee_table[0][v_p.ptr[7]] ^ + wuffs_crc32__ieee_table[1][v_p.ptr[6]] ^ + wuffs_crc32__ieee_table[2][v_p.ptr[5]] ^ + wuffs_crc32__ieee_table[3][v_p.ptr[4]] ^ + wuffs_crc32__ieee_table[4][255 & (v_s >> 24)] ^ + wuffs_crc32__ieee_table[5][255 & (v_s >> 16)] ^ + wuffs_crc32__ieee_table[6][255 & (v_s >> 8)] ^ + wuffs_crc32__ieee_table[7][255 & (v_s >> 0)]); + v_p.ptr += 8; + v_s ^= + ((((uint32_t)(v_p.ptr[0])) << 0) | (((uint32_t)(v_p.ptr[1])) << 8) | + (((uint32_t)(v_p.ptr[2])) << 16) | (((uint32_t)(v_p.ptr[3])) << 24)); + v_s = (wuffs_crc32__ieee_table[0][v_p.ptr[7]] ^ + wuffs_crc32__ieee_table[1][v_p.ptr[6]] ^ + wuffs_crc32__ieee_table[2][v_p.ptr[5]] ^ + wuffs_crc32__ieee_table[3][v_p.ptr[4]] ^ + wuffs_crc32__ieee_table[4][255 & (v_s >> 24)] ^ + wuffs_crc32__ieee_table[5][255 & (v_s >> 16)] ^ + wuffs_crc32__ieee_table[6][255 & (v_s >> 8)] ^ + wuffs_crc32__ieee_table[7][255 & (v_s >> 0)]); + v_p.ptr += 8; + v_s ^= + ((((uint32_t)(v_p.ptr[0])) << 0) | (((uint32_t)(v_p.ptr[1])) << 8) | + (((uint32_t)(v_p.ptr[2])) << 16) | (((uint32_t)(v_p.ptr[3])) << 24)); + v_s = (wuffs_crc32__ieee_table[0][v_p.ptr[7]] ^ + wuffs_crc32__ieee_table[1][v_p.ptr[6]] ^ + wuffs_crc32__ieee_table[2][v_p.ptr[5]] ^ + wuffs_crc32__ieee_table[3][v_p.ptr[4]] ^ + wuffs_crc32__ieee_table[4][255 & (v_s >> 24)] ^ + wuffs_crc32__ieee_table[5][255 & (v_s >> 16)] ^ + wuffs_crc32__ieee_table[6][255 & (v_s >> 8)] ^ + wuffs_crc32__ieee_table[7][255 & (v_s >> 0)]); + v_p.ptr += 8; + v_s ^= + ((((uint32_t)(v_p.ptr[0])) << 0) | (((uint32_t)(v_p.ptr[1])) << 8) | + (((uint32_t)(v_p.ptr[2])) << 16) | (((uint32_t)(v_p.ptr[3])) << 24)); + v_s = (wuffs_crc32__ieee_table[0][v_p.ptr[7]] ^ + wuffs_crc32__ieee_table[1][v_p.ptr[6]] ^ + wuffs_crc32__ieee_table[2][v_p.ptr[5]] ^ + wuffs_crc32__ieee_table[3][v_p.ptr[4]] ^ + wuffs_crc32__ieee_table[4][255 & (v_s >> 24)] ^ + wuffs_crc32__ieee_table[5][255 & (v_s >> 16)] ^ + wuffs_crc32__ieee_table[6][255 & (v_s >> 8)] ^ + wuffs_crc32__ieee_table[7][255 & (v_s >> 0)]); + v_p.ptr += 8; + v_s ^= + ((((uint32_t)(v_p.ptr[0])) << 0) | (((uint32_t)(v_p.ptr[1])) << 8) | + (((uint32_t)(v_p.ptr[2])) << 16) | (((uint32_t)(v_p.ptr[3])) << 24)); + v_s = (wuffs_crc32__ieee_table[0][v_p.ptr[7]] ^ + wuffs_crc32__ieee_table[1][v_p.ptr[6]] ^ + wuffs_crc32__ieee_table[2][v_p.ptr[5]] ^ + wuffs_crc32__ieee_table[3][v_p.ptr[4]] ^ + wuffs_crc32__ieee_table[4][255 & (v_s >> 24)] ^ + wuffs_crc32__ieee_table[5][255 & (v_s >> 16)] ^ + wuffs_crc32__ieee_table[6][255 & (v_s >> 8)] ^ + wuffs_crc32__ieee_table[7][255 & (v_s >> 0)]); + v_p.ptr += 8; + } + v_p.len = 8; + uint8_t* i_end1_p = i_slice_p.ptr + (i_slice_p.len / 8) * 8; + while (v_p.ptr < i_end1_p) { + v_s ^= + ((((uint32_t)(v_p.ptr[0])) << 0) | (((uint32_t)(v_p.ptr[1])) << 8) | + (((uint32_t)(v_p.ptr[2])) << 16) | (((uint32_t)(v_p.ptr[3])) << 24)); + v_s = (wuffs_crc32__ieee_table[0][v_p.ptr[7]] ^ + wuffs_crc32__ieee_table[1][v_p.ptr[6]] ^ + wuffs_crc32__ieee_table[2][v_p.ptr[5]] ^ + wuffs_crc32__ieee_table[3][v_p.ptr[4]] ^ + wuffs_crc32__ieee_table[4][255 & (v_s >> 24)] ^ + wuffs_crc32__ieee_table[5][255 & (v_s >> 16)] ^ + wuffs_crc32__ieee_table[6][255 & (v_s >> 8)] ^ + wuffs_crc32__ieee_table[7][255 & (v_s >> 0)]); + v_p.ptr += 8; } v_p.len = 1; uint8_t* i_end2_p = i_slice_p.ptr + i_slice_p.len; diff --git a/std/crc32/common_crc32.wuffs b/std/crc32/common_crc32.wuffs index c1f3de70..975c1d3b 100644 --- a/std/crc32/common_crc32.wuffs +++ b/std/crc32/common_crc32.wuffs @@ -27,7 +27,7 @@ pub struct ieee_hasher?( pub func ieee_hasher.update(x slice base.u8)(checksum base.u32) { var s base.u32 = 0xFFFFFFFF ^ this.state - iterate (step:8, unroll:1)(p slice base.u8:in.x) { + iterate (step:8, unroll:8)(p slice base.u8:in.x) { // TODO: explain the slicing-by-8 algorithm. s ^= ((p[0] as base.u32) << 0) | ((p[1] as base.u32) << 8) |