mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
Tighten copy_from_history32__bco preconditions
This commit is contained in:
1 parent
4806f6160d
commit
7de631fb96
6 files changed
+7
-10
No files matched your search
@@ -232,7 +232,7 @@ static inline uint32_t wuffs_base__writer1__copy_from_history32(
|
||||
// version of the wuffs_base__writer1__copy_from_history32 function above. The
|
||||
// caller needs to prove that:
|
||||
// - start != NULL
|
||||
// - distance != 0
|
||||
// - distance > 0
|
||||
// - distance <= (*ptr_ptr - start)
|
||||
// - length <= (end - *ptr_ptr)
|
||||
static inline uint32_t wuffs_base__writer1__copy_from_history32__bco(
|
||||
|
||||
@@ -34,7 +34,7 @@ const baseImpl = "" +
|
||||
"_slice_u8){});\n}\n\nstatic inline wuffs_base__slice_u8 wuffs_base__slice_u8__subslice_j(\n wuffs_base__slice_u8 s,\n uint64_t j) {\n if ((j <= SIZE_MAX) && (j <= s.len)) {\n return ((wuffs_base__slice_u8){.ptr = s.ptr, .len = j});\n }\n return ((wuffs_base__slice_u8){});\n}\n\nstatic inline wuffs_base__slice_u8 wuffs_base__slice_u8__subslice_ij(\n wuffs_base__slice_u8 s,\n uint64_t i,\n uint64_t j) {\n if ((i <= j) && (j <= SIZE_MAX) && (j <= s.len)) {\n return ((wuffs_base__slice_u8){\n .ptr = s.ptr + i,\n .len = j - i,\n });\n }\n return ((wuffs_base__slice_u8){});\n}\n\n// wuffs_base__slice_u8__prefix returns up to the first up_to bytes of s.\nstatic inline wuffs_base__slice_u8 wuffs_base__slice_u8__prefix(\n wuffs_base__slice_u8 s,\n uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.len = up_to;\n }\n return s;\n}\n\n// wuffs_base__slice_u8__suffix returns up to the last up_to bytes of s.\nstatic inline wuffs_base__slice_u8 wuffs_base__slice_u8_suffix(\n wuffs_base__slice_u8 " +
|
||||
"s,\n uint64_t up_to) {\n if ((uint64_t)(s.len) > up_to) {\n s.ptr += (uint64_t)(s.len) - up_to;\n s.len = up_to;\n }\n return s;\n}\n\n// wuffs_base__slice_u8__copy_from_slice calls memmove(dst.ptr, src.ptr,\n// length) where length is the minimum of dst.len and src.len.\n//\n// Passing a wuffs_base__slice_u8 with all fields NULL or zero (a valid, empty\n// slice) is valid and results in a no-op.\nstatic inline uint64_t wuffs_base__slice_u8__copy_from_slice(\n wuffs_base__slice_u8 dst,\n wuffs_base__slice_u8 src) {\n size_t length = dst.len < src.len ? dst.len : src.len;\n if (length > 0) {\n memmove(dst.ptr, src.ptr, length);\n }\n return length;\n}\n\nstatic inline uint32_t wuffs_base__writer1__copy_from_history32(\n uint8_t** ptr_ptr,\n uint8_t* start, // May be NULL, meaning an unmarked writer1.\n uint8_t* end,\n uint32_t distance,\n uint32_t length) {\n if (!start || !distance) {\n return 0;\n }\n uint8_t* ptr = *ptr_ptr;\n if ((size_t)(ptr - start) < (size_t)(distance)) {\n return 0;\n }" +
|
||||
"\n start = ptr - distance;\n size_t n = end - ptr;\n if ((size_t)(length) > n) {\n length = n;\n } else {\n n = length;\n }\n // TODO: unrolling by 3 seems best for the std/deflate benchmarks, but that\n // is mostly because 3 is the minimum length for the deflate format. This\n // function implementation shouldn't overfit to that one format. Perhaps the\n // copy_from_history32 Wuffs method should also take an unroll hint argument,\n // and the cgen can look if that argument is the constant expression '3'.\n //\n // See also wuffs_base__writer1__copy_from_history32__bco below.\n //\n // Alternatively, or additionally, have a sloppy_copy_from_history32 method\n // that copies 8 bytes at a time, possibly writing more than length bytes?\n for (; n >= 3; n -= 3) {\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n }\n for (; n; n--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\n// wuffs_base__writer1__copy_from_history32__bco is a Bounds Check Optimized\n// version of " +
|
||||
"the wuffs_base__writer1__copy_from_history32 function above. The\n// caller needs to prove that:\n// - start != NULL\n// - distance != 0\n// - distance <= (*ptr_ptr - start)\n// - length <= (end - *ptr_ptr)\nstatic inline uint32_t wuffs_base__writer1__copy_from_history32__bco(\n uint8_t** ptr_ptr,\n uint8_t* start,\n uint8_t* end,\n uint32_t distance,\n uint32_t length) {\n uint8_t* ptr = *ptr_ptr;\n start = ptr - distance;\n uint32_t n = length;\n for (; n >= 3; n -= 3) {\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n }\n for (; n; n--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\nstatic inline uint32_t wuffs_base__writer1__copy_from_reader32(\n uint8_t** ptr_wptr,\n uint8_t* wend,\n uint8_t** ptr_rptr,\n uint8_t* rend,\n uint32_t length) {\n uint8_t* wptr = *ptr_wptr;\n size_t n = length;\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n uint8_t* rptr = *ptr_rptr;\n if (n > rend - rptr) {\n n = rend - rptr;\n }\n if (n > 0) {\n " +
|
||||
"the wuffs_base__writer1__copy_from_history32 function above. The\n// caller needs to prove that:\n// - start != NULL\n// - distance > 0\n// - distance <= (*ptr_ptr - start)\n// - length <= (end - *ptr_ptr)\nstatic inline uint32_t wuffs_base__writer1__copy_from_history32__bco(\n uint8_t** ptr_ptr,\n uint8_t* start,\n uint8_t* end,\n uint32_t distance,\n uint32_t length) {\n uint8_t* ptr = *ptr_ptr;\n start = ptr - distance;\n uint32_t n = length;\n for (; n >= 3; n -= 3) {\n *ptr++ = *start++;\n *ptr++ = *start++;\n *ptr++ = *start++;\n }\n for (; n; n--) {\n *ptr++ = *start++;\n }\n *ptr_ptr = ptr;\n return length;\n}\n\nstatic inline uint32_t wuffs_base__writer1__copy_from_reader32(\n uint8_t** ptr_wptr,\n uint8_t* wend,\n uint8_t** ptr_rptr,\n uint8_t* rend,\n uint32_t length) {\n uint8_t* wptr = *ptr_wptr;\n size_t n = length;\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n uint8_t* rptr = *ptr_rptr;\n if (n > rend - rptr) {\n n = rend - rptr;\n }\n if (n > 0) {\n " +
|
||||
"memmove(wptr, rptr, n);\n *ptr_wptr += n;\n *ptr_rptr += n;\n }\n return n;\n}\n\nstatic inline uint64_t wuffs_base__writer1__copy_from_slice(\n uint8_t** ptr_wptr,\n uint8_t* wend,\n wuffs_base__slice_u8 src) {\n uint8_t* wptr = *ptr_wptr;\n size_t n = src.len;\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n if (n > 0) {\n memmove(wptr, src.ptr, n);\n *ptr_wptr += n;\n }\n return n;\n}\n\nstatic inline uint32_t wuffs_base__writer1__copy_from_slice32(\n uint8_t** ptr_wptr,\n uint8_t* wend,\n wuffs_base__slice_u8 src,\n uint32_t length) {\n uint8_t* wptr = *ptr_wptr;\n size_t n = src.len;\n if (n > length) {\n n = length;\n }\n if (n > wend - wptr) {\n n = wend - wptr;\n }\n if (n > 0) {\n memmove(wptr, src.ptr, n);\n *ptr_wptr += n;\n }\n return n;\n}\n\n// Note that the *__limit and *__mark methods are private (in base-impl.h) not\n// public (in base-header.h). We assume that, at the boundary between user code\n// and Wuffs code, the reader1 and writer1's private_impl fields (including" +
|
||||
"\n// limit and mark) are NULL. Otherwise, some internal assumptions break down.\n// For example, limits could be represented as pointers, even though\n// conceptually they are counts, but that pointer-to-count correspondence\n// becomes invalid if a buffer is re-used (e.g. on resuming a coroutine).\n//\n// Admittedly, some of the Wuffs test code calls these methods, but that test\n// code is still Wuffs code, not user code. Other Wuffs test code modifies\n// private_impl fields directly.\n\nstatic inline wuffs_base__reader1 wuffs_base__reader1__limit(\n wuffs_base__reader1* o,\n uint64_t* ptr_to_len) {\n wuffs_base__reader1 ret = *o;\n ret.private_impl.limit.ptr_to_len = ptr_to_len;\n ret.private_impl.limit.next = &o->private_impl.limit;\n return ret;\n}\n\nstatic inline wuffs_base__empty_struct wuffs_base__reader1__mark(\n wuffs_base__reader1* o,\n uint8_t* mark) {\n o->private_impl.mark = mark;\n return ((wuffs_base__empty_struct){});\n}\n\n// TODO: static inline wuffs_base__writer1 wuffs_base__writer1__limit()\n\nst" +
|
||||
"atic inline wuffs_base__empty_struct wuffs_base__writer1__mark(\n wuffs_base__writer1* o,\n uint8_t* mark) {\n o->private_impl.mark = mark;\n return ((wuffs_base__empty_struct){});\n}\n" +
|
||||
|
||||
+1
-1
@@ -587,7 +587,7 @@ static inline uint32_t wuffs_base__writer1__copy_from_history32(
|
||||
// version of the wuffs_base__writer1__copy_from_history32 function above. The
|
||||
// caller needs to prove that:
|
||||
// - start != NULL
|
||||
// - distance != 0
|
||||
// - distance > 0
|
||||
// - distance <= (*ptr_ptr - start)
|
||||
// - length <= (end - *ptr_ptr)
|
||||
static inline uint32_t wuffs_base__writer1__copy_from_history32__bco(
|
||||
|
||||
+1
-1
@@ -605,7 +605,7 @@ static inline uint32_t wuffs_base__writer1__copy_from_history32(
|
||||
// version of the wuffs_base__writer1__copy_from_history32 function above. The
|
||||
// caller needs to prove that:
|
||||
// - start != NULL
|
||||
// - distance != 0
|
||||
// - distance > 0
|
||||
// - distance <= (*ptr_ptr - start)
|
||||
// - length <= (end - *ptr_ptr)
|
||||
static inline uint32_t wuffs_base__writer1__copy_from_history32__bco(
|
||||
|
||||
+1
-1
@@ -716,7 +716,7 @@ static inline uint32_t wuffs_base__writer1__copy_from_history32(
|
||||
// version of the wuffs_base__writer1__copy_from_history32 function above. The
|
||||
// caller needs to prove that:
|
||||
// - start != NULL
|
||||
// - distance != 0
|
||||
// - distance > 0
|
||||
// - distance <= (*ptr_ptr - start)
|
||||
// - length <= (end - *ptr_ptr)
|
||||
static inline uint32_t wuffs_base__writer1__copy_from_history32__bco(
|
||||
|
||||
@@ -82,7 +82,7 @@ func (q *checker) optimizeNonSuspendible(n *a.Expr) error {
|
||||
// Check Optimized version of CopyFromHistory32. As per cgen/base-impl.h, the
|
||||
// conditions are:
|
||||
// - start != NULL
|
||||
// - distance != 0
|
||||
// - distance > 0
|
||||
// - distance <= (*ptr_ptr - start)
|
||||
// - length <= (end - *ptr_ptr)
|
||||
func (q *checker) optimizeCopyFromHistory32(n *a.Expr) error {
|
||||
@@ -106,10 +106,7 @@ check0:
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check "distance != 0".
|
||||
//
|
||||
// TODO: check that typeof(distance) is an unsigned integer type, so that
|
||||
// an equivalent test is "distance > 0".
|
||||
// Check "distance > 0".
|
||||
check1:
|
||||
for {
|
||||
for _, x := range q.facts {
|
||||
|
||||
Reference in new issue
Block a user