|
| 1 | +// Generated from DISC_CPU/src/msort.cpp and DISC_CPU/src/msort_dataset.h. |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <cstddef> |
| 5 | +#include <limits> |
| 6 | + |
| 7 | +#ifndef PREALLOCATE |
| 8 | +#define PREALLOCATE 0 |
| 9 | +#endif |
| 10 | + |
| 11 | +using type = unsigned int; |
| 12 | + |
| 13 | +unsigned int stack_space[256] __asm__("stack_space") __attribute__((aligned(16), used)); |
| 14 | + |
| 15 | +extern "C" __attribute__((naked, section(".text.startup"))) void _start(void) { |
| 16 | + __asm__ volatile( |
| 17 | + "la gp, __global_pointer$\n" |
| 18 | + "la sp, stack_space\n" |
| 19 | + "addi sp, sp, 1024\n" |
| 20 | + "jal ra, main\n" |
| 21 | + "ebreak\n" |
| 22 | + "1: j 1b\n" |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +static inline void setStats(int enable) { |
| 27 | + if (enable) { |
| 28 | + __asm__ volatile(".global stat_start\nstat_start:"); |
| 29 | + } else { |
| 30 | + __asm__ volatile(".global stat_end\nstat_end:"); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +template <typename T> |
| 35 | +static inline void printArray(const char name[], int n, const T arr[]) { |
| 36 | + (void)name; |
| 37 | + (void)n; |
| 38 | + (void)arr; |
| 39 | +} |
| 40 | + |
| 41 | +template <typename T> |
| 42 | +static inline int verify(int n, const volatile T* test, const T* expected) { |
| 43 | + for (int i = 0; i < (n / 2) * 2; i += 2) { |
| 44 | + T t0 = test[i]; |
| 45 | + T t1 = test[i + 1]; |
| 46 | + T e0 = expected[i]; |
| 47 | + T e1 = expected[i + 1]; |
| 48 | + if (t0 != e0) { |
| 49 | + return i + 1; |
| 50 | + } |
| 51 | + if (t1 != e1) { |
| 52 | + return i + 2; |
| 53 | + } |
| 54 | + } |
| 55 | + if ((n & 1) != 0 && test[n - 1] != expected[n - 1]) { |
| 56 | + return n; |
| 57 | + } |
| 58 | + return 0; |
| 59 | +} |
| 60 | + |
| 61 | +static constexpr int DATA_SIZE = 100; |
| 62 | + |
| 63 | +static type input_data[DATA_SIZE] = { |
| 64 | + 179, 968, 116, 259, 844, 769, 182, 1002, 1011, 856, 392, 36, 383, 959, 527, 275, 512, |
| 65 | + 874, 851, 592, 238, 608, 930, 457, 0, 234, 563, 168, 844, 513, 886, 730, 767, 159, 743, |
| 66 | + 657, 970, 139, 518, 686, 272, 222, 940, 569, 492, 393, 304, 70, 766, 148, 363, 478, 236, |
| 67 | + 841, 480, 258, 321, 262, 110, 192, 602, 351, 855, 125, 105, 136, 996, 687, 27, 26, 527, |
| 68 | + 531, 576, 826, 567, 469, 391, 537, 388, 759, 325, 819, 744, 668, 69, 1011, 344, 264, 132, |
| 69 | + 439, 565, 703, 719, 643, 556, 601, 596, 27, 26, 783, |
| 70 | +}; |
| 71 | + |
| 72 | +static type verify_data[DATA_SIZE] = { |
| 73 | + 0, 26, 26, 27, 27, 36, 69, 70, 105, 110, 116, 125, 132, 136, 139, 148, 159, 168, 179, 182, |
| 74 | + 192, 222, 234, 236, 238, 258, 259, 262, 264, 272, 275, 304, 321, 325, 344, 351, 363, 383, |
| 75 | + 388, 391, 392, 393, 439, 457, 469, 478, 480, 492, 512, 513, 518, 527, 527, 531, 537, 556, |
| 76 | + 563, 565, 567, 569, 576, 592, 596, 601, 602, 608, 643, 657, 668, 686, 687, 703, 719, 730, |
| 77 | + 743, 744, 759, 766, 767, 769, 783, 819, 826, 841, 844, 844, 851, 855, 856, 874, 886, 930, |
| 78 | + 940, 959, 968, 970, 996, 1002, 1011, 1011, |
| 79 | +}; |
| 80 | + |
| 81 | +static constexpr type kInf = std::numeric_limits<type>::max(); |
| 82 | + |
| 83 | +static void sort(std::size_t n, type arr_in[], type scratch_in[]) { |
| 84 | + type* a = arr_in; |
| 85 | + type* b = scratch_in; |
| 86 | + |
| 87 | + for (std::size_t i = 1; i < n; i <<= 1) { |
| 88 | + std::swap(a, b); |
| 89 | + |
| 90 | + for (std::size_t j = 0; j < n; j += (i << 1)) { |
| 91 | + std::size_t l_end = std::min(j + i, n); |
| 92 | + std::size_t r_end = std::min(j + (i << 1), n); |
| 93 | + |
| 94 | + for (std::size_t l = j, r = l_end, k = j; l < l_end || r < r_end; ++k) { |
| 95 | + type v0 = (l < l_end) ? b[l] : kInf; |
| 96 | + type v1 = (r < r_end) ? b[r] : kInf; |
| 97 | + |
| 98 | + if (v0 <= v1) { |
| 99 | + a[k] = v0; |
| 100 | + ++l; |
| 101 | + } else { |
| 102 | + a[k] = v1; |
| 103 | + ++r; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (a != arr_in) { |
| 110 | + for (std::size_t i = 0; i < n; ++i) { |
| 111 | + arr_in[i] = a[i]; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +extern "C" int main() { |
| 117 | + static type scratch[DATA_SIZE]; |
| 118 | + |
| 119 | + printArray("input", DATA_SIZE, input_data); |
| 120 | + printArray("verify", DATA_SIZE, verify_data); |
| 121 | + |
| 122 | +#if PREALLOCATE |
| 123 | + sort(DATA_SIZE, verify_data, scratch); |
| 124 | + if (verify(DATA_SIZE, input_data, input_data)) { |
| 125 | + return 1; |
| 126 | + } |
| 127 | +#endif |
| 128 | + |
| 129 | + setStats(1); |
| 130 | + sort(DATA_SIZE, input_data, scratch); |
| 131 | + setStats(0); |
| 132 | + |
| 133 | + printArray("test", DATA_SIZE, input_data); |
| 134 | + return verify(DATA_SIZE, input_data, verify_data); |
| 135 | +} |
0 commit comments