authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-10-03 23:39:47+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-10-04 11:25:29+02:00
logb352564b36052053260696809ba19a7497e25940
tree6a01b1554bbbaf1a9a289a78533b3b84b93af929
parentebcdfebaa642c592bae13323c52dd1ec4f242f84

big ints: Some extra comments


1 files changed, 34 insertions(+), 6 deletions(-)

lib/std/math/big/int.zig+34-6
......@@ -2158,15 +2158,27 @@ pub const Managed = struct {
21582158 r.setMetadata(m.positive, m.len);
21592159 }
21602160
2161 /// r = a + b with 2s-complement wrapping semantics.
2162 ///
2163 /// r, a and b may be aliases. If r aliases a or b, then caller must call
2164 /// `r.ensureTwosCompCapacity` prior to calling `add`.
2165 ///
2166 /// Returns an error if memory could not be allocated.
21612167 pub fn addWrap(r: *Managed, a: Const, b: Const, signedness: std.builtin.Signedness, bit_count: usize) Allocator.Error!void {
2162 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
2168 try r.ensureTwosCompCapacity(bit_count);
21632169 var m = r.toMutable();
21642170 m.addWrap(a, b, signedness, bit_count);
21652171 r.setMetadata(m.positive, m.len);
21662172 }
21672173
2174 /// r = a + b with 2s-complement saturating semantics.
2175 ///
2176 /// r, a and b may be aliases. If r aliases a or b, then caller must call
2177 /// `r.ensureTwosCompCapacity` prior to calling `add`.
2178 ///
2179 /// Returns an error if memory could not be allocated.
21682180 pub fn addSat(r: *Managed, a: Const, b: Const, signedness: std.builtin.Signedness, bit_count: usize) Allocator.Error!void {
2169 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
2181 try r.ensureTwosCompCapacity(bit_count);
21702182 var m = r.toMutable();
21712183 m.addSat(a, b, signedness, bit_count);
21722184 r.setMetadata(m.positive, m.len);
......@@ -2184,15 +2196,27 @@ pub const Managed = struct {
21842196 r.setMetadata(m.positive, m.len);
21852197 }
21862198
2199 /// r = a - b with 2s-complement wrapping semantics.
2200 ///
2201 /// r, a and b may be aliases. If r aliases a or b, then caller must call
2202 /// `r.ensureTwosCompCapacity` prior to calling `add`.
2203 ///
2204 /// Returns an error if memory could not be allocated.
21872205 pub fn subWrap(r: *Managed, a: Const, b: Const, signedness: std.builtin.Signedness, bit_count: usize) Allocator.Error!void {
2188 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
2206 try r.ensureTwosCompCapacity(bit_count);
21892207 var m = r.toMutable();
21902208 m.subWrap(a, b, signedness, bit_count);
21912209 r.setMetadata(m.positive, m.len);
21922210 }
21932211
2212 /// r = a - b with 2s-complement saturating semantics.
2213 ///
2214 /// r, a and b may be aliases. If r aliases a or b, then caller must call
2215 /// `r.ensureTwosCompCapacity` prior to calling `add`.
2216 ///
2217 /// Returns an error if memory could not be allocated.
21942218 pub fn subSat(r: *Managed, a: Const, b: Const, signedness: std.builtin.Signedness, bit_count: usize) Allocator.Error!void {
2195 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
2219 try r.ensureTwosCompCapacity(bit_count);
21962220 var m = r.toMutable();
21972221 m.subSat(a, b, signedness, bit_count);
21982222 r.setMetadata(m.positive, m.len);
......@@ -2229,7 +2253,7 @@ pub const Managed = struct {
22292253 /// rma = a * b with 2s-complement wrapping semantics.
22302254 ///
22312255 /// rma, a and b may be aliases. However, it is more efficient if rma does not alias a or b.
2232 /// If rma aliases a or b, then caller must call `rma.ensureCapacity(calcTwosCompLimbCount(bit_count))`
2256 /// If rma aliases a or b, then caller must call `ensureTwosCompCapacity`
22332257 /// prior to calling `mul`.
22342258 ///
22352259 /// Returns an error if memory could not be allocated.
......@@ -2242,7 +2266,7 @@ pub const Managed = struct {
22422266 if (rma.limbs.ptr == b.limbs.ptr)
22432267 alias_count += 1;
22442268
2245 try rma.ensureCapacity(calcTwosCompLimbCount(bit_count));
2269 try rma.ensureTwosCompCapacity(bit_count);
22462270 var m = rma.toMutable();
22472271 if (alias_count == 0) {
22482272 m.mulWrapNoAlias(a, b, signedness, bit_count, rma.allocator);
......@@ -2255,6 +2279,10 @@ pub const Managed = struct {
22552279 rma.setMetadata(m.positive, m.len);
22562280 }
22572281
2282 pub fn ensureTwosCompCapacity(r: *Managed, bit_count: usize) !void {
2283 try r.ensureCapacity(calcTwosCompLimbCount(bit_count));
2284 }
2285
22582286 pub fn ensureAddScalarCapacity(r: *Managed, a: Const, scalar: anytype) !void {
22592287 try r.ensureCapacity(math.max(a.limbs.len, calcLimbLen(scalar)) + 1);
22602288 }