Skip to content

Commit

Permalink
pop-count: clear stub
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Oct 24, 2023
1 parent 9403a87 commit 1196ba4
Showing 1 changed file with 1 addition and 20 deletions.
21 changes: 1 addition & 20 deletions exercises/practice/pop-count/pop-count.wat
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
(module
(func (export "eggCount") (param $number i32) (result i32)
(local $remaining i32)
(local $count i32)

(if (i32.eq (local.get $number) (i32.const 0))(then
(return (i32.const 0))
))

(local.set $remaining (i32.const 0))
(local.set $count (i32.const 0))

;; do while $remaining != 0
(loop
;; Clear least-significant 1 bit using
;; $remaining -= $remaining & -$remaining
(local.set $remaining (i32.sub (local.get $remaining) (i32.and (local.get $remaining) (i32.sub (i32.const 0) (local.get $remaining)))))
(local.set $count (i32.add (local.get $count) (i32.const 1)))
(br_if 0 (i32.ne (local.get $remaining) (i32.const 0)))
)

(return (local.get $count))
(return (i32.const 42))
)
)

0 comments on commit 1196ba4

Please sign in to comment.