frontend/mpfr_expr API

Purpose

frontend/mpfr_expr parses reference data produced with GNU MPFR and executes it against binary. Six formats are supported: MPFR’s square-root data_check files, integer-power rows, an elementary-function matrix with exception flags, two corpora for decimal character conversion (mpfr_strtofr and mpfr_get_str), and the seeded random differential rows (mpfr-random-v1) and their decimal counterpart (mpfr-decimal-random-v2). The package does no IO; the runner is cli/mpfr_expr_cli. See the tutorial for the workflow and the design page for the pass rules.

Importing

Add the package to the import block of your moon.pkg:

import {
  "Luna-Flow/floating-tools/frontend/mpfr_expr",
}

The examples call it through the alias @mpfr_expr; rows are written as text, so binary does not need to be imported.

Common row syntax

All five parsers split the text at \n, trim each line, and skip empty lines and lines starting with #. Fields are separated by spaces, tabs or carriage returns. Line numbers start at 1. A rounding field is one of

FieldBinaryRoundingMode
nRoundTiesToEven
naRoundTiesToAway
zRoundTowardZero
uRoundTowardPositive
dRoundTowardNegative
aRoundAwayFromZero

and a number is read by @binary.Binary::from_hex (0x…p… with an integer hexadecimal significand and no point, inf, -inf, nan) at the precision named for that field; a significand with more bits than that precision is rounded to nearest-even. Each parser returns Ok(document) when every line is valid and otherwise Err with one ParseDiagnostic per invalid line.

Square-root data

parse_sqrt_data

parse_sqrt_data(source, text) parses rows of five fields: input_precision output_precision rounding input expected.

pub fn parse_sqrt_data(String, String) -> Result[MpfrDocument, Array[ParseDiagnostic]]

The input is read at input_precision bits and the expected value at output_precision bits. Row ids are sqrt:LINE.

execute_sqrt_data

execute_sqrt_data(document) computes input.sqrt_ctx(ctx) for every row, where ctx is BinaryContext::new(precision=output_precision, rounding~).

pub fn execute_sqrt_data(MpfrDocument) -> RunSummary

A row passes when the result is equal (==) to the expected Binary. Flags are not compared.

MpfrDocument, MpfrCase

MpfrDocument is a parsed square-root file; MpfrCase is one of its rows and has no public methods.

pub struct MpfrDocument {
  // private fields
}

pub struct MpfrCase {
  // private fields
}

MpfrDocument::source, MpfrDocument::case_count

source returns the name given to the parser and case_count the number of rows.

pub fn MpfrDocument::source(Self) -> String
pub fn MpfrDocument::case_count(Self) -> Int

Integer powers

parse_pow_data

parse_pow_data(source, text) parses rows of ten fields.

pub fn parse_pow_data(String, String) -> Result[MpfrPowDocument, Array[ParseDiagnostic]]

The fields are precision rounding input_coefficient_hex input_exponent2 input_negative exponent expected_coefficient_hex expected_exponent2 expected_negative inexact, where the two numbers are (−1)negative⋅coefficient⋅2exponent2(-1)^{\text{negative}} \cdot \text{coefficient} \cdot 2^{\text{exponent2}}, the signs and inexact are 0 or 1, and exponent is the integer power. The input is read at 512 bits and the expected value at precision bits. Row ids are pow:LINE. The coefficient fields are bare hexadecimal digits; the parser builds 0xCOEFFpEXP from them.

execute_pow_data

execute_pow_data(document) computes input.pown_ctx(exponent, ctx) with ctx = BinaryContext::new(precision=precision, rounding~).

pub fn execute_pow_data(MpfrPowDocument) -> RunSummary

A row passes when the result is equal (==) to the expected value, the inexact flag equals the row’s flag, and no underflow, overflow, division by zero or invalid flag is raised.

MpfrPowDocument, MpfrPowCase

MpfrPowDocument is a parsed power file; MpfrPowCase is one row and has no public methods.

pub struct MpfrPowDocument {
  // private fields
}

pub struct MpfrPowCase {
  // private fields
}

MpfrPowDocument::source, MpfrPowDocument::case_count

These methods return the name given to the parser and the number of rows.

pub fn MpfrPowDocument::source(Self) -> String
pub fn MpfrPowDocument::case_count(Self) -> Int

Elementary functions

parse_elementary_data

parse_elementary_data(source, text) parses rows of ten fields: 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 is one of 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 and y are operands read at 512 bits (y is - when unused), n is the integer argument of rootn, pown and compound, expected is read at precision bits, and the last three fields are 0 or 1. Row ids are op:LINE.

A row of the two-operand functions pow, powr, hypot, atan2 and atan2pi whose y is - is the diagnostic "invalid MPFR elementary field".

execute_elementary_data

execute_elementary_data(document) evaluates every row with the checked try_*_ctx method of Binary for its operation (sqrt_ctx and pown_ctx for sqrt and pown) in BinaryContext::new(precision=precision, rounding~).

pub fn execute_decimal_random_data(MpfrDecimalRandomDocument) -> RunSummary

pub fn execute_elementary_data(MpfrElementaryDocument) -> RunSummary

A row passes when the result compares equal to the expected value (compare == 0, so every NaN equals every NaN) with the same sign when it is zero, the inexact, invalid and division-by-zero flags equal the row’s flags, and neither underflow nor overflow is raised. If a try_*_ctx method returns Err (for example a certification failure) the row fails with the message "certification failure for MPFR differential case".

MpfrElementaryDocument

MpfrElementaryDocument is a parsed elementary file.

pub struct MpfrElementaryDocument {
  // private fields
}

MpfrElementaryDocument::source, MpfrElementaryDocument::case_count

These methods return the name given to the parser and the number of rows.

pub fn MpfrElementaryDocument::source(Self) -> String
pub fn MpfrElementaryDocument::case_count(Self) -> Int

Decimal character conversion

parse_strtofr_data

parse_strtofr_data(source, text) parses rows of seven fields: precision rounding input expected inexact overflow underflow.

pub fn parse_strtofr_data(String, String) -> Result[MpfrStrtofrDocument, Array[ParseDiagnostic]]

input is a decimal literal kept as text, expected is the mpfr_strtofr result read at precision bits, and the last three fields are 0 or 1. Row ids are strtofr:LINE.

execute_strtofr_data

execute_strtofr_data(document) evaluates Binary::from_string_ctx(input, ctx) with ctx = BinaryContext::new(precision=precision, rounding~).

pub fn execute_strtofr_data(MpfrStrtofrDocument) -> RunSummary

A row passes when the result compares equal to the expected value, with the same sign (including zero) and class, the inexact, overflow and underflow flags equal the row’s flags, and neither invalid nor division by zero is raised. A literal rejected by the parser fails the row.

MpfrStrtofrDocument, MpfrStrtofrDocument::source, MpfrStrtofrDocument::case_count

MpfrStrtofrDocument is a parsed file; the methods return the name given to the parser and the number of rows.

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) parses two row kinds.

pub fn parse_decimal_output_data(String, String) -> Result[MpfrDecimalOutputDocument, Array[ParseDiagnostic]]

fixed digits precision rounding value expected_digits expected_exponent10 inexact (eight fields) is a value printed with digits significant digits: expected_digits is the digit string and expected_exponent10 the decimal exponent of its first digit. shortest precision value expected_digit_count (four fields) is the number of significant digits of the shortest round-trip form. Values are read at precision bits. Row ids are fixed:LINE and shortest:LINE.

execute_decimal_output_data

execute_decimal_output_data(document) runs Binary::to_decimal_string_ctx for a fixed row and Binary::to_shortest_string for a shortest row.

pub fn execute_decimal_output_data(MpfrDecimalOutputDocument) -> RunSummary

A fixed row passes when the printed digits, the exponent and the sign match and the inexact flag equals the row’s flag (no overflow or underflow). A shortest row passes when the text has exactly expected_digit_count significant digits (leading zeros and the trailing zeros of positional notation are not counted) and Binary::from_string(text, precision~) returns the same value.

MpfrDecimalOutputDocument, MpfrDecimalOutputDocument::source, MpfrDecimalOutputDocument::case_count

pub struct MpfrDecimalOutputDocument {
  // private fields
}

pub fn MpfrDecimalOutputDocument::source(Self) -> String
pub fn MpfrDecimalOutputDocument::case_count(Self) -> Int

Seeded random rows

mpfr-random-v1 rows come from tools/generate_mpfr_random_oracle.c: one row per case, with precisions up to 10,000, six rounding directions, an optional exponent range with its tininess rule, up to three exact operands and the MPFR result with its flags. The full field list is in 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]]

Parses the text (the first argument names the source). Blank lines and lines starting with # are skipped. Every other line must have thirteen fields, a known operation (add sub mul div sqrt fma strtofr getstr shortest or one of the elementary names), a rounding of n na z u d a, a positive precision, an exponent range that is either - - - or two integers and a/b, exact operands ([-]0x<hex>p<exp>, inf, -inf, nan, -) and flags equal to - or a subset of xouvz. Operands and results are read at least at the row’s precision, because reading a value below the smallest subnormal of a lower precision would flush it to zero. Each bad row is one diagnostic.

execute_random_data

pub fn execute_random_data(MpfrRandomDocument) -> RunSummary

Runs every row with the row’s BinaryContext (bounded when the row has an exponent range, unbounded otherwise) and passes a row only when the value and the flags agree. Values are compared with their sign for zeros and infinities; a NaN result must be a quiet NaN. A domain error of the elementary functions (ArithmeticError::is_domain_error) counts as the invalid operation: a quiet NaN with flag v; any other error fails the row with the error message. The row id is the first field of the row.

Seeded random decimal rows

mpfr-decimal-random-v2 rows come from tools/generate_mpfr_decimal_random.py: one row per case for the IEEE decimal elementary functions (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), with a decimal context (precision and exponent range of an IEEE format, one of the five IEEE rounding directions: half_even, half_up, down, ceiling, floor), exact decimal operands and the expected value with its flags. The expected value is the rounding of a rigorous 768-bit MPFR enclosure, never one MPFR result, and a row exists only when both enclosure endpoints round alike and the rounded value lies outside the enclosure (the true value is not representable), so exact results are not sampled by this corpus.

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]]

Parses the text (the first argument names the source). Blank lines and lines starting with # are skipped. Every other line must have ten fields: an identifier, a known operation, a rounding of half_even half_up down ceiling floor (the five rounding-direction attributes of IEEE 754-2019 4.3), a positive precision, an integer emax that forms a valid DecimalContext (the IEEE format of the precision and emax: emin = 1 - emax, tininess before rounding), one operand, a second operand or - (required by pow, hypot and atan2), an integer (rootn and pown; 0 otherwise), the expected value and flags equal to - or a subset of xouvz (inexact, overflow, underflow, invalid operation, division by zero) in that order. Every digit written in an operand or the expected value is kept. Each bad row is one diagnostic.

execute_decimal_random_data

pub fn execute_decimal_random_data(MpfrDecimalRandomDocument) -> RunSummary

Runs every row through the try_*_ctx form of the operation with the row’s context, so a certification failure fails the row instead of becoming a quiet NaN. A row passes only when the result has the expected value and cohort (total_order_compare equal, so 1.0 and 1.00 differ) and the flags agree. The row id is the first field of the row.

MpfrDecimalRandomDocument, MpfrDecimalRandomDocument::source, MpfrDecimalRandomDocument::case_count, MpfrDecimalRandomDocument::case_ids

MpfrDecimalRandomDocument is a parsed mpfr-decimal-random-v2 corpus (IEEE decimal elementary functions against MPFR); source is the label it was parsed from, case_count the number of rows and case_ids their ids in order.

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 is a parsed mpfr-random-v1 corpus (binary operations against MPFR) with the same three accessors.

pub fn MpfrRandomDocument::source(Self) -> String
pub fn MpfrRandomDocument::case_count(Self) -> Int32
pub fn MpfrRandomDocument::case_ids(Self) -> Array[String]

case_ids lists the row ids in order, which the differential harness uses to name the failing case of a batch.

Diagnostics and results

ParseDiagnostic

ParseDiagnostic is one invalid line.

pub struct ParseDiagnostic {
  // private fields
} derive(Eq, @debug.Debug)

ParseDiagnostic::source, ParseDiagnostic::line, ParseDiagnostic::message

These methods return the name given to the parser, the 1-based line number and the message.

pub fn ParseDiagnostic::source(Self) -> String
pub fn ParseDiagnostic::line(Self) -> Int
pub fn ParseDiagnostic::message(Self) -> String

Messages name the problem, for example "MPFR pow row must contain ten fields", "invalid MPFR pow field" or "invalid MPFR elementary expected value".

CaseResult

CaseResult is the outcome of one row.

pub struct CaseResult {
  // private fields
}

CaseResult::id, CaseResult::passed, CaseResult::message

These methods return the row id, whether the row passed, and a message.

pub fn CaseResult::id(Self) -> String
pub fn CaseResult::passed(Self) -> Bool
pub fn CaseResult::message(Self) -> String

The message is empty for a pass and otherwise shows expected and actual values in hexadecimal (and the compared flags for power and elementary rows).

RunSummary

RunSummary aggregates the rows of one document.

pub struct RunSummary {
  // private fields
}

RunSummary::total_cases, RunSummary::passed_cases, RunSummary::failed_cases

These methods return the number of rows, and how many passed and failed.

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 returns a copy of the per-row results; success is the verdict.

pub fn RunSummary::results(Self) -> Array[CaseResult]
pub fn RunSummary::success(Self) -> Bool

Every row is executed, so total=passed+failed\text{total} = \text{passed} + \text{failed}; success is true when no row failed. results returns the results in row order.

///|
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 implementations

ParseDiagnostic::equal, ParseDiagnostic::not_equal, ParseDiagnostic::to_repr

These methods compare location and message and render a diagnostic for Debug. Use ==, != and debug_inspect in new code.

pub fn ParseDiagnostic::equal(Self, Self) -> Bool
pub fn ParseDiagnostic::not_equal(Self, Self) -> Bool
pub fn ParseDiagnostic::to_repr(Self) -> @debug.Repr

Complete public interface

This snapshot is the generated pkg.generated.mbti of the package. It is the authority when prose and interface disagree.

// 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