leo/examples/hackers-delight/ntzdebruijn
Collin Chin d44457fc32
[Feature] Implement leo execute (#2491)
* bump snarkvm rev

* update default gitignore

* impl leo execute

* bump snarkvm 0.14.5

* modify examples wip

* update run.sh examples

* impl env file

* clippy warning

* fix auction example

* fix auction example env

* generate new private key for new env - tests failing due to env not found err

* commit error changes

* Fix tests; clippy

* Get examples working

* leo build checks that build dir is well formed; clippy

* Clean up

* Update examples/README.md

Co-authored-by: d0cd <pranavsaig@gmail.com>
Signed-off-by: Collin Chin <16715212+collinc97@users.noreply.github.com>

* do not commit .avm files

* use snarkvm commands

---------

Signed-off-by: Collin Chin <16715212+collinc97@users.noreply.github.com>
Co-authored-by: Pranav Gaddamadugu <pranav@aleo.org>
Co-authored-by: d0cd <pranavsaig@gmail.com>
2023-07-19 18:04:09 -07:00
..
build [Feature] Implement leo execute (#2491) 2023-07-19 18:04:09 -07:00
inputs Reorganize hackers delight examples 2022-09-13 17:22:01 +02:00
src fix typo in program name 2022-10-06 21:30:30 -07:00
.env [Feature] Implement leo execute (#2491) 2023-07-19 18:04:09 -07:00
.gitignore [Feature] Implement leo execute (#2491) 2023-07-19 18:04:09 -07:00
program.json [Feature] Implement leo execute (#2491) 2023-07-19 18:04:09 -07:00
README.md Reorganize hackers delight examples 2022-09-13 17:22:01 +02:00

src/ntzdebruijn.leo

Build Guide

To compile and run this Leo program, run:

leo run

The Algorithm

This algorithm is detailed in "Hacker's Delight, 2nd edition" by Henry S. Warren, section 5-4, figure 5-26. Here is a summary.

After handling the all-zeros case, we isolate the rightmost 1 bit in the 32-bit input by using the C idiom x & (-x). In Leo, the -x is written as 0u32.sub_wrapped(x).

A constant was discovered with the property that when it was multiplied by 1, 2, 4, ... 2**31, the 32 values all had different high 5-bit patterns. This constant is 0x04D7651F.

In the algorithm, the 5 high bits are used as an index into the table {0, 1, 2, 24, 3, 19, ...}. The table's values were chosen so that they gave the correct number of trailing zeros for the inputs.

For example, if the isolated bit has 4 trailing zeros, the number is 2**4. The high 5 bits of 2**4 * 0x04D7651F are 01001 which is 9. The table value at index 9 is therefore 4.

This algorithm was proposed by Danny Dubé in the comp.compression.research newsgroup: https://groups.google.com/g/comp.compression.research/c/x0NaZ3CJ6O4/m/PfGuchA7o60J

A description of de Bruijn cycles and their use for bit indexing can be seen here: http://supertech.csail.mit.edu/papers/debruijn.pdf