|
| 1 | +#include <stdint.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +extern char __heap_base[]; |
| 6 | +extern char __heap_end[]; |
| 7 | + |
| 8 | +static void* __arena_beg = __heap_base; |
| 9 | +static void* __arena_end = __heap_end; |
| 10 | + |
| 11 | +static void __arena_grow(size_t size) { |
| 12 | + size_t avail = __arena_end - __arena_beg; |
| 13 | + if (size > avail) { |
| 14 | + size_t npages = (size - avail + 0xfffful) >> 16; |
| 15 | + size_t old = __builtin_wasm_memory_grow(0, npages); |
| 16 | + if (old == ~0ul) __builtin_trap(); |
| 17 | + __arena_end += npages << 16; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +__attribute__((always_inline)) void free(void*) {} |
| 22 | + |
| 23 | +__attribute__((malloc)) void* malloc(size_t size) { |
| 24 | + if (size == 0) return NULL; |
| 25 | + size = (size + 15) & ~15; |
| 26 | + void* res = __arena_beg; |
| 27 | + __arena_grow(size); |
| 28 | + __arena_beg += size; |
| 29 | + return res; |
| 30 | +} |
| 31 | + |
| 32 | +__attribute__((malloc)) void* calloc(size_t nelem, size_t elsize) { |
| 33 | + return malloc(nelem * elsize); |
| 34 | +} |
| 35 | + |
| 36 | +__attribute__((malloc)) void* aligned_alloc(size_t align, size_t size) { |
| 37 | + if ((align & -align) != align) __builtin_trap(); |
| 38 | + align = (align - ((uintptr_t)__arena_beg & (align - 1))) & (align - 1); |
| 39 | + return malloc(size + align) + align; |
| 40 | +} |
| 41 | + |
| 42 | +void* realloc(void* ptr, size_t size) { |
| 43 | + size_t copy = __arena_beg - ptr; |
| 44 | + void* res = malloc(size); |
| 45 | + if (ptr != NULL && res != NULL) { |
| 46 | + if (size < copy) copy = size; |
| 47 | + memcpy(res, ptr, copy); |
| 48 | + } |
| 49 | + return res; |
| 50 | +} |
| 51 | + |
| 52 | +__attribute__((noreturn)) void abort(void) { __builtin_trap(); } |
| 53 | + |
| 54 | +// Shellsort with Gonnet & Baeza-Yates gap sequence. |
| 55 | +// Simple, no recursion, doesn't use the C stack. |
| 56 | +// Clang auto-vectorizes the inner loop. |
| 57 | + |
| 58 | +void qsort(void* base, size_t nel, size_t width, |
| 59 | + int (*comp)(const void*, const void*)) { |
| 60 | + // If nel is zero, we're required to do nothing. |
| 61 | + // If it's one, the array is already sorted. |
| 62 | + size_t wnel = width * nel; |
| 63 | + size_t gap = nel; |
| 64 | + while (gap > 1) { |
| 65 | + // Use 64-bit unsigned arithmetic to avoid intermediate overflow. |
| 66 | + // Absent overflow, gap will be strictly less than its previous value. |
| 67 | + // Once it is one or zero, set it to one: do a final pass, and stop. |
| 68 | + gap = (5ull * gap - 1) / 11; |
| 69 | + if (gap == 0) gap = 1; |
| 70 | + |
| 71 | + // It'd be undefined behavior for wnel to overflow a size_t; |
| 72 | + // or if width is zero: the base pointer would be invalid. |
| 73 | + // Since gap is stricly less than nel, we can assume |
| 74 | + // wgap is strictly less than wnel. |
| 75 | + size_t wgap = width * gap; |
| 76 | + __builtin_assume(wgap < wnel); |
| 77 | + for (size_t i = wgap; i < wnel; i += width) { |
| 78 | + // Even without overflow flags, the overflow builtin helps the compiler. |
| 79 | + for (size_t j = i; !__builtin_sub_overflow(j, wgap, &j);) { |
| 80 | + char* a = j + (char*)base; |
| 81 | + char* b = a + wgap; |
| 82 | + if (comp(a, b) <= 0) break; |
| 83 | + |
| 84 | + // This well known loop is automatically vectorized. |
| 85 | + size_t s = width; |
| 86 | + do { |
| 87 | + char tmp = *a; |
| 88 | + *a++ = *b; |
| 89 | + *b++ = tmp; |
| 90 | + } while (--s); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments