Skip to content

Commit

Permalink
Add more sorting algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Apr 27, 2023
1 parent b7bf052 commit 7c9ef92
Show file tree
Hide file tree
Showing 17 changed files with 49,004 additions and 5 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ include libc/time/time.mk # │
include libc/stdio/stdio.mk #
include third_party/libcxx/libcxx.mk #
include net/net.mk #
include third_party/vqsort/vqsort.mk #
include libc/log/log.mk #
include third_party/bzip2/bzip2.mk #
include dsp/core/core.mk #
Expand Down
1 change: 1 addition & 0 deletions examples/examples.mk
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ EXAMPLES_DIRECTDEPS = \
THIRD_PARTY_SED \
THIRD_PARTY_STB \
THIRD_PARTY_TR \
THIRD_PARTY_VQSORT \
THIRD_PARTY_XED \
THIRD_PARTY_ZLIB \
TOOL_BUILD_LIB \
Expand Down
25 changes: 25 additions & 0 deletions examples/vqsort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
│ To the extent possible under law, Justine Tunney has waived │
│ all copyright and related or neighboring rights to this file, │
│ as it is written in the following disclaimers: │
│ • http://unlicense.org/ │
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
╚─────────────────────────────────────────────────────────────────*/
#endif
#include "third_party/vqsort/vqsort.h"
#include "libc/macros.internal.h"
#include "libc/stdio/stdio.h"
#include "third_party/vqsort/vqsort.h"

// how to sort one gigabyte of 64-bit integers per second

int main(int argc, char *argv[]) {
int64_t A[] = {9, 3, -3, 5, 23, 7};
vqsort_int64(A, ARRAYLEN(A));
for (int i = 0; i < ARRAYLEN(A); ++i) {
if (i) printf(" ");
printf("%ld", A[i]);
}
printf("\n");
}
3 changes: 3 additions & 0 deletions libc/mem/alg.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ int _tarjan(int, const int (*)[2], int, int[], int[], int *)
char *_replacestr(const char *, const char *, const char *)
paramsnonnull() __algalloc;

bool radix_sort_int32(int32_t *, size_t);
bool radix_sort_int64(int64_t *, size_t);

COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ALG_ALG_H_ */
101 changes: 101 additions & 0 deletions libc/mem/radix_sort_int32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2023 Justine Alexandra Roberts Tunney │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/mem/alg.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"

// Credit: Andrew Schein. 2009. Open-source C++ implementation of Radix
// Sort for double-precision floating points. (2009).

#define HIST_SIZE (size_t)2048
#define get_byte_0(v) ((v)&0x7FF)
#define get_byte_1(v) (((v) >> 11) & 0x7FF)
#define get_byte_2_flip_sign(v) (((unsigned)(v) >> 22) ^ 0x200)

bool radix_sort_int32(int32_t *A, size_t n) {
int32_t *T, *reader, *writer;
size_t i, pos, sum0, sum1, sum2, tsum, *b0, *b1, *b2;

if (n < HIST_SIZE) {
_intsort(A, n);
return true;
}

if (!(T = (int32_t *)malloc(n * sizeof(int32_t)))) {
return false;
}

if (!(b0 = (size_t *)calloc(HIST_SIZE * 3, sizeof(size_t)))) {
free(T);
return false;
}

b1 = b0 + HIST_SIZE;
b2 = b1 + HIST_SIZE;

for (i = 0; i < n; i++) {
b0[get_byte_0(A[i])]++;
b1[get_byte_1(A[i])]++;
b2[get_byte_2_flip_sign(A[i])]++;
}

sum0 = sum1 = sum2 = tsum = 0;

for (i = 0; i < HIST_SIZE; i++) {
tsum = b0[i] + sum0;
b0[i] = sum0 - 1;
sum0 = tsum;

tsum = b1[i] + sum1;
b1[i] = sum1 - 1;
sum1 = tsum;

tsum = b2[i] + sum2;
b2[i] = sum2 - 1;
sum2 = tsum;
}

writer = T;
reader = A;
for (i = 0; i < n; i++) {
pos = get_byte_0(reader[i]);
writer[++b0[pos]] = reader[i];
}

writer = A;
reader = T;
for (i = 0; i < n; i++) {
pos = get_byte_1(reader[i]);
writer[++b1[pos]] = reader[i];
}

writer = T;
reader = A;
for (i = 0; i < n; i++) {
pos = get_byte_2_flip_sign(reader[i]);
writer[++b2[pos]] = reader[i];
}

memcpy(A, T, n * sizeof(int));

free(b0);
free(T);
return true;
}
144 changes: 144 additions & 0 deletions libc/mem/radix_sort_int64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2023 Justine Alexandra Roberts Tunney │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/mem/alg.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"

// Credit: Andrew Schein. 2009. Open-source C++ implementation of Radix
// Sort for double-precision floating points. (2009).

#define HIST_SIZE (size_t)2048
#define get_byte_0(v) ((v)&0x7FF)
#define get_byte_1(v) (((v) >> 11) & 0x7FF)
#define get_byte_2(v) (((v) >> 22) & 0x7FF)
#define get_byte_3(v) (((v) >> 33) & 0x7FF)
#define get_byte_4(v) (((v) >> 44) & 0x7FF)
#define get_byte_5(v) (((v) >> 55) & 0x7FF)
#define get_byte_2_flip_sign(v) (((unsigned)(v) >> 22) ^ 0x200)
#define get_byte_5_flip_sign(v) ((((v) >> 55) & 0x7FF) ^ 0x400)

bool radix_sort_int64(int64_t *A, size_t n) {
int64_t *T, *reader, *writer;
size_t *b0, *b1, *b2, *b3, *b4, *b5;
size_t i, pos, sum0, sum1, sum2, sum3, sum4, sum5, tsum;

if (n < HIST_SIZE) {
_longsort(A, n);
return true;
}

if (!(T = (int64_t *)malloc(n * sizeof(int64_t)))) {
return false;
}

if (!(b0 = (size_t *)calloc(HIST_SIZE * 6, sizeof(size_t)))) {
free(T);
return false;
}

b1 = b0 + HIST_SIZE;
b2 = b1 + HIST_SIZE;
b3 = b2 + HIST_SIZE;
b4 = b3 + HIST_SIZE;
b5 = b4 + HIST_SIZE;

for (i = 0; i < n; i++) {
b0[get_byte_0(A[i])]++;
b1[get_byte_1(A[i])]++;
b2[get_byte_2(A[i])]++;
b3[get_byte_3(A[i])]++;
b4[get_byte_4(A[i])]++;
b5[get_byte_5_flip_sign(A[i])]++;
}

sum0 = sum1 = sum2 = sum3 = sum4 = sum5 = tsum = 0;

for (i = 0; i < HIST_SIZE; i++) {
tsum = b0[i] + sum0;
b0[i] = sum0 - 1;
sum0 = tsum;

tsum = b1[i] + sum1;
b1[i] = sum1 - 1;
sum1 = tsum;

tsum = b2[i] + sum2;
b2[i] = sum2 - 1;
sum2 = tsum;

tsum = b3[i] + sum3;
b3[i] = sum3 - 1;
sum3 = tsum;

tsum = b4[i] + sum4;
b4[i] = sum4 - 1;
sum4 = tsum;

tsum = b5[i] + sum5;
b5[i] = sum5 - 1;
sum5 = tsum;
}

writer = T;
reader = A;
for (i = 0; i < n; i++) {
pos = get_byte_0(reader[i]);
writer[++b0[pos]] = reader[i];
}

writer = A;
reader = T;
for (i = 0; i < n; i++) {
pos = get_byte_1(reader[i]);
writer[++b1[pos]] = reader[i];
}

writer = T;
reader = A;
for (i = 0; i < n; i++) {
pos = get_byte_2(reader[i]);
writer[++b2[pos]] = reader[i];
}

writer = A;
reader = T;
for (i = 0; i < n; i++) {
pos = get_byte_3(reader[i]);
writer[++b3[pos]] = reader[i];
}

writer = T;
reader = A;
for (i = 0; i < n; i++) {
pos = get_byte_4(reader[i]);
writer[++b4[pos]] = reader[i];
}

writer = A;
reader = T;
for (i = 0; i < n; i++) {
pos = get_byte_5_flip_sign(reader[i]);
writer[++b5[pos]] = reader[i];
}

free(b0);
free(T);
return true;
}
Loading

0 comments on commit 7c9ef92

Please sign in to comment.