From f64bf4af6166f0d66fe8e5c0db61b1d0a5fa6945 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 4 May 2018 11:54:36 +1000 Subject: [PATCH] Allow p[i] for ptr-typed p; speed up std/crc32 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 483MB/s ± 1% 2263MB/s ± 3% +368.94% (p=0.000 n=48+5) wuffs_crc32_ieee_100k/clang 482MB/s ± 1% 2257MB/s ± 3% +367.96% (p=0.000 n=48+5) wuffs_crc32_ieee_10k/gcc 419MB/s ± 1% 2473MB/s ± 1% +489.93% (p=0.000 n=48+5) wuffs_crc32_ieee_100k/gcc 419MB/s ± 1% 2465MB/s ± 1% +488.02% (p=0.000 n=47+5) wuffs_gzip_decode_10k/clang 153MB/s ± 1% 208MB/s ± 0% +35.30% (p=0.000 n=48+5) wuffs_gzip_decode_100k/clang 168MB/s ± 1% 236MB/s ± 0% +40.67% (p=0.000 n=50+4) wuffs_gzip_decode_10k/gcc 156MB/s ± 1% 231MB/s ± 3% +47.91% (p=0.000 n=50+5) wuffs_gzip_decode_100k/gcc 172MB/s ± 1% 267MB/s ± 0% +55.64% (p=0.000 n=48+5) --- cmd/wuffs-c/internal/cgen/statement.go | 8 +------- doc/changelog.md | 1 + gen/c/std/crc32.c | 10 +++++++++- lang/ast/ast.go | 5 +++++ lang/check/bounds.go | 4 ++++ lang/check/type.go | 5 +++-- std/crc32/common_crc32.wuffs | 13 ++++++++++++- 7 files changed, 35 insertions(+), 11 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/statement.go b/cmd/wuffs-c/internal/cgen/statement.go index 99db62bd..0489cd58 100644 --- a/cmd/wuffs-c/internal/cgen/statement.go +++ b/cmd/wuffs-c/internal/cgen/statement.go @@ -244,14 +244,8 @@ 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 { - // TODO: remove this hack. - hack := 1 - if step != 1 { - hack = 0 - } - b.printf("uint8_t* %send0_%s = %sslice_%s.ptr + (%sslice_%s.len / %d) * %d;\n", - iPrefix, name, iPrefix, name, iPrefix, name, uStep, uStep*hack) + iPrefix, name, iPrefix, name, iPrefix, name, uStep, uStep) b.printf("while (%s%s < %send0_%s) {\n", vPrefix, name, iPrefix, name) for i := 0; i < unroll; i++ { for _, o := range n.Body() { diff --git a/doc/changelog.md b/doc/changelog.md index 0d903c60..9618add0 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -19,6 +19,7 @@ - Renamed `buf1`, `reader1`, etc to `io_buffer`, `io_reader`, etc. - Renamed `u32`, `io_reader`, etc to `base.u32`, `base.io_reader`, etc. - Added a `base.image_config` type. +- Allow `p[i]` for pointer-typed `p`. - Tweaked how marks and limits work. - Supported animated (not just single frame) GIFs. - Marked the `std/gif` LZW decoder as private. diff --git a/gen/c/std/crc32.c b/gen/c/std/crc32.c index c78035ea..27a5a10c 100644 --- a/gen/c/std/crc32.c +++ b/gen/c/std/crc32.c @@ -2016,9 +2016,17 @@ uint32_t wuffs_crc32__ieee_hasher__update(wuffs_crc32__ieee_hasher* self, { wuffs_base__slice_u8 i_slice_p = a_x; uint8_t* v_p = i_slice_p.ptr; - uint8_t* i_end0_p = i_slice_p.ptr + (i_slice_p.len / 8) * 0; + uint8_t* i_end0_p = i_slice_p.ptr + (i_slice_p.len / 8) * 8; while (v_p < i_end0_p) { v_s ^= (wuffs_base__load_u32le(v_p + 0)); + v_s = (wuffs_crc32__ieee_table[0][v_p[7]] ^ + wuffs_crc32__ieee_table[1][v_p[6]] ^ + wuffs_crc32__ieee_table[2][v_p[5]] ^ + wuffs_crc32__ieee_table[3][v_p[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 += 8; } uint8_t* i_end1_p = i_slice_p.ptr + i_slice_p.len; diff --git a/lang/ast/ast.go b/lang/ast/ast.go index e4330221..29604819 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -729,6 +729,11 @@ func (n *TypeExpr) IsArrayType() bool { return n.id0 == t.IDArray } +func (n *TypeExpr) IsPtrType() bool { + // TODO: nptr?? + return n.id0 == t.IDPtr +} + func (n *TypeExpr) IsSliceType() bool { return n.id0 == t.IDSlice } diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 2bcf6f85..1be5038c 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -855,6 +855,9 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, lengthExpr := (*a.Expr)(nil) if lTyp := lhs.MType(); lTyp.IsArrayType() { lengthExpr = lTyp.ArrayLength() + } else if lTyp.IsPtrType() { + // TODO: don't skip bounds checking p[i] for ptr-typed p. + break } else { lengthExpr = makeSliceLengthExpr(lhs) } @@ -874,6 +877,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, return nil, nil, nil } + // TODO: slicing "ptr T" types? lengthExpr := (*a.Expr)(nil) if lTyp := lhs.MType(); lTyp.IsArrayType() { lengthExpr = lTyp.ArrayLength() diff --git a/lang/check/type.go b/lang/check/type.go index 5f00c369..bd102ac8 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -471,8 +471,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { return err } lTyp := lhs.MType() - if key := lTyp.Decorator(); key != t.IDArray && key != t.IDSlice { - return fmt.Errorf("check: %s is an index expression but %s has type %s, not an array or slice type", + if key := lTyp.Decorator(); key != t.IDArray && key != t.IDPtr && key != t.IDSlice { + return fmt.Errorf("check: %s is an index expression but %s has type %s, not an array, ptr or slice type", n.Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm)) } rTyp := rhs.MType() @@ -518,6 +518,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { n.Str(q.tm), lhs.Str(q.tm), lTyp.Str(q.tm)) case t.IDArray: n.SetMType(a.NewTypeExpr(t.IDSlice, 0, 0, nil, nil, lTyp.Inner())) + // TODO: case t.IDPtr? case t.IDSlice: n.SetMType(lTyp) } diff --git a/std/crc32/common_crc32.wuffs b/std/crc32/common_crc32.wuffs index e2701d67..d097af25 100644 --- a/std/crc32/common_crc32.wuffs +++ b/std/crc32/common_crc32.wuffs @@ -28,8 +28,19 @@ 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 ptr base.u8:in.x) { - // TODO: bounds checking for the ptr base.u8 methods. + // TODO: explain the slicing-by-8 algorithm. + // + // TODO: bounds checking for the ptr base.u8 methods and the index + // expressions "p[7]" etc. s ^= p.read_u32le(at:0) + s = ieee_table[0][p[7]] ^ + ieee_table[1][p[6]] ^ + ieee_table[2][p[5]] ^ + ieee_table[3][p[4]] ^ + ieee_table[4][0xFF & (s >> 24)] ^ + ieee_table[5][0xFF & (s >> 16)] ^ + ieee_table[6][0xFF & (s >> 8)] ^ + ieee_table[7][0xFF & (s >> 0)] } else { s = ieee_table[0][((s & 0xFF) as base.u8) ^ deref p] ^ (s >> 8) }