@@ -11,15 +11,20 @@ import bytearray
11
11
12
12
// Wrappers
13
13
// --------
14
+ /// A with explicit big-endian order
14
15
record BE[A](raw: A)
16
+ /// A with explicit little-endian order
15
17
record LE[A](raw: A)
18
+ /// A with explicit width in current unit
19
+ /// (bits for bitstreams, bytes for bytestreams)
16
20
record OfWidth[A](raw: A, width: Int)
21
+ /// explicitly signed A
17
22
record Signed[A](raw: A)
18
23
19
24
/// Bits
20
25
type Bit { B0(); B1() }
21
26
22
- /// Effect alias
27
+ /// Splices allowed in hex/byte stream literals
23
28
effect HexSplices = {
24
29
splice[Char], splice[String],
25
30
splice[Unit],
@@ -33,39 +38,57 @@ effect HexSplices = {
33
38
34
39
// Splitting
35
40
// ---------
41
+
42
+ /// emit bytes of the given int as a w bytes in little-endian byte order
36
43
def bytesLE(int: Int, w: Int): Unit / emit[Byte] = {
37
44
var c = int
38
45
repeat(w){
39
46
do emit(mod(c, 256).toByte)
40
47
c = c / 256
41
48
}
42
49
}
50
+
51
+ /// emit bytes of the given int as a 4 bytes in little-endian byte order
43
52
def bytesLE(int: Int): Unit / emit[Byte] = bytesLE(int, 4)
53
+
54
+ /// emit bytes of the given int as a w bytes in big-endian byte order
44
55
def bytesBE(n: Int, width: Int): Unit / emit[Byte] = {
45
56
var pos = pow(256, width - 1)
46
57
repeat(width){
47
58
do emit((bitwiseAnd(n, pos * 255) / pos).toByte)
48
59
pos = pos / 256
49
60
}
50
61
}
62
+
63
+ /// emit bytes of the given int as a 4 bytes in big-endian byte order
51
64
def bytesBE(n: Int): Unit / emit[Byte] = bytesBE(n, 4)
65
+
66
+ /// emit bytes of the given int as a 4 bytes in big-endian byte order
52
67
def bytes(n: Int): Unit / emit[Byte] = bytesBE(n)
68
+
69
+ /// emit bytes of the given int as width bytes (in 2s-complement) in little-endian byte order
53
70
def signedBytesLE(int: Int, width: Int): Unit / emit[Byte] = {
54
71
if (int < 0) {
55
72
bytesLE(bitwiseNot(neg(int)) + 1, width)
56
73
} else {
57
74
bytesLE(int, width)
58
75
}
59
76
}
77
+ /// emit bytes of the given int as width bytes (in 2s-complement) in big-endian byte order
60
78
def signedBytesBE(int: Int, width: Int): Unit / emit[Byte] = {
61
79
if (int < 0) {
62
80
bytesBE(bitwiseNot(neg(int)) + 1, width)
63
81
} else {
64
82
bytesBE(int, width)
65
83
}
66
84
}
85
+
86
+ /// emit bytes of the given int as 4 bytes (in 2s-complement) in little-endian byte order
67
87
def signedBytesLE(int: Int): Unit / emit[Byte] = signedBytesLE(int, 4)
88
+
89
+ /// emit bytes of the given int as 4 bytes (in 2s-complement) in big-endian byte order
68
90
def signedBytesBE(int: Int): Unit / emit[Byte] = signedBytesBE(int, 4)
91
+
69
92
def bitsBE(int: Int): Unit / emit[Bit] = bitsBE(int, 32)
70
93
def collectBitsBE{ body: => Unit / emit[Bit] }: Int = {
71
94
var res = 0
0 commit comments