frontend/testfloat_expr API

Purpose

frontend/testfloat_expr parses Berkeley TestFloat test vectors and executes them against binary in the binary16, binary32, binary64 and binary128 interchange formats. A TestFloatSpec describes the function, rounding mode, tininess rule and exactness that produced a vector file; parse_testfloat reads the vectors and execute_document runs them. The package does no IO; the runner is cli/testfloat_expr_cli. The tutorial shows the workflow and the design page specifies the pass rule.

Importing

Add the package to the import block of your moon.pkg; import binary as well when you compare the format or rounding mode of a specification:

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

The examples use the aliases @testfloat_expr and @binary.

Function specification

TestFloatOperation

TestFloatOperation lists the TestFloat functions the executor supports.

pub(all) enum TestFloatOperation {
  Add
  Subtract
  Multiply
  Divide
  SquareRoot
  MulAdd
  Remainder
  RoundToInt
  ToInt32
  ToInt64
  ToUInt32
  ToUInt64
  Equal
  LessEqual
  Less
  EqualSignaling
  LessEqualQuiet
  LessQuiet
} derive(Eq, @debug.Debug)
ConstructorTestFloat nameOperandsbinary method
Add, Subtract, Multiply, Divideadd, sub, mul, div2add_ctx, sub_ctx, mul_ctx, div_ctx
SquareRootsqrt1sqrt_ctx
MulAddmulAdd3fma_ctx
Remainderrem2remainder_ctx
RoundToIntroundToInt1to_integral_exact_ctx if exact, else to_integral_value_ctx
ToInt32, ToInt64, ToUInt32, ToUInt64to_i32, to_i64, to_ui32, to_ui641to_int_ctx, to_int64_ctx, to_uint_ctx, to_uint64_ctx with exact~
ConvertFormat(format)to_f16, to_f32, to_f64, to_f1281convert_format (operand in the format of the function name, result in format)
Equal, LessEqual, Lesseq, le, lt2compare_quiet_equal, compare_signaling_less_equal, compare_signaling_less
EqualSignaling, LessEqualQuiet, LessQuieteq_signaling, le_quiet, lt_quiet2compare_signaling_equal, compare_quiet_less_equal, compare_quiet_less

TestFloatSpec

TestFloatSpec is the configuration of one TestFloat run.

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

TestFloatSpec::parse

TestFloatSpec::parse(function_name, rounding, tininess?, exact?) builds a specification from TestFloat names.

pub fn TestFloatSpec::parse(String, String, tininess? : String, exact? : Bool) -> Result[Self, String]

function_name is FORMAT_OPERATION: the part before the first _ is the format (f16, f32, f64, f128) and the rest is the operation (add, mulAdd, to_ui64, to_f32, le_quiet, …). Names are matched case-insensitively with -, _ and spaces removed. TestFloat functions outside the supported operations, such as i32_to_f64 or f64_to_i32_r_minMag, are rejected; f64_to_f32 is the conversion of a binary64 operand to binary32 (the first column is read in the format of the name’s prefix, the result column in the destination format). rounding is one of rnear_even, rnear_maxMag, rminMag, rmin, rmax (also accepted without the leading r, or as the IEEE names roundTiesToEven, roundTiesToAway, roundTowardZero, roundTowardNegative, roundTowardPositive). tininess is "after" (default) or "before" (also tininessAfter, tininessBefore). exact (default false) selects TestFloat’s -exact variants of roundToInt and the integer conversions. Errors are "unsupported TestFloat function name …", "… format …", "… operation …", "… rounding mode …" and "… tininess mode …".

///|
test "spec parse" {
  let spec = @testfloat_expr.TestFloatSpec::parse(
    "f128_mulAdd",
    "rminMag",
    tininess="before",
  ).unwrap()
  inspect(spec.operation() == @testfloat_expr.MulAdd, content="true")
  inspect(spec.format() == @binary.Binary128, content="true")
  inspect(spec.rounding() == @binary.RoundTowardZero, content="true")
  inspect(
    @testfloat_expr.TestFloatSpec::parse("f64_mul", "rodd") is Err(_),
    content="true",
  )
}

TestFloatSpec::function_name, TestFloatSpec::format, TestFloatSpec::operation, TestFloatSpec::rounding, TestFloatSpec::tininess, TestFloatSpec::exact

These accessors return the parsed configuration.

pub fn TestFloatSpec::function_name(Self) -> String
pub fn TestFloatSpec::format(Self) -> @binary.BinaryInterchangeFormat
pub fn TestFloatSpec::operation(Self) -> TestFloatOperation
pub fn TestFloatSpec::rounding(Self) -> @binary.BinaryRoundingMode
pub fn TestFloatSpec::tininess(Self) -> @binary.TininessDetection
pub fn TestFloatSpec::exact(Self) -> Bool

function_name is the name as given. exact is stored for every operation but only changes the result of roundToInt and the four integer conversions.

Parsing

parse_testfloat

parse_testfloat(source, text, spec) parses the vectors of one function.

pub fn parse_testfloat(String, String, TestFloatSpec) -> Result[TestFloatDocument, Array[ParseDiagnostic]]

Lines are trimmed; empty lines and lines starting with # are skipped. A vector has k+2k + 2 white-space separated fields, where kk is the operand count of the operation: kk operand encodings, the expected result and a two-digit hexadecimal flag mask. Operand encodings are hexadecimal bit patterns of spec.format(). The expected result is an encoding for arithmetic operations, a hexadecimal integer of at most 16 digits for conversions, and 0 or 1 for comparisons. A wrong field count gives "unexpected TestFloat field count", an unreadable field "invalid TestFloat hexadecimal field". The result is Ok only when no line is invalid.

TestFloatDocument

TestFloatDocument is a parsed vector file.

pub struct TestFloatDocument {
  // private fields
}

TestFloatDocument::source, TestFloatDocument::spec, TestFloatDocument::cases, TestFloatDocument::case_count

These methods return the name given to the parser, the specification, a copy of the vectors in file order, and their number.

pub fn TestFloatDocument::source(Self) -> String
pub fn TestFloatDocument::spec(Self) -> TestFloatSpec
pub fn TestFloatDocument::cases(Self) -> Array[TestFloatCase]
pub fn TestFloatDocument::case_count(Self) -> Int

TestFloatCase

TestFloatCase is one vector.

pub struct TestFloatCase {
  // private fields
}

TestFloatCase::id, TestFloatCase::line

These methods return the id FUNCTION:LINE, for example f64_mulAdd:17, and the 1-based line number of the vector in the parsed text.

pub fn TestFloatCase::id(Self) -> String
pub fn TestFloatCase::line(Self) -> Int
///|
test "vector ids" {
  let spec = @testfloat_expr.TestFloatSpec::parse("f16_mul", "rnear_even").unwrap()
  let text =
    #|# a comment line
    #|3C00 4000 4000 00
    #|
  let document = @testfloat_expr.parse_testfloat("mul.tv", text, spec).unwrap()
  inspect(document.case_count(), content="1")
  inspect(document.cases()[0].id(), content="f16_mul:2")
  inspect(document.spec().function_name(), content="f16_mul")
}

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

Execution

execute_document

execute_document(document, options?) executes the selected vectors.

pub fn execute_document(TestFloatDocument, options? : RunOptions) -> RunSummary

Vector kk (counting from 0 in file order) is executed when k mod n=ik \bmod n = i for shard count nn and index ii. Operands are decoded to Binary and the operation runs in spec.format().context(rounding~, tininess~). The vector passes when:

  • arithmetic: the result, re-encoded in the format with the same rounding and tininess, has exactly the expected bit pattern, or the expected result is any NaN and the actual result is a quiet NaN; and the SoftFloat mask of the operation’s flags combined with the encoding flags equals the expected mask;
  • integer conversion: if the expected mask has the invalid bit 10, the conversion must report invalid (None) and its value field is ignored; otherwise the conversion must return the expected integer (signed results compared as two’s-complement bit patterns); in both cases the flag masks must be equal;
  • comparison: the boolean and the flag mask are equal.

Every selected vector is executable; there are no skipped vectors.

RunOptions

RunOptions selects a shard.

pub struct RunOptions {
  // private fields
}

RunOptions::new

RunOptions::new(shard_count?, shard_index?) builds the options.

pub fn RunOptions::new(shard_count? : Int, shard_index? : Int) -> Self

Defaults: one shard, index 0. RunOptions::new aborts unless shard_count > 0 and 0 <= shard_index < shard_count.

RunOptions::shard_count, RunOptions::shard_index

These accessors return the shard count and index.

pub fn RunOptions::shard_count(Self) -> Int
pub fn RunOptions::shard_index(Self) -> Int

Results

CaseResult

CaseResult is the outcome of one vector.

pub struct CaseResult {
  // private fields
}

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

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

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

Messages: empty for a pass; "value mismatch: expected HEX, actual HEX", "integer mismatch: …", "comparison mismatch: …", or "flags mismatch: expected M, actual M" (masks in decimal).

RunSummary

RunSummary aggregates a run.

pub struct RunSummary {
  // private fields
}

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

These methods return the number of vectors in the document, the number this shard executed, and how many of those passed and failed.

pub fn RunSummary::total_cases(Self) -> Int
pub fn RunSummary::selected_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 results in vector order; success is the verdict.

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

total_cases is the number of vectors in the document, selected_cases = passed_cases + failed_cases the number executed by this shard, and success is true when none failed.

RunSummary::arms

arms returns the names of the operations that ran, in the order they first ran. Each operation of the TestFloat frontend is a function that records its own name when it executes; the command-line summary prints the set as arms. The verification tools key a task by the code those operations reach.

pub fn RunSummary::arms(Self) -> Array[String]

Trait implementations

These methods come from derive(Eq, @debug.Debug). Use ==, != and debug_inspect in new code.

TestFloatOperation::equal, TestFloatOperation::not_equal, TestFloatOperation::to_repr

equal compares constructors; to_repr renders the constructor name.

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

TestFloatSpec::equal, TestFloatSpec::not_equal, TestFloatSpec::to_repr

equal compares all six fields, including function_name as written, so f64_mul and F64_MUL give unequal specifications with the same behaviour.

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

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

equal compares location and message; to_repr renders both.

pub fn ParseDiagnostic::equal(Self, Self) -> Bool
pub fn ParseDiagnostic::not_equal(Self, Self) -> Bool
pub fn ParseDiagnostic::to_repr(Self) -> @debug.Repr
///|
test "spec equality" {
  let a = @testfloat_expr.TestFloatSpec::parse("f64_mul", "rnear_even").unwrap()
  let b = @testfloat_expr.TestFloatSpec::parse("F64_MUL", "rnear_even").unwrap()
  inspect(a.operation() == b.operation(), content="true")
  inspect(a == b, content="false")
}

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

import {
  "Luna-Flow/floating/binary",
  "moonbitlang/core/debug",
}

// Values
pub fn execute_document(TestFloatDocument, options? : RunOptions) -> RunSummary

pub fn parse_testfloat(String, String, TestFloatSpec) -> Result[TestFloatDocument, 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

#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 RunOptions {
  // private fields
}
pub fn RunOptions::new(shard_count? : Int32, shard_index? : Int32) -> Self
pub fn RunOptions::shard_count(Self) -> Int32
pub fn RunOptions::shard_index(Self) -> Int32

pub struct RunSummary {
  // private fields
}
pub fn RunSummary::arms(Self) -> Array[String]
pub fn RunSummary::failed_cases(Self) -> Int32
pub fn RunSummary::passed_cases(Self) -> Int32
pub fn RunSummary::results(Self) -> Array[CaseResult]
pub fn RunSummary::selected_cases(Self) -> Int32
pub fn RunSummary::success(Self) -> Bool
pub fn RunSummary::total_cases(Self) -> Int32

pub struct TestFloatCase {
  // private fields
}
pub fn TestFloatCase::id(Self) -> String
pub fn TestFloatCase::line(Self) -> Int32

pub struct TestFloatDocument {
  // private fields
}
pub fn TestFloatDocument::case_count(Self) -> Int32
pub fn TestFloatDocument::cases(Self) -> Array[TestFloatCase]
pub fn TestFloatDocument::source(Self) -> String
pub fn TestFloatDocument::spec(Self) -> TestFloatSpec

#derive(Eq, @debug.Debug)
pub(all) enum TestFloatOperation {
  Add
  Subtract
  Multiply
  Divide
  SquareRoot
  MulAdd
  Remainder
  RoundToInt
  ToInt32
  ToInt64
  ToUInt32
  ToUInt64
  ConvertFormat(@binary.BinaryInterchangeFormat)
  Equal
  LessEqual
  Less
  EqualSignaling
  LessEqualQuiet
  LessQuiet
}
pub fn TestFloatOperation::equal(Self, Self) -> Bool
pub fn TestFloatOperation::not_equal(Self, Self) -> Bool
pub fn TestFloatOperation::to_repr(Self) -> @debug.Repr

#derive(Eq, @debug.Debug)
pub struct TestFloatSpec {
  // private fields
}
pub fn TestFloatSpec::equal(Self, Self) -> Bool
pub fn TestFloatSpec::exact(Self) -> Bool
pub fn TestFloatSpec::format(Self) -> @binary.BinaryInterchangeFormat
pub fn TestFloatSpec::function_name(Self) -> String
pub fn TestFloatSpec::not_equal(Self, Self) -> Bool
pub fn TestFloatSpec::operation(Self) -> TestFloatOperation
pub fn TestFloatSpec::parse(String, String, tininess? : String, exact? : Bool) -> Result[Self, String]
pub fn TestFloatSpec::rounding(Self) -> @binary.BinaryRoundingMode
pub fn TestFloatSpec::tininess(Self) -> @binary.TininessDetection
pub fn TestFloatSpec::to_repr(Self) -> @debug.Repr

// Type aliases

// Traits