diff --git a/internal/cgen/base/io-private.h b/internal/cgen/base/io-private.h index 2aad2fbd..4b0b8a92 100644 --- a/internal/cgen/base/io-private.h +++ b/internal/cgen/base/io-private.h @@ -229,6 +229,42 @@ wuffs_private_impl__io_writer__limited_copy_u32_from_history_fast( return length; } +// wuffs_private_impl__io_writer__limited_copy_u32_from_history_fast_return_cusp +// is like the +// wuffs_private_impl__io_writer__limited_copy_u32_from_history_fast function, +// but also returns the cusp: a byte pair (as a u16le) being the last byte of +// and next byte after the copied history. +// +// For example, if history was [10, 11, 12, 13, 14, 15, 16, 17, 18] then: +// - copying l=3, d=8 produces [11, 12, 13] and the cusp is (13, 14). +// - copying l=3, d=2 produces [17, 18, 17] and the cusp is (17, 18). +// +// The caller needs to prove that: +// - length <= (io2_w - *ptr_iop_w) +// - distance >= 1 +// - distance <= (*ptr_iop_w - io0_w) +static inline uint32_t // +wuffs_private_impl__io_writer__limited_copy_u32_from_history_fast_return_cusp( + uint8_t** ptr_iop_w, + uint8_t* io0_w, + uint8_t* io2_w, + uint32_t length, + uint32_t distance) { + uint8_t* p = *ptr_iop_w; + uint8_t* q = p - distance; + uint32_t n = length; + for (; n >= 3; n -= 3) { + *p++ = *q++; + *p++ = *q++; + *p++ = *q++; + } + for (; n; n--) { + *p++ = *q++; + } + *ptr_iop_w = p; + return (uint32_t)wuffs_base__peek_u16le__no_bounds_check(q - 1); +} + // wuffs_private_impl__io_writer__limited_copy_u32_from_history_8_byte_chunks_distance_1_fast // copies the previous byte (the one immediately before *ptr_iop_w), copying 8 // byte chunks at a time. Each chunk contains 8 repetitions of the same byte. diff --git a/internal/cgen/builtin.go b/internal/cgen/builtin.go index 0dc2d494..17538ab0 100644 --- a/internal/cgen/builtin.go +++ b/internal/cgen/builtin.go @@ -306,7 +306,8 @@ func (g *gen) writeBuiltinIOWriter(b *buffer, recv *a.Expr, method t.ID, args [] case t.IDLimitedCopyU32FromHistory, t.IDLimitedCopyU32FromHistory8ByteChunksDistance1Fast, t.IDLimitedCopyU32FromHistory8ByteChunksFast, - t.IDLimitedCopyU32FromHistoryFast: + t.IDLimitedCopyU32FromHistoryFast, + t.IDLimitedCopyU32FromHistoryFastReturnCusp: b.printf("wuffs_private_impl__io_writer__%s(\n&%s%s, %s%s, %s%s", method.Str(g.tm), iopPrefix, recvName, io0Prefix, recvName, io2Prefix, recvName) for _, o := range args { diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index cc02bf3f..75b2d1cd 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -579,6 +579,13 @@ var funcsOther = [...]string{ // For now, that's all implicitly checked (i.e. hard coded). "io_writer.limited_copy_u32_from_history_fast!(up_to: u32, distance: u32) u32", + // TODO: this should have explicit pre-conditions: + // - up_to <= this.length() + // - distance >= 1 + // - distance <= this.history_length() + // For now, that's all implicitly checked (i.e. hard coded). + "io_writer.limited_copy_u32_from_history_fast_return_cusp!(up_to: u32, distance: u32) u32[..= 0xFFFF]", + // TODO: this should have explicit pre-conditions: // - (up_to + 8) <= this.length() // - distance >= 8 diff --git a/lang/check/bounds.go b/lang/check/bounds.go index c8809b12..de538319 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -1211,7 +1211,8 @@ func (q *checker) bcheckExprCallSpecialCases(n *a.Expr, depth uint32) (bounds, e return bounds{}, err } - } else if method == t.IDLimitedCopyU32FromHistoryFast { + } else if (method == t.IDLimitedCopyU32FromHistoryFast) || + (method == t.IDLimitedCopyU32FromHistoryFastReturnCusp) { if err := q.canLimitedCopyU32FromHistoryFast(recv, n.Args(), nil, one, nil); err != nil { return bounds{}, err } diff --git a/lang/token/list.go b/lang/token/list.go index 4569406f..09587e10 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -524,9 +524,10 @@ const ( IDLimitedCopyU32FromHistory8ByteChunksDistance1Fast = ID(0x172) IDLimitedCopyU32FromHistory8ByteChunksFast = ID(0x173) IDLimitedCopyU32FromHistoryFast = ID(0x174) - IDLimitedCopyU32FromReader = ID(0x175) - IDLimitedCopyU32FromSlice = ID(0x176) - IDLimitedCopyU32ToSlice = ID(0x177) + IDLimitedCopyU32FromHistoryFastReturnCusp = ID(0x175) + IDLimitedCopyU32FromReader = ID(0x176) + IDLimitedCopyU32FromSlice = ID(0x177) + IDLimitedCopyU32ToSlice = ID(0x178) // -------- 0x180 block. @@ -970,6 +971,7 @@ var builtInsByID = [nBuiltInIDs]string{ IDLimitedCopyU32FromHistory8ByteChunksDistance1Fast: "limited_copy_u32_from_history_8_byte_chunks_distance_1_fast", IDLimitedCopyU32FromHistory8ByteChunksFast: "limited_copy_u32_from_history_8_byte_chunks_fast", IDLimitedCopyU32FromHistoryFast: "limited_copy_u32_from_history_fast", + IDLimitedCopyU32FromHistoryFastReturnCusp: "limited_copy_u32_from_history_fast_return_cusp", IDLimitedCopyU32FromReader: "limited_copy_u32_from_reader", IDLimitedCopyU32FromSlice: "limited_copy_u32_from_slice", IDLimitedCopyU32ToSlice: "limited_copy_u32_to_slice", diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 5f58f500..6d12573c 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -14783,6 +14783,42 @@ wuffs_private_impl__io_writer__limited_copy_u32_from_history_fast( return length; } +// wuffs_private_impl__io_writer__limited_copy_u32_from_history_fast_return_cusp +// is like the +// wuffs_private_impl__io_writer__limited_copy_u32_from_history_fast function, +// but also returns the cusp: a byte pair (as a u16le) being the last byte of +// and next byte after the copied history. +// +// For example, if history was [10, 11, 12, 13, 14, 15, 16, 17, 18] then: +// - copying l=3, d=8 produces [11, 12, 13] and the cusp is (13, 14). +// - copying l=3, d=2 produces [17, 18, 17] and the cusp is (17, 18). +// +// The caller needs to prove that: +// - length <= (io2_w - *ptr_iop_w) +// - distance >= 1 +// - distance <= (*ptr_iop_w - io0_w) +static inline uint32_t // +wuffs_private_impl__io_writer__limited_copy_u32_from_history_fast_return_cusp( + uint8_t** ptr_iop_w, + uint8_t* io0_w, + uint8_t* io2_w, + uint32_t length, + uint32_t distance) { + uint8_t* p = *ptr_iop_w; + uint8_t* q = p - distance; + uint32_t n = length; + for (; n >= 3; n -= 3) { + *p++ = *q++; + *p++ = *q++; + *p++ = *q++; + } + for (; n; n--) { + *p++ = *q++; + } + *ptr_iop_w = p; + return (uint32_t)wuffs_base__peek_u16le__no_bounds_check(q - 1); +} + // wuffs_private_impl__io_writer__limited_copy_u32_from_history_8_byte_chunks_distance_1_fast // copies the previous byte (the one immediately before *ptr_iop_w), copying 8 // byte chunks at a time. Each chunk contains 8 repetitions of the same byte.