@@ -95,29 +95,7 @@ def signedBytesLE(int: Int): Unit / emit[Byte] = signedBytesLE(int, 4)
95
95
/// emit bytes of the given int as 4 bytes (in 2s-complement) in big-endian byte order
96
96
def signedBytesBE(int: Int): Unit / emit[Byte] = signedBytesBE(int, 4)
97
97
98
- /// emit bits of the given int as 32 Bits in big-endian bit order
99
- def bitsBE(int: Int): Unit / emit[Bit] = bitsBE(int, 32)
100
-
101
- /// collect bits in big-endian bit order into an Int
102
- def collectBitsBE{ body: => Unit / emit[Bit] }: Int = {
103
- var res = 0
104
- try body() with emit[Bit] { b =>
105
- res = b match {
106
- case B0() => res * 2
107
- case B1() => res * 2 + 1
108
- }
109
- resume(())
110
- }
111
- res
112
- }
113
98
114
- /// bitwise not on integers
115
- def bitwiseNot(n: Int): Int = {
116
- // TODO currently implemented in Effekt, should use extern ?
117
- collectBitsBE{
118
- try bitsBE(n) with emit[Bit]{ b => resume(do emit(not(b))) }
119
- }
120
- }
121
99
122
100
// Splicers
123
101
// --------
@@ -207,6 +185,7 @@ def tracking[A]{ body: => Unit / { emit[A], getPos, pad[A] } }: Unit / emit[A] =
207
185
208
186
// From/to Bytes
209
187
// -------------
188
+ /// emit bits of the given Byte as 8 Bits in little-endian bit order
210
189
def bitsLE(byte: Byte): Unit / emit[Bit] = {
211
190
val v = byte.toInt
212
191
var mask = 1
@@ -218,6 +197,8 @@ def bitsLE(byte: Byte): Unit / emit[Bit] = {
218
197
mask = mask * 2
219
198
}
220
199
}
200
+
201
+ /// emit bits of the given Byte as 8 Bits in big-endian bit order
221
202
def bitsBE(byte: Byte): Unit / emit[Bit] = {
222
203
val v = byte.toInt
223
204
var mask = 128
@@ -229,7 +210,11 @@ def bitsBE(byte: Byte): Unit / emit[Bit] = {
229
210
mask = mask / 2
230
211
}
231
212
}
213
+
214
+ /// emit bits of the given Byte as 8 Bits in big-endian bit order
232
215
def bits(byte: Byte): Unit / emit[Bit] = bitsBE(byte)
216
+
217
+ /// emit bits of the given Byte as width Bits in little-endian bit order
233
218
def bitsLE(v: Int, width: Int): Unit / emit[Bit] = {
234
219
var mask = 1
235
220
repeat(width){
@@ -240,18 +225,8 @@ def bitsLE(v: Int, width: Int): Unit / emit[Bit] = {
240
225
mask = mask * 2
241
226
}
242
227
}
243
- def pow(n: Int, exp: Int): Int = {
244
- def go(n: Int, exp: Int, acc: Int): Int = {
245
- if (exp == 0) {
246
- acc
247
- } else if (mod(exp, 2) == 0) {
248
- go(n * n, exp / 2, acc)
249
- } else {
250
- go(n * n, exp / 2, acc * n)
251
- }
252
- }
253
- go(n, exp, 1)
254
- }
228
+
229
+ /// emit bits of the given int as width Bits in big-endian bit order
255
230
def bitsBE(v: Int, width: Int): Unit / emit[Bit] = {
256
231
var mask = pow(2, width - 1)
257
232
repeat(width){
@@ -262,6 +237,31 @@ def bitsBE(v: Int, width: Int): Unit / emit[Bit] = {
262
237
mask = mask / 2
263
238
}
264
239
}
240
+
241
+ /// emit bits of the given int as 32 Bits in big-endian bit order
242
+ def bitsBE(int: Int): Unit / emit[Bit] = bitsBE(int, 32)
243
+
244
+ /// collect bits in big-endian bit order into an Int
245
+ def collectBitsBE{ body: => Unit / emit[Bit] }: Int = {
246
+ var res = 0
247
+ try body() with emit[Bit] { b =>
248
+ res = b match {
249
+ case B0() => res * 2
250
+ case B1() => res * 2 + 1
251
+ }
252
+ resume(())
253
+ }
254
+ res
255
+ }
256
+
257
+ /// bitwise not on integers
258
+ def bitwiseNot(n: Int): Int = {
259
+ // TODO currently implemented in Effekt, should use extern ?
260
+ collectBitsBE{
261
+ try bitsBE(n) with emit[Bit]{ b => resume(do emit(not(b))) }
262
+ }
263
+ }
264
+
265
265
def ungroupBytes{ body: => Unit / emit[Byte] }: Unit / emit[Bit] =
266
266
for[Byte]{body}{ b => bits(b) }
267
267
def twoscomplementLE{ body: => Unit / emit[Bit] }: Unit / emit[Bit] = {
0 commit comments