Debian Codegraph @

internal/bytealg/count_amd64.go

raw
1 // Copyright 2023 Charlie Vieth. All rights reserved.
2 // Use of this source code is governed by the MIT license.
3
4 //go:build amd64
5 // +build amd64
6
7 package bytealg
8
9 // Make golangci-lint think these functions are accessed since it
10 // cannot see accesses in assembly.
11 var _ = countGeneric
12 var _ = countGenericString
13
14 // Backup implementation to use by assembly when POPCNT is not available.
15 func countGeneric(b []byte, c byte) int {
16 if 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' {
17 n := 0
18 c |= ' '
19 for _, cc := range b {
20 if cc|' ' == c {
21 n++
22 }
23 }
24 return n
25 }
26 n := 0
27 for _, x := range b {
28 if x == c {
29 n++
30 }
31 }
32 return n
33 }
34
35 // Backup implementation to use by assembly when POPCNT is not available.
36 func countGenericString(s string, c byte) int {
37 if 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' {
38 n := 0
39 c |= ' '
40 for i := 0; i < len(s); i++ {
41 if s[i]|' ' == c {
42 n++
43 }
44 }
45 return n
46 }
47 n := 0
48 for i := 0; i < len(s); i++ {
49 if s[i] == c {
50 n++
51 }
52 }
53 return n
54 }