internal/binary_kernel API
用途
internal/binary_kernel 提供 Binary 系数所需的整数算术:非负任意精度整数 Coeff,及其比较、加法、减法、移位和位运算;乘法算法(朴素乘法、Karatsuba、Toom-3 和双素数 NTT);除法(Knuth 算法 D 与 Burnikel-Ziegler);整数平方根;GCD;以及与字节和任意进制文本之间的转换,并带有在各算法之间选择的分目标阈值。每个函数都是其参数的精确函数。本包不抛出任何状况,也没有舍入模式、上下文或标志的概念,这些都属于 binary。internal/binary_kernel 还声明内部枚举 BinRepr,即 Binary 的表示(每个目标一个文件:repr_nonjs.mbt 和 repr_js.mbt);参见 binary 设计页的“值的表示”一节。它是内部包:Luna-Flow/floating 之外的代码无法导入。binary 的公共系数类型 BinCoeff 是 Coeff 之上的 newtype,其方法(构造、转换、比较和查询)转发到本包;算术只存在于这里。Coeff 这个名字出现在 BinCoeff 的接口中,但模块之外的用户无法导入本包,因此无法对它调用或比较任何东西。参见教程和设计页。
导入
在模块内部,将该包添加到 moon.pkg 中:
import {
"Luna-Flow/floating/internal/binary_kernel" @binary_kernel,
}
然后把包用到的名字一次性引入作用域,例如放在单独的文件里:
///|
using @binary_kernel { type Coeff, bin_coeff_word }
本手册的示例检查器没有内部子包的别名,所以这些示例标为 nocheck。
系数类型
Coeff
非负整数。在 Native、LLVM、Wasm 和 Wasm-GC 上它是 CoeffRepr 之上的 newtype;在 JavaScript 上它是宿主 bigint。值是持久的:没有任何函数会修改操作数。
CoeffRepr
非 JavaScript 目标上的存储:低于 和 的值分别用 Inline64(low) 和 Inline128(high, low),更大的值用 Heap(words, len),其中有 len 个小端 64 位肢,最高肢非零。构造器可读是因为 binary 的单字快速路径会对它们做模式匹配;构造值请使用下面的函数。
函数族
| 族 | 函数 |
|---|---|
| 构造与转换 | Coeff::zero, Coeff::one, Coeff::from_uint64, Coeff::from_bytes_be, Coeff::parse, bin_coeff_from_words, bin_coeff_inline, Coeff::to_uint64, Coeff::to_bytes_be, Coeff::to_radix_string, Coeff::to_string |
| 形态查询 | Coeff::is_zero, Coeff::is_odd, Coeff::bit_length, Coeff::ctz, Coeff::test_bit, bin_coeff_word |
| 比较 | Coeff::compare, Coeff::equal, Coeff::not_equal, Coeff::op_lt, Coeff::op_le, Coeff::op_gt, Coeff::op_ge |
| 加、减、移位 | Coeff::add, Coeff::sub_checked, Coeff::shift_left, Coeff::shift_right, Coeff::shl, Coeff::shr, bin_coeff_add_normalized_odd, bin_coeff_sub_normalized_odd |
| 位运算 | Coeff::bit_and, Coeff::bit_or, Coeff::bit_xor |
| 乘与除 | Coeff::mul, bin_coeff_mul_short_ok, bin_coeff_mul_short, bin_words_mul, bin_words_mul_short, bin_coeff_short_product_limbs, Coeff::square, Coeff::div_rem_checked, bin_coeff_short_div_rem, bin_coeff_div_scaled_ok, bin_coeff_div_scaled_quotient, bin_coeff_div_short_ok, bin_coeff_div_short_quotient, bin_words_div_scaled_quotient, bin_words_div_short_quotient, bin_decline_words, bin_coeff_short_division_guard_bits |
| 数论与开方 | Coeff::gcd, Coeff::pow_nat, bin_coeff_sqrt_rem_kernel, bin_coeff_sqrt_rem_scaled, bin_coeff_sqrt_scaled_rounded, bin_words_sqrt_scaled_rounded |
| 肢行(native 上为 C,其他目标为可移植实现) | bin_limbs_mul_1, bin_limbs_addmul_1, bin_limbs_submul_1, bin_limbs_product, bin_limbs_window_combine |
| 机器字 | binary_fast_bits, binary_fast_mul_high, binary_fast_div_wide, binary_fast_shl_high, binary_fast_shl_low |
分目标阈值(bin_coeff_*_min_words、*_words、*_cutoff)是私有的:它们位于 coeff_thresholds_<target>.mbt,由 moon.pkg 选择。前置条件用 abort 检查(负的移位量、零除数):它们是调用者的编程错误,而不是运算的状况。
完整公共接口
此快照是该包生成的 pkg.generated.mbti。当正文与接口不一致时,以接口为准。
// Generated using `moon info`, DON'T EDIT IT
package "Luna-Flow/floating/internal/binary_kernel"
import {
"moonbitlang/core/debug",
}
// Values
pub fn bin_coeff_add_normalized_odd(Coeff, Coeff) -> (Coeff, Int32)
pub fn bin_coeff_div_scaled_ok(Coeff, Int32, Coeff) -> Bool
pub fn bin_coeff_div_scaled_quotient(Coeff, Int32, Coeff) -> Coeff
pub fn bin_coeff_div_short_ok(Coeff, Int32, Coeff) -> Bool
pub fn bin_coeff_div_short_quotient(Coeff, Int32, Coeff, Int32) -> Coeff
pub fn bin_coeff_from_words(FixedArray[UInt64], Int32) -> Coeff
pub fn bin_coeff_inline(UInt64, UInt64) -> Coeff
pub fn bin_coeff_mul_short(Coeff, Coeff, Int32) -> Coeff
pub fn bin_coeff_mul_short_ok(Coeff, Coeff, Int32) -> Bool
pub fn bin_coeff_short_div_rem(Coeff, Coeff) -> (Coeff, Coeff)
pub let bin_coeff_short_division_guard_bits : Int32
pub fn bin_coeff_short_product_limbs(Int32) -> Int32
pub fn bin_coeff_sqrt_rem_kernel(Coeff) -> (Coeff, Coeff)
pub fn bin_coeff_sqrt_rem_scaled(Coeff, Int32) -> (Coeff, Coeff)
pub fn bin_coeff_sqrt_scaled_rounded(Coeff, Int32, Int32) -> Coeff
pub fn bin_coeff_sub_normalized_odd(Coeff, Coeff) -> (Coeff, Int32)
pub fn bin_coeff_word(Coeff, Int32) -> UInt64
pub let bin_decline_words : FixedArray[UInt64]
pub fn bin_limbs_addmul_1(FixedArray[UInt64], Int32, FixedArray[UInt64], Int32, Int32, UInt64) -> UInt64
pub fn bin_limbs_mul_1(FixedArray[UInt64], Int32, FixedArray[UInt64], Int32, Int32, UInt64) -> UInt64
pub fn bin_limbs_product(FixedArray[UInt64], Int32, FixedArray[UInt64], Int32, Int32, FixedArray[UInt64], Int32, Int32) -> Unit
pub fn bin_limbs_submul_1(FixedArray[UInt64], Int32, FixedArray[UInt64], Int32, Int32, UInt64) -> UInt64
pub fn bin_limbs_window_combine(FixedArray[UInt64], Int32, FixedArray[UInt64], Int32, Int32, Int32, FixedArray[UInt64], Int32, Int32, Int32, Bool, UInt64) -> UInt64
pub fn bin_words_div_scaled_quotient(Coeff, Int32, Coeff) -> FixedArray[UInt64]
pub fn bin_words_div_short_quotient(Coeff, Int32, Coeff, Int32) -> FixedArray[UInt64]
pub fn bin_words_mul(Coeff, Coeff) -> FixedArray[UInt64]
pub fn bin_words_mul_short(Coeff, Coeff, Int32) -> FixedArray[UInt64]
pub fn bin_words_sqrt_scaled_rounded(Coeff, Int32, Int32) -> FixedArray[UInt64]
pub fn binary_fast_bits(UInt64) -> Int32
pub fn binary_fast_div_wide(UInt64, UInt64, UInt64) -> UInt64
pub fn binary_fast_mul_high(UInt64, UInt64) -> UInt64
pub fn binary_fast_shl_high(UInt64, Int32) -> UInt64
pub fn binary_fast_shl_low(UInt64, Int32) -> UInt64
// Errors
// Types and methods
#derive(Eq, @debug.Debug)
pub(all) enum BinRepr {
Fin64(negative~ : Bool, word~ : UInt64, exponent2~ : Int32, precision~ : Int32)
Fin128(negative~ : Bool, high~ : UInt64, low~ : UInt64, exponent2~ : Int32, precision~ : Int32)
FinBig(negative~ : Bool, coefficient~ : Coeff, exponent2~ : Int32, precision~ : Int32)
Inf(negative~ : Bool, precision~ : Int32)
NaN(negative~ : Bool, signaling~ : Bool, payload~ : Coeff, precision~ : Int32)
}
#derive(@debug.Debug)
pub struct Coeff(CoeffRepr)
pub fn Coeff::add(Self, Self) -> Self
pub fn Coeff::add_shifted_left(Self, Int32, Self) -> Self
pub fn Coeff::bit_and(Self, Self) -> Self
pub fn Coeff::bit_length(Self) -> Int32
pub fn Coeff::bit_or(Self, Self) -> Self
pub fn Coeff::bit_xor(Self, Self) -> Self
pub fn Coeff::compare(Self, Self) -> Int32
pub fn Coeff::ctz(Self) -> Int32
pub fn Coeff::div_rem_checked(Self, Self) -> Result[(Self, Self), String]
pub fn Coeff::equal(Self, Self) -> Bool
pub fn Coeff::from_bytes_be(BytesView) -> Self
pub fn Coeff::from_uint64(UInt64) -> Self
pub fn Coeff::gcd(Self, Self) -> Self
pub fn Coeff::is_odd(Self) -> Bool
pub fn Coeff::is_zero(Self) -> Bool
pub fn Coeff::mul(Self, Self) -> Self
pub fn Coeff::not_equal(Self, Self) -> Bool
pub fn Coeff::one() -> Self
pub fn Coeff::op_ge(Self, Self) -> Bool
pub fn Coeff::op_gt(Self, Self) -> Bool
pub fn Coeff::op_le(Self, Self) -> Bool
pub fn Coeff::op_lt(Self, Self) -> Bool
pub fn Coeff::output(Self, &Logger) -> Unit
pub fn Coeff::parse(String, radix? : Int32) -> Result[Self, String]
pub fn Coeff::pow_nat(Self, UInt32) -> Self
pub fn Coeff::shift_left(Self, Int32) -> Self
pub fn Coeff::shift_right(Self, Int32) -> Self
pub fn Coeff::shl(Self, Int32) -> Self
pub fn Coeff::shr(Self, Int32) -> Self
pub fn Coeff::square(Self) -> Self
pub fn Coeff::sub_checked(Self, Self) -> Result[Self, String]
pub fn Coeff::test_bit(Self, Int32) -> Bool
pub fn Coeff::to_bytes_be(Self) -> Bytes
pub fn Coeff::to_radix_string(Self, Int32) -> String
pub fn Coeff::to_repr(Self) -> @debug.Repr
pub fn Coeff::to_string(Self) -> String
pub fn Coeff::to_uint64(Self) -> UInt64?
pub fn Coeff::zero() -> Self
pub impl Add for Coeff
pub impl Compare for Coeff
pub impl Eq for Coeff
pub impl Mul for Coeff
pub impl Shl for Coeff
pub impl Show for Coeff
pub impl Shr for Coeff
#derive(@debug.Debug)
pub enum CoeffRepr {
Inline64(UInt64)
Inline128(UInt64, UInt64)
Heap(FixedArray[UInt64], Int32)
}
// Type aliases
// Traits