Introduce spaceship comparison for Processes

If currently two unsigned values are compared via `a - b`, in the case b
is actually bigger than a, the result will not be an negative number (as
-1 is expected) but a huge positive number as the subtraction is an
unsigned subtraction.

Avoid over-/underflow affected operations; use comparisons.
Modern compilers will generate sane code, like:
    xor     eax, eax
    cmp     rdi, rsi
    seta    al
    sbb     eax, 0
    ret
This commit is contained in:
Christian Göttsche
2020-11-04 17:46:24 +01:00
committed by BenBE
parent d785b1bbc3
commit 397b5c4bd0
7 changed files with 111 additions and 84 deletions

View File

@ -220,6 +220,7 @@ void OpenBSDProcess_writeField(const Process* this, RichString* str, ProcessFiel
long OpenBSDProcess_compare(const void* v1, const void* v2) {
const OpenBSDProcess *p1, *p2;
const Settings *settings = ((const Process*)v1)->settings;
if (settings->direction == 1) {
p1 = (const OpenBSDProcess*)v1;
p2 = (const OpenBSDProcess*)v2;
@ -227,6 +228,7 @@ long OpenBSDProcess_compare(const void* v1, const void* v2) {
p2 = (const OpenBSDProcess*)v1;
p1 = (const OpenBSDProcess*)v2;
}
switch (settings->sortKey) {
// add OpenBSD-specific fields here
default: