internal/binary_kernel API
目的
internal/binary_kernel は Binary の係数に必要な整数演算を提供します。非負の任意精度整数 Coeff、その比較、加算、減算、シフト、ビット演算、乗算アルゴリズム(教科書的乗算、Karatsuba、Toom-3、2 素数 NTT)、除算(Knuth のアルゴリズム D と Burnikel-Ziegler)、整数平方根、GCD、バイト列および任意の基数の文字列との相互変換であり、アルゴリズムを選ぶターゲット別のしきい値も含みます。すべての関数は引数の厳密な関数です。このパッケージは状態を送出せず、丸めモード、コンテキスト、フラグの概念も持ちません。それらは binary に属します。internal/binary_kernel は内部列挙型 BinRepr、すなわち Binary の表現も宣言します(ターゲットごとに 1 ファイル: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 ビットリムを持ち、最上位リムは 0 ではありません。binary の 1 ワード用高速パスがこれらにパターンマッチするため、コンストラクタは読み取り可能です。値の構築には下の関数を使ってください。
関数のグループ
| グループ | 関数 |
|---|---|
| 構築と変換 | 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