Skip to content

Commit

Permalink
Merge branch 'main' into immutable-vec
Browse files Browse the repository at this point in the history
  • Loading branch information
Lampese authored Jan 17, 2025
2 parents 7db85fd + 31a207a commit 9cbac29
Show file tree
Hide file tree
Showing 12 changed files with 269 additions and 61 deletions.
27 changes: 15 additions & 12 deletions builtin/bigint_js.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ pub impl Show for BigInt with output(self, logger) {
}

///|
pub extern "js" fn to_string(self : BigInt) -> String =
pub extern "js" fn BigInt::to_string(self : BigInt) -> String =
#|(x) => String(x)

///|
pub extern "js" fn BigInt::from_hex(str : String) -> BigInt =
#|(x) => x.startsWith('-') ? -BigInt(`0x${x.slice(1)}`) : BigInt(`0x${x}`)

///|
pub extern "js" fn to_hex(self : BigInt, uppercase~ : Bool = true) -> String =
pub extern "js" fn BigInt::to_hex(
self : BigInt,
uppercase~ : Bool = true
) -> String =
#|(x, uppercase) => {
#| const r = x.toString(16);
#| return uppercase ? r.toUpperCase() : r;
Expand All @@ -68,7 +71,7 @@ pub fn BigInt::from_octets(octets : Bytes, signum~ : Int = 1) -> BigInt {
}

///|
pub fn to_octets(self : BigInt, length? : Int) -> Bytes {
pub fn BigInt::to_octets(self : BigInt, length? : Int) -> Bytes {
if self < 0 {
abort("negative BigInt")
}
Expand Down Expand Up @@ -162,15 +165,15 @@ pub extern "js" fn BigInt::op_mod(self : BigInt, other : BigInt) -> BigInt =
#|(x, y) => x % y

///|
extern "js" fn modpow_ffi(
extern "js" fn BigInt::modpow_ffi(
self : BigInt,
exponent : BigInt,
modulus : BigInt
) -> BigInt =
#|(x, y, z) => x ** y % z

///|
extern "js" fn pow_ffi(self : BigInt, exponent : BigInt) -> BigInt =
extern "js" fn BigInt::pow_ffi(self : BigInt, exponent : BigInt) -> BigInt =
#|(x, y) => x ** y

///|
Expand All @@ -187,9 +190,9 @@ pub fn BigInt::pow(
if modulus <= 0 {
abort("non-positive modulus")
} else {
modpow_ffi(self, exponent, modulus)
self.modpow_ffi(exponent, modulus)
}
None => pow_ffi(self, exponent)
None => self.pow_ffi(exponent)
}
}

Expand All @@ -214,23 +217,23 @@ pub fn BigInt::op_shr(self : BigInt, n : Int) -> BigInt {
}

///|
extern "js" fn js_shl(self : BigInt, other : Int) -> BigInt =
extern "js" fn BigInt::js_shl(self : BigInt, other : Int) -> BigInt =
#|(x, y) => x << BigInt(y)

///|
extern "js" fn js_shr(self : BigInt, other : Int) -> BigInt =
extern "js" fn BigInt::js_shr(self : BigInt, other : Int) -> BigInt =
#|(x, y) => x >> BigInt(y)

///|
pub extern "js" fn land(self : BigInt, other : BigInt) -> BigInt =
pub extern "js" fn BigInt::land(self : BigInt, other : BigInt) -> BigInt =
#|(x, y) => x & y

///|
pub extern "js" fn lor(self : BigInt, other : BigInt) -> BigInt =
pub extern "js" fn BigInt::lor(self : BigInt, other : BigInt) -> BigInt =
#|(x, y) => x | y

///|
pub extern "js" fn lxor(self : BigInt, other : BigInt) -> BigInt =
pub extern "js" fn BigInt::lxor(self : BigInt, other : BigInt) -> BigInt =
#|(x, y) => x ^ y

///|
Expand Down
26 changes: 13 additions & 13 deletions builtin/bigint_nonjs.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,16 @@ pub fn BigInt::op_mul(self : BigInt, other : BigInt) -> BigInt {
return zero
}
let ret = if self.len < karatsuba_threshold || other.len < karatsuba_threshold {
grade_school_mul(self, other)
self.grade_school_mul(other)
} else {
karatsuba_mul(self, other)
self.karatsuba_mul(other)
}
{ ..ret, sign: if self.sign == other.sign { Positive } else { Negative } }
}

///|
// Simplest way to multiply two BigInts.
fn grade_school_mul(self : BigInt, other : BigInt) -> BigInt {
fn BigInt::grade_school_mul(self : BigInt, other : BigInt) -> BigInt {
let self_len = self.len
let other_len = other.len
let mut len = self_len + other_len
Expand All @@ -248,7 +248,7 @@ fn grade_school_mul(self : BigInt, other : BigInt) -> BigInt {

///|
// Karatsuba multiplication
fn karatsuba_mul(self : BigInt, other : BigInt) -> BigInt {
fn BigInt::karatsuba_mul(self : BigInt, other : BigInt) -> BigInt {
let half = (max(self.len, other.len) + 1) / 2
let (xl, xh) = self.split(half)
let (yl, yh) = other.split(half)
Expand Down Expand Up @@ -287,14 +287,14 @@ pub fn BigInt::op_div(self : BigInt, other : BigInt) -> BigInt {
// Handle negative numbers
if self.sign == Negative {
if other.sign == Negative {
grade_school_div(-self, -other).0
BigInt::grade_school_div(-self, -other).0
} else {
-grade_school_div(-self, other).0
-BigInt::grade_school_div(-self, other).0
}
} else if other.sign == Negative {
-grade_school_div(self, -other).0
-BigInt::grade_school_div(self, -other).0
} else {
return grade_school_div(self, other).0
return BigInt::grade_school_div(self, other).0
}
}

Expand All @@ -307,21 +307,21 @@ pub fn BigInt::op_mod(self : BigInt, other : BigInt) -> BigInt {
// Handle negative numbers
if self.sign == Negative {
if other.sign == Negative {
-grade_school_div(-self, -other).1
-BigInt::grade_school_div(-self, -other).1
} else {
-grade_school_div(-self, other).1
-BigInt::grade_school_div(-self, other).1
}
} else if other.sign == Negative {
grade_school_div(self, -other).1
BigInt::grade_school_div(self, -other).1
} else {
grade_school_div(self, other).1
BigInt::grade_school_div(self, other).1
}
}

///|
// Simplest way to divide two BigInts.
// Assumption: other != zero.
fn grade_school_div(self : BigInt, other : BigInt) -> (BigInt, BigInt) {
fn BigInt::grade_school_div(self : BigInt, other : BigInt) -> (BigInt, BigInt) {
// Handle edge cases
if self < other {
return (zero, self)
Expand Down
1 change: 0 additions & 1 deletion builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ impl ArrayView {
rev_foldi[A, B](Self[A], init~ : B, (Int, B, A) -> B) -> B
swap[T](Self[T], Int, Int) -> Unit
to_json[X : ToJson](Self[X]) -> Json
to_string[X : Show](Self[X]) -> String
unsafe_get[T](Self[T], Int) -> T
}
impl[T : Compare] Compare for ArrayView[T]
Expand Down
6 changes: 3 additions & 3 deletions builtin/int64_js.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn MyInt64::op_neg(self : MyInt64) -> MyInt64 {
}

///|
fn add_hi_lo(self : MyInt64, bhi : Int, blo : Int) -> MyInt64 {
fn MyInt64::add_hi_lo(self : MyInt64, bhi : Int, blo : Int) -> MyInt64 {
let { hi: ahi, lo: alo } = self
let lo = alo + blo
let s = lo >> 31
Expand All @@ -48,15 +48,15 @@ fn add_hi_lo(self : MyInt64, bhi : Int, blo : Int) -> MyInt64 {

///|
fn MyInt64::op_add(self : MyInt64, other : MyInt64) -> MyInt64 {
add_hi_lo(self, other.hi, other.lo)
self.add_hi_lo(other.hi, other.lo)
}

///|
fn MyInt64::op_sub(self : MyInt64, other : MyInt64) -> MyInt64 {
if other.lo == 0 {
{ hi: self.hi - other.hi, lo: self.lo }
} else {
add_hi_lo(self, other.hi.lnot(), other.lo.lnot() + 1)
self.add_hi_lo(other.hi.lnot(), other.lo.lnot() + 1)
}
}

Expand Down
12 changes: 0 additions & 12 deletions builtin/show.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@ pub impl Show for Bytes with output(self, logger) {
logger.write_string("\"")
}

///|
pub impl Show for Bytes with to_string(self) {
let buf = StringBuilder::new()
Show::output(self, buf)
buf.to_string()
}

///|
pub impl Show for BytesView with output(self, logger) {
fn to_hex_digit(i : Int) -> Char {
Expand Down Expand Up @@ -220,11 +213,6 @@ pub impl[X : Show] Show for ArrayView[X] with output(self, logger) {
logger.write_iter(self.iter())
}

///|
pub fn ArrayView::to_string[X : Show](self : ArrayView[X]) -> String {
Show::to_string(self)
}

///|
/// Convert Char to String
/// @intrinsic %char.to_string
Expand Down
Loading

0 comments on commit 9cbac29

Please sign in to comment.