Skip to content

Commit 397668e

Browse files
committed
Deduplicate uint* comparison operator logic
1 parent df9eb5e commit 397668e

File tree

1 file changed

+20
-69
lines changed

1 file changed

+20
-69
lines changed

src/uint256.h

Lines changed: 20 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -308,85 +308,28 @@ class base_uint
308308
return ret;
309309
}
310310

311-
312-
friend inline bool operator<(const base_uint& a, const base_uint& b)
313-
{
314-
for (int i = base_uint::WIDTH-1; i >= 0; i--)
315-
{
316-
if (a.pn[i] < b.pn[i])
317-
return true;
318-
else if (a.pn[i] > b.pn[i])
319-
return false;
320-
}
321-
return false;
322-
}
323-
324-
friend inline bool operator<=(const base_uint& a, const base_uint& b)
325-
{
326-
for (int i = base_uint::WIDTH-1; i >= 0; i--)
327-
{
328-
if (a.pn[i] < b.pn[i])
329-
return true;
330-
else if (a.pn[i] > b.pn[i])
331-
return false;
311+
int CompareTo(const base_uint& b) const {
312+
for (int i = base_uint::WIDTH-1; i >= 0; i--) {
313+
if (pn[i] < b.pn[i])
314+
return -1;
315+
if (pn[i] > b.pn[i])
316+
return 1;
332317
}
333-
return true;
334-
}
335-
336-
friend inline bool operator>(const base_uint& a, const base_uint& b)
337-
{
338-
for (int i = base_uint::WIDTH-1; i >= 0; i--)
339-
{
340-
if (a.pn[i] > b.pn[i])
341-
return true;
342-
else if (a.pn[i] < b.pn[i])
343-
return false;
344-
}
345-
return false;
318+
return 0;
346319
}
347320

348-
friend inline bool operator>=(const base_uint& a, const base_uint& b)
349-
{
350-
for (int i = base_uint::WIDTH-1; i >= 0; i--)
351-
{
352-
if (a.pn[i] > b.pn[i])
353-
return true;
354-
else if (a.pn[i] < b.pn[i])
321+
bool EqualTo(uint64_t b) const {
322+
for (int i = base_uint::WIDTH-1; i >= 2; i--) {
323+
if (pn[i])
355324
return false;
356325
}
357-
return true;
358-
}
359-
360-
friend inline bool operator==(const base_uint& a, const base_uint& b)
361-
{
362-
for (int i = 0; i < base_uint::WIDTH; i++)
363-
if (a.pn[i] != b.pn[i])
364-
return false;
365-
return true;
366-
}
367-
368-
friend inline bool operator==(const base_uint& a, uint64_t b)
369-
{
370-
if (a.pn[0] != (unsigned int)b)
326+
if (pn[1] != (b >> 32))
371327
return false;
372-
if (a.pn[1] != (unsigned int)(b >> 32))
328+
if (pn[0] != (b & 0xfffffffful))
373329
return false;
374-
for (int i = 2; i < base_uint::WIDTH; i++)
375-
if (a.pn[i] != 0)
376-
return false;
377330
return true;
378331
}
379332

380-
friend inline bool operator!=(const base_uint& a, const base_uint& b)
381-
{
382-
return (!(a == b));
383-
}
384-
385-
friend inline bool operator!=(const base_uint& a, uint64_t b)
386-
{
387-
return (!(a == b));
388-
}
389-
390333
friend inline const base_uint operator+(const base_uint& a, const base_uint& b) { return base_uint(a) += b; }
391334
friend inline const base_uint operator-(const base_uint& a, const base_uint& b) { return base_uint(a) -= b; }
392335
friend inline const base_uint operator*(const base_uint& a, const base_uint& b) { return base_uint(a) *= b; }
@@ -397,6 +340,14 @@ class base_uint
397340
friend inline const base_uint operator>>(const base_uint& a, int shift) { return base_uint(a) >>= shift; }
398341
friend inline const base_uint operator<<(const base_uint& a, int shift) { return base_uint(a) <<= shift; }
399342
friend inline const base_uint operator*(const base_uint& a, uint32_t b) { return base_uint(a) *= b; }
343+
friend inline bool operator==(const base_uint& a, const base_uint& b) { return a.CompareTo(b) == 0; }
344+
friend inline bool operator!=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) != 0; }
345+
friend inline bool operator>(const base_uint& a, const base_uint& b) { return a.CompareTo(b) > 0; }
346+
friend inline bool operator<(const base_uint& a, const base_uint& b) { return a.CompareTo(b) < 0; }
347+
friend inline bool operator>=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) >= 0; }
348+
friend inline bool operator<=(const base_uint& a, const base_uint& b) { return a.CompareTo(b) <= 0; }
349+
friend inline bool operator==(const base_uint& a, uint64_t b) { return a.EqualTo(b); }
350+
friend inline bool operator!=(const base_uint& a, uint64_t b) { return !a.EqualTo(b); }
400351

401352
std::string GetHex() const
402353
{

0 commit comments

Comments
 (0)