Skip to content

Commit

Permalink
[examples] a few rewordings
Browse files Browse the repository at this point in the history
  • Loading branch information
bendyarm authored and d0cd committed Sep 13, 2022
1 parent d38e33e commit f7d1b5c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
13 changes: 8 additions & 5 deletions examples/ntzgaudet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ To compile and run this Leo program, run:
leo run
```

## The Algorithm

This algorithm is described in "Hacker's Delight, 2nd edition" by Henry
S. Warren, section 5-4, section 5-24, as interesting due to being branch-free,
not using table lookups, and having parallelism. It is attributed to Dean Gaudet
Expand All @@ -19,11 +21,12 @@ written as `0u32.sub_wrapped(x)`. The result is stored in `y`.
Then we compute six intermediate variables that count different numbers
of trailing zeros. The first variable, `bz`, just counts 1 if `y` is completely zero.

Then we do binary search in parallel, using 5 masks, each looking at
a different subset of 16 bits. For example, `b4` counts 16 if the low
16 bits are zero and counts zero otherwise. Then `b3` uses a mask
`y & 0x00FF00FF` to count eight 0-bits if the result is zero and zero 0-bits otherwise.
The masks for `b2`, `b1`, and `b0` potentially count four, two, and one 0-bits similarly.
To get the other five variables, we do binary search in parallel, using 5 masks,
each looking at a different symmetric pattern of 16 bits. For example, `b4` counts 16 if
the low 16 bits are zero and counts zero otherwise. Then `b3` uses a mask `y &
0x00FF00FF` to count eight 0-bits if the result is zero and zero 0-bits
otherwise. The masks for `b2`, `b1`, and `b0` can count four, two, and
one 0-bits similarly.

The varables `bz, b4, .., b0` are all independent, and their values are added up
for the result.
9 changes: 5 additions & 4 deletions examples/ntzloops/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ expressed in Leo as `!x & x.sub_wrapped(1u32);`.
Then we simply count the 1-bits by right shifting until `x` is zero and
counting the number of shifts using the variable `n`.

To get the effect of a while loop in Leo, one must use a `for` loop with the maximum
possible number of iterations, and then check the while-analogous condition within
the for loop. Once the condition is false, the loop completes but the `if` statement
inside the loop prevents any further operations.
To get the effect of a while loop in Leo, one must use a `for` loop with the
enough iterations to accommodate all possible inputs, and then check the
while condition within the for loop. Once the condition is false, the
loop continues until finished but the `if` statement inside the loop prevents
any further operations.
2 changes: 1 addition & 1 deletion examples/ntzreisers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ divide the 33 arguments 0, 1, 2, ..., 2**31, the remainder is different for
each argument. This constant is 37.

In the algorithm, the remainder is used as an index into a table of size 37,
with 4 entries unused. The table's values were chosen so that they gave the
with 4 entries unused. The table's values were chosen so that they give the
correct number of trailing zeros for the inputs.

This algorithm was proposed by John Reiser in the comp.arch.arithmetic newsgroup
Expand Down

0 comments on commit f7d1b5c

Please sign in to comment.