frontend/mpfr_expr API
用途
frontend/mpfr_expr 解析由 GNU MPFR 生成的参考数据,并对照 binary 执行。支持六种格式:MPFR 的平方根 data_check 文件、整数幂行、带异常标志的初等函数矩阵、两个十进制字符转换语料(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。
通用行语法
五个解析器都在 \n 处切分文本,修剪每一行,并跳过空行和以 # 开头的行。字段以空格、制表符或回车符分隔。行号从 1 开始。舍入字段为以下之一
| 字段 | BinaryRoundingMode |
|---|---|
n | RoundTiesToEven |
na | RoundTiesToAway |
z | RoundTowardZero |
u | RoundTowardPositive |
d | RoundTowardNegative |
a | RoundAwayFromZero |
数值由 @binary.Binary::from_hex 以该字段所指定的精度读取(0x…p…,其有效数为不带小数点的十六进制整数;以及 inf、-inf、nan);位数多于该精度的有效数按就近舍入(偶数优先)舍入。每个解析器在所有行都有效时返回 Ok(document),否则返回 Err,其中每个无效行对应一个 ParseDiagnostic。
平方根数据
parse_sqrt_data
parse_sqrt_data(source, text) 解析由五个字段组成的行: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 是其中的一行,没有公开方法。
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) 解析由十个字段组成的行。
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,其中两个数均为 ,各符号位和 inexact 为 0 或 1,exponent 是整数幂指数。输入以 512 位读取,期望值以 precision 位读取。行 id 为 pow:LINE。系数字段是不带前缀的十六进制数字;解析器由它们构造 0xCOEFFpEXP。
execute_pow_data
execute_pow_data(document) 计算 input.pown_ctx(exponent, ctx),其中 ctx = BinaryContext::new(precision=precision, rounding~)。
pub fn execute_pow_data(MpfrPowDocument) -> RunSummary
当结果与期望值相等(==)、不精确标志等于该行的标志,并且没有引发下溢、上溢、除以零或无效标志时,该行通过。
MpfrPowDocument, MpfrPowCase
MpfrPowDocument 是一个已解析的幂文件;MpfrPowCase 是其中的一行,没有公开方法。
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) 解析由十个字段组成的行: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。
双操作数函数 pow、powr、hypot、atan2 和 atan2pi 中 y 为 - 的行是诊断 "invalid MPFR elementary field"。
execute_elementary_data
execute_elementary_data(document) 在 BinaryContext::new(precision=precision, rounding~) 中,用 Binary 对应其运算的 checked 方法 try_*_ctx(对 sqrt 和 pown 则为 sqrt_ctx 和 pown_ctx)对每一行求值。
pub fn execute_decimal_random_data(MpfrDecimalRandomDocument) -> RunSummary
pub fn execute_elementary_data(MpfrElementaryDocument) -> RunSummary
当结果与期望值比较相等(compare == 0,因此任意 NaN 都与任意 NaN 相等)且结果为零时符号相同,不精确、无效和除以零标志等于该行的标志,并且既没有引发下溢也没有引发上溢时,该行通过。若某个 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) 解析含七个字段的行:precision rounding input expected inexact overflow underflow。
pub fn parse_strtofr_data(String, String) -> Result[MpfrStrtofrDocument, Array[ParseDiagnostic]]
input 是按文本保留的十进制字面量,expected 是以 precision 位读取的 mpfr_strtofr 结果,最后三个字段为 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 或除零时,该行通过。被解析器拒绝的字面量会使该行失败。
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) 解析两种行。
pub fn parse_decimal_output_data(String, String) -> Result[MpfrDecimalOutputDocument, Array[ParseDiagnostic]]
fixed digits precision rounding value expected_digits expected_exponent10 inexact(八个字段)是以 digits 位有效数字打印的值:expected_digits 是数字串,expected_exponent10 是其首位数字的十进制指数。shortest precision value expected_digit_count(四个字段)是最短往返形式的有效数字个数。值以 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
当打印的数字、指数和符号都匹配,且 inexact 标志等于该行的标志(无上溢或下溢)时,fixed 行通过。当文本恰有 expected_digit_count 位有效数字(不计前导零和位置记法的尾随零),且 Binary::from_string(text, precision~) 返回相同的值时,shortest 行通过。
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:每个用例一行,精度最高 10,000,有六种舍入方向、可选的带 tininess 规则的指数范围、最多三个精确操作数,以及带标志的 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]]
解析文本(第一个参数为来源名)。空行和以 # 开头的行被跳过。其余每行必须有十三个字段、一个已知运算(add sub mul div sqrt fma strtofr getstr shortest 或某个初等函数名)、取值为 n na z u d a 的舍入、正的精度、要么是 - - - 要么是两个整数加 a/b 的指数范围、精确操作数([-]0x<hex>p<exp>、inf、-inf、nan、-),以及等于 - 或 xouvz 子集的 flags。操作数和结果至少按该行的精度读取,因为以较低精度读取低于其最小次正规数的值会把它清零。每个错误行产生一条诊断。
execute_random_data
pub fn execute_random_data(MpfrRandomDocument) -> RunSummary
用该行的 BinaryContext 运行每一行(有指数范围时为有界,否则为 unbounded),只有当值和标志都一致时该行才通过。比较值时区分零和无穷的符号;NaN 结果必须是安静 NaN。初等函数的定义域错误(ArithmeticError::is_domain_error)视为无效操作:带标志 v 的安静 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),带十进制上下文(IEEE 格式的精度和指数范围,IEEE 的五种舍入方向之一: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]]
解析文本(第一个参数是来源名)。空行和以 # 开头的行被跳过。其余每一行必须有十个字段:标识符、已知的运算、half_even half_up down ceiling floor 之一的舍入(IEEE 754-2019 4.3 的五种舍入方向属性)、正的精度、构成合法 DecimalContext 的整数 emax(精度与 emax 所确定的 IEEE 格式:emin = 1 - emax,舍入前检测微小性)、一个操作数、第二个操作数或 -(pow、hypot 与 atan2 必需)、一个整数(rootn 与 pown 使用,否则为 0)、期望值,以及等于 - 或 xouvz 的子集(inexact、overflow、underflow、invalid operation、division by zero,按此顺序)的 flags。操作数和期望值中写出的每一位数字都会保留。每个错误行产生一条诊断。
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 语料(二进制运算对照 MPFR),提供同样的三个访问器。
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 是一个无效行。
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 是一行的结果。
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
通过时消息为空,否则以十六进制显示期望值和实际值(对于幂行和初等函数行,还显示所比较的标志)。
RunSummary
RunSummary 汇总一个文档中的各行。
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")
}
trait 实现
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