You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/modules/ROOT/pages/examples.adoc
+32Lines changed: 32 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -278,6 +278,38 @@ midpoint(-100, -50) = -75
278
278
----
279
279
====
280
280
281
+
[#examples_checked]
282
+
== Checked Arithmetic
283
+
284
+
.This https://github.com/cppalliance/int128/blob/develop/examples/checked_arithmetic.cpp[example] demonstrates checked addition, subtraction, and multiplication following the C23 checked-integer contract
Copy file name to clipboardExpand all lines: doc/modules/ROOT/pages/utilities.adoc
+137-1Lines changed: 137 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,6 +60,142 @@ Negative bases are reduced before exponentiation; `(std::numeric_limits<int128_t
60
60
| `base == 0` and `exp > 0`
61
61
| `0`
62
62
63
-
| Signed overload with `m <= 0` or `exp < 0`
63
+
| Signed overload with non-positive `m` or negative `exp`
64
64
| `0` (modular exponentiation requires a positive modulus; a negative exponent would require a modular inverse, which this interface does not provide)
65
65
|===
66
+
67
+
[#ipow]
68
+
== Integer Power
69
+
70
+
Computes `base ^ exp` by exponentiation by squaring, with a non-negative 64-bit exponent.
71
+
Unlike `powm` there is no modulus: the result is the true power reduced modulo `2^128`, which is the same rollover behavior as the library's `operator*`.
72
+
`ipow(base, exp)` is therefore equivalent to multiplying `base` by itself `exp` times.
The exponent is unsigned, so negative powers (which are not integers) cannot be requested.
88
+
Because the result wraps on overflow rather than saturating or reporting an error, `ipow` is appropriate when rollover semantics are intended.
89
+
90
+
=== Special Cases
91
+
92
+
[cols="1,1", options="header"]
93
+
|===
94
+
| Input | Result
95
+
96
+
| `exp == 0`
97
+
| `1` (including `ipow(0, 0) == 1`, following the conventional definition `0^0 == 1`)
98
+
99
+
| `base == 0` and `exp > 0`
100
+
| `0`
101
+
102
+
| `base ^ exp` exceeds 128 bits
103
+
| The low 128 bits of the true power, matching the rollover of `operator*`
104
+
|===
105
+
106
+
[#isqrt]
107
+
== Integer Square Root
108
+
109
+
Computes the integer square root `floor(sqrt(n))`: the largest integer `r` whose square does not exceed `n`.
110
+
The computation runs entirely in integer arithmetic using Newton's method, so it is exact (no floating-point rounding) and usable in a `constexpr` context.
| `floor(sqrt(n))`, the largest `r` whose square does not exceed `n` (so `isqrt(0) == 0` and `isqrt(1) == 1`)
136
+
|===
137
+
138
+
[#checked]
139
+
== Checked Arithmetic
140
+
141
+
`ckd_add`, `ckd_sub`, and `ckd_mul` implement the checked integer arithmetic interface introduced by C23's `<stdckdint.h>`, but without requiring a C23 toolchain; they are available in C++14 and later.
142
+
143
+
Each function computes `a + b`, `a - b`, or `a * b` respectively, as if both operands were represented in a signed integer type with infinite range, and then converts that mathematical result to the type pointed to by `result`.
144
+
The function returns `false` when `*result` correctly represents the mathematical result of the operation.
145
+
Otherwise it returns `true`, and `*result` is set to the mathematical result wrapped around (reduced modulo `2^N`) to the width `N` of `*result`.
146
+
`*result` is always written, whether or not the operation overflowed.
147
+
148
+
[source, c++]
149
+
----
150
+
namespace boost {
151
+
namespace int128 {
152
+
153
+
template <typename T1, typename T2, typename T3>
154
+
BOOST_INT128_HOST_DEVICE constexpr bool ckd_add(T1* result, T2 a, T3 b) noexcept;
155
+
156
+
template <typename T1, typename T2, typename T3>
157
+
BOOST_INT128_HOST_DEVICE constexpr bool ckd_sub(T1* result, T2 a, T3 b) noexcept;
158
+
159
+
template <typename T1, typename T2, typename T3>
160
+
BOOST_INT128_HOST_DEVICE constexpr bool ckd_mul(T1* result, T2 a, T3 b) noexcept;
161
+
162
+
} // namespace int128
163
+
} // namespace boost
164
+
----
165
+
166
+
The three type parameters are independent: the result type and the two operand types may differ in width and signedness.
167
+
The operation always uses the exact mathematical value of each operand, so a negative signed value added to an unsigned value, or a product that needs up to 256 bits internally, is evaluated correctly.
168
+
169
+
Following the C23 rules, `T1`, `T2`, and `T3` may be any integer type other than `bool`, plain `char`, an enumerated type, or a bit-precise (`_BitInt`) type.
170
+
In addition to the standard and extended integer types, the library's `uint128_t` and `int128_t` are accepted.
171
+
172
+
The following example exercises all three operations, including the wrap-around, the `INT128_MIN * -1` case, and the mixed-type behavior described above.
173
+
174
+
.This https://github.com/cppalliance/int128/blob/develop/examples/checked_arithmetic.cpp[example] demonstrates checked addition, subtraction, and multiplication following the C23 checked-integer contract
0 commit comments