Skip to content

Commit 3c1577d

Browse files
committed
Add fast comparisons back to bigint
1 parent 2b42ef4 commit 3c1577d

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/big-int/bigint.hh

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,9 @@ public:
221221

222222
// Eliminate need for explicit casts when comparing.
223223

224-
int compare (int n) const { return compare (llong_t (n)); }
225-
int compare (unsigned n) const { return compare (ullong_t (n)); }
224+
int compare (unsigned long n) const { return compare (static_cast<ullong_t>(n)); }
225+
int compare (int n) const { return compare (static_cast<llong_t> (n)); }
226+
int compare (unsigned n) const { return compare (static_cast<ullong_t>(n)); }
226227

227228
// Tests. These are faster than comparing with 0 or of course %2.
228229

@@ -330,4 +331,28 @@ inline bool operator>= (const BigInt &lhs, const BigInt &rhs) { return lhs.compa
330331
inline bool operator== (const BigInt &lhs, const BigInt &rhs) { return lhs.compare(rhs) == 0; }
331332
inline bool operator!= (const BigInt &lhs, const BigInt &rhs) { return lhs.compare(rhs) != 0; }
332333

334+
335+
// These operators are all associative, so we can define them all for
336+
// primitives on the LHS and RHS.
337+
#define COMPARISON_OPERATORS(OTHER) \
338+
inline bool operator< (const BigInt &lhs, OTHER rhs) { return lhs.compare(rhs) < 0; } \
339+
inline bool operator> (const BigInt &lhs, OTHER rhs) { return lhs.compare(rhs) > 0; } \
340+
inline bool operator<= (const BigInt &lhs, OTHER rhs) { return lhs.compare(rhs) <= 0; } \
341+
inline bool operator>= (const BigInt &lhs, OTHER rhs) { return lhs.compare(rhs) >= 0; } \
342+
inline bool operator== (const BigInt &lhs, OTHER rhs) { return lhs.compare(rhs) == 0; } \
343+
inline bool operator!= (const BigInt &lhs, OTHER rhs) { return lhs.compare(rhs) != 0; } \
344+
inline bool operator< (OTHER lhs, const BigInt &rhs) { return -rhs.compare(lhs) < 0; } \
345+
inline bool operator> (OTHER lhs, const BigInt &rhs) { return -rhs.compare(lhs) > 0; } \
346+
inline bool operator<= (OTHER lhs, const BigInt &rhs) { return -rhs.compare(lhs) <= 0; } \
347+
inline bool operator>= (OTHER lhs, const BigInt &rhs) { return -rhs.compare(lhs) >= 0; } \
348+
inline bool operator== (OTHER lhs, const BigInt &rhs) { return -rhs.compare(lhs) == 0; } \
349+
inline bool operator!= (OTHER lhs, const BigInt &rhs) { return -rhs.compare(lhs) != 0; }
350+
351+
COMPARISON_OPERATORS(BigInt::llong_t)
352+
COMPARISON_OPERATORS(BigInt::ullong_t)
353+
COMPARISON_OPERATORS(unsigned long)
354+
COMPARISON_OPERATORS(int)
355+
COMPARISON_OPERATORS(unsigned)
356+
#undef COMPARISON_OPERATORS
357+
333358
#endif//ndef BIGINT_HH

0 commit comments

Comments
 (0)