Parma Polyhedra Library requirements
C++ numerical result and rounding protocols

C++ numerical result and rounding protocols

UID:
VERSION:
CLASSIFICATION:
PREFIX:
Type
Level
MID
UID
REFS
Title
Statement
Rationale
Comment
BIBLIOGRAPHY
STATUS
COMPONENT
APPLICABILITY
VERIFICATION_METHODS
VERIFICATION_CRITERIA
SOURCE
LANGUAGE
VISIBILITY
INTERFACE
SW_SPEC
1
PPL-SPEC-000121

Result shall encode a computed-value class, a relation between an exact result and its computed value where comparable, and applicable outcome flags or diagnostic detail. VC_NORMAL, VC_MINUS_INFINITY, VC_PLUS_INFINITY and VC_NAN shall denote the normal-number, negative-infinity, positive-infinity and not-a-number classes.

For an arithmetic or conversion result, VR_EQ shall mean that exact and computed values are equal; VR_LT shall mean that the exact value is less than the computed value, and VR_GT shall mean that it is greater. VR_LE, VR_GE and VR_NE shall admit the union of their named relations. VR_LGE shall admit all three ordering relations and VR_EMPTY shall admit none. A relation containing equality and a strict relation shall not promise exactness. The class shall be interpreted together with the relation and any diagnostic; a normal class alone shall not promise an exact successful operation.

The corresponding V_EQ, V_LT, V_GT, V_LE, V_GE, V_NE, V_LGE and V_EMPTY shall have those normal-class relation encodings. V_EQ_MINUS_INFINITY and V_EQ_PLUS_INFINITY shall encode exact infinities. V_GT_MINUS_INFINITY and V_LT_PLUS_INFINITY shall encode their named strict relations with an infinite computed value. V_LT_INF and V_GT_SUP shall encode negative and positive bounded range overflow with a normal computed-value class and the indicated relation. V_NAN and each named invalid-operation diagnostic shall have NaN class. V_UNKNOWN_NEG_OVERFLOW and V_UNKNOWN_POS_OVERFLOW shall denote an unknown result following the named sign of intermediate overflow; they shall not describe an exact or ordinary inexact success.

The V_UNREPRESENTABLE flag shall indicate that the computed value cannot be represented in the selected destination storage. Absence of that flag shall not by itself promise finite value, exactness or success. These enum values shall be available without library initialization and shall not depend on a coefficient profile.

A result code carries several independent aspects of numerical meaning. Relation direction is from the exact result to the computed value, which determines whether a strict result was rounded upward or downward.

APPROVED
NUMERICS
IF_CPP
Test, Analysis, Inspection

Check all named classes, relation unions, normal and special result constants, disjoint masks and diagnostic uniqueness. Use a literal expected classification table, including exact infinity and intermediate-overflow diagnostics. Do not equate missing unrepresentability flags with successful finite calculation.

src/Result_defs.hh; tests/numericprotocols1.cc.

CPP
Public

Public header: <ppl.hh>; types and representative named constants:

enum Result_Class {
  VC_NORMAL = 0U << 4, VC_MINUS_INFINITY = 1U << 4,
  VC_PLUS_INFINITY = 2U << 4, VC_NAN = 3U << 4, VC_MASK = VC_NAN
};
enum Result_Relation {
  VR_EMPTY = 0U, VR_EQ = 1U, VR_LT = 2U, VR_GT = 4U,
  VR_NE = VR_LT | VR_GT, VR_LE = VR_EQ | VR_LT,
  VR_GE = VR_EQ | VR_GT, VR_LGE = VR_LT | VR_EQ | VR_GT,
  VR_MASK = VR_LGE
};
enum Result {
  V_EMPTY = VR_EMPTY, V_EQ = VR_EQ, V_LT = VR_LT, V_GT = VR_GT,
  V_NE = VR_NE, V_LE = VR_LE, V_GE = VR_GE, V_LGE = VR_LGE,
  V_OVERFLOW = 1U << 6, V_LT_INF = V_LT | V_OVERFLOW,
  V_GT_SUP = V_GT | V_OVERFLOW,
  V_LT_PLUS_INFINITY = V_LT | VC_PLUS_INFINITY,
  V_GT_MINUS_INFINITY = V_GT | VC_MINUS_INFINITY,
  V_EQ_MINUS_INFINITY = V_EQ | VC_MINUS_INFINITY,
  V_EQ_PLUS_INFINITY = V_EQ | VC_PLUS_INFINITY,
  V_NAN = VC_NAN,
  // Named NaN-class diagnostics are listed below.
  V_UNREPRESENTABLE = 1U << 7
};

The NaN-class diagnostic names shall be V_CVT_STR_UNK, V_DIV_ZERO, V_INF_ADD_INF, V_INF_DIV_INF, V_INF_MOD, V_INF_MUL_ZERO, V_INF_SUB_INF, V_MOD_ZERO, V_SQRT_NEG, V_UNKNOWN_NEG_OVERFLOW and V_UNKNOWN_POS_OVERFLOW. Their diagnostic payload shall distinguish the named outcomes.

SW_SPEC
2
PPL-SPEC-000122
PPL-SPEC-000122
Numerical result composition and field extraction

For Result values, operator& shall return the bitwise intersection, operator| the bitwise union, and operator- the left operand with all bits present in the right operand cleared. Subtraction shall be flag removal, not arithmetic subtraction of the underlying enum values. Operands shall remain unchanged.

result_class(r) shall retain exactly the VC_MASK bits and return Result_Class. result_relation(r) shall retain exactly the VR_MASK bits and return Result_Relation. result_relation_class(r) shall retain exactly the union of those masks and return Result. In particular, these field extractors shall discard unrepresentability, bounded-overflow and diagnostic-payload bits. They shall preserve a contained class or relation when other fields are added or removed. Extracting fields or composing bits shall not turn an otherwise unsupported code combination into a supported arithmetic result. These pure operations shall require no initialized library state.

For constant-evaluable inputs, these operations shall support constant evaluation under the same stated preconditions.

Bit composition and field extraction must preserve their separate meanings. Flag removal is especially important before dispatching a diagnostic carrying an unrepresentable-storage annotation.

APPROVED
NUMERICS
IF_CPP
Test, Analysis, Inspection

Exhaustively combine the named results and flags, compare operations with unsigned bit-set oracles and preserve input values. Enumerate every class/relation combination with and without storage/overflow flags; verify exact return types and that diagnostic payloads are removed from extracted fields. Check the constant-expression operations with translation-time assertions in addition to the runtime contract tests.

src/Result_defs.hh and src/Result_inlines.hh; tests/numericprotocols1.cc; tests/numericpublic1.cc.

CPP
Public

Public header: <ppl.hh>; declarations in PPL:

constexpr Result operator&(Result x, Result y);
constexpr Result operator|(Result x, Result y);
constexpr Result operator-(Result x, Result y);
constexpr Result_Class result_class(Result r);
constexpr Result_Relation result_relation(Result r);
constexpr Result result_relation_class(Result r);
SW_SPEC
3
PPL-SPEC-000123
Overflow-sign and storage-representability result queries

result_overflow(r) shall return -1 when the computed-value class is VC_MINUS_INFINITY and 1 when it is VC_PLUS_INFINITY. For normal-class results it shall return -1 for V_LT_INF and 1 for V_GT_SUP. It shall return zero for other normal-class codes and for NaN-class codes, including the unknown-intermediate-overflow diagnostics. Its zero result shall not promise that a NaN diagnostic is a successful operation; 🔗 8. Exception classification of numerical result codes shall classify exceptions separately. Class-based infinity classification shall retain its sign when V_UNREPRESENTABLE is present.

result_representable(r) shall return true exactly when the V_UNREPRESENTABLE flag is absent. This shall test the storage annotation, without promising a normal class, exactness or the absence of an arithmetic failure. Both queries shall preserve the supplied code and require no initialized library state.

For constant-evaluable inputs, these operations shall support constant evaluation under the same stated preconditions.

Overflow-sign and storage-representability queries answer specific questions about a result code. Their names do not replace class and diagnostic interpretation.

APPROVED
NUMERICS
IF_CPP
Test, Analysis, Inspection

Check literal expected signs for every named result, including both finite overflow markers, exact and inexact infinities and NaN-class intermediate overflow. Add and remove unrepresentability for each class and verify the boolean annotation independently. Exercise every switch case and normal-class default. Check the constant-expression operations with translation-time assertions in addition to the runtime contract tests.

src/Result_inlines.hh; tests/numericprotocols1.cc.

CPP
Public

Public header: <ppl.hh>; inline interfaces in PPL:

constexpr int result_overflow(Result r);
constexpr bool result_representable(Result r);
SW_SPEC
4
PPL-SPEC-000124

ROUND_DOWN and ROUND_UP shall request bounds toward negative and positive infinity, respectively. ROUND_IGNORE shall delegate rounding and permit lazily evaluated relation information; ROUND_NATIVE shall be its alias. ROUND_NOT_NEEDED shall require the caller to ensure that the requested operation's result is exact and representable in its destination. ROUND_STRICT_RELATION shall request determination of the exact ordering relation rather than a weaker relation containing it. ROUND_DIRECT and ROUND_INVERSE shall alias ROUND_UP and ROUND_DOWN, respectively.

A supported direction shall contain one of the four base directions with an optional strict-relation flag. Bitwise operator& and operator| shall intersect and unite direction bits. round_dir shall retain exactly the ROUND_DIR_MASK bits. Each named direction predicate shall return true exactly when that base direction is selected, independently of the strict flag. round_not_requested shall return true exactly for ROUND_IGNORE or ROUND_NOT_NEEDED. round_strict_relation shall return true exactly when the strict-relation bit is present. These operations shall preserve the arguments, require no initialized library state and leave the hardware rounding state unchanged.

For constant-evaluable inputs, these operations shall support constant evaluation under the same stated preconditions.

Rounding requests are numerical preconditions and intent, rather than instructions to change hardware state at every inspection. The strict flag is independent of the requested bound direction.

APPROVED
NUMERICS
IF_CPP
Test, Analysis, Inspection

Exhaust all four supported base directions with and without the strict flag. Check every predicate against a literal truth table, bitwise composition and exact return types; test both short-circuit conditions of round_not_requested. Preserve the hardware state across pure enum operations. Check the constant-expression operations with translation-time assertions in addition to the runtime contract tests.

src/Rounding_Dir_defs.hh and src/Rounding_Dir_inlines.hh; tests/numericprotocols1.cc; tests/numericpublic1.cc.

CPP
Public

Public header: <ppl.hh>; PPL declarations:

enum Rounding_Dir {
  ROUND_DOWN = 0U, ROUND_UP = 1U,
  ROUND_IGNORE = 6U, ROUND_NATIVE = ROUND_IGNORE,
  ROUND_NOT_NEEDED = 7U, ROUND_DIRECT = ROUND_UP,
  ROUND_INVERSE = ROUND_DOWN, ROUND_DIR_MASK = 7U,
  ROUND_STRICT_RELATION = 8U,
  ROUND_CHECK = ROUND_DIRECT | ROUND_STRICT_RELATION
};
constexpr Rounding_Dir operator&(Rounding_Dir x, Rounding_Dir y);
constexpr Rounding_Dir operator|(Rounding_Dir x, Rounding_Dir y);
constexpr Rounding_Dir round_dir(Rounding_Dir dir);
constexpr bool round_down(Rounding_Dir dir);
constexpr bool round_up(Rounding_Dir dir);
constexpr bool round_ignore(Rounding_Dir dir);
constexpr bool round_not_needed(Rounding_Dir dir);
constexpr bool round_not_requested(Rounding_Dir dir);
constexpr bool round_direct(Rounding_Dir dir);
constexpr bool round_inverse(Rounding_Dir dir);
constexpr bool round_strict_relation(Rounding_Dir dir);
SW_SPEC
5
PPL-SPEC-000125
PPL-SPEC-000125
Inversion of a supported rounding bound direction

For ROUND_UP or ROUND_DOWN, optionally combined with ROUND_STRICT_RELATION, inverse(dir) shall exchange upward and downward rounding and preserve the strict flag. ROUND_IGNORE, with or without that flag, shall be its own inverse. Applying inverse twice shall restore the supplied supported direction.

The base ROUND_NOT_NEEDED shall not be an admitted input to this operation. The function shall preserve its argument and shall not inspect or change hardware rounding state. It shall require no initialized library state.

For constant-evaluable inputs, these operations shall support constant evaluation under the same stated preconditions.

Inverting a bound exchanges its safe direction while preserving requested precision of the reported relation. A no-rounding-needed precondition is not a bound direction to invert.

APPROVED
NUMERICS
IF_CPP
Test, Analysis, Inspection

Check the six admitted values against literal inverse answers, strict-flag preservation, the involution identity and unchanged hardware rounding. Keep invalid direction values and ROUND_NOT_NEEDED outside successful-use tests. Check the constant-expression operations with translation-time assertions in addition to the runtime contract tests.

src/Rounding_Dir_defs.hh and src/Rounding_Dir_inlines.hh; tests/numericprotocols1.cc.

CPP
Public

Public header: <ppl.hh>; declaration in PPL:

constexpr Rounding_Dir inverse(Rounding_Dir dir);
SW_SPEC
6
PPL-SPEC-000126
PPL-SPEC-000126
Mapping a bound direction to the platform FPU encoding

When PPL_CAN_CONTROL_FPU is nonzero, round_fpu_dir(dir) shall map ROUND_UP and ROUND_DOWN to the configured platform encodings for upward and downward FPU rounding, respectively. An optional ROUND_STRICT_RELATION flag shall not change that encoding. Only those two base directions shall be admitted. The mapping shall not set, inspect or otherwise change FPU state and shall require no initialized library state.

No callable definition shall be promised when PPL_CAN_CONTROL_FPU is zero. In that configuration, callers shall not invoke this mapping.

For constant-evaluable inputs, these operations shall support constant evaluation under the same stated preconditions.

The abstract rounding request and the platform control-word encoding have separate representations. A pure conversion between them must not acquire ownership of the FPU state.

APPROVED
NUMERICS
IF_CPP, NATIVE_FP_AVAILABLE
Test, Analysis, Inspection

When FPU control is configured, compare both bound directions with independently captured platform constants, with and without the strict flag, and verify that the current hardware state is preserved. Compile a no-control configuration without invoking the unavailable definition. Check the constant-expression operations with translation-time assertions in addition to the runtime contract tests.

src/Rounding_Dir_defs.hh and src/Rounding_Dir_inlines.hh; tests/numericprotocols1.cc.

CPP
Public

Public header: <ppl.hh>; configured PPL interface:

constexpr fpu_rounding_direction_type round_fpu_dir(Rounding_Dir dir);

The implementation shall be available under the configuration condition stated above.

SW_SPEC
7
PPL-SPEC-000127

For a request equal to ROUND_NOT_NEEDED, rounding_dir(dir) shall return ROUND_CHECK in an assertion-enabled build and ROUND_NOT_NEEDED in a build defining NDEBUG. For every other supported direction value, including one carrying a strict-relation flag, it shall return that value unchanged.

check_result(r, dir) shall return r unchanged. When dir is exactly ROUND_NOT_NEEDED, the caller shall independently guarantee that the requested numerical result is exact and representable. An assertion-enabled build shall reject a reported relation excluding equality: VR_EMPTY, VR_LT, VR_GT or VR_NE. It shall accept VR_EQ, VR_LE, VR_GE or VR_LGE as compatible with that precondition, preserving the original code. Acceptance of a weak relation shall not establish exactness: a backend without usable inexact-status reporting may return a relation containing equality and strict alternatives even for an exact result. A build defining NDEBUG shall impose no diagnostic check. The checker shall not translate numerical result codes into exceptions or repair an inexact result. For other supported direction values no exact-relation check shall be imposed by this checker. Both functions shall require no initialized library state and shall not change hardware rounding state.

A reported relation is a set of possible comparisons between the exact and computed results. A set containing equality is compatible with an exactness precondition even when the backend cannot establish equality alone. The diagnostic rejects contradictory information without replacing the caller's exactness and representability guarantees or overflow-policy handling.

APPROVED
NUMERICS
IF_CPP
Test, Analysis, Inspection

Enumerate every supported direction and both build modes; check request translation against the documented truth table. Check all equality-containing relations, including exact infinity codes, for bit-for-bit preservation under the no-rounding-needed request, and every named result for other directions. In isolated assertion-enabled child processes require rejection of every relation excluding equality and of a known inexact native operation when precise inexact status is available. With only inexact-status reporting disabled, retain admitted native floating-point formats and FPU control, check exact integer and narrowing conversions and preserve their weak backend statuses. Successful numerical-use tests shall satisfy the caller's exactness and representability preconditions.

src/Checked_Number_inlines.hh; tests/numericprotocols1.cc; tests/numericexactnessnostatus1.cc.

CPP
Internal

Internal inline interfaces in Checked_Number_inlines.hh within PPL:

Rounding_Dir rounding_dir(Rounding_Dir dir);
Result check_result(Result r, Rounding_Dir dir);
SW_SPEC
8
PPL-SPEC-000128

throw_result_exception(r) shall always throw a standard exception and shall not return normally. It shall first ignore V_UNREPRESENTABLE for exception classification. V_EMPTY and the invalid-operation diagnostics V_NAN, V_CVT_STR_UNK, V_DIV_ZERO, V_INF_ADD_INF, V_INF_DIV_INF, V_INF_MOD, V_INF_MUL_ZERO, V_INF_SUB_INF, V_MOD_ZERO and V_SQRT_NEG shall produce std::domain_error.

V_EQ_MINUS_INFINITY, V_GT_MINUS_INFINITY, V_LT_INF, V_EQ_PLUS_INFINITY, V_LT_PLUS_INFINITY, V_GT_SUP, V_UNKNOWN_NEG_OVERFLOW and V_UNKNOWN_POS_OVERFLOW shall produce std::overflow_error. The ordinary nonempty relation results V_EQ, V_LT, V_LE, V_GT, V_GE, V_NE and V_LGE shall produce std::logic_error. An otherwise unrecognized result encoding shall also produce std::logic_error. Exception-message text shall not be prescribed.

This function shall classify the supplied code; it shall not reexecute an arithmetic operation or require initialized library state. Calling it on an ordinary relation code shall still throw, even when that code describes a successful numerical result.

Exception dispatch is deliberately total over supplied result codes. Its classification of NaN-class intermediate overflow differs from a simple overflow-sign query and must be explicit.

APPROVED
NUMERICS
IF_CPP
Test, Analysis, Inspection

Check each named result against a literal exception-category table, both with and without the unrepresentable flag. Require a thrown exception of the intended standard category, distinguish overflow_error from its runtime_error base, and cover the unrecognized-code default. Do not constrain diagnostic wording.

src/Checked_Number_defs.hh and src/Checked_Number.cc; tests/numericprotocols1.cc.

CPP
Public

Public header: <ppl.hh>; PPL declaration:

[[noreturn]] void throw_result_exception(Result r);
SW_SPEC
9
PPL-SPEC-001727
PPL-SPEC-001727
Checked arithmetic specialization forwarding

A PPL_SPECIALIZE_FUN specialization shall provide its static function with the operation's declared storage qualifiers, auxiliary argument order and return type. It shall forward those arguments and the corresponding policy template parameters, in order, to the supplied scalar implementation, return its result and propagate its exceptions. The wrapper shall perform no separate rounding, numerical conversion or policy-handler call. The selected scalar implementation's mathematical contract and preconditions shall apply; this mechanical forwarding contract shall not replace that operation-specific contract. Distinct generated functions shall retain distinct identities.

A specialization wrapper must preserve the selected implementation and its argument order without adding arithmetic behavior.

APPROVED
NUMERICS
IF_CPP
Analysis, Inspection

Inspect each arity, qualifier and auxiliary-argument mapping; compile representative integer, floating, GMP integer and rational dispatches and retain their existing operation-level tests.

src/checked_defs.hh; src/checked_int_inlines.hh; src/checked_float_inlines.hh; src/checked_mpz_inlines.hh; src/checked_mpq_inlines.hh.

CPP
Internal

The static function members generated by PPL_SPECIALIZE_FUN1_*, PPL_SPECIALIZE_FUN2_*, PPL_SPECIALIZE_FUN3_* and PPL_SPECIALIZE_FUN5_* in src/checked_defs.hh.