frontend/mpfr_expr API
目的
frontend/mpfr_expr は GNU MPFR で作られた参照データを解析し、binary に対して実行します。6 つの形式をサポートします:MPFR の平方根 data_check ファイル、整数冪の行、例外フラグ付きの初等関数マトリクス、10 進文字変換の 2 つのコーパス(mpfr_strtofr と mpfr_get_str)、そしてシード付きランダム差分行(mpfr-random-v1)とその十進版(mpfr-decimal-random-v2)。このパッケージは IO を行いません。ランナーは cli/mpfr_expr_cli です。作業の流れはチュートリアル、合否の規則は設計ページを参照してください。
インポート
moon.pkg の import ブロックにパッケージを追加します。
import {
"Luna-Flow/floating-tools/frontend/mpfr_expr",
}
例では別名 @mpfr_expr を通じて呼び出します。行はテキストとして書かれるため、binary をインポートする必要はありません。
共通の行構文
5 つのパーサーはいずれも、テキストを \n で分割し、各行をトリムし、空行と # で始まる行を読み飛ばします。フィールドは空白、タブ、キャリッジリターンで区切られます。行番号は 1 から始まります。丸めフィールドは次のいずれかです
| フィールド | BinaryRoundingMode |
|---|---|
n | RoundTiesToEven |
na | RoundTiesToAway |
z | RoundTowardZero |
u | RoundTowardPositive |
d | RoundTowardNegative |
a | RoundAwayFromZero |
数は、そのフィールドに指定された精度で @binary.Binary::from_hex(小数点のない整数の 16 進仮数を持つ 0x…p…、inf、-inf、nan)によって読み取られます。その精度より多いビットを持つ仮数は最近接偶数丸めされます。各パーサは、すべての行が有効なら Ok(document) を、そうでなければ無効な行ごとに 1 つの ParseDiagnostic を含む Err を返します。
平方根データ
parse_sqrt_data
parse_sqrt_data(source, text) は 5 つのフィールド input_precision output_precision rounding input expected からなる行を構文解析します。
pub fn parse_sqrt_data(String, String) -> Result[MpfrDocument, Array[ParseDiagnostic]]
入力は input_precision ビットで、期待値は output_precision ビットで読み取られます。行 id は sqrt:LINE です。
execute_sqrt_data
execute_sqrt_data(document) は各行について input.sqrt_ctx(ctx) を計算します。ここで ctx は BinaryContext::new(precision=output_precision, rounding~) です。
pub fn execute_sqrt_data(MpfrDocument) -> RunSummary
結果が期待される Binary と等しい(==)とき、その行は合格です。フラグは比較されません。
MpfrDocument, MpfrCase
MpfrDocument は構文解析済みの平方根ファイルです。MpfrCase はその 1 行で、公開メソッドはありません。
pub struct MpfrDocument {
// private fields
}
pub struct MpfrCase {
// private fields
}
MpfrDocument::source, MpfrDocument::case_count
source はパーサに与えられた名前を、case_count は行数を返します。
pub fn MpfrDocument::source(Self) -> String
pub fn MpfrDocument::case_count(Self) -> Int
整数べき
parse_pow_data
parse_pow_data(source, text) は 10 個のフィールドからなる行を構文解析します。
pub fn parse_pow_data(String, String) -> Result[MpfrPowDocument, Array[ParseDiagnostic]]
フィールドは precision rounding input_coefficient_hex input_exponent2 input_negative exponent expected_coefficient_hex expected_exponent2 expected_negative inexact です。ここで 2 つの数は であり、符号と inexact は 0 または 1、exponent は整数のべき指数です。入力は 512 ビットで、期待値は precision ビットで読み取られます。行 id は pow:LINE です。係数のフィールドは接頭辞のない 16 進数字であり、パーサはそれらから 0xCOEFFpEXP を組み立てます。
execute_pow_data
execute_pow_data(document) は ctx = BinaryContext::new(precision=precision, rounding~) として input.pown_ctx(exponent, ctx) を計算します。
pub fn execute_pow_data(MpfrPowDocument) -> RunSummary
結果が期待値と等しく(==)、inexact フラグが行のフラグと等しく、アンダーフロー・オーバーフロー・ゼロ除算・invalid のいずれのフラグも発生しないとき、その行は合格です。
MpfrPowDocument, MpfrPowCase
MpfrPowDocument は構文解析済みのべき乗ファイルです。MpfrPowCase はその 1 行で、公開メソッドはありません。
pub struct MpfrPowDocument {
// private fields
}
pub struct MpfrPowCase {
// private fields
}
MpfrPowDocument::source, MpfrPowDocument::case_count
これらのメソッドは、パーサに渡された名前と行の数を返します。
pub fn MpfrPowDocument::source(Self) -> String
pub fn MpfrPowDocument::case_count(Self) -> Int
初等関数
parse_elementary_data
parse_elementary_data(source, text) は 10 個のフィールド op precision rounding x y n expected inexact invalid divbyzero からなる行を構文解析します。
pub fn parse_decimal_random_data(String, String) -> Result[MpfrDecimalRandomDocument, Array[ParseDiagnostic]]
pub fn parse_elementary_data(String, String) -> Result[MpfrElementaryDocument, Array[ParseDiagnostic]]
op は exp、exp2、exp10、expm1、exp2m1、exp10m1、ln、log2、log10、log1p、log2p1、log10p1、sqrt、rsqrt、rootn、pown、compound、pow、powr、hypot、sin、cos、tan、sinpi、cospi、tanpi、asin、acos、atan、atan2、asinpi、acospi、atanpi、atan2pi、sinh、cosh、tanh、asinh、acosh、atanh のいずれかです。x と y は 512 ビットで読むオペランド(未使用の y は -)、n は rootn、pown、compound の整数引数、expected は precision ビットで読み、最後の三つのフィールドは 0 または 1 です。行 id は op:LINE です。
2 オペランド関数 pow、powr、hypot、atan2、atan2pi で y が - の行は、診断 "invalid MPFR elementary field" になります。
execute_elementary_data
execute_elementary_data(document) は各行を、その演算に対応する Binary の checked な try_*_ctx メソッド(sqrt と pown については sqrt_ctx と pown_ctx)で BinaryContext::new(precision=precision, rounding~) のもとで評価します。
pub fn execute_decimal_random_data(MpfrDecimalRandomDocument) -> RunSummary
pub fn execute_elementary_data(MpfrElementaryDocument) -> RunSummary
結果が期待値と等しいと比較され(compare == 0。そのため、すべての NaN はすべての NaN と等しい)、ゼロの場合は符号も同じであり、inexact・invalid・ゼロ除算の各フラグが行のフラグと等しく、アンダーフローもオーバーフローも発生しないとき、その行は合格です。try_*_ctx メソッドが Err を返した場合(例えば認証の失敗)、その行はメッセージ "certification failure for MPFR differential case" で失敗します。
MpfrElementaryDocument
MpfrElementaryDocument は構文解析済みの初等関数ファイルです。
pub struct MpfrElementaryDocument {
// private fields
}
MpfrElementaryDocument::source, MpfrElementaryDocument::case_count
これらのメソッドは、パーサに渡された名前と行の数を返します。
pub fn MpfrElementaryDocument::source(Self) -> String
pub fn MpfrElementaryDocument::case_count(Self) -> Int
十進文字変換
parse_strtofr_data
parse_strtofr_data(source, text) は 7 フィールドの行 precision rounding input expected inexact overflow underflow を解析します。
pub fn parse_strtofr_data(String, String) -> Result[MpfrStrtofrDocument, Array[ParseDiagnostic]]
input はテキストのまま保持される十進リテラル、expected は precision ビットで読み込んだ mpfr_strtofr の結果で、最後の 3 フィールドは 0 か 1 です。行 id は strtofr:LINE です。
execute_strtofr_data
execute_strtofr_data(document) は ctx = BinaryContext::new(precision=precision, rounding~) で Binary::from_string_ctx(input, ctx) を評価します。
pub fn execute_strtofr_data(MpfrStrtofrDocument) -> RunSummary
結果が期待値と等しく、符号(ゼロを含む)と分類が同じで、inexact・overflow・underflow フラグが行のフラグと等しく、invalid と division by zero が立たなければ、その行は合格です。パーサーに拒否されたリテラルは行を不合格にします。
MpfrStrtofrDocument, MpfrStrtofrDocument::source, MpfrStrtofrDocument::case_count
MpfrStrtofrDocument は解析済みのファイルです。メソッドはパーサーに渡された名前と行数を返します。
pub struct MpfrStrtofrDocument {
// private fields
}
pub fn MpfrStrtofrDocument::source(Self) -> String
pub fn MpfrStrtofrDocument::case_count(Self) -> Int
parse_decimal_output_data
parse_decimal_output_data(source, text) は 2 種類の行を解析します。
pub fn parse_decimal_output_data(String, String) -> Result[MpfrDecimalOutputDocument, Array[ParseDiagnostic]]
fixed digits precision rounding value expected_digits expected_exponent10 inexact(8 フィールド)は digits 桁の有効数字で出力した値です。expected_digits は桁の文字列、expected_exponent10 はその先頭桁の十進指数です。shortest precision value expected_digit_count(4 フィールド)は最短の往復形式の有効桁数です。値は precision ビットで読み込みます。行 id は fixed:LINE と shortest:LINE です。
execute_decimal_output_data
execute_decimal_output_data(document) は fixed 行で Binary::to_decimal_string_ctx、shortest 行で Binary::to_shortest_string を実行します。
pub fn execute_decimal_output_data(MpfrDecimalOutputDocument) -> RunSummary
fixed 行は、出力された桁、指数、符号が一致し、inexact フラグが行のフラグと等しい(オーバーフローとアンダーフローはなし)とき合格です。shortest 行は、テキストの有効桁数がちょうど expected_digit_count(先頭のゼロと位取り表記の末尾のゼロは数えない)で、Binary::from_string(text, precision~) が同じ値を返すとき合格です。
MpfrDecimalOutputDocument, MpfrDecimalOutputDocument::source, MpfrDecimalOutputDocument::case_count
pub struct MpfrDecimalOutputDocument {
// private fields
}
pub fn MpfrDecimalOutputDocument::source(Self) -> String
pub fn MpfrDecimalOutputDocument::case_count(Self) -> Int
シード付きランダム行
mpfr-random-v1 の行は tools/generate_mpfr_random_oracle.c から来ます。1 ケース 1 行で、精度は最大 10,000、6 つの丸め方向、tininess 規則付きの任意の指数範囲、最大 3 つの正確なオペランド、フラグ付きの MPFR の結果を持ちます。完全なフィールド一覧は testdata/binary/README.md にあります。
id op rounding precision emin emax tininess a b c n expected flags
parse_random_data
pub fn parse_random_data(String, String) -> Result[MpfrRandomDocument, Array[ParseDiagnostic]]
テキストを解析します(最初の引数はソース名)。空行と # で始まる行は読み飛ばします。それ以外の各行は、13 個のフィールド、既知の演算(add sub mul div sqrt fma strtofr getstr shortest または初等関数名のいずれか)、n na z u d a のいずれかの丸め、正の精度、- - - または 2 つの整数と a/b からなる指数範囲、正確なオペランド([-]0x<hex>p<exp>、inf、-inf、nan、-)、- または xouvz の部分集合である flags を持たなければなりません。オペランドと結果は少なくともその行の精度で読み込まれます。より低い精度の最小非正規数より小さい値を読むと 0 に潰れてしまうためです。不正な行ごとに診断が 1 つ出ます。
execute_random_data
pub fn execute_random_data(MpfrRandomDocument) -> RunSummary
各行をその行の BinaryContext で実行し(指数範囲があれば有界、なければ unbounded)、値とフラグが一致したときだけその行を合格にします。値はゼロと無限大の符号も含めて比較され、NaN の結果は quiet NaN でなければなりません。初等関数の定義域エラー(ArithmeticError::is_domain_error)は無効演算として扱われ、フラグ v 付きの quiet NaN になります。それ以外のエラーは、そのエラーメッセージとともにその行を失敗にします。行 ID は行の最初のフィールドです。
シード付きランダム十進行
mpfr-decimal-random-v2 行は tools/generate_mpfr_decimal_random.py が生成します。IEEE 十進の初等関数(exp exp2 exp10 expm1 ln log2 log10 log1p sin cos tan sinpi cospi tanpi asin acos atan sinh cosh tanh asinh acosh atanh pow hypot atan2 rootn pown)について 1 ケース 1 行で、十進コンテキスト(IEEE 形式の精度と指数範囲、IEEE の 5 つの丸め方向のいずれか:half_even、half_up、down、ceiling、floor)、正確な十進オペランド、期待値とフラグを持ちます。期待値は厳密な 768 ビットの MPFR 包含区間を丸めた結果であり、MPFR の単一の結果ではありません。包含区間の両端が同じに丸められ、丸めた値が区間の外にある(真値が表現不能な)場合にだけ行が作られるため、このコーパスは正確な結果を抽出しません。
id op rounding precision emax a b n expected flags
parse_decimal_random_data
pub fn parse_decimal_random_data(String, String) -> Result[MpfrDecimalRandomDocument, Array[ParseDiagnostic]]
テキストを解析する(第1引数はソース名)。空行と # で始まる行は読み飛ばす。それ以外の各行は10個のフィールドを持たなければならない:識別子、既知の演算、half_even half_up down ceiling floor のいずれかの丸め(IEEE 754-2019 4.3 の5つの丸め方向属性)、正の精度、有効な DecimalContext を成す整数 emax(精度と emax で定まる IEEE 形式:emin = 1 - emax、丸め前にタイニネスを検出)、オペランド1つ、2つ目のオペランドまたは -(pow、hypot、atan2 で必須)、整数(rootn と pown で使用、それ以外は 0)、期待値、そして - または xouvz の部分集合(inexact、overflow、underflow、invalid operation、division by zero、この順)である flags。オペランドと期待値に書かれた桁はすべて保持される。不正な行ごとに診断が1件となる。
execute_decimal_random_data
pub fn execute_decimal_random_data(MpfrDecimalRandomDocument) -> RunSummary
行のコンテキストで各行を演算の try_*_ctx 形式に通して実行するため、認証の失敗は黙った NaN にならず、その行の失敗になります。結果の値と cohort が期待値に等しく(total_order_compare で比較するので 1.0 と 1.00 は異なります)、フラグが一致したときだけ行は合格します。行 id は行の最初のフィールドです。
MpfrDecimalRandomDocument, MpfrDecimalRandomDocument::source, MpfrDecimalRandomDocument::case_count, MpfrDecimalRandomDocument::case_ids
MpfrDecimalRandomDocument は解析済みの mpfr-decimal-random-v2 コーパス(IEEE decimal の初等関数を MPFR と照合するもの)です。source は解析時のラベル、case_count は行数、case_ids は各行の id を順に返します。
pub struct MpfrDecimalRandomDocument {
// private fields
}
pub fn MpfrDecimalRandomDocument::source(Self) -> String
pub fn MpfrDecimalRandomDocument::case_count(Self) -> Int32
pub fn MpfrDecimalRandomDocument::case_ids(Self) -> Array[String]
MpfrRandomDocument, MpfrRandomDocument::source, MpfrRandomDocument::case_count, MpfrRandomDocument::case_ids
MpfrRandomDocument は解析済みの mpfr-random-v1 コーパス(2 進演算を MPFR と照合するもの)で、同じ 3 つのアクセサを持ちます。
pub fn MpfrRandomDocument::source(Self) -> String
pub fn MpfrRandomDocument::case_count(Self) -> Int32
pub fn MpfrRandomDocument::case_ids(Self) -> Array[String]
case_ids は行 ID を順に列挙します。差分ハーネスはこれを使ってバッチ内の失敗ケースを特定します。
診断と結果
ParseDiagnostic
ParseDiagnostic は 1 つの無効な行です。
pub struct ParseDiagnostic {
// private fields
} derive(Eq, @debug.Debug)
ParseDiagnostic::source, ParseDiagnostic::line, ParseDiagnostic::message
これらのメソッドは、パーサに渡された名前、1 始まりの行番号、およびメッセージを返します。
pub fn ParseDiagnostic::source(Self) -> String
pub fn ParseDiagnostic::line(Self) -> Int
pub fn ParseDiagnostic::message(Self) -> String
メッセージは問題を示します。例えば "MPFR pow row must contain ten fields"、"invalid MPFR pow field"、"invalid MPFR elementary expected value" などです。
CaseResult
CaseResult は 1 行の結果です。
pub struct CaseResult {
// private fields
}
CaseResult::id, CaseResult::passed, CaseResult::message
これらのメソッドは、行 id、その行が合格したかどうか、およびメッセージを返します。
pub fn CaseResult::id(Self) -> String
pub fn CaseResult::passed(Self) -> Bool
pub fn CaseResult::message(Self) -> String
メッセージは合格なら空で、そうでなければ期待値と実際の値を 16 進で示します(べき乗と初等関数の行では比較したフラグも示します)。
RunSummary
RunSummary は 1 つの文書の行を集計します。
pub struct RunSummary {
// private fields
}
RunSummary::total_cases, RunSummary::passed_cases, RunSummary::failed_cases
これらのメソッドは、行の数と、合格した数および失敗した数を返します。
pub fn RunSummary::total_cases(Self) -> Int
pub fn RunSummary::passed_cases(Self) -> Int
pub fn RunSummary::failed_cases(Self) -> Int
RunSummary::results, RunSummary::success
results は行ごとの結果のコピーを返し、success は判定です。
pub fn RunSummary::results(Self) -> Array[CaseResult]
pub fn RunSummary::success(Self) -> Bool
すべての行が実行されるため です。success はどの行も失敗しなかったとき真です。results は結果を行順に返します。
///|
test "mpfr summary" {
let rows =
#|53 53 n 0x0p0 0x0p0
#|53 53 u 0x3p0 0x1bb67ae8584cabp-52
#|
let summary = @mpfr_expr.execute_sqrt_data(
@mpfr_expr.parse_sqrt_data("s", rows).unwrap(),
)
inspect(summary.total_cases(), content="2")
inspect(summary.passed_cases(), content="2")
}
トレイト実装
ParseDiagnostic::equal, ParseDiagnostic::not_equal, ParseDiagnostic::to_repr
これらのメソッドは位置とメッセージを比較し、診断を Debug 用に描画します。新しいコードでは ==、!=、debug_inspect を使ってください。
pub fn ParseDiagnostic::equal(Self, Self) -> Bool
pub fn ParseDiagnostic::not_equal(Self, Self) -> Bool
pub fn ParseDiagnostic::to_repr(Self) -> @debug.Repr
公開インターフェース全体
このスナップショットは、パッケージの生成された pkg.generated.mbti です。説明文とインターフェースが食い違う場合は、こちらが正となります。
// Generated using `moon info`, DON'T EDIT IT
package "Luna-Flow/floating-tools/frontend/mpfr_expr"
import {
"moonbitlang/core/debug",
}
// Values
pub fn execute_decimal_output_data(MpfrDecimalOutputDocument) -> RunSummary
pub fn execute_decimal_random_data(MpfrDecimalRandomDocument) -> RunSummary
pub fn execute_elementary_data(MpfrElementaryDocument) -> RunSummary
pub fn execute_pow_data(MpfrPowDocument) -> RunSummary
pub fn execute_random_data(MpfrRandomDocument) -> RunSummary
pub fn execute_sqrt_data(MpfrDocument) -> RunSummary
pub fn execute_strtofr_data(MpfrStrtofrDocument) -> RunSummary
pub fn parse_decimal_output_data(String, String) -> Result[MpfrDecimalOutputDocument, Array[ParseDiagnostic]]
pub fn parse_decimal_random_data(String, String) -> Result[MpfrDecimalRandomDocument, Array[ParseDiagnostic]]
pub fn parse_elementary_data(String, String) -> Result[MpfrElementaryDocument, Array[ParseDiagnostic]]
pub fn parse_pow_data(String, String) -> Result[MpfrPowDocument, Array[ParseDiagnostic]]
pub fn parse_random_data(String, String) -> Result[MpfrRandomDocument, Array[ParseDiagnostic]]
pub fn parse_sqrt_data(String, String) -> Result[MpfrDocument, Array[ParseDiagnostic]]
pub fn parse_strtofr_data(String, String) -> Result[MpfrStrtofrDocument, Array[ParseDiagnostic]]
// Errors
// Types and methods
pub struct CaseResult {
// private fields
}
pub fn CaseResult::id(Self) -> String
pub fn CaseResult::message(Self) -> String
pub fn CaseResult::passed(Self) -> Bool
pub struct MpfrCase {
// private fields
}
pub struct MpfrDecimalOutputDocument {
// private fields
}
pub fn MpfrDecimalOutputDocument::case_count(Self) -> Int32
pub fn MpfrDecimalOutputDocument::source(Self) -> String
pub struct MpfrDecimalRandomDocument {
// private fields
}
pub fn MpfrDecimalRandomDocument::case_count(Self) -> Int32
pub fn MpfrDecimalRandomDocument::case_ids(Self) -> Array[String]
pub fn MpfrDecimalRandomDocument::source(Self) -> String
pub struct MpfrDocument {
// private fields
}
pub fn MpfrDocument::case_count(Self) -> Int32
pub fn MpfrDocument::source(Self) -> String
pub struct MpfrElementaryDocument {
// private fields
}
pub fn MpfrElementaryDocument::case_count(Self) -> Int32
pub fn MpfrElementaryDocument::source(Self) -> String
pub struct MpfrPowCase {
// private fields
}
pub struct MpfrPowDocument {
// private fields
}
pub fn MpfrPowDocument::case_count(Self) -> Int32
pub fn MpfrPowDocument::source(Self) -> String
pub struct MpfrRandomDocument {
// private fields
}
pub fn MpfrRandomDocument::case_count(Self) -> Int32
pub fn MpfrRandomDocument::case_ids(Self) -> Array[String]
pub fn MpfrRandomDocument::source(Self) -> String
pub struct MpfrStrtofrDocument {
// private fields
}
pub fn MpfrStrtofrDocument::case_count(Self) -> Int32
pub fn MpfrStrtofrDocument::source(Self) -> String
#derive(Eq, @debug.Debug)
pub struct ParseDiagnostic {
// private fields
}
pub fn ParseDiagnostic::equal(Self, Self) -> Bool
pub fn ParseDiagnostic::line(Self) -> Int32
pub fn ParseDiagnostic::message(Self) -> String
pub fn ParseDiagnostic::not_equal(Self, Self) -> Bool
pub fn ParseDiagnostic::source(Self) -> String
pub fn ParseDiagnostic::to_repr(Self) -> @debug.Repr
pub struct RunSummary {
// private fields
}
pub fn RunSummary::failed_cases(Self) -> Int32
pub fn RunSummary::passed_cases(Self) -> Int32
pub fn RunSummary::results(Self) -> Array[CaseResult]
pub fn RunSummary::success(Self) -> Bool
pub fn RunSummary::total_cases(Self) -> Int32
// Type aliases
// Traits