Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

align BigInt to JavaScript native bigint #1158

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions builtin/bigint_deprecated.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2024 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// @alert deprecated "Use infix bitwise operator `>>` instead"
pub fn asr(self : BigInt, n : Int) -> BigInt {
self >> n
}

/// Left shift a bigint
/// The sign of the result is the same as the sign of the input.
/// Only the absolute value is shifted.
///
/// @alert deprecated "Use infix bitwise operator `<<` instead"
pub fn shl(self : BigInt, n : Int) -> BigInt {
self << n
}

/// Left shift a bigint
/// The sign of the result is the same as the sign of the input.
/// Only the absolute value is shifted.
///
/// @alert deprecated "Use infix bitwise operator `<<` instead"
pub fn lsl(self : BigInt, n : Int) -> BigInt {
self << n
}

/// Right shift a bigint
/// The sign of the result is the same as the sign of the input.
/// Only the absolute value is shifted.
///
/// @alert deprecated "Use infix bitwise operator `>>` instead"
pub fn shr(self : BigInt, n : Int) -> BigInt {
self >> n
}
166 changes: 166 additions & 0 deletions builtin/bigint_js.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Copyright 2024 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

type BigInt

pub fn BigInt::from_string(str : String) -> BigInt {
if str.length() == 0 {
abort("empty string")
}
BigInt::js_from_string(str)
}

extern "js" fn BigInt::js_from_string(str : String) -> BigInt =
#|(x) => BigInt(x)

pub impl Show for BigInt with output(self, logger) {
logger.write_string(self.to_string())
}

pub extern "js" fn 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 =
#|(x, uppercase) => {
#| const r = x.toString(16);
#| return uppercase ? r.toUpperCase() : r;
#|}

extern "js" fn hex2(b : Byte) -> String =
#|(x) => x.toString(16).padStart(2, '0')

pub fn BigInt::from_octets(octets : Bytes, ~signum : Int = 1) -> BigInt {
if signum < 0 {
return -1N * BigInt::from_octets(octets, signum=1)
}
if signum == 0 {
return 0N
}
let str = StringBuilder::new()
str.write_string("0x")
for octet in octets {
str.write_string(hex2(octet))
}
BigInt::from_string(str.to_string())
}

pub fn to_octets(self : BigInt, ~length? : Int) -> Bytes {
if self < 0 {
abort("negative BigInt")
}

// TODO: Optimize this
let buf = []
fn to_bytes() {
let len = buf.length()
let len = match length {
Some(len2) =>
if len2 <= 0 {
abort("negative length")
} else if len2 > len {
len2
} else {
len
}
None => len
}
let res = Bytes::new(len)
let mut j = res.length() - 1
for octet in buf {
res[j] = octet
j -= 1
}
res
}

if self == 0 {
buf.push(0)
return to_bytes()
}
let mut x = self
while x > 0 {
buf.push(x.to_byte())
x = x >> 8
}
to_bytes()
}

pub extern "js" fn compare(self : BigInt, other : BigInt) -> Int =
#|(x, y) => x < y ? -1 : x > y ? 1 : 0

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

pub extern "js" fn BigInt::from_int(x : Int) -> BigInt =
#|(x) => BigInt(x)

pub extern "js" fn BigInt::from_int64(x : Int64) -> BigInt =
#|(x) => BigInt(x.hi) * 0x100000000n + BigInt(x.lo >>> 0)

pub extern "js" fn BigInt::from_uint64(x : UInt64) -> BigInt =
#|(x) => BigInt(x.hi >>> 0) * 0x100000000n + BigInt(x.lo >>> 0)

pub extern "js" fn is_zero(self : BigInt) -> Bool =
#|(x) => x === 0n

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

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

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

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

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

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

pub extern "js" fn pow(
self : BigInt,
other : BigInt,
~modulus : BigInt = 1N
) -> BigInt =
#|(x, y, z) => (x ** y) % z

extern "js" fn to_byte(self : BigInt) -> Byte =
#|(x) => Number(BigInt.asUintN(8, x)) | 0

pub fn op_shl(self : BigInt, n : Int) -> BigInt {
if n < 0 {
abort("negative shift count")
}
self.js_shl(n)
}

pub fn op_shr(self : BigInt, n : Int) -> BigInt {
if n < 0 {
abort("negative shift count")
}
self.js_shr(n)
}

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

extern "js" fn js_shr(self : BigInt, other : Int) -> BigInt =
#|(x, y) => x >> BigInt(y)
19 changes: 19 additions & 0 deletions builtin/bigint_js_wbtest.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2024 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

fn check_len(_a : BigInt) -> Unit! {
assert_true!(true)
}

let zero = 0N
59 changes: 0 additions & 59 deletions builtin/bigint.mbt → builtin/bigint_nonjs.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -424,42 +424,6 @@ fn grade_school_div(self : BigInt, other : BigInt) -> (BigInt, BigInt) {

// Bitwise Operations

/// @alert deprecated "Use infix bitwise operator `<<` instead"
pub fn lsl(self : BigInt, n : Int) -> BigInt {
if n < 0 {
abort("negative shift count")
}
if not(self.is_zero()) {
let new_limbs = FixedArray::make(
self.len + (n + radix_bit_len - 1) / radix_bit_len, // ceiling(n / radix_bit_len)
0U,
)
let a = self.limbs
let r = n % radix_bit_len
let lz = n / radix_bit_len // number of leading zeros
let mut len = self.len + lz
if r != 0 {
let mut carry = 0UL
for i = 0; i < self.len; i = i + 1 {
carry = carry | (a[i].to_uint64() << r)
new_limbs[i + lz] = (carry % radix).to_uint()
carry = carry >> radix_bit_len
}
if carry != 0 {
new_limbs[self.len + lz] = carry.to_uint()
len += 1
}
} else {
for i = 0; i < self.len; i = i + 1 {
new_limbs[i + lz] = a[i]
}
}
{ limbs: new_limbs, sign: self.sign, len }
} else {
zero
}
}

/// Left shift a bigint
/// The sign of the result is the same as the sign of the input.
/// Only the absolute value is shifted.
Expand Down Expand Up @@ -497,15 +461,6 @@ pub fn op_shl(self : BigInt, n : Int) -> BigInt {
}
}

/// Left shift a bigint
/// The sign of the result is the same as the sign of the input.
/// Only the absolute value is shifted.
///
/// @alert deprecated "Use infix bitwise operator `<<` instead"
pub fn shl(self : BigInt, n : Int) -> BigInt {
self << n
}

/// Right shift a bigint
/// The sign of the result is the same as the sign of the input.
/// Only the absolute value is shifted.
Expand Down Expand Up @@ -547,20 +502,6 @@ pub fn op_shr(self : BigInt, n : Int) -> BigInt {
}
}

/// Right shift a bigint
/// The sign of the result is the same as the sign of the input.
/// Only the absolute value is shifted.
///
/// @alert deprecated "Use infix bitwise operator `>>` instead"
pub fn shr(self : BigInt, n : Int) -> BigInt {
self >> n
}

/// @alert deprecated "Use infix bitwise operator `>>` instead"
pub fn asr(self : BigInt, n : Int) -> BigInt {
self >> n
}

// Comparison Operations

/// Check if a bigint is zero
Expand Down
Loading