From c2168d89a8b835b2482fbbb20033ae3469f60392 Mon Sep 17 00:00:00 2001 From: Jille Timmermans Date: Fri, 13 Sep 2024 09:41:44 +0200 Subject: [PATCH] Fallback code: Always zero memory beforehand This makes the API contract stricter. --- README.md | 1 + purego.go | 11 +++++++++++ typeswitch.go | 10 +++++----- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 14e8757..c7bc212 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ The easiest way to explain is to just show the fallback code. ```go func goVectorEquals[T uint8 | uint16 | uint32 | uint64](dst []byte, search T, rows []T) { + clear(dst) for i, v := range rows { if search == v { dst[i/8] |= 1 << (i % 8) diff --git a/purego.go b/purego.go index 8ec3759..0b1981b 100644 --- a/purego.go +++ b/purego.go @@ -1,6 +1,7 @@ package vectorcmp func goVectorEquals[T uint8 | uint16 | uint32 | uint64](dst []byte, search T, rows []T) { + zero(dst) for i, v := range rows { if search == v { dst[i/8] |= 1 << (i % 8) @@ -9,6 +10,7 @@ func goVectorEquals[T uint8 | uint16 | uint32 | uint64](dst []byte, search T, ro } func goVectorGreaterThan[T uint8 | uint16 | uint32 | uint64](dst []byte, search T, rows []T) { + zero(dst) for i, v := range rows { if search > v { dst[i/8] |= 1 << (i % 8) @@ -17,6 +19,7 @@ func goVectorGreaterThan[T uint8 | uint16 | uint32 | uint64](dst []byte, search } func goVectorLessThan[T uint8 | uint16 | uint32 | uint64](dst []byte, search T, rows []T) { + zero(dst) for i, v := range rows { if search < v { dst[i/8] |= 1 << (i % 8) @@ -25,6 +28,7 @@ func goVectorLessThan[T uint8 | uint16 | uint32 | uint64](dst []byte, search T, } func goVectorGreaterEquals[T uint8 | uint16 | uint32 | uint64](dst []byte, search T, rows []T) { + zero(dst) for i, v := range rows { if search >= v { dst[i/8] |= 1 << (i % 8) @@ -33,9 +37,16 @@ func goVectorGreaterEquals[T uint8 | uint16 | uint32 | uint64](dst []byte, searc } func goVectorLesserEquals[T uint8 | uint16 | uint32 | uint64](dst []byte, search T, rows []T) { + zero(dst) for i, v := range rows { if search <= v { dst[i/8] |= 1 << (i % 8) } } } + +func zero(dst []byte) { + for i := range dst { + dst[i] = 0 + } +} diff --git a/typeswitch.go b/typeswitch.go index ad35008..3e4fce2 100644 --- a/typeswitch.go +++ b/typeswitch.go @@ -1,9 +1,9 @@ package vectorcmp // VectorEquals compares each entry in rows to search and enables the corresponding bit in dst if equal. -// dst must be zeroed before this call. // Prefer calling VectorEquals8/16/32/64 directly. // +// clear(dst) // for i, v := range rows { // if search == v { // dst[i/8] |= 1 << (i % 8) @@ -25,9 +25,9 @@ func VectorEquals[T uint8 | uint16 | uint32 | uint64](dst []byte, search T, rows } // VectorGreaterThan compares each entry in rows to search and enables the corresponding bit in dst if search > rows[i]. -// dst must be zeroed before this call. // Prefer calling VectorGreaterThan8/16/32/64 directly. // +// clear(dst) // for i, v := range rows { // if search > v { // dst[i/8] |= 1 << (i % 8) @@ -49,9 +49,9 @@ func VectorGreaterThan[T uint8 | uint16 | uint32 | uint64](dst []byte, search T, } // VectorLessThan compares each entry in rows to search and enables the corresponding bit in dst if search < rows[i]. -// dst must be zeroed before this call. // Prefer calling VectorLessThan8/16/32/64 directly. // +// clear(dst) // for i, v := range rows { // if search < v { // dst[i/8] |= 1 << (i % 8) @@ -73,9 +73,9 @@ func VectorLessThan[T uint8 | uint16 | uint32 | uint64](dst []byte, search T, ro } // VectorGreaterEquals compares each entry in rows to search and enables the corresponding bit in dst if search >= rows[i]. -// dst must be zeroed before this call. // Prefer calling VectorGreaterEquals8/16/32/64 directly. // +// clear(dst) // for i, v := range rows { // if search >= v { // dst[i/8] |= 1 << (i % 8) @@ -97,9 +97,9 @@ func VectorGreaterEquals[T uint8 | uint16 | uint32 | uint64](dst []byte, search } // VectorLesserEquals compares each entry in rows to search and enables the corresponding bit in dst if search <= rows[i]. -// dst must be zeroed before this call. // Prefer calling VectorLesserEquals8/16/32/64 directly. // +// clear(dst) // for i, v := range rows { // if search <= v { // dst[i/8] |= 1 << (i % 8)