Parma Polyhedra Library requirements
C++ finite integral scalar conversions

C++ finite integral scalar conversions

1. Exact conversion between native and checked integral scalars
UID: PPL-SPEC-000161
STATUS: APPROVED
COMPONENT: NUMERICS
APPLICABILITY: IF_CPP
VERIFICATION_METHODS: Test, Analysis, Inspection
LANGUAGE: CPP
VISIBILITY: Public
RELATIONS (Parent):
STATEMENT:

The native integral types covered here shall be char , signed char , unsigned char , short , unsigned short , int , unsigned int , long , unsigned long , long long and unsigned long long . An integral source or destination may instead be Checked_Number<T, Check_Overflow_Policy<T>> for one of those native types T . For these integral types, the policy shall reserve no integer payload for infinity or NaN. The scalar wrapper range shall be the full native range, independent of the selected global coefficient profile.

For a source value representable in the destination, assign_r shall assign that exact integer and return V_EQ . This shall hold for the four base directions in 🔗 4. Rounding-direction intent, composition and inspection , with or without the strict flag. The source may be the destination when its type admits both roles; the entry value shall be preserved. Native destinations shall use that same overflow policy through their _r adapter. Out-of-range outcomes shall follow 🔗 5. Integral scalar conversion range outcomes .

The returned Result shall report the conversion directly; the _r operation shall not invoke the destination policy's handle_result . The destination shall be live and assignable. A distinct source shall remain unchanged. Callers shall synchronize access overlapping modification of an argument object. No library initialization object, thread initialization or coefficient-constant stage shall be required. Available resources shall limit temporary integer storage.

RATIONALE:

Finite integral embedding and narrowing have the same exact meaning when the value fits. A checked scalar policy belongs to the instantiated scalar, rather than to whichever global coefficient alias was configured.

INTERFACE:

Public header: <ppl.hh>; effective PPL template instantiation:

Result assign_r(To& to, const From& from, Rounding_Dir dir);

The admitted source and destination types shall be as stated above.

VERIFICATION_CRITERIA:

Exercise every listed native source/destination type pair and native/wrapper source/destination combination. Check exact status and values for common signed values, zero and all endpoints that fit the destination, with all admitted directions and exact self-aliases. Preserve distinct sources and compare endpoints with unbounded integer values.

SOURCE:

src/checked_int_inlines.hh ; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh ; tests/integerconversionspec1.cc ; tests/numericpublic1.cc. tests/smallconstantassign1.cc.

PPL-SPEC-000162
2. Exact conversion from an unbounded integer to integral storage
UID: PPL-SPEC-000162
STATUS: APPROVED
COMPONENT: NUMERICS
APPLICABILITY: IF_CPP, SCALAR_MPZ
VERIFICATION_METHODS: Test, Analysis, Inspection
LANGUAGE: CPP
VISIBILITY: Public
RELATIONS (Parent):
STATEMENT:

For a finite mpz_class source whose integer value is representable in a native or checked integral destination admitted by 🔗 1. Exact conversion between native and checked integral scalars , assign_r shall assign that exact integer and return V_EQ . It shall admit both signed and unsigned destination ranges, including the least signed integer and the maximum unsigned integer.

The four base directions in 🔗 4. Rounding-direction intent, composition and inspection , with or without the strict flag, shall not change the exact value or classification. Out-of-range outcomes shall follow 🔗 5. Integral scalar conversion range outcomes . The _r conversion shall return its result directly without policy-handler dispatch. The destination shall be live and assignable. A distinct source shall remain unchanged. Callers shall synchronize access overlapping modification of an argument object. No library initialization object, thread initialization or coefficient-constant stage shall be required. Available resources shall limit temporary integer storage.

RATIONALE:

An unbounded integer can represent a destination endpoint exactly, including a signed minimum whose positive magnitude does not fit in that signed type.

INTERFACE:

Public header: <ppl.hh>; effective PPL template instantiation:

Result assign_r(To& to, const From& from, Rounding_Dir dir);

The admitted source and destination types shall be as stated above; From shall be mpz_class.

VERIFICATION_CRITERIA:

Use an unbounded integer oracle for each signed and unsigned endpoint, zero, both signs and representable interior values. Check native and checked destinations with every direction and strict flag, exact status and source preservation. Include native types as wide as long long and unsigned long long.

SOURCE:

src/checked_int_inlines.hh ; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh ; tests/integerconversionspec1.cc ; tests/numericpublic1.cc.

3. Directed rational-to-integral conversion within range
UID: PPL-SPEC-000163
STATUS: APPROVED
COMPONENT: NUMERICS
APPLICABILITY: IF_CPP, SCALAR_MPQ
VERIFICATION_METHODS: Test, Analysis, Inspection
LANGUAGE: CPP
VISIBILITY: Public
RELATIONS (Parent):
STATEMENT:

For a finite canonical rational source with value a , assign_r with base direction ROUND_DOWN shall assign ⌊a⌋ ; with base direction ROUND_UP it shall assign ⌈a⌉ . The destination shall be one of the native or checked integral types in 🔗 1. Exact conversion between native and checked integral scalars . Canonical rational representation shall follow 🔗 1. Exact rational assignment from rational and integral sources .

For a directed integer result representable in the destination, an integral source value shall produce V_EQ . A nonintegral value rounded downward shall produce V_GT and a nonintegral value rounded upward shall produce V_LT , with exact-to-computed relation orientation as in 🔗 1. Numerical result classes and relation meanings . Adding ROUND_STRICT_RELATION shall preserve these values and definite classifications.

Out-of-range outcomes for either destination form shall follow 🔗 6. Rational conversion at and beyond integral endpoints . This conversion shall return Result without policy-handler dispatch. The destination shall be live and assignable. A distinct source shall remain unchanged. Callers shall synchronize access overlapping modification of an argument object. No library initialization object, thread initialization or coefficient-constant stage shall be required. Available resources shall limit temporary integer storage. Rational-source conversions shall use thread-local temporary storage in a thread-safe build; callers shall serialize those conversions across threads in a non-thread-safe build.

RATIONALE:

Rational conversion selects a neighboring integer according to the requested direction. Its exact relation can be established from integer quotient and remainder without floating-point approximation.

INTERFACE:

Public header: <ppl.hh>; effective PPL template instantiation:

Result assign_r(To& to, const From& from, Rounding_Dir dir);

The admitted source and destination types shall be as stated above; From shall be mpq_class.

VERIFICATION_CRITERIA:

Use literal fractions and an independent integer quotient/remainder oracle for both signs, exact integers, halves and proper fractions. Check floor/ceiling values and definite result relations in every native and checked destination, including rounded values equal to either endpoint and source values just outside an endpoint that round inward.

SOURCE:

src/checked_int_inlines.hh ; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh ; tests/integerconversionspec1.cc .

4. Truncated rational-to-integral conversion and lazy relation requests
UID: PPL-SPEC-000164
STATUS: APPROVED
COMPONENT: NUMERICS
APPLICABILITY: IF_CPP, SCALAR_MPQ
VERIFICATION_METHODS: Test, Analysis, Inspection
LANGUAGE: CPP
VISIBILITY: Public
RELATIONS (Parent):
STATEMENT:

For a finite canonical rational source with value a , base direction ROUND_IGNORE shall assign trunc(a) , toward zero, when that integer is representable in a destination from 🔗 1. Exact conversion between native and checked integral scalars . With no strict-relation flag, successful conversion shall return V_LGE ; it shall not claim exactness from an unchanged integer payload alone.

ROUND_NOT_NEEDED shall require a to be an integer representable in the destination. For that valid use, the assigned integer shall be exact. With no strict-relation flag, the returned classification may be lazy V_LGE in a build defining NDEBUG ; the assertion-enabled request translation in 🔗 7. Internal no-rounding-needed request and result checking shall instead establish V_EQ .

When ROUND_STRICT_RELATION is supplied with either base direction, the conversion shall report the definite relation requested by 🔗 4. Rounding-direction intent, composition and inspection . A nonintegral positive value truncated toward zero shall produce V_GT ; a nonintegral negative value shall produce V_LT ; an integral value shall produce V_EQ . The exactness and representability preconditions of ROUND_NOT_NEEDED shall still apply when it carries that flag.

Truncation range failures under ROUND_IGNORE shall follow 🔗 6. Rational conversion at and beyond integral endpoints . The returned result shall not cause automatic policy-handler dispatch. The destination shall be live and assignable. A distinct source shall remain unchanged. Callers shall synchronize access overlapping modification of an argument object. No library initialization object, thread initialization or coefficient-constant stage shall be required. Available resources shall limit temporary integer storage. Rational-source conversions shall use thread-local temporary storage in a thread-safe build; callers shall serialize those conversions across threads in a non-thread-safe build.

RATIONALE:

A lazy relation and an exact assigned payload are distinct observations. A strict relation request must determine which side of the truncated integer contains the rational value.

INTERFACE:

Public header: <ppl.hh>; effective PPL template instantiation:

Result assign_r(To& to, const From& from, Rounding_Dir dir);

The admitted source and destination types shall be as stated above; From shall be mpq_class.

VERIFICATION_CRITERIA:

Check signed truncation answers and lazy V_LGE results for unflagged ignore requests. Check exact representable no-rounding-needed values in assertion and nonassertion builds, with their documented classifications. Independently check definite relations for strict requests, including negative proper fractions, positive proper fractions and exact integers. Preserve source components.

SOURCE:

src/checked_int_inlines.hh ; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh ; tests/integerconversionspec1.cc .

5. Integral scalar conversion range outcomes
UID: PPL-SPEC-000165
STATUS: APPROVED
COMPONENT: NUMERICS
APPLICABILITY: IF_CPP
VERIFICATION_METHODS: Test, Analysis, Inspection
LANGUAGE: CPP
VISIBILITY: Public
RELATIONS (Parent):
STATEMENT:

For a native integral, checked integral or mpz_class source, a native integral or Checked_Number<T, Check_Overflow_Policy<T>> destination shall detect a source integer outside its full native T range. The native source and destination types shall be those in 🔗 1. Exact conversion between native and checked integral scalars . This shall be a Result -returning conversion; an arithmetic range failure shall not invoke handle_result or throw an overflow exception automatically.

If the source is below the destination minimum, ROUND_UP shall store that minimum and return V_LT_INF . ROUND_DOWN or ROUND_IGNORE shall return V_GT_MINUS_INFINITY | V_UNREPRESENTABLE . If the source is above the destination maximum, ROUND_DOWN shall store that maximum and return V_GT_SUP . ROUND_UP or ROUND_IGNORE shall return V_LT_PLUS_INFINITY | V_UNREPRESENTABLE . An optional strict-relation flag shall not change these range outcomes.

An unrepresentable result shall not promise a meaningful destination payload; the destination shall remain assignable and destructible. ROUND_NOT_NEEDED shall require an exact representable result and shall therefore exclude these out-of-range requests, with or without a strict flag. These guarantees shall not extend to a Checked_Number destination whose policy disables overflow checking. The destination shall be live and assignable. A distinct source shall remain unchanged. Callers shall synchronize access overlapping modification of an argument object. No library initialization object, thread initialization or coefficient-constant stage shall be required. Available resources shall limit temporary integer storage.

RATIONALE:

A checked conversion returning Result must expose both the safe inward endpoint and the inability to represent the outward bound. It must allow its caller to choose subsequent policy handling.

INTERFACE:

Public header: <ppl.hh>; effective PPL template instantiation:

Result assign_r(To& to, const From& from, Rounding_Dir dir);

The admitted source and destination types shall be as stated above; To shall be an admitted native or checked destination type.

VERIFICATION_CRITERIA:

Generate integers immediately outside every checked signed/unsigned range using an unbounded oracle. For native and checked integral sources, test each type pair whenever the source can represent the out-of-range value; also use mpz sources for every endpoint. Verify exact result codes, inward stored endpoints, unrepresentability flags, lack of automatic exception dispatch and reusable outputs.

SOURCE:

src/checked_int_inlines.hh ; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh ; tests/integerconversionspec1.cc .

6. Rational conversion at and beyond integral endpoints
UID: PPL-SPEC-000166
STATUS: APPROVED
COMPONENT: NUMERICS
APPLICABILITY: IF_CPP, SCALAR_MPQ
VERIFICATION_METHODS: Test, Analysis, Inspection
LANGUAGE: CPP
VISIBILITY: Public
RELATIONS (Parent):
STATEMENT:

For a finite canonical rational source and a native or checked integral destination from 🔗 1. Exact conversion between native and checked integral scalars , assign_r shall distinguish a representable directed or truncated integer from range failure without policy-handler dispatch. Canonical representation shall follow 🔗 1. Exact rational assignment from rational and integral sources .

If truncating the rational toward zero already lies below or above the destination range, the conversion shall return the corresponding range outcome specified in 🔗 5. Integral scalar conversion range outcomes for the requested base direction. If that truncation lies within range but subsequent upward rounding would increase the maximum, it shall return V_LT_PLUS_INFINITY | V_UNREPRESENTABLE . If subsequent downward rounding would decrease the minimum, it shall return V_GT_MINUS_INFINITY | V_UNREPRESENTABLE . These shall not be reported as successful wrapped integers.

Representable rounded values shall follow 🔗 3. Directed rational-to-integral conversion within range or 🔗 4. Truncated rational-to-integral conversion and lazy relation requests , including a rational just beyond an endpoint whose requested integer rounds inward to that endpoint. An unrepresentable outcome shall preserve destination assignability and destructibility without a payload value promise. ROUND_NOT_NEEDED shall admit only exact representable integer results. A Checked_Number destination whose policy disables overflow checking shall remain outside this range-detection guarantee. The destination shall be live and assignable. A distinct source shall remain unchanged. Callers shall synchronize access overlapping modification of an argument object. No library initialization object, thread initialization or coefficient-constant stage shall be required. Available resources shall limit temporary integer storage. Rational-source conversions shall use thread-local temporary storage in a thread-safe build; callers shall serialize those conversions across threads in a non-thread-safe build.

RATIONALE:

Fractional endpoint crossings can fail during the directed increment or decrement even when the truncated quotient fits. They differ from a quotient already outside the destination range.

INTERFACE:

Public header: <ppl.hh>; effective PPL template instantiation:

Result assign_r(To& to, const From& from, Rounding_Dir dir);

The admitted source and destination types shall be as stated above; From shall be mpq_class and To an admitted native or checked destination type.

VERIFICATION_CRITERIA:

Use exact thirds and halves immediately inside and outside every signed and unsigned endpoint, plus integral and fractional values more than one unit beyond it. Compare quotient/remainder-derived range cases and exact returned flags, both directed signs and strict requests; check valid unflagged lazy truncation separately. Require inward endpoint values and reusable outputs while preserving source components.

SOURCE:

src/checked_int_inlines.hh ; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh ; tests/integerconversionspec1.cc .