internal/decimal_kernel API
目的
internal/decimal_kernel は、decimal(IEEE 754)と decimal_gda(General Decimal Arithmetic)が共に必要とする 10 進係数の整数演算を担います。永続的な係数の型、加算、減算、比較、10 のべきによるシフト、桁数の計算、末尾のゼロの除去、リム単位の除算、そしてターゲットごとの閾値を持つ乗算・除算アルゴリズムです。どの関数も引数の厳密な関数です。このパッケージは条件を一切発生させず、丸めモード、コンテキスト、フラグの概念も持ちません。それらは 2 つの 10 進パッケージそれぞれの、標準に依存する最終化ルーチンの役割です。これは内部パッケージで、Luna-Flow/floating の外のコードからはインポートできず、どちらの 10 進パッケージもその型を公開しません。チュートリアルと設計ページも参照してください。
インポート
モジュール内では、moon.pkg にパッケージを追加します。
import {
"Luna-Flow/floating/internal/decimal_kernel" @decimal_kernel,
}
そのうえで、パッケージが使う名前を、たとえば専用のファイルで一度だけスコープに入れます。
///|
using @decimal_kernel {
type DecCoeff,
dec_coeff_from_int,
dec_coeff_magnitude_add_repr,
}
本マニュアルのサンプル検査器には内部サブパッケージのエイリアスがないため、これらのサンプルは nocheck としています。
係数の型
DecCoeff
非負整数で、 未満の値には Small(UInt64)、それ以外には Limbs(units, digits) を使います。後者は基数 のリトルエンディアンのリムで、先頭にゼロのリムを持たず、桁数をキャッシュします。値は永続的で、オペランドのリムを書き換える関数はありません。2 つの 10 進パッケージがいくつかの高速経路でパターンマッチするため、コンストラクタは公開されています。通常は以下の関数を使ってください。
DecCoeffRounding
8 つの丸め方向(HalfEven、HalfUp、HalfDown、Down、Ceiling、Floor、Up、ZeroFiveUp)を表す単純な列挙型です。これは状態ではなく方向です。各 10 進パッケージは自分の丸めモードの型を、全域的な match でこれに変換し、丸めのグループの関数は、保持される値が最後の桁で 1 単位増えるかどうかだけに答えます。
SignedDecCoeff
符号と DecCoeff の大きさの組で、中間値が負になりうるアルゴリズム(Toom-3、Newton 逆数)に使います。ゼロは決して負になりません。この型は pub で、フィールドは読めますが、値を作れるのはこのパッケージだけです。
関数のグループ
関数はすべて dec_coeff_* という名前です。この接頭辞がないのは remove_factor10_repr、exact_divide_by_power_of_ten_repr、trim_trailing_decimal_zeros_repr です。
| グループ | 関数 |
|---|---|
| 構築と変換 | dec_coeff_zero, dec_coeff_from_int, dec_coeff_from_nonnegative_int, dec_coeff_from_int_magnitude, dec_coeff_from_decimal_digits_repr, dec_coeff_from_bigint_repr, dec_coeff_to_bigint_repr, DecCoeff::to_string, dec_coeff_from_unit_accumulator, DecCoeff::to_bigint |
| 形状の問い合わせ | DecCoeff::digits10, DecCoeff::is_zero, dec_coeff_unit_len, dec_coeff_limb_at, dec_coeff_is_odd, dec_coeff_copy_limbs, dec_coeff_small_value, dec_coeff_matches_unit_repr, dec_coeff_slice, dec_coeff_small_digits |
| 加算・減算・比較 | dec_coeff_magnitude_add_repr, dec_coeff_magnitude_sub_repr, dec_coeff_magnitude_add_unit, dec_coeff_magnitude_sub_unit, dec_coeff_signed_add_repr, dec_coeff_compare_repr, dec_coeff_compare_scaled_repr, dec_coeff_compare_half_power10, dec_coeff_unit_add_sub |
| 10 のべきと末尾ゼロの除去 | dec_coeff_shift_power10_repr, dec_coeff_power10_repr, remove_factor10_repr, exact_divide_by_power_of_ten_repr, trim_trailing_decimal_zeros_repr |
| 乗算 | dec_coeff_multiply_repr, dec_coeff_square_repr, dec_coeff_multiply_by_unit_repr |
| 除算 | dec_coeff_div_rem_repr, dec_coeff_mod_repr, dec_coeff_div_rem_by_unit_repr, dec_coeff_div_rem_knuth_repr, dec_coeff_div_rem_burnikel_ziegler, dec_coeff_div_rem_newton_try |
| 整数論 | dec_coeff_gcd_repr, dec_coeff_count_factor_unit_repr, dec_coeff_pow_unit_repr, dec_coeff_exact_power_repr, dec_coeff_exact_power_if_fits_repr |
| 丸めの判断 | DecCoeffRounding, dec_coeff_should_increment, dec_coeff_round_div, dec_coeff_round_div_bigint, dec_coeff_round_div_pow10, dec_coeff_round_div_pow10_owned, dec_coeff_round_accumulator, dec_coeff_round_pow10_coeff, dec_coeff_discards_nonzero, dec_coeff_accumulator_digits, dec_coeff_accumulator_discards_nonzero, dec_coeff_round_to_digits, dec_coeff_huge_shift_rounded_unit, dec_coeff_sqrt_should_increment, dec_coeff_addend_rounding_step, dec_coeff_round_div_expansion, dec_coeff_div_scaled_inexact |
| 厳密なヘルパー | compare_finite_magnitude_repr, dec_coeff_integer_sqrt_rem_repr, dec_coeff_floor_nth_root_repr, dec_coeff_exact_nth_root_repr, has_finite_decimal_expansion_repr, decimal_ln_atanh_scaled_coeff, decimal_round_scaled_coeff, decimal_floor_div_scaled_repr, dec_coeff_shift_power10_mod_wide_repr, dec_coeff_to_bounded_int, is_power_of_ten_coeff, decimal_small_pow10, dec_coeff_shift_power10_mod_repr |
| 桁ごとの論理演算 | logical_binary_digits_repr, coefficient_shift_digits_repr, coefficient_rotate_digits_repr, logical_coeff_digits_lsd, logical_coeff_from_digits_lsd |
| 符号付きの値 | SignedDecCoeff, SignedDecCoeff::add, SignedDecCoeff::neg, SignedDecCoeff::sub, dec_coeff_signed_compare_int_repr, dec_coeff_signed_step_unit_repr, signed_dec_coeff, signed_dec_coeff_from_int_product |
| しきい値と定数 | dec_coeff_base, dec_coeff_burnikel_ziegler_threshold, dec_coeff_burnikel_ziegler_threshold_16k, dec_coeff_burnikel_ziegler_threshold_8k, dec_coeff_burnikel_ziegler_threshold_large, dec_coeff_dpun, dec_coeff_karatsuba_square_threshold, dec_coeff_karatsuba_threshold, dec_coeff_newton_enabled, dec_coeff_newton_threshold, dec_coeff_ntt_mul_threshold, dec_coeff_ntt_mul_threshold_16k, dec_coeff_ntt_mul_threshold_32k, dec_coeff_ntt_mul_threshold_64k, dec_coeff_ntt_mul_threshold_large, dec_coeff_ntt_square_threshold, dec_coeff_ntt_square_threshold_16k, dec_coeff_ntt_square_threshold_32k, dec_coeff_ntt_square_threshold_64k, dec_coeff_ntt_square_threshold_8k, dec_coeff_ntt_square_threshold_large, dec_coeff_ntt_square_toom3_threshold, dec_coeff_ntt_square_toom3_threshold_16k, dec_coeff_ntt_square_toom3_threshold_32k, dec_coeff_ntt_square_toom3_threshold_64k, dec_coeff_ntt_square_toom3_threshold_8k, dec_coeff_ntt_square_toom3_threshold_large, dec_coeff_small_limit, dec_coeff_small_powers10, dec_coeff_toom3_threshold。目標ごとに異なるものは coeff_thresholds_<target>.mbt で選ばれます |
事前条件は abort で検査します(負のシフト量、ゼロの除数、ゼロを下回る減算、 の外にある 1 リムの除数)。これらは呼び出し側のプログラミング上の誤りであり、演算の条件ではありません。
公開インターフェース全体
このスナップショットは、パッケージの生成された pkg.generated.mbti です。説明文とインターフェースが食い違う場合は、こちらが正となります。
// Generated using `moon info`, DON'T EDIT IT
package "Luna-Flow/floating/internal/decimal_kernel"
import {
"moonbitlang/core/bigint",
"moonbitlang/core/debug",
}
// Values
pub fn coefficient_rotate_digits_repr(DecCoeff, Int32, Int32) -> DecCoeff
pub fn coefficient_shift_digits_repr(DecCoeff, Int32, Int32) -> DecCoeff
pub fn compare_finite_magnitude_repr(DecCoeff, Int32, DecCoeff, Int32) -> Int32
pub fn dec_coeff_accumulator_digits(Array[UInt32], Int32) -> Int32
pub fn dec_coeff_accumulator_discards_nonzero(Array[UInt32], Int32) -> Bool
pub fn dec_coeff_addend_rounding_step(Bool, Bool, Bool, Bool, Int32, DecCoeffRounding) -> Int32
pub let dec_coeff_base : Int32
pub let dec_coeff_burnikel_ziegler_threshold : Int32
pub let dec_coeff_burnikel_ziegler_threshold_16k : Int32
pub let dec_coeff_burnikel_ziegler_threshold_8k : Int32
pub let dec_coeff_burnikel_ziegler_threshold_large : Int32
pub fn dec_coeff_compare_half_power10(DecCoeff) -> Int32
pub fn dec_coeff_compare_repr(DecCoeff, DecCoeff) -> Int32
pub fn dec_coeff_compare_scaled_repr(DecCoeff, DecCoeff, Int32) -> Int32
pub fn dec_coeff_copy_limbs(DecCoeff) -> Array[UInt32]
pub fn dec_coeff_count_factor_unit_repr(DecCoeff, Int32) -> (DecCoeff, Int32)
pub fn dec_coeff_discards_nonzero(DecCoeff, Int32) -> Bool
pub fn dec_coeff_div_rem_burnikel_ziegler(DecCoeff, DecCoeff) -> (DecCoeff, DecCoeff)
pub fn dec_coeff_div_rem_by_unit_repr(DecCoeff, Int32) -> (DecCoeff, Int32)
pub fn dec_coeff_div_rem_knuth_repr(DecCoeff, DecCoeff) -> (DecCoeff, DecCoeff)
pub fn dec_coeff_div_rem_newton_try(DecCoeff, DecCoeff) -> (DecCoeff, DecCoeff)?
pub fn dec_coeff_div_rem_repr(DecCoeff, DecCoeff) -> (DecCoeff, DecCoeff)
pub fn dec_coeff_div_scaled_inexact(DecCoeff, Int32, DecCoeff) -> DecCoeff
pub let dec_coeff_dpun : Int32
pub fn dec_coeff_exact_nth_root_repr(DecCoeff, Int32) -> DecCoeff?
pub fn dec_coeff_exact_power_if_fits_repr(DecCoeff, Int32, Int32) -> DecCoeff?
pub fn dec_coeff_exact_power_repr(DecCoeff, Int32) -> DecCoeff
pub fn dec_coeff_floor_nth_root_repr(DecCoeff, Int32) -> DecCoeff
pub fn dec_coeff_from_bigint_repr(@bigint.BigInt) -> DecCoeff
pub fn dec_coeff_from_decimal_digits_repr(String) -> DecCoeff?
pub fn dec_coeff_from_int(Int32) -> DecCoeff
pub fn dec_coeff_from_int_magnitude(Int32) -> DecCoeff
pub fn dec_coeff_from_nonnegative_int(Int32) -> DecCoeff
pub fn dec_coeff_from_unit_accumulator(Array[UInt32], Int32) -> DecCoeff
pub fn dec_coeff_gcd_repr(DecCoeff, DecCoeff) -> DecCoeff
pub fn dec_coeff_huge_shift_rounded_unit(DecCoeff, Bool, DecCoeffRounding) -> DecCoeff
pub fn dec_coeff_integer_sqrt_rem_repr(DecCoeff) -> (DecCoeff, DecCoeff)
pub fn dec_coeff_is_odd(DecCoeff) -> Bool
pub let dec_coeff_karatsuba_square_threshold : Int32
pub let dec_coeff_karatsuba_threshold : Int32
pub fn dec_coeff_limb_at(DecCoeff, Int32) -> UInt32
pub fn dec_coeff_magnitude_add_repr(DecCoeff, DecCoeff) -> DecCoeff
pub fn dec_coeff_magnitude_add_unit(DecCoeff, Int32) -> DecCoeff
pub fn dec_coeff_magnitude_sub_repr(DecCoeff, DecCoeff) -> DecCoeff
pub fn dec_coeff_magnitude_sub_unit(DecCoeff, Int32) -> DecCoeff
pub fn dec_coeff_matches_unit_repr(DecCoeff, Int32) -> Bool
pub fn dec_coeff_mod_repr(DecCoeff, DecCoeff) -> DecCoeff
pub fn dec_coeff_mod_unit(DecCoeff, Int32) -> Int32
pub fn dec_coeff_multiply_by_unit_repr(DecCoeff, Int32) -> DecCoeff
pub fn dec_coeff_multiply_repr(DecCoeff, DecCoeff) -> DecCoeff
pub let dec_coeff_newton_enabled : Bool
pub let dec_coeff_newton_threshold : Int32
pub let dec_coeff_ntt_mul_threshold : Int32
pub let dec_coeff_ntt_mul_threshold_16k : Int32
pub let dec_coeff_ntt_mul_threshold_32k : Int32
pub let dec_coeff_ntt_mul_threshold_64k : Int32
pub let dec_coeff_ntt_mul_threshold_large : Int32
pub let dec_coeff_ntt_square_threshold : Int32
pub let dec_coeff_ntt_square_threshold_16k : Int32
pub let dec_coeff_ntt_square_threshold_32k : Int32
pub let dec_coeff_ntt_square_threshold_64k : Int32
pub let dec_coeff_ntt_square_threshold_8k : Int32
pub let dec_coeff_ntt_square_threshold_large : Int32
pub let dec_coeff_ntt_square_toom3_threshold : Int32
pub let dec_coeff_ntt_square_toom3_threshold_16k : Int32
pub let dec_coeff_ntt_square_toom3_threshold_32k : Int32
pub let dec_coeff_ntt_square_toom3_threshold_64k : Int32
pub let dec_coeff_ntt_square_toom3_threshold_8k : Int32
pub let dec_coeff_ntt_square_toom3_threshold_large : Int32
pub fn dec_coeff_pow_unit_repr(Int32, Int32) -> DecCoeff
pub fn dec_coeff_power10_repr(Int32) -> DecCoeff
pub fn dec_coeff_round_accumulator(Array[UInt32], Int32, Int32, Bool, DecCoeffRounding) -> DecCoeff
pub fn dec_coeff_round_div(DecCoeff, DecCoeff, Bool, DecCoeffRounding) -> DecCoeff
pub fn dec_coeff_round_div_bigint(@bigint.BigInt, @bigint.BigInt, Bool, DecCoeffRounding) -> @bigint.BigInt
pub fn dec_coeff_round_div_expansion(DecCoeff, DecCoeff, Bool, DecCoeffRounding) -> (DecCoeff, Bool)
pub fn dec_coeff_round_div_pow10(DecCoeff, Int32, Bool, DecCoeffRounding) -> (DecCoeff, Bool)
pub fn dec_coeff_round_div_pow10_owned(Array[UInt32], Int32, Int32, Bool, DecCoeffRounding) -> (DecCoeff, Bool)
pub fn dec_coeff_round_pow10_coeff(DecCoeff, Int32, Bool, DecCoeffRounding) -> DecCoeff
pub fn dec_coeff_round_to_digits(DecCoeff, Int32, Int32, Bool, DecCoeffRounding) -> (DecCoeff, Int32, Bool, Bool)
pub fn dec_coeff_shift_power10_mod_repr(DecCoeff, Int32, DecCoeff) -> DecCoeff
pub fn dec_coeff_shift_power10_mod_wide_repr(DecCoeff, Int64, DecCoeff) -> DecCoeff
pub fn dec_coeff_shift_power10_repr(DecCoeff, Int32) -> DecCoeff
pub fn dec_coeff_should_increment(DecCoeff, DecCoeff, DecCoeff, Bool, DecCoeffRounding) -> Bool
pub fn dec_coeff_signed_add_repr(Bool, DecCoeff, Bool, DecCoeff) -> (Bool, DecCoeff)
pub fn dec_coeff_signed_compare_int_repr(Bool, DecCoeff, Int32) -> Int32
pub fn dec_coeff_signed_step_unit_repr(Bool, DecCoeff, Bool) -> (Bool, DecCoeff)
pub fn dec_coeff_slice(DecCoeff, Int32, Int32) -> DecCoeff
pub fn dec_coeff_small_digits(UInt64) -> Int32
pub let dec_coeff_small_limit : UInt64
pub let dec_coeff_small_powers10 : Array[UInt64]
pub fn dec_coeff_small_value(DecCoeff) -> UInt64?
pub fn dec_coeff_sqrt_rounding(DecCoeff) -> (DecCoeff, DecSqrtRemainder)
pub fn dec_coeff_sqrt_should_increment(DecCoeff, Int32, Bool, DecCoeffRounding) -> Bool
pub fn dec_coeff_square_repr(DecCoeff) -> DecCoeff
pub fn dec_coeff_to_bigint_repr(DecCoeff) -> @bigint.BigInt
pub fn dec_coeff_to_bounded_int(DecCoeff, Int32) -> Int32?
pub let dec_coeff_toom3_threshold : Int32
pub fn dec_coeff_unit_add_sub(Array[UInt32], Int32, Array[UInt32], Int32, Int32, Array[UInt32], Int32) -> Int32
pub fn dec_coeff_unit_len(DecCoeff) -> Int32
pub fn dec_coeff_zero() -> DecCoeff
pub fn decimal_floor_div_scaled_repr(Bool, DecCoeff, DecCoeff) -> Int32
pub fn decimal_ln_atanh_scaled_coeff(DecCoeff, DecCoeff, Int32) -> SignedDecCoeff
pub fn decimal_round_scaled_coeff(SignedDecCoeff, Int32) -> SignedDecCoeff
pub fn decimal_small_pow10(Int32) -> Int32
pub fn exact_divide_by_power_of_ten_repr(DecCoeff, Int32) -> DecCoeff?
pub fn has_finite_decimal_expansion_repr(DecCoeff, DecCoeff) -> Bool
pub fn is_power_of_ten_coeff(DecCoeff) -> Bool
pub fn logical_binary_digits_repr(DecCoeff, DecCoeff, Int32, (Bool, Bool) -> Bool) -> DecCoeff
pub fn logical_coeff_digits_lsd(DecCoeff, Int32) -> Array[Int32]
pub fn logical_coeff_from_digits_lsd(Array[Int32]) -> DecCoeff
pub fn remove_factor10_repr(DecCoeff, Int32) -> (DecCoeff, Int32)
pub fn signed_dec_coeff(Bool, DecCoeff) -> SignedDecCoeff
pub fn signed_dec_coeff_from_int_product(Int32, DecCoeff) -> SignedDecCoeff
pub fn trim_trailing_decimal_zeros_repr(DecCoeff, Int32, max_drop? : Int32) -> (DecCoeff, Int32, Int32)
// Errors
// Types and methods
#derive(@debug.Debug)
pub(all) enum DecCoeff {
Small(UInt64)
Limbs(Array[UInt32], Int32)
}
pub fn DecCoeff::digits10(Self) -> Int32
pub fn DecCoeff::is_zero(Self) -> Bool
pub fn DecCoeff::to_bigint(Self) -> @bigint.BigInt
pub fn DecCoeff::to_string(Self) -> String
#derive(Eq)
pub(all) enum DecCoeffRounding {
HalfEven
HalfUp
HalfDown
Down
Ceiling
Floor
Up
ZeroFiveUp
}
#derive(Eq, @debug.Debug)
pub(all) enum DecSqrtRemainder {
Zero
UpToRoot
AboveRoot
}
pub struct SignedDecCoeff {
negative : Bool
magnitude : DecCoeff
}
pub fn SignedDecCoeff::add(Self, Self) -> Self
pub fn SignedDecCoeff::neg(Self) -> Self
pub fn SignedDecCoeff::sub(Self, Self) -> Self
// Type aliases
// Traits