@@ -314,8 +314,10 @@ lazyMux muxFn c tm fm =
314
314
f <- fm
315
315
muxFn c t f
316
316
317
- -- selectV merger maxValue valueFn index returns valueFn v when index has value v
318
- -- if index is greater than maxValue, it returns valueFn maxValue. Use the ite op from merger.
317
+ -- @selectV merger maxValue valueFn vx@ treats @vx@ as an index, represented
318
+ -- as a big-endian list of bits. It does a binary lookup, using @merger@ as an
319
+ -- if-then-else operator. If the index is greater than @maxValue@, then it
320
+ -- returns @valueFn maxValue@.
319
321
selectV :: (SBool -> b -> b -> b ) -> Natural -> (Natural -> b ) -> SWord -> b
320
322
selectV merger maxValue valueFn vx =
321
323
case svAsInteger vx of
@@ -326,7 +328,13 @@ selectV merger maxValue valueFn vx =
326
328
where
327
329
impl _ x | x > maxValue || x < 0 = valueFn maxValue
328
330
impl 0 y = valueFn y
329
- impl i y = merger (svTestBit vx j) (impl j (y `setBit` j)) (impl j y) where j = i - 1
331
+ impl i y =
332
+ -- NB: `i` counts down in each iteration, so we use svTestBit (a
333
+ -- little-endian indexing function) to ensure that the bits are processed
334
+ -- in big-endian order. Alternatively, we could have `i` count up and use
335
+ -- svAt (a big-endian indexing function), but we use svTestBit as it is
336
+ -- slightly cheaper to compute.
337
+ merger (svTestBit vx j) (impl j (y `setBit` j)) (impl j y) where j = i - 1
330
338
331
339
-- Big-endian version of svTestBit
332
340
svAt :: SWord -> Int -> SBool
0 commit comments