Skip to content

Commit d185d7d

Browse files
more docs and structure
1 parent 5e0f2c6 commit d185d7d

File tree

2 files changed

+50
-34
lines changed

2 files changed

+50
-34
lines changed

libraries/common/binstream.effekt

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,7 @@ def signedBytesLE(int: Int): Unit / emit[Byte] = signedBytesLE(int, 4)
9595
/// emit bytes of the given int as 4 bytes (in 2s-complement) in big-endian byte order
9696
def signedBytesBE(int: Int): Unit / emit[Byte] = signedBytesBE(int, 4)
9797

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-
}
11398

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-
}
12199

122100
// Splicers
123101
// --------
@@ -207,6 +185,7 @@ def tracking[A]{ body: => Unit / { emit[A], getPos, pad[A] } }: Unit / emit[A] =
207185

208186
// From/to Bytes
209187
// -------------
188+
/// emit bits of the given Byte as 8 Bits in little-endian bit order
210189
def bitsLE(byte: Byte): Unit / emit[Bit] = {
211190
val v = byte.toInt
212191
var mask = 1
@@ -218,6 +197,8 @@ def bitsLE(byte: Byte): Unit / emit[Bit] = {
218197
mask = mask * 2
219198
}
220199
}
200+
201+
/// emit bits of the given Byte as 8 Bits in big-endian bit order
221202
def bitsBE(byte: Byte): Unit / emit[Bit] = {
222203
val v = byte.toInt
223204
var mask = 128
@@ -229,7 +210,11 @@ def bitsBE(byte: Byte): Unit / emit[Bit] = {
229210
mask = mask / 2
230211
}
231212
}
213+
214+
/// emit bits of the given Byte as 8 Bits in big-endian bit order
232215
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
233218
def bitsLE(v: Int, width: Int): Unit / emit[Bit] = {
234219
var mask = 1
235220
repeat(width){
@@ -240,18 +225,8 @@ def bitsLE(v: Int, width: Int): Unit / emit[Bit] = {
240225
mask = mask * 2
241226
}
242227
}
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
255230
def bitsBE(v: Int, width: Int): Unit / emit[Bit] = {
256231
var mask = pow(2, width - 1)
257232
repeat(width){
@@ -262,6 +237,31 @@ def bitsBE(v: Int, width: Int): Unit / emit[Bit] = {
262237
mask = mask / 2
263238
}
264239
}
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+
265265
def ungroupBytes{ body: => Unit / emit[Byte] }: Unit / emit[Bit] =
266266
for[Byte]{body}{ b => bits(b) }
267267
def twoscomplementLE{ body: => Unit / emit[Bit] }: Unit / emit[Bit] = {

libraries/common/effekt.effekt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,22 @@ def pow(base: Double, exponent: Int): Double = {
419419
}
420420
}
421421

422+
/// Computes n^exp for integers.
423+
///
424+
/// Precondition: exp >= 0
425+
def pow(n: Int, exp: Int): Int = {
426+
def go(n: Int, exp: Int, acc: Int): Int = {
427+
if (exp == 0) {
428+
acc
429+
} else if (mod(exp, 2) == 0) {
430+
go(n * n, exp / 2, acc)
431+
} else {
432+
go(n * n, exp / 2, acc * n)
433+
}
434+
}
435+
go(n, exp, 1)
436+
}
437+
422438
extern pure def pow(base: Double, exponent: Double): Double =
423439
js "Math.pow(${base}, ${exponent})"
424440
chez "(expt ${base} ${exponent})"

0 commit comments

Comments
 (0)