leo/examples/hackers-delight/ntzseals
Pranav Gaddamadugu f7e0352500 WIP
2024-05-18 16:43:53 -07:00
..
build WIP 2024-05-18 16:43:53 -07:00
inputs WIP removing input files from examples 2024-01-15 15:45:07 -08:00
src refactor ntzseals 2024-04-11 14:19:08 -07:00
.env Update default PK in examples .env 2024-05-15 08:22:10 -07:00
.gitignore [Feature] Implement leo execute (#2491) 2023-07-19 18:04:09 -07:00
leo.lock make example compatible w/ stubs 2023-12-11 13:19:42 -08:00
program.json [Feature] Implement leo execute (#2491) 2023-07-19 18:04:09 -07:00
README.md Update examples and CI 2024-01-15 15:45:08 -08:00

src/ntzseals.leo

Build Guide

To compile and run this Leo program, run:

leo run <function-name> <inputs>

The Algorithm

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

First 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 by 0, 1, 2, 4, ... 2**31, the 33 values all had different patterns of their 6 highest bits. The constant was also chosen to make multiplication easy to do with a small number of shifts and adds on conventional hardware.

In the algorithm, the 6 high bits are used as an index into a table, where 31 of the entries are unused. The 33 used values were chosen so that they gave the correct number of trailing zeros for the inputs.

This algorithm was proposed by David Seal in the comp.sys.acorn.tech newsgroup, February 16, 1994: https://groups.google.com/g/comp.sys.acorn.tech/c/blRy-AiIQ-0/m/3JxNHeKN75IJ

A further post that includes the table used was made by Michael Williams in the comp.arch.arithmetic newsgroup, December 4, 1998: https://groups.google.com/g/comp.arch.arithmetic/c/yBt-QHRVEGE/m/stFPPMD0b7AJ