~/bend-docscommunity

algo.bend checks

raw source on the hub · import bend-osdev-algo@0.1.0.0/algo.bend as Algo

bend-osdev-algo: the small algorithms an operating system kernel uses everywhere. This file: bit search in a word.

Bit search works on the bits of Base's Word(n) (least significant bit first), generic in the width, so that one proof covers U32 and W64:

popcount the number of set bits (the kernel's hweight) ffs 1 + the index of the lowest set bit, 0 for zero (the kernel's and C's ffs) fls 1 + the index of the highest set bit, 0 for zero (the kernel's fls) first_set the index of the lowest set bit, None for zero (__ffs) last_set the index of the highest set bit, None for zero (__fls) ctz the number of zero bits below the lowest set bit (the width for zero) clz the number of zero bits above the highest set bit (the width for zero)

Bitmaps are in bitmap.bend, binary search in bsearch.bend.

2 imports
import Base
import bend-osdev-word64@0.1.0.0/word64.bend as W

Definitions

def bitw.test source · line 26 · raw

@n:Nat -> @w:Word(n) -> @k:Nat -> Bool

bit k (False at or past the width)

def bitw.popcount.step source · line 37 · raw

@b:Bool -> @k:Nat -> Nat

def bitw.popcount source · line 44 · raw

@n:Nat -> @w:Word(n) -> Nat

def bitw.ffs.step source · line 54 · raw

@b:Bool -> @k:Nat -> Nat

k is ffs of the bits above the lowest one

def bitw.ffs source · line 65 · raw

@n:Nat -> @w:Word(n) -> Nat

def bitw.fls.step source · line 75 · raw

@k:Nat -> @b:Bool -> Nat

k is fls of the bits above the lowest one

def bitw.fls source · line 86 · raw

@n:Nat -> @w:Word(n) -> Nat

def bitw.ctz.step source · line 95 · raw

@b:Bool -> @k:Nat -> Nat

def bitw.ctz source · line 102 · raw

@n:Nat -> @w:Word(n) -> Nat

def bitw.clz source · line 111 · raw

@+n:Nat -> @w:Word(n) -> Nat

def bitw.index source · line 115 · raw

@m:Nat -> Maybe<&2, Nat>

a 1-based position (ffs, fls) as a 0-based index

def bitw.to_list source · line 123 · raw

@n:Nat -> @w:Word(n) -> @rest:List<&2, Bool> -> List<&2, Bool>

the bits of w, least significant first, in front of rest

def bits32.word source · line 134 · raw

@x:U32 -> Word(32n)

def bits32.test source · line 140 · raw

@x:U32 -> @k:Nat -> Bool

bit k of x (False for k >= 32)

def bits32.popcount source · line 143 · raw

@x:U32 -> Nat

def bits32.ffs source · line 146 · raw

@x:U32 -> Nat

def bits32.fls source · line 149 · raw

@x:U32 -> Nat

def bits32.first_set source · line 152 · raw

@x:U32 -> Maybe<&2, Nat>

def bits32.last_set source · line 155 · raw

@x:U32 -> Maybe<&2, Nat>

def bits32.ctz source · line 158 · raw

@x:U32 -> Nat

def bits32.clz source · line 161 · raw

@x:U32 -> Nat

def bits64.word source · line 166 · raw

@+a:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> Word(64n)

def bits64.test source · line 170 · raw

@a:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> @k:Nat -> Bool

bit k of a (False for k >= 64)

def bits64.popcount source · line 173 · raw

@a:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> Nat

def bits64.ffs source · line 176 · raw

@a:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> Nat

def bits64.fls source · line 179 · raw

@a:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> Nat

def bits64.first_set source · line 182 · raw

@a:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> Maybe<&2, Nat>

def bits64.last_set source · line 185 · raw

@a:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> Maybe<&2, Nat>

def bits64.ctz source · line 188 · raw

@a:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> Nat

def bits64.clz source · line 191 · raw

@a:0x44cf1401f78136bfc15c4c5be1cd20f0/word64.W64 -> Nat