This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Private Function
r = a & b with 2s complement semantics. r may alias. a and b must not be 0. Returns true when the result is positive. We assume a.len >= b.len here, so:
when b is positive, r requires at least b.len limbs of storage,
when b is negative but a is positive, r requires at least a.len limbs of storage,
when both a and b are negative, r requires at least a.len + 1 limbs of storage.
fnllsignedand(r: []Limb, a: []constLimb, a_positive: bool, b: []constLimb, b_positive: bool) bool {
assert(a.len != 0andb.len != 0);
assert(a.len >= b.len);
assert(r.len >= if (b_positive) b.lenelseif (a_positive) a.lenelsea.len + 1);
if (a_positiveandb_positive) {
// Trivial case, result is positive.vari: usize = 0;
while (i < b.len) : (i += 1) {
r[i] = a[i] & b[i];
}
// With b = 0 we have a & 0 = 0, so the upper bytes are zero.// Omit setting them here and simply discard them whenever// llsignedand is called.returntrue;
} elseif (!a_positiveandb_positive) {
// Result is positive.// r = (--a) & b// = ~(-a - 1) & bvari: usize = 0;
vara_borrow: u1 = 1;
while (i < b.len) : (i += 1) {
constov = @subWithOverflow(a[i], a_borrow);
a_borrow = ov[1];
r[i] = ~ov[0] & b[i];
}
// With b = 0 we have ~(a - 1) & 0 = 0, so the upper bytes are zero.// Omit setting them here and simply discard them whenever// llsignedand is called.returntrue;
} elseif (a_positiveand !b_positive) {
// Result is positive.// r = a & (--b)// = a & ~(-b - 1)vari: usize = 0;
varb_borrow: u1 = 1;
while (i < b.len) : (i += 1) {
constov = @subWithOverflow(b[i], b_borrow);
b_borrow = ov[1];
r[i] = a[i] & ~ov[0];
}
assert(b_borrow == 0); // b was 0// With b = 0 and b_borrow = 0 we have a & ~(0 - 0) = a & ~0 = a, so// the upper bytes are the same as those of a.while (i < a.len) : (i += 1) {
r[i] = a[i];
}
returntrue;
} else {
// Result is negative.// r = (--a) & (--b)// = ~(-a - 1) & ~(-b - 1)// = ~((-a - 1) | (-b - 1))// = -(((-a - 1) | (-b - 1)) + 1)vari: usize = 0;
vara_borrow: u1 = 1;
varb_borrow: u1 = 1;
varr_carry: u1 = 1;
while (i < b.len) : (i += 1) {
constov1 = @subWithOverflow(a[i], a_borrow);
a_borrow = ov1[1];
constov2 = @subWithOverflow(b[i], b_borrow);
b_borrow = ov2[1];
constov3 = @addWithOverflow(ov1[0] | ov2[0], r_carry);
r[i] = ov3[0];
r_carry = ov3[1];
}
// b is at least 1, so this should never underflow.assert(b_borrow == 0); // b was 0// With b = 0 and b_borrow = 0 we get (-a - 1) | (0 - 0) = (-a - 1) | 0 = -a - 1.while (i < a.len) : (i += 1) {
constov1 = @subWithOverflow(a[i], a_borrow);
a_borrow = ov1[1];
constov2 = @addWithOverflow(ov1[0], r_carry);
r[i] = ov2[0];
r_carry = ov2[1];
}
assert(a_borrow == 0); // a was 0.// The final addition can overflow here, so we need to keep that in mind.r[i] = r_carry;
returnfalse;
}
}
r = a & b with 2s complement semantics. r may alias. a and b must not be 0. Returns
truewhen the result is positive. We assumea.len >= b.lenhere, so:b.lenlimbs of storage,a.lenlimbs of storage,a.len + 1limbs of storage.