Parma Polyhedra Library requirements
C++ numerical result and rounding protocols
1. Numerical result classes and relation meanings 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.

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

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.

3. Overflow-sign and storage-representability result queries PPL-SPEC-000123

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.

4. Rounding-direction intent, composition and inspection 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.

PPL-SPEC-000125
5. Inversion of a supported rounding bound direction PPL-SPEC-000125

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.

PPL-SPEC-000126
6. Mapping a bound direction to the platform FPU encoding PPL-SPEC-000126

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.

7. Internal no-rounding-needed request and result checking 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.

8. Exception classification of numerical result codes 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.

PPL-SPEC-001727
9. Checked arithmetic specialization forwarding PPL-SPEC-001727

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.