C++ integral coefficient helper operations
For a live finite Coefficient, raw_value(x) shall return a reference to its underlying stored integer. The mutable overload shall refer to mutable storage and the const overload shall refer to const storage. In the mpz profile, that object shall be x itself and the referenced type shall be mpz_class. In bounded profiles it shall be the configured signed native integer member; both member x.raw_value() and free raw_value(x) shall refer to that same member. A returned reference shall remain valid only while its owning coefficient remains alive. A caller changing the raw value shall supply a finite value representable by that storage and satisfy its policy invariants; raw mutation shall not perform an arithmetic overflow check.
sgn(x) shall return -1, 0 or 1 exactly when the entry integer value is negative, zero or positive. cmp(x, y) shall return an int whose sign agrees with the ordering of the entry integer values. The magnitude of a nonzero comparison result shall not be prescribed. Neither query shall change either coefficient. These queries shall admit the entire selected integer range, including the bounded minimum.
The initialized-thread and object-synchronization conditions of 🔗 2. Finite coefficient construction, copying and comparison shall apply. Distinct source objects shall remain unchanged. The mpz profile shall be subject to available computational resources.
Raw access exposes representation identity; sign comparison exposes only mathematical order and must remain independent of backend comparison magnitudes.
Public header: <ppl.hh>; PPL names shall admit the following calls for mutable Coefficient x and const Coefficient y:
auto& mutable_storage = raw_value(x); const auto& constant_storage = raw_value(y); int sign = sgn(y); int ordering = cmp(x, y);
The exact reference types shall be as specified above. The GMP sign function is supplied by its integer provider. The member accessor forms shall apply only to the Checked_Number profiles.
Check exact reference types and addresses, mutate a raw value within range and observe the owning coefficient, and preserve const operands. Check signs and all comparison sign outcomes for zero, both signs, equal distinct objects, bounded endpoints and multi-limb GMP values.
src/GMP_Integer_defs.hh and src/GMP_Integer_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; tests/coefficient4.cc.
- PPL-LLR-000001 Exact integral coefficient calculations (Specifies)
- PPL-LLR-000002 Detection of checked coefficient overflow (Specifies)
- PPL-LLR-000301 Meaning of successful exact and approximate results (Specifies)
- PPL-LLR-000311 Public C++ access to supported facilities (Specifies)
- PPL-LLR-000442 Explicit numerical operation contracts (Specifies)
For live finite coefficients with source entry value a, neg_assign(x, y) shall assign − a and abs_assign(x, y) shall assign |a|, where y supplies a. The one-argument forms shall apply the same operation to the destination's own entry value. The two-argument destination may be the source itself.
A successful bounded result shall be representable. In checked profiles, negating or taking the absolute value of the least representable integer shall report std::overflow_error and retain the surviving-output guarantee of 🔗 5. Checked coefficient overflow and result-policy dispatch. A native bounded caller shall satisfy the representability precondition.
The initialized-thread and object-synchronization conditions of 🔗 2. Finite coefficient construction, copying and comparison shall apply. Distinct source objects shall remain unchanged. The mpz profile shall be subject to available computational resources.
Named assignments must retain the same mathematical sign conventions as ordinary coefficient arithmetic, including supported in-place use.
Public header: <ppl.hh>. The selected coefficient provider shall admit:
void neg_assign(Coefficient& x); void neg_assign(Coefficient& x, const Coefficient& y); void abs_assign(Coefficient& x); void abs_assign(Coefficient& x, const Coefficient& y);
Bounded forms shall be the corresponding function-template instantiations.
Check all four forms for zero and both signs, explicit two-argument self-aliasing, preservation of a distinct source, and large GMP values. Check bounded endpoint-adjacent results and require overflow exceptions for both checked MIN operations, then reassign surviving outputs.
src/GMP_Integer_defs.hh and src/GMP_Integer_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; tests/coefficient4.cc.
- PPL-LLR-000002 Detection of checked coefficient overflow (Specifies)
- PPL-LLR-000414 Integer quotient and remainder semantics (Specifies)
- PPL-LLR-000301 Meaning of successful exact and approximate results (Specifies)
- PPL-LLR-000311 Public C++ access to supported facilities (Specifies)
- PPL-LLR-000442 Explicit numerical operation contracts (Specifies)
For source entry values a and nonzero b, rem_assign(x, y, z) shall assign r = a − btrunc(a ⁄ b), where y supplies a and z supplies b. A nonzero remainder shall have the dividend's sign and satisfy |r| < |b|. This shall admit the bounded minimum divided by -1 and assign zero without requiring a representable quotient.
exact_div_assign(x, y, z) shall require nonzero b and exact divisibility of a by b. On success it shall assign a ⁄ b. The quotient shall be representable for successful bounded use. A checked quotient outside that range shall report std::overflow_error with the surviving-output guarantee of 🔗 5. Checked coefficient overflow and result-policy dispatch. No invalid divisor or nondivisibility rejection shall be promised.
For either operation the destination may be either source, including when both sources denote the same nonzero object; calculations shall use entry values.
The initialized-thread and object-synchronization conditions of 🔗 2. Finite coefficient construction, copying and comparison shall apply. Distinct source objects shall remain unchanged. The mpz profile shall be subject to available computational resources.
Exact division has a stronger input precondition than truncated quotient calculation. An explicit remainder assignment must preserve the already defined signed convention even where the corresponding bounded quotient cannot be represented.
Public header: <ppl.hh>. For operands satisfying the respective preconditions, the selected provider shall admit:
void rem_assign(Coefficient& x, const Coefficient& y, const Coefficient& z); void exact_div_assign(Coefficient& x, const Coefficient& y, const Coefficient& z);
The bounded forms shall be function-template instantiations.
Check every dividend/divisor sign combination, exact and inexact remainders, zero dividends and unit divisors against literal or unbounded answers. Exercise separate output, dividend alias, divisor alias and all-same nonzero arguments. Check MIN remainder -1 and checked MIN exact division by -1 separately.
src/GMP_Integer_defs.hh and src/GMP_Integer_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; tests/coefficient4.cc.
For live finite source entry values a and b, gcd_assign(x, y, z) shall assign the nonnegative greatest common divisor g. It shall assign zero for two zero sources and |a| when the other source is zero. lcm_assign(x, y, z) shall assign zero if either source is zero; otherwise it shall assign the positive least common multiple |a ⁄ g| |b|.
The destination may be either source, and the two sources may be the same object. Each calculation shall use their entry values. A successful bounded result shall be representable. For native bounded least-common-multiple calculation with two nonzero sources, both source absolute values shall additionally be representable. Native intermediate arithmetic shall be representable; in particular, no overflow shall be required to form a representable result.
In checked profiles, an unrepresentable nonnegative divisor or multiple, or an unrepresentable intermediate absolute value, shall report std::overflow_error with the surviving-output guarantee of 🔗 5. Checked coefficient overflow and result-policy dispatch. The zero-multiple case shall not require taking the absolute value of a nonzero source.
The initialized-thread and object-synchronization conditions of 🔗 2. Finite coefficient construction, copying and comparison shall apply. Distinct source objects shall remain unchanged. The mpz profile shall be subject to available computational resources.
Nonnegative normalization removes sign ambiguity, while explicit zero and alias rules make these common divisor primitives usable by reduction algorithms.
Public header: <ppl.hh>. The selected coefficient provider shall admit:
void gcd_assign(Coefficient& x, const Coefficient& y, const Coefficient& z); void lcm_assign(Coefficient& x, const Coefficient& y, const Coefficient& z);
Bounded forms shall be function-template instantiations.
Check zeros, every sign combination, coprime and noncoprime inputs, equal inputs and all destination/source alias arrangements. Verify literal expected answers and divisibility with an unbounded oracle. Include large GMP values, representable bounded MIN gcd results, zero LCM with MIN, and checked positive-range overflow.
src/GMP_Integer_defs.hh and src/GMP_Integer_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; src/checked_inlines.hh; tests/coefficient4.cc.
For live finite source entry values a and b, successful gcdext_assign(g, s, t, a, b) shall assign the nonnegative greatest common divisor to g and integer coefficients to s and t satisfying as + bt = g. For two zero inputs, g shall be zero; no particular Bezout coefficients shall be required. Other cases shall also not prescribe a unique pair of Bezout coefficients.
The three output objects shall be pairwise distinct and shall be distinct from both source objects. The source objects may be the same object. This contract shall not admit other aliasing. All bounded output values and each evaluated Euclidean coefficient, quotient, product and difference shall be representable for successful bounded use. When the second source is nonzero, the absolute values of both bounded sources shall additionally be representable. When the second source is zero, the first absolute value shall be representable.
Checked unrepresentable source absolute-value acquisition shall report std::overflow_error with the surviving-output guarantee of 🔗 5. Checked coefficient overflow and result-policy dispatch. This shall not promise that the unchecked native intermediate expressions within the extended Euclidean algorithm report overflow; the stated intermediate preconditions remain applicable in every bounded profile.
The initialized-thread and object-synchronization conditions of 🔗 2. Finite coefficient construction, copying and comparison shall apply. Distinct source objects shall remain unchanged. The mpz profile shall be subject to available computational resources.
The Bezout identity is the portable result. Its coefficients need not coincide across backends, and multiple outputs require an explicit nonaliasing and intermediate-range contract.
Public header: <ppl.hh>. The selected coefficient provider shall admit:
void gcdext_assign(Coefficient& g, Coefficient& s, Coefficient& t, const Coefficient& a, const Coefficient& b);
Bounded forms shall be the corresponding function-template instantiations.
Check the Bezout identity using unbounded arithmetic, nonnegative gcd, zero combinations, equal and mixed-sign inputs, swapped inputs and coprime/noncoprime cases. Use small exhaustive bounded operands whose every algorithm intermediate is representable, plus large GMP inputs. Check checked MIN absolute-value failures and reassign all surviving outputs.
src/GMP_Integer_defs.hh and src/GMP_Integer_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; src/checked_inlines.hh; tests/coefficient4.cc.
- PPL-LLR-000001 Exact integral coefficient calculations (Specifies)
- PPL-LLR-000002 Detection of checked coefficient overflow (Specifies)
- PPL-LLR-000301 Meaning of successful exact and approximate results (Specifies)
- PPL-LLR-000311 Public C++ access to supported facilities (Specifies)
- PPL-LLR-000442 Explicit numerical operation contracts (Specifies)
For destination and source entry values d, a and b, successful add_mul_assign(x, y, z) shall assign d + ab and successful sub_mul_assign(x, y, z) shall assign d − ab. The destination may be either or both sources, and the two sources may be the same object; each operand shall be interpreted at entry.
Successful bounded use shall require both the product ab and the final sum or difference to be representable. These shall be two arithmetic steps, with no promise of a wider fused accumulator. A checked overflow in either step shall report std::overflow_error with the surviving-output guarantee of 🔗 5. Checked coefficient overflow and result-policy dispatch. This shall include an unrepresentable product whose addition to or subtraction from the destination could have cancelled into range: the operation may fail at that intermediate step. Native bounded callers shall satisfy both representability conditions.
The initialized-thread and object-synchronization conditions of 🔗 2. Finite coefficient construction, copying and comparison shall apply. Distinct source objects shall remain unchanged. The mpz profile shall be subject to available computational resources.
Product accumulation is useful without an extra coefficient object, but a bounded two-step computation does not imply fused mathematical range.
Public header: <ppl.hh>. The selected coefficient provider shall admit:
void add_mul_assign(Coefficient& x, const Coefficient& y, const Coefficient& z); void sub_mul_assign(Coefficient& x, const Coefficient& y, const Coefficient& z);
Bounded forms shall be function-template instantiations.
Check zero, mixed signs, cancellation and all operand alias patterns with entry-value answers. Include multi-limb GMP products. For checked profiles separately overflow the product in both signs with positive, zero and negative destinations, and overflow only the final sum or difference. Require overflow also when a product overflow could have cancelled into range; reassign surviving outputs.
src/GMP_Integer_defs.hh and src/GMP_Integer_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; src/checked_int_inlines.hh; tests/coefficient4.cc.
For a live finite source with nonnegative entry value a, sqrt_assign(x, y) shall assign q = ⌊√(a)⌋. Equivalently q ≥ 0 and q2 ≤ a < (q + 1)2. The destination may be the source itself and shall use its entry value. Every nonnegative input representable in the selected profile shall be admitted; verifying the mathematical inequalities shall not require the caller to form their possibly unrepresentable bounded squares. No negative input rejection guarantee shall be inferred from this contract.
The initialized-thread and object-synchronization conditions of 🔗 2. Finite coefficient construction, copying and comparison shall apply. Distinct source objects shall remain unchanged. The mpz profile shall be subject to available computational resources.
Integer square root is the floor of the nonnegative real root. Specifying inequalities avoids dependence on floating-point conversion or rounding.
Public header: <ppl.hh>. The selected provider shall admit:
void sqrt_assign(Coefficient& x, const Coefficient& y);
For bounded profiles this shall be a function-template instantiation.
Check zero, one, exact squares, neighboring nonsquares, separate and aliased outputs, every nonnegative int8 value and the maximum of every bounded width. Establish both root inequalities using unbounded arithmetic. Include GMP values beyond floating-point exactness and large perfect-square neighbors.
src/GMP_Integer_defs.hh and src/GMP_Integer_inlines.hh; src/Checked_Number_defs.hh and src/Checked_Number_inlines.hh; src/checked_int_inlines.hh; tests/coefficient4.cc.
For finite source entry values a and b, not both zero, normalize2(x, y, n_x, n_y) shall assign a ⁄ g and b ⁄ g to the corresponding outputs, where g = gcd(a, b) > 0. It shall preserve each nonzero source's sign, produce a pair with greatest common divisor one, and preserve the ratio where defined.
The two outputs shall be distinct. n_x may be x and n_y may be y; other aliasing shall be excluded. For bounded successful use, the nonnegative gcd and both exact quotients shall be representable. Native intermediate arithmetic shall be representable. A checked gcd or quotient overflow shall report std::overflow_error with the surviving-output guarantee of 🔗 5. Checked coefficient overflow and result-policy dispatch.
The initialized-thread and object-synchronization conditions of 🔗 2. Finite coefficient construction, copying and comparison shall apply. Distinct source objects shall remain unchanged. The mpz profile shall be subject to available computational resources.
Reduction is a simultaneous mathematical operation on entry values even when its corresponding outputs reuse the source objects.
Internal interface declared in math_utilities_defs.hh within PPL:
void normalize2(Coefficient_traits::const_reference x, Coefficient_traits::const_reference y, Coefficient& n_x, Coefficient& n_y);
The trait's source parameter forms shall follow 🔗 1. Configured integral coefficient type and return trait.
Check coprime and reducible pairs, every sign combination and one zero source. Verify literal normalized values, gcd one and exact reconstruction with an unbounded oracle. Exercise separate outputs, each permitted single alias and both permitted aliases. Check representable MIN/odd pairs and checked MIN/zero gcd overflow.
src/math_utilities_defs.hh and src/math_utilities_inlines.hh; tests/coefficient4.cc.
For live finite Coefficient values, is_even(x) shall return true exactly when the integer value of x is divisible by two. is_additive_inverse(x, y) shall return true exactly when their mathematical integer sum is zero, without requiring evaluation of a bounded sum. It shall return false when the mathematical inverse of x is outside the selected integer range. Both predicates shall admit every representable source value, preserve their inputs and return bool.
For entry values a and b, min_assign(x, y) and max_assign(x, y) shall assign min(a, b) and max(a, b), respectively. The destination and source may be the same object. These assignments shall admit the full selected range and shall preserve a distinct source.
The initialized-thread and object-synchronization conditions of 🔗 2. Finite coefficient construction, copying and comparison shall apply. Distinct source objects shall remain unchanged. The mpz profile shall be subject to available computational resources.
These helpers express predicates and ordering, whose integer meaning does not itself require an overflowing arithmetic intermediate.
Internal interfaces declared in math_utilities_defs.hh; for the configured coefficient type, the following template instantiations shall be callable with these effective parameter and return types:
bool is_even(const Coefficient& x); bool is_additive_inverse(const Coefficient& x, const Coefficient& y); void min_assign(Coefficient& x, const Coefficient& y); void max_assign(Coefficient& x, const Coefficient& y);
These forms describe the supported coefficient instantiations of the generic templates; they shall not introduce additional nontemplate overloads.
Check parity of zero, both signs, odd and even values and bounded endpoints. Check inverse and noninverse pairs, zero, equal nonzero sources and bounded MIN against every representative peer, including itself. Exercise min/max for both orderings, equal distinct values and self-assignment, preserve sources, and include large GMP values.
src/math_utilities_defs.hh and src/math_utilities_inlines.hh; tests/coefficient4.cc.