Merge core:simd/util into core:bytes

This commit is contained in:
Feoramund
2024-08-10 07:17:03 -04:00
parent e7e7fe766a
commit c69fa87d53
8 changed files with 164 additions and 246 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
package benchmarks
@(require) import "bytes"
@(require) import "crypto"
@(require) import "hash"
@(require) import "simd/util"
@@ -1,15 +1,15 @@
package benchmark_simd_util
package benchmark_bytes
import "core:bytes"
import "core:fmt"
import "core:log"
import simd_util "core:simd/util"
import "core:testing"
import "core:time"
// These are the normal, unoptimized algorithms.
plain_index_byte :: proc "contextless" (s: []u8, c: byte) -> (res: int) #no_bounds_check {
plain_index_byte :: proc(s: []u8, c: byte) -> (res: int) #no_bounds_check {
for i := 0; i < len(s); i += 1 {
if s[i] == c {
return i
@@ -18,7 +18,7 @@ plain_index_byte :: proc "contextless" (s: []u8, c: byte) -> (res: int) #no_boun
return -1
}
plain_last_index_byte :: proc "contextless" (s: []u8, c: byte) -> (res: int) #no_bounds_check {
plain_last_index_byte :: proc(s: []u8, c: byte) -> (res: int) #no_bounds_check {
for i := len(s)-1; i >= 0; i -= 1 {
if s[i] == c {
return i
@@ -37,7 +37,7 @@ sizes := [?]int {
1024 * 1024 * 1024,
}
run_trial_size :: proc(p: proc "contextless" ([]u8, byte) -> int, size: int, idx: int, warmup: int, runs: int) -> (timing: time.Duration) {
run_trial_size :: proc(p: proc([]u8, byte) -> int, size: int, idx: int, warmup: int, runs: int) -> (timing: time.Duration) {
data := make([]u8, size)
defer delete(data)
@@ -95,9 +95,9 @@ benchmark_plain_index_hot :: proc(t: ^testing.T) {
benchmark_simd_index_cold :: proc(t: ^testing.T) {
report: string
for size in sizes {
timing := run_trial_size(simd_util.index_byte, size, size - 1, 0, 1)
timing := run_trial_size(bytes.index_byte, size, size - 1, 0, 1)
report = fmt.tprintf("%s\n +++ % 8M | %v", report, size, timing)
timing = run_trial_size(simd_util.last_index_byte, size, 0, 0, 1)
timing = run_trial_size(bytes.last_index_byte, size, 0, 0, 1)
report = fmt.tprintf("%s\n (last) +++ % 8M | %v", report, size, timing)
}
log.info(report)
@@ -107,9 +107,9 @@ benchmark_simd_index_cold :: proc(t: ^testing.T) {
benchmark_simd_index_hot :: proc(t: ^testing.T) {
report: string
for size in sizes {
timing := run_trial_size(simd_util.index_byte, size, size - 1, HOT, HOT)
timing := run_trial_size(bytes.index_byte, size, size - 1, HOT, HOT)
report = fmt.tprintf("%s\n +++ % 8M | %v", report, size, timing)
timing = run_trial_size(simd_util.last_index_byte, size, 0, HOT, HOT)
timing = run_trial_size(bytes.last_index_byte, size, 0, HOT, HOT)
report = fmt.tprintf("%s\n (last) +++ % 8M | %v", report, size, timing)
}
log.info(report)
@@ -1,6 +1,6 @@
package test_core_simd_util
package test_core_bytes
import simd_util "core:simd/util"
import "core:bytes"
import "core:testing"
@test
@@ -15,30 +15,30 @@ test_index_byte_sanity :: proc(t: ^testing.T) {
// Find it at the end.
data[n-1] = 'o'
if !testing.expect_value(t, simd_util.index_byte(data, 'o'), n-1) {
if !testing.expect_value(t, bytes.index_byte(data, 'o'), n-1) {
return
}
if !testing.expect_value(t, simd_util.last_index_byte(data, 'o'), n-1) {
if !testing.expect_value(t, bytes.last_index_byte(data, 'o'), n-1) {
return
}
data[n-1] = '-'
// Find it in the middle.
data[n/2] = 'o'
if !testing.expect_value(t, simd_util.index_byte(data, 'o'), n/2) {
if !testing.expect_value(t, bytes.index_byte(data, 'o'), n/2) {
return
}
if !testing.expect_value(t, simd_util.last_index_byte(data, 'o'), n/2) {
if !testing.expect_value(t, bytes.last_index_byte(data, 'o'), n/2) {
return
}
data[n/2] = '-'
// Find it at the start.
data[0] = 'o'
if !testing.expect_value(t, simd_util.index_byte(data, 'o'), 0) {
if !testing.expect_value(t, bytes.index_byte(data, 'o'), 0) {
return
}
if !testing.expect_value(t, simd_util.last_index_byte(data, 'o'), 0) {
if !testing.expect_value(t, bytes.last_index_byte(data, 'o'), 0) {
return
}
}
@@ -47,8 +47,8 @@ test_index_byte_sanity :: proc(t: ^testing.T) {
@test
test_index_byte_empty :: proc(t: ^testing.T) {
a: [1]u8
testing.expect_value(t, simd_util.index_byte(a[0:0], 'o'), -1)
testing.expect_value(t, simd_util.last_index_byte(a[0:0], 'o'), -1)
testing.expect_value(t, bytes.index_byte(a[0:0], 'o'), -1)
testing.expect_value(t, bytes.last_index_byte(a[0:0], 'o'), -1)
}
@test
@@ -65,12 +65,12 @@ test_index_byte_multiple_hits :: proc(t: ^testing.T) {
data[n-5] = 'o'
// Find the first one.
if !testing.expect_value(t, simd_util.index_byte(data, 'o'), n-5) {
if !testing.expect_value(t, bytes.index_byte(data, 'o'), n-5) {
return
}
// Find the last one.
if !testing.expect_value(t, simd_util.last_index_byte(data, 'o'), n-1) {
if !testing.expect_value(t, bytes.last_index_byte(data, 'o'), n-1) {
return
}
}
@@ -88,19 +88,19 @@ test_index_byte_zero :: proc(t: ^testing.T) {
// Positive hit.
data[n-1] = 0
if !testing.expect_value(t, simd_util.index_byte(data[:n], 0), n-1) {
if !testing.expect_value(t, bytes.index_byte(data[:n], 0), n-1) {
return
}
if !testing.expect_value(t, simd_util.last_index_byte(data[:n], 0), n-1) {
if !testing.expect_value(t, bytes.last_index_byte(data[:n], 0), n-1) {
return
}
// Test for false positives.
data[n-1] = '-'
if !testing.expect_value(t, simd_util.index_byte(data[:n], 0), -1) {
if !testing.expect_value(t, bytes.index_byte(data[:n], 0), -1) {
return
}
if !testing.expect_value(t, simd_util.last_index_byte(data[:n], 0), -1) {
if !testing.expect_value(t, bytes.last_index_byte(data[:n], 0), -1) {
return
}
}
@@ -117,22 +117,22 @@ test_misaligned_data :: proc(t: ^testing.T) {
for m in 1..<n {
data[n-1] = 'o'
if !testing.expect_value(t, simd_util.index_byte(data[m:n], 'o'), n-1-m) {
if !testing.expect_value(t, bytes.index_byte(data[m:n], 'o'), n-1-m) {
return
}
data[n-1] = '-'
data[m+(n-m)/2] = 'o'
if !testing.expect_value(t, simd_util.index_byte(data[m:n], 'o'), (n-m)/2) {
if !testing.expect_value(t, bytes.index_byte(data[m:n], 'o'), (n-m)/2) {
return
}
if !testing.expect_value(t, simd_util.last_index_byte(data[m:n], 'o'), (n-m)/2) {
if !testing.expect_value(t, bytes.last_index_byte(data[m:n], 'o'), (n-m)/2) {
return
}
data[m+(n-m)/2] = '-'
data[m] = 'o'
if !testing.expect_value(t, simd_util.last_index_byte(data[m:n], 'o'), 0) {
if !testing.expect_value(t, bytes.last_index_byte(data[m:n], 'o'), 0) {
return
}
data[m] = '-'
+1 -1
View File
@@ -9,6 +9,7 @@ download_assets :: proc() {
}
}
@(require) import "bytes"
@(require) import "c/libc"
@(require) import "compress"
@(require) import "container"
@@ -34,7 +35,6 @@ download_assets :: proc() {
@(require) import "path/filepath"
@(require) import "reflect"
@(require) import "runtime"
@(require) import "simd/util"
@(require) import "slice"
@(require) import "strconv"
@(require) import "strings"