-
Notifications
You must be signed in to change notification settings - Fork 5
/
Buffer32.fm
34 lines (30 loc) · 1.24 KB
/
Buffer32.fm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// This is compiled to a mutable array. Because of that, buffers must be used
// linearly. This is NOT checked yet, so, right now, buffer compilation is
// unsafe until linearity checks are added.
T Buffer32
| Buffer32.new(depth: Nat, array: Array(U32, depth));
Buffer32.get(idx: U32, buf: Buffer32): U32
get dep arr = buf
get wrd = idx
let idx = Word.trim<32>(dep, wrd)
Array.get<U32, dep>(idx, arr)
Buffer32.set(idx: U32, val: U32, buf: Buffer32): Buffer32
get dep arr = buf
get wrd = idx
let idx = Word.trim<32>(dep, wrd)
def arr = Array.set<U32, dep>(idx, val, arr)
Buffer32.new(dep, arr)
Buffer32.alloc(depth: Nat): Buffer32
Buffer32.new(depth, Array.alloc<U32>(depth, U32.0))
// Converts a hex string into a Buffer32.
// TODO. We need to:
// 1. Compute the length of the string with String.length32
// 2. Divide by 8 to get the buffer length (add U32 consts on Lang?)
// 3. Use Nat.succ(Word.nat_log2<32>(...)) to compute its depth
// 4. Allocate the buffer with Buffer32.alloc
// 5. Split hex into chunks of 8 chars (32 bits)
// 6. Convert it into a list of U32 using Char.hex_value32
// 7. Loop with U32.for, using Buffer32.set(i, list[i], buf)
// Use U32 whenever possible
Buffer32.parse_hex(hex: String): Buffer32
Buffer32.parse_hex(hex)