Skip to content

Commit a5daf90

Browse files
committed
try to reconcile impl with different int representations
1 parent d9f6e60 commit a5daf90

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

libraries/common/effekt.effekt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ extern def infixNeq(x: Bool, y: Bool) at {}: Bool =
319319

320320
// Math ops
321321
// ========
322+
323+
// Numeric representations in the backends:
324+
// LLVM: int64_t (64-bit integer)
325+
// JS: 64-bit floating-point (64-bit IEEE 754)
326+
// Chez: 64-bit integer
327+
322328
extern def infixAdd(x: Int, y: Int) at {}: Int =
323329
js "(${x} + ${y})"
324330
chez "(+ ${x} ${y})"
@@ -343,7 +349,7 @@ extern def infixSub(x: Int, y: Int) at {}: Int =
343349
llvm "%z = sub %Int ${x}, ${y} ret %Int %z"
344350
vm "effekt::infixSub(Int, Int)"
345351

346-
// Computes the quotient based on truncated-division
352+
/// Computes the quotient based on truncated-division
347353
def quot(x: Int, y: Int): Int =
348354
infixDiv(x, y)
349355

@@ -356,13 +362,13 @@ def quot(x: Int, y: Int): Int =
356362
def div(x: Int, y: Int): Int = {
357363
val q = x / y
358364
val r = x.rem(y)
359-
q - (r.bitwiseShr(31).bitwiseAnd(y.bitwiseShr(31).bitwiseOr(1)))
365+
q - r.bitwiseMsb().bitwiseAnd(y.bitwiseMsb().bitwiseOr(1))
360366
}
361367

362368
/// Computes the result of Euclidean modulo
363369
def mod(x: Int, y: Int): Int = {
364370
val r = x.rem(y)
365-
r + y.abs.bitwiseAnd(r.bitwiseShr(31))
371+
r + y.abs.bitwiseAnd(r.bitwiseMsb())
366372
}
367373

368374
extern def infixAdd(x: Double, y: Double) at {}: Double =
@@ -693,6 +699,10 @@ extern def bitwiseXor(x: Int, y: Int) at {}: Int =
693699
llvm "%z = xor %Int ${x}, ${y} ret %Int %z"
694700
vm "effekt::bitwiseXor(Int, Int)"
695701

702+
extern def bitwiseMsb(x: Int) at {}: Int =
703+
js "${x} >> 31"
704+
chez "(bitwise-arithmetic-shift-right (bitwise-and ${x} (- (expt 2 64) 1)) 63)"
705+
llvm "%z = ashr %Int ${x}, 63 ret %Int %z"
696706

697707
// Byte operations
698708
// ===============

0 commit comments

Comments
 (0)