IntCompare
Description
Compare an integer against another integer-like value. Dispatches on the type of rhs to the matching internal handler.
Parameters
| Name | Direction | Description |
|---|---|---|
lhs |
in | Left-hand integer |
rhs |
in | Right-hand operand (Int, pointer, u64, or i64 compatible type) |
Usage example (from documentation)
int cmp = IntCompare(&value, 42);Success
Returns -1 if lhs < rhs, 0 if equal, 1 if lhs > rhs.
Failure
Pure comparison cannot fail. LOG_FATAL if either operand pointer is NULL (propagated from the underlying handler’s ValidateInt).
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Compare.h:106:
/// TAGS: Int, Compare, Equal, Generic
///
# define IntEQ(lhs, rhs) (IntCompare((lhs), (rhs)) == 0)
///
/// Test whether `lhs` is strictly less than `rhs`.
- In
Compare.h:121:
/// TAGS: Int, Compare, LessThan, Generic
///
# define IntLT(lhs, rhs) (IntCompare((lhs), (rhs)) < 0)
///
/// Test whether `lhs` is less than or equal to `rhs`.
- In
Compare.h:136:
/// TAGS: Int, Compare, LessEqual, Generic
///
# define IntLE(lhs, rhs) (IntCompare((lhs), (rhs)) <= 0)
///
/// Test whether `lhs` is strictly greater than `rhs`.
- In
Compare.h:151:
/// TAGS: Int, Compare, GreaterThan, Generic
///
# define IntGT(lhs, rhs) (IntCompare((lhs), (rhs)) > 0)
///
/// Test whether `lhs` is greater than or equal to `rhs`.
- In
Compare.h:166:
/// TAGS: Int, Compare, GreaterEqual, Generic
///
# define IntGE(lhs, rhs) (IntCompare((lhs), (rhs)) >= 0)
///
/// Test whether two numeric values differ.
- In
Compare.h:181:
/// TAGS: Int, Compare, NotEqual, Generic
///
# define IntNE(lhs, rhs) (IntCompare((lhs), (rhs)) != 0)
#endif- In
Compare.c:22:
bool test_int_compare(void) {
WriteFmt("Testing IntCompare\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Compare.c:30:
Int c = IntFromBinary("000101010", &alloc.base);
bool result = IntCompare(&a, &b) < 0;
result = result && (IntCompare(&b, &a) > 0);
result = result && (IntCompare(&b, &c) == 0);- In
Compare.c:31:
bool result = IntCompare(&a, &b) < 0;
result = result && (IntCompare(&b, &a) > 0);
result = result && (IntCompare(&b, &c) == 0);- In
Compare.c:32:
bool result = IntCompare(&a, &b) < 0;
result = result && (IntCompare(&b, &a) > 0);
result = result && (IntCompare(&b, &c) == 0);
IntDeinit(&a);- In
Compare.c:67:
bool test_int_compare_generic(void) {
WriteFmt("Testing IntCompare generic dispatch\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Compare.c:77:
IntShiftLeft(&big, 80);
bool result = (IntCompare(&value, &same) == 0);
result = result && (IntCompare(&value, &same) == 0);
result = result && (IntCompare(&value, 42) == 0);- In
Compare.c:78:
bool result = (IntCompare(&value, &same) == 0);
result = result && (IntCompare(&value, &same) == 0);
result = result && (IntCompare(&value, 42) == 0);
result = result && (IntCompare(&value, 100ULL) < 0);- In
Compare.c:79:
bool result = (IntCompare(&value, &same) == 0);
result = result && (IntCompare(&value, &same) == 0);
result = result && (IntCompare(&value, 42) == 0);
result = result && (IntCompare(&value, 100ULL) < 0);
result = result && (IntCompare(&value, -1) > 0);- In
Compare.c:80:
result = result && (IntCompare(&value, &same) == 0);
result = result && (IntCompare(&value, 42) == 0);
result = result && (IntCompare(&value, 100ULL) < 0);
result = result && (IntCompare(&value, -1) > 0);
result = result && (IntCompare(&big, UINT64_MAX) > 0);- In
Compare.c:81:
result = result && (IntCompare(&value, 42) == 0);
result = result && (IntCompare(&value, 100ULL) < 0);
result = result && (IntCompare(&value, -1) > 0);
result = result && (IntCompare(&big, UINT64_MAX) > 0);
result = result && IntEQ(&value, 42);- In
Compare.c:82:
result = result && (IntCompare(&value, 100ULL) < 0);
result = result && (IntCompare(&value, -1) > 0);
result = result && (IntCompare(&big, UINT64_MAX) > 0);
result = result && IntEQ(&value, 42);
result = result && IntLE(&value, 42);- In
Compare.c:201:
// ---------------------------------------------------------------------------
bool test_m24_compare_u64_64bit_equal(void) {
WriteFmt("Testing IntCompare(2^63, 2^63) == 0\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Compare.c:208:
bool fail = (IntBitLength(&lhs) != 64); // sanity: exactly 64 bits.
fail = fail || (IntCompare(&lhs, 0x8000000000000000u) != 0);
IntDeinit(&lhs);- In
Compare.c:218:
// `lhs_value < rhs` branch alongside the wide-value guard.
bool test_m24_compare_u64_below(void) {
WriteFmt("Testing IntCompare(7, 9) == -1\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Compare.c:224:
Int lhs = IntFrom(7u, &alloc.base);
bool fail = (IntCompare(&lhs, 9u) != -1);
IntDeinit(&lhs);- In
Math.c:763:
IntModMul(&check, &result_value, &b, &m);
result = result && (IntCompare(&check, 10) == 0);
IntDeinit(&a);- In
Math.c:852:
bool result = IntModSqrt(&root, &value, &mod);
IntSquareMod(&check, &root, &mod);
result = result && (IntCompare(&check, 10) == 0);
IntDeinit(&value);- In
Math.c:872:
bool result = !IntModSqrt(&root, &value, &mod);
result = result && (IntCompare(&root, 99) == 0);
IntDeinit(&value);- In
Math.c:949:
bool result = !IntModDiv(&result_value, &a, &b, &m);
result = result && (IntCompare(&result_value, 99) == 0);
IntDeinit(&a);- In
Math.c:994:
bool result = !IntDivMod("ient, &remainder, ÷nd, &divisor);
result = result && (IntCompare("ient, 99) == 0);
result = result && (IntCompare(&remainder, 77) == 0);- In
Math.c:995:
result = result && (IntCompare("ient, 99) == 0);
result = result && (IntCompare(&remainder, 77) == 0);
IntDeinit(÷nd);- In
Math.c:1015:
bool result = !IntRootRem(&root, &remainder, &value, 0);
result = result && (IntCompare(&root, 99) == 0);
result = result && (IntCompare(&remainder, 77) == 0);- In
Math.c:1016:
result = result && (IntCompare(&root, 99) == 0);
result = result && (IntCompare(&remainder, 77) == 0);
IntDeinit(&value);- In
Math.c:1034:
IntDiv("ient, ÷nd, 0u);
bool result = IntCompare("ient, 99) == 0;
IntDeinit(÷nd);- In
Math.c:1051:
IntMod(&result_value, &value, 0u);
bool result = IntCompare(&result_value, 99) == 0;
IntDeinit(&value);- In
Math.c:1070:
bool result = !IntModDiv(&result_value, &a, &b, &m);
result = result && (IntCompare(&result_value, 99) == 0);
IntDeinit(&a);- In
Math.c:1126:
bool result = !IntPowMod(&result_value, &base, &exp, &mod);
result = result && (IntCompare(&result_value, 99) == 0);
IntDeinit(&base);- In
Math.c:1156:
// result written, returns true, and root^2 == value (mod p).
result = result && (IntCompare(&check, 2) == 0);
// root must lie in [0, p).
result = result && (IntCompare(&root, 7) < 0);- In
Math.c:1158:
result = result && (IntCompare(&check, 2) == 0);
// root must lie in [0, p).
result = result && (IntCompare(&root, 7) < 0);
result = result && (IntCompare(&root, 0) >= 0);
// root must actually have been overwritten away from the sentinel 99.
- In
Math.c:1159:
// root must lie in [0, p).
result = result && (IntCompare(&root, 7) < 0);
result = result && (IntCompare(&root, 0) >= 0);
// root must actually have been overwritten away from the sentinel 99.
result = result && (IntCompare(&root, 99) != 0);- In
Math.c:1161:
result = result && (IntCompare(&root, 0) >= 0);
// root must actually have been overwritten away from the sentinel 99.
result = result && (IntCompare(&root, 99) != 0);
IntDeinit(&value);- In
Math.c:1183:
bool result = !IntModSqrt(&root, &value, &mod);
result = result && (IntCompare(&root, 99) == 0);
IntDeinit(&value);- In
Math.c:1212:
IntSquareMod(&check, &root, &mod);
result = result && (IntCompare(&check, 2) == 0);
result = result && (IntCompare(&root, 17) < 0);
result = result && (IntCompare(&root, 0) >= 0);- In
Math.c:1213:
result = result && (IntCompare(&check, 2) == 0);
result = result && (IntCompare(&root, 17) < 0);
result = result && (IntCompare(&root, 0) >= 0);
result = result && (IntCompare(&root, 99) != 0);- In
Math.c:1214:
result = result && (IntCompare(&check, 2) == 0);
result = result && (IntCompare(&root, 17) < 0);
result = result && (IntCompare(&root, 0) >= 0);
result = result && (IntCompare(&root, 99) != 0);- In
Math.c:1215:
result = result && (IntCompare(&root, 17) < 0);
result = result && (IntCompare(&root, 0) >= 0);
result = result && (IntCompare(&root, 99) != 0);
IntDeinit(&value);- In
Math.c:1242:
IntSquareMod(&check, &root, &mod);
result = result && (IntCompare(&check, 4) == 0);
result = result && (IntCompare(&root, 17) < 0);
result = result && (IntCompare(&root, 0) >= 0);- In
Math.c:1243:
result = result && (IntCompare(&check, 4) == 0);
result = result && (IntCompare(&root, 17) < 0);
result = result && (IntCompare(&root, 0) >= 0);- In
Math.c:1244:
result = result && (IntCompare(&check, 4) == 0);
result = result && (IntCompare(&root, 17) < 0);
result = result && (IntCompare(&root, 0) >= 0);
IntDeinit(&value);- In
Math.c:1272:
IntSquareMod(&check, &root, &mod);
result = result && (IntCompare(&check, 3) == 0);
result = result && (IntCompare(&root, 97) < 0);
result = result && (IntCompare(&root, 0) >= 0);- In
Math.c:1273:
result = result && (IntCompare(&check, 3) == 0);
result = result && (IntCompare(&root, 97) < 0);
result = result && (IntCompare(&root, 0) >= 0);
result = result && (IntCompare(&root, 99) != 0);- In
Math.c:1274:
result = result && (IntCompare(&check, 3) == 0);
result = result && (IntCompare(&root, 97) < 0);
result = result && (IntCompare(&root, 0) >= 0);
result = result && (IntCompare(&root, 99) != 0);- In
Math.c:1275:
result = result && (IntCompare(&root, 97) < 0);
result = result && (IntCompare(&root, 0) >= 0);
result = result && (IntCompare(&root, 99) != 0);
IntDeinit(&value);- In
Math.c:1301:
IntSquareMod(&check, &root, &mod);
result = result && (IntCompare(&check, 2) == 0);
result = result && (IntCompare(&root, 257) < 0);
result = result && (IntCompare(&root, 0) >= 0);- In
Math.c:1302:
result = result && (IntCompare(&check, 2) == 0);
result = result && (IntCompare(&root, 257) < 0);
result = result && (IntCompare(&root, 0) >= 0);- In
Math.c:1303:
result = result && (IntCompare(&check, 2) == 0);
result = result && (IntCompare(&root, 257) < 0);
result = result && (IntCompare(&root, 0) >= 0);
IntDeinit(&value);- In
Math.c:1327:
IntSquareMod(&check, &root, &mod);
result = result && (IntCompare(&check, 10) == 0);
result = result && (IntCompare(&root, 13) < 0);- In
Math.c:1328:
result = result && (IntCompare(&check, 10) == 0);
result = result && (IntCompare(&root, 13) < 0);
IntDeinit(&value);- In
Math.c:1350:
bool result = !IntModSqrt(&root, &value, &mod);
result = result && (IntCompare(&root, 99) == 0);
IntDeinit(&value);- In
Math.c:1374:
bool result = IntModSqrt(&root, &value, &mod);
// result overwritten to exactly 0 (not the 99 sentinel).
result = result && (IntCompare(&root, 0) == 0);
IntDeinit(&value);- In
Math.c:1396:
bool result = IntModSqrt(&root, &value, &mod);
result = result && (IntCompare(&root, 0) == 0);
IntDeinit(&value);- In
Math.c:1420:
bool result = IntModSqrt(&root, &value, &mod);
result = result && (IntCompare(&root, 1) == 0);
IntDeinit(&value);- In
Math.c:1443:
bool result = !IntModSqrt(&root, &value, &mod);
result = result && (IntCompare(&root, 99) == 0);
IntDeinit(&value);- In
Math.c:1464:
bool result = !IntModSqrt(&root, &value, &mod);
result = result && (IntCompare(&root, 99) == 0);
IntDeinit(&value);- In
Math.c:1486:
bool result = !IntModSqrt(&root, &value, &mod);
result = result && (IntCompare(&root, 99) == 0);
IntDeinit(&value);- In
Math.c:1514:
// root^2 must equal the *reduced* value 2, not 23.
result = result && (IntCompare(&check, 2) == 0);
result = result && (IntCompare(&root, 7) < 0);- In
Math.c:1515:
// root^2 must equal the *reduced* value 2, not 23.
result = result && (IntCompare(&check, 2) == 0);
result = result && (IntCompare(&root, 7) < 0);
IntDeinit(&value);- In
Math.c:1849:
bool result = IntLCM(&result_value, &a, &b);
result = result && IntIsZero(&result_value);
result = result && (IntCompare(&result_value, 0) == 0);
IntDeinit(&a);- In
Math.c:1957:
bool ok = IntRootRem(&root, &remainder, &value, 3);
bool result = ok;
result = result && (IntCompare(&root, 0) == 0);
result = result && (IntCompare(&remainder, 0) == 0);- In
Math.c:1958:
bool result = ok;
result = result && (IntCompare(&root, 0) == 0);
result = result && (IntCompare(&remainder, 0) == 0);
IntDeinit(&value);- In
Math.c:2134:
bool rejected = !IntPow(&power, &base, &exp);
// Result must be left untouched on rejection.
bool result = rejected && (IntCompare(&power, &sentinel) == 0);
IntDeinit(&base);- In
Math.c:2258:
Int value = IntFrom(5u, &alloc.base);
bool fail = (IntCompare(&value, 5u) != 0);
fail = fail || (IntBitLength(&value) != 3);- In
Math.c:2276:
Int value = IntFrom(1u, &alloc.base);
bool fail = (IntCompare(&value, 1u) != 0);
fail = fail || (IntBitLength(&value) != 1);- In
Math.c:2779:
bool ok = IntDiv("ient, ÷nd, 0u);
bool result = (ok == false);
result = result && (IntCompare("ient, 99) == 0);
IntDeinit(÷nd);- In
Math.c:2797:
bool ok = IntDiv("ient, ÷nd, (i64)0);
bool result = (ok == false);
result = result && (IntCompare("ient, 99) == 0);
IntDeinit(÷nd);- In
Math.c:2989:
bool ok = IntModSqrt(&root, &value, &mod);
IntSquareMod(&check, &root, &mod);
bool result = ok && (IntCompare(&check, 2) == 0);
IntDeinit(&value);- In
Math.c:3344:
bool ok = int_pow_mod(&result_value, &base, &exp, &mod);
ok = ok && (IntCompare(&result_value, 0) == 0);
IntDeinit(&base);- In
Math.c:3365:
bool ok = int_pow_mod(&result_value, &base, &exp, &mod);
ok = ok && (IntCompare(&result_value, 0) == 0);
IntDeinit(&base);- In
Math.c:4733:
bool ok = IntModInv(&inverse, &value, &modulus);
ok = ok && IntCompare(&inverse, 23u) == 0;
IntDeinit(&value);- In
Math.c:4755:
bool ok = IntModSqrt(&root, &value, &modulus);
ok = ok && IntSquareMod(&check, &root, &modulus);
ok = ok && IntCompare(&check, 9u) == 0;
IntDeinit(&value);- In
Math.c:4778:
bool ok = IntModSqrt(&root, &value, &modulus);
ok = ok && IntSquareMod(&check, &root, &modulus);
ok = ok && IntCompare(&check, 2u) == 0;
IntDeinit(&value);- In
Math.c:4799:
// ar >= br branch: 5 - 2 = 3 (mod 7)
Int a1 = IntFrom(5u, alloc), b1 = IntFrom(2u, alloc);
bool ok = IntModSub(&r, &a1, &b1, &m) && IntCompare(&r, 3u) == 0;
// ar < br branch: 2 - 5 = -3 = 4 (mod 7)
Int a2 = IntFrom(2u, alloc), b2 = IntFrom(5u, alloc);- In
Math.c:4802:
// ar < br branch: 2 - 5 = -3 = 4 (mod 7)
Int a2 = IntFrom(2u, alloc), b2 = IntFrom(5u, alloc);
ok = ok && IntModSub(&r, &a2, &b2, &m) && IntCompare(&r, 4u) == 0;
// equal branch: 5 - 12 = 0 (mod 7)
Int a3 = IntFrom(5u, alloc), b3 = IntFrom(12u, alloc);- In
Math.c:4829:
bool ok = IntNextPrime(&next, &value);
ok = ok && IntCompare(&next, 101u) == 0;
IntDeinit(&value);- In
Convert.c:248:
bool test_int_compare_ignores_leading_zeros(void) {
WriteFmt("Testing IntCompare leading-zero normalization\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Convert.c:255:
Int rhs = IntFrom(11, ALLOCATOR_OF(&alloc));
bool result = IntCompare(&lhs, &rhs) == 0;
result = result && IntEQ(&lhs, &rhs);- In
Convert.c:1335:
Int value = IntFromBytesBE((const u8 *)0, 0, &alloc.base);
bool fail = (IntCompare(&value, 0u) != 0);
IntDeinit(&value);- In
Convert.c:1353:
Int value = IntFromBytesBE(bytes, sizeof(bytes), &alloc.base);
bool fail = (IntCompare(&value, 0x010203u) != 0);
IntDeinit(&value);- In
Convert.c:1395:
bool fail = !parsed;
fail = fail || (IntCompare(&out, 5u) != 0);
IntDeinit(&out);- In
Convert.c:1628:
Int parsed = IntFromStrRadix(&hex, 16, alloc);
bool ok = IntCompare(&value, &parsed) == 0;
StrDeinit(&hex);- In
Access.c:217:
bool result = ok;
result = result && (IntToU64(&root) == 12345);
result = result && (IntCompare(&remainder, 0) == 0);
IntDeinit(&value);- In
Access.c:302:
bool fail = !ok; // must report success.
fail = fail || (IntCompare(&value, 7u) != 0); // value preserved.
IntDeinit(&value);- In
Write.c:4408:
Zstr z = "9z";
StrReadFmt(z, "{}", v);
bool ok = (IntCompare(&v, 9) == 0);
IntDeinit(&v);- In
Write.c:4425:
Zstr z = "5";
StrReadFmt(z, "{}", v);
bool ok = (IntCompare(&v, 5) == 0);
IntDeinit(&v);- In
Write.c:4441:
Zstr z = "+8";
StrReadFmt(z, "{}", v);
bool ok = (IntCompare(&v, 8) == 0);
IntDeinit(&v);- In
Write.c:4457:
Zstr z = "9_";
StrReadFmt(z, "{}", v);
bool ok = (IntCompare(&v, 123) == 0);
IntDeinit(&v);- In
Write.c:4474:
Zstr z = "0xFF";
StrReadFmt(z, "{x}", v);
bool ok = (IntCompare(&v, 200) == 0);
IntDeinit(&v);- In
Write.c:4490:
Zstr z = "0XFF";
StrReadFmt(z, "{x}", v);
bool ok = (IntCompare(&v, 200) == 0);
IntDeinit(&v);- In
Write.c:4505:
Zstr z = "ff";
StrReadFmt(z, "{x}", v);
bool ok = (IntCompare(&v, 255) == 0);
IntDeinit(&v);- In
Write.c:4520:
Zstr z = "0b1";
StrReadFmt(z, "{b}", v);
bool ok = (IntCompare(&v, 5) == 0);
IntDeinit(&v);- In
Write.c:4534:
Zstr z = "0B1";
StrReadFmt(z, "{b}", v);
bool ok = (IntCompare(&v, 5) == 0);
IntDeinit(&v);- In
Write.c:4548:
Zstr z = "101";
StrReadFmt(z, "{b}", v);
bool ok = (IntCompare(&v, 5) == 0);
IntDeinit(&v);- In
Write.c:4563:
Zstr z = "0o7";
StrReadFmt(z, "{o}", v);
bool ok = (IntCompare(&v, 9) == 0);
IntDeinit(&v);- In
Write.c:4577:
Zstr z = "0O7";
StrReadFmt(z, "{o}", v);
bool ok = (IntCompare(&v, 9) == 0);
IntDeinit(&v);- In
Write.c:4591:
Zstr z = "17";
StrReadFmt(z, "{o}", v);
bool ok = (IntCompare(&v, 15) == 0);
IntDeinit(&v);- In
Write.c:4606:
Zstr z = " 42";
StrReadFmt(z, "{}", v);
bool ok = (IntCompare(&v, 42) == 0);
IntDeinit(&v);
Last updated on