Skip to content

Commit

Permalink
split span.count into its own PR
Browse files Browse the repository at this point in the history
Signed-off-by: martinvuyk <[email protected]>
  • Loading branch information
martinvuyk committed Nov 21, 2024
1 parent ea3de9c commit 7f64beb
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 46 deletions.
6 changes: 0 additions & 6 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,6 @@ what we publish.
([PR #3160](https://github.com/modularml/mojo/pull/3160) by
[@bgreni](https://github.com/bgreni))

- `Span` now implements a generic `.count()` method which can be passed a
function that returns a boolean SIMD vector. The function counts how many
times it returns true evaluating it in a vectorized manner. This works for any
`Span[Scalar[D]]` e.g. `Span[Byte]`. PR [#3686](https://github.com/modularml/mojo/pull/3686)
by [@martinvuyk](https://github.com/martinvuyk).

- `StringRef` now implements `split()` which can be used to split a
`StringRef` into a `List[StringRef]` by a delimiter.
([PR #2705](https://github.com/modularml/mojo/pull/2705) by [@fknfilewalker](https://github.com/fknfilewalker))
Expand Down
40 changes: 0 additions & 40 deletions stdlib/src/utils/span.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -358,43 +358,3 @@ struct Span[
return Span[T, _lit_mut_cast[origin, False].result](
ptr=self._data, length=self._len
)

fn count[
D: DType, //, func: fn[w: Int] (SIMD[D, w]) -> SIMD[DType.bool, w]
](self: Span[Scalar[D]]) -> Int:
"""Count the amount of times the function returns `True`.
Parameters:
D: The DType.
func: The function to evaluate.
Returns:
The amount of times the function returns `True`.
"""

alias widths = (256, 128, 64, 32, 16, 8)
var ptr = self.unsafe_ptr()
var num_bytes = len(self)
var amnt = 0
var processed = 0

@parameter
for i in range(len(widths)):
alias w = widths.get[i, Int]()

@parameter
if simdwidthof[D]() >= w:
for _ in range((num_bytes - processed) // w):
var vec = (ptr + processed).load[width=w]()

@parameter
if w >= 256:
amnt += int(func(vec).cast[DType.uint16]().reduce_add())
else:
amnt += int(func(vec).cast[DType.uint8]().reduce_add())
processed += w

for i in range(num_bytes - processed):
amnt += int(func(ptr[processed + i]))

return amnt

0 comments on commit 7f64beb

Please sign in to comment.