|
10 | 10 | #include <time.h> |
11 | 11 | #include <assert.h> |
12 | 12 |
|
13 | | -#ifndef HAVE_GLIBC_COMPAR_FN_T |
14 | | -typedef int (*__compar_fn_t)(const void *, const void *); |
15 | | -#endif |
16 | | - |
17 | | -// ----------------------------------------------------------------------------- |
18 | | - |
19 | | -static inline int compare_int64s(const void* a, const void* b); |
20 | | - |
21 | | -static inline int compare_int64s(const void* a, const void* b) |
22 | | -{ |
23 | | - int64_t arg1 = *(const int64_t*)a; |
24 | | - int64_t arg2 = *(const int64_t*)b; |
25 | | - |
26 | | - if (arg1 < arg2) return -1; |
27 | | - if (arg1 > arg2) return 1; |
28 | | - return 0; |
29 | | -} |
30 | | - |
31 | | - |
32 | | -static inline double difftimespecs(struct timespec* t0, struct timespec* t1) |
33 | | -{ |
34 | | - return (double)(t1->tv_sec - t0->tv_sec) |
35 | | - + ((double)(t1->tv_nsec - t0->tv_nsec) / 1000000000.0); |
36 | | -} |
37 | | - |
38 | | -static inline bool prefix(const char *pre, const char *str) |
39 | | -{ |
40 | | - return strncmp(pre, str, strlen(pre)) == 0; |
41 | | -} |
42 | | - |
43 | | -static inline void our_memcpy(void *dst, void *src, size_t nbytes) |
44 | | -{ |
45 | | - memcpy(dst, src, nbytes); |
46 | | -} |
47 | | - |
48 | | -// Taken from glibc. |
49 | | -static inline void SWAP(void *a, void *b, size_t elt_size) |
50 | | -{ |
51 | | - size_t __size = elt_size; |
52 | | - char *__a = (char*)a; |
53 | | - char *__b = (char*)b; |
54 | | - do { |
55 | | - char __tmp = * __a; |
56 | | - *__a++ = *__b; |
57 | | - *__b++ = __tmp; |
58 | | - } while (--__size > 0); |
59 | | -} |
60 | | - |
61 | | -template<typename T> |
62 | | -static inline void SWAP2(T *a, int i, int j) |
63 | | -{ |
64 | | - T _tmp = a[i]; |
65 | | - a[i] = a[j]; |
66 | | - a[j] = _tmp; |
67 | | -} |
68 | | - |
69 | | -// ----------------------------------------------------------------------------- |
70 | | - |
71 | | -typedef struct slice_t_ { |
72 | | - void *base; |
73 | | - size_t total_elems; |
74 | | - size_t elt_size; |
75 | | -} slice_t; |
76 | | - |
77 | | -typedef struct slice_prod_t_ { |
78 | | - slice_t left; |
79 | | - slice_t right; |
80 | | -} slice_prod_t; |
81 | | - |
82 | | -static inline size_t slice_length(const slice_t *sl) |
| 13 | +int64_t* __attribute__ ((noinline)) fill_array_rand_seq(size_t total_elems) |
83 | 14 | { |
84 | | - return sl->total_elems; |
85 | | -} |
86 | | - |
87 | | -static inline void *slice_nth(const slice_t *sl, size_t n) |
88 | | -{ |
89 | | - return (char*) sl->base + (n * sl->elt_size); |
90 | | -} |
91 | | - |
92 | | -static inline void slice_copy(slice_t *src, slice_t *dst) |
93 | | -{ |
94 | | - our_memcpy(dst->base, src->base, (src->total_elems * src->elt_size)); |
95 | | - return; |
96 | | -} |
97 | | - |
98 | | -static inline void slice_inplace_update(slice_t *sl, size_t i, void* elt) { |
99 | | - void* dst = slice_nth(sl, i); |
100 | | - our_memcpy(dst, elt, sl->elt_size); |
101 | | -} |
102 | | - |
103 | | -// "idx" is the start index, "elems" is the length of the new slice. |
104 | | -static inline slice_t slice_narrow(const slice_t *sl, size_t idx, size_t elems) |
105 | | -{ |
106 | | - return (slice_t) { (char*) sl->base + (sl->elt_size * idx), elems, sl->elt_size }; |
107 | | -} |
108 | | - |
109 | | -static inline slice_prod_t slice_split_at(const slice_t *sl, size_t idx) |
110 | | -{ |
111 | | - /* |
112 | | - slice_t left = (slice_t) { sl->base, idx, sl->elt_size }; |
113 | | - slice_t right = (slice_t) { (char*) sl->base + (sl->elt_size * idx), |
114 | | - (sl->total_elems - idx), |
115 | | - sl->elt_size }; |
116 | | - */ |
117 | | - size_t len = slice_length(sl); |
118 | | - slice_t left = slice_narrow(sl, 0, idx); |
119 | | - slice_t right = slice_narrow(sl, idx, len-idx); |
120 | | - return (slice_prod_t) { left, right }; |
121 | | -} |
122 | | - |
123 | | -static inline void slice_print(const slice_t *sl) |
124 | | -{ |
125 | | - printf("["); |
126 | | - char *elt; |
127 | | - for (size_t i = 0; i < sl->total_elems; i++) { |
128 | | - elt = (char*) sl->base + (i * sl->elt_size); |
129 | | - if (sl->elt_size == sizeof(int64_t)) { |
130 | | - printf("%ld, ", *(int64_t*) elt); |
131 | | - } else { |
132 | | - printf("%p, ", (void*) elt); |
133 | | - } |
| 15 | + void *nums; |
| 16 | + nums = (int64_t*) malloc(total_elems * sizeof(int64_t)); |
| 17 | + if (nums == NULL) { |
| 18 | + fprintf(stderr, "Couldn't allocate memory for output array.\n"); |
| 19 | + return NULL; |
134 | 20 | } |
135 | | - printf("]\n"); |
136 | | -} |
137 | | - |
138 | | -static inline void slice_assert_sorted(__compar_fn_t cmp, const slice_t *sl) |
139 | | -{ |
140 | | - size_t len = slice_length(sl); |
141 | | - void *a, *b; |
142 | | - for (size_t i = 0; i < len-1; i++) { |
143 | | - a = slice_nth(sl, i); |
144 | | - b = slice_nth(sl, i+1); |
145 | | - if ((*cmp)(a,b) > 0) { |
146 | | - fprintf(stderr, "Elements at %zu and %zu are not sorted.", i, i+1); |
147 | | - exit(1); |
148 | | - } |
| 21 | + int64_t *elt = (int64_t *) nums; |
| 22 | + for (uint64_t i = 0; i <= total_elems-1; i++) { |
| 23 | + elt = (int64_t*) nums + i; |
| 24 | + *elt = rand(); |
149 | 25 | } |
150 | | - printf("Sorted: OK\n"); |
| 26 | + return (int64_t *) nums; |
151 | 27 | } |
152 | 28 |
|
153 | 29 | static inline void slice_assert_sorted_2(int64_t *arr, int size) |
|
0 commit comments