Skip to content

Commit

Permalink
Fallback code: Always zero memory beforehand
Browse files Browse the repository at this point in the history
This makes the API contract stricter.
  • Loading branch information
Jille committed Sep 13, 2024
1 parent 7641307 commit c2168d8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions purego.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
}
}
10 changes: 5 additions & 5 deletions typeswitch.go
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit c2168d8

Please sign in to comment.