File: crypto_sort_uint32.c

package info (click to toggle)
tinyssh 20250501-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,388 kB
  • sloc: ansic: 20,245; sh: 1,582; python: 1,449; makefile: 913
file content (36 lines) | stat: -rw-r--r-- 820 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "crypto_uint32.h"
#include "crypto_sort_uint32.h"

static void minmax(crypto_uint32 *x, crypto_uint32 *y) {

    crypto_uint32 xi = *x;
    crypto_uint32 yi = *y;
    crypto_uint32 xy = xi ^ yi;
    crypto_uint32 c = yi - xi;

    c ^= xy & (c ^ yi ^ 0x80000000);
    c >>= 31;
    c &= 1;
    c = -c;
    c &= xy;
    *x = xi ^ c;
    *y = yi ^ c;
}

void crypto_sort_uint32_tinyssh(void *xv, long long n) {

    long long top, p, q, i;
    crypto_uint32 *x = xv;

    if (n < 2) return;
    top = 1;
    while (top < n - top) top += top;

    for (p = top; p > 0; p >>= 1) {
        for (i = 0; i < n - p; ++i)
            if (!(i & p)) minmax(x + i, x + i + p);
        for (q = top; q > p; q >>= 1)
            for (i = 0; i < n - q; ++i)
                if (!(i & p)) minmax(x + i + p, x + i + q);
    }
}