Source/Packages

Accelerator.SM86.FieldEncoding

packages/hardware/architectures/nvidia-sm86/src/Accelerator/SM86/FieldEncoding.alpha

794 lines86 declarations28.4 KiBSHA-256 400363ed48c6

def · lines 236–263

sm86PlaceBitAt

Full file
Place ONE bit, given the bit's TARGET position and the bit itself. This used to take the field's value and a bit index and recover the bit with `value / 2^bitIndex mod 2`. That `2^bitIndex` is the reason the encoder could not be run: naturals are unary here, so `naturalPowerOfTwo 31` -- what a field at bit 31 needs -- materialises a successor chain of 2,147,483,648 elements, and the divide beside it is repeated subtraction over that numeral. Measured, 2^22 alone took 921 ms; one instruction is 128 such placements. The caller now threads the value, halving it per bit, so the only power left here is `2^(targetBit mod 8)` -- at most 128, because a bit's home inside a byte is what it is regardless of how wide the field is.
236def sm86PlaceBitAt =
237  (lambda unrestricted targetBit : Nat .
238    unrestricted sourceBit : Nat .
239    unrestricted encoded : Bytes .
240    app
241      (lambda unrestricted targetByteIndex : Nat .
242        app
243          (lambda unrestricted targetBitIndex : Nat .
244            app
245              (lambda unrestricted targetPower : Nat .
246                app
247                  (lambda unrestricted priorByte : Nat .
248                    app
249                      (lambda unrestricted priorBit : Nat .
250                        sm86ReplaceByteAt
251                          targetByteIndex
252                          (pipe priorByte
253                            (naturalSaturatingSubtract (naturalMultiply priorBit targetPower))
254                            (naturalAdd (naturalMultiply sourceBit targetPower))
255                            nat-to-byte)
256                          encoded)
257                      (naturalModuloUnchecked
258                        (naturalDivideUnchecked priorByte targetPower)
259                        sm86EncodingNaturalTwo))
260                  (byte-to-nat (sm86ByteAt targetByteIndex encoded)))
261              (naturalPowerOfTwo targetBitIndex))
262          (naturalModuloUnchecked targetBit sm86EncodingNaturalEight))
263      (naturalDivideUnchecked targetBit sm86EncodingNaturalEight))

The compiler supplied declaration spans and resolved links from this source snapshot. This page does not assert that this file belongs to a checked closure.