ref: 107f35bf07dd8c88468e07d7af8935e2947a7e2d merge-asm/main.c -rw-r--r-- 11.7 KiB View raw
                                                                                
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#define N 1024 * 1024 * 32
#define time struct timespec
#define RAND() (rand() % (N * 4))

static uint64_t XS[N];
static uint64_t YS[N];
static uint64_t ZS[2 * N];
static uint64_t TRUTH[2 * N];

void asm_nb_rev(uint64_t *, size_t, uint64_t *, size_t, uint64_t *, size_t);
void branching(uint64_t *, size_t, uint64_t *, size_t, uint64_t *, size_t);

// TODO: read and inline this into `take_time`.
time diff(time start, time end) {
  time temp;
  if ((end.tv_nsec - start.tv_nsec) < 0) {
    temp.tv_sec = end.tv_sec - start.tv_sec - 1;
    temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
  } else {
    temp.tv_sec = end.tv_sec - start.tv_sec;
    temp.tv_nsec = end.tv_nsec - start.tv_nsec;
  }
  return temp;
}

// Comparison for `qsort`
int uint64_t_cmp(const void *a, const void *b) {
  return *((uint64_t *)a) - *((uint64_t *)b);
}

// Generate the XS and YS.
void prepare(int seed) {
  srand(seed);
  for (int i = 0; i < N; i++) {
    XS[i] = RAND();
    YS[i] = RAND();
  }
  qsort(XS, N, 8, uint64_t_cmp);
  qsort(YS, N, 8, uint64_t_cmp);
}

// C translation of the branching version.
void branching(uint64_t *xs, size_t xmax, uint64_t *ys, size_t ymax,
               uint64_t *zs, size_t zmax) {
  size_t i = 0, j = 0, k = 0;
  while (k < zmax) {
    if (xs[i] < ys[j]) {
      zs[k++] = xs[i++];
      if (i == xmax) { // x_done
        memcpy(zs + k, ys + j, 8 * (zmax - k));
        return;
      }
    } else {
      zs[k++] = ys[j++];
      if (j == ymax) { // y_done
        memcpy(zs + k, xs + i, 8 * (zmax - k));
        return;
      }
    }
  }
  // z_done
}

uint64_t one_if_lt(int64_t a, int64_t b) {
  if (a < b)
    return 1;
  return 0;
}

uint64_t min(uint64_t a, uint64_t b) { return a <= b ? a : b; }

// C translation of the non-branching version.
void nonbranching_but_branching(uint64_t *xs, size_t xmax, uint64_t *ys,
                                size_t ymax, uint64_t *zs, size_t zmax) {
  size_t i = 0, j = 0, k = 0;
  uint64_t xi = xs[i], yj = ys[j];
  while ((i < xmax) && (j < ymax) && (k < zmax)) {
    int64_t t = one_if_lt(xi, yj);
    yj = min(xi, yj);
    zs[k] = yj;
    i += t;
    xi = xs[i];
    t ^= 1; // 1 if y < x
    j += t;
    yj = ys[j];
    k += 1;
  }
  if (i == xmax)
    memcpy(zs + k, ys + j, 8 * (zmax - k));
  if (j == ymax)
    memcpy(zs + k, xs + i, 8 * (zmax - k));
}

void nonbranching(uint64_t *xs, size_t xmax, uint64_t *ys, size_t ymax,
                  uint64_t *zs, size_t zmax) {
  size_t i = 0, j = 0, k = 0;
  uint64_t xi = xs[i], yj = ys[j];
  while ((i < xmax) && (j < ymax) && (k < zmax)) {
    // 1 if x < y, 0 otherwise
    uint64_t neg = (xi - yj) >> 63;
    yj = neg * xi + (1 - neg) * yj;
    zs[k] = yj;
    i += neg;
    xi = xs[i];
    neg ^= 1; // 1 if y < x
    j += neg;
    yj = ys[j];
    k += 1;
  }
  if (i == xmax)
    memcpy(zs + k, ys + j, 8 * (zmax - k));
  if (j == ymax)
    memcpy(zs + k, xs + i, 8 * (zmax - k));
}

// "Reversed" indices: point to the first element outside of the array
// and have the indices be negative.
void nonbranching_but_branching_reverse(uint64_t *xs, size_t xmax, uint64_t *ys,
                                        size_t ymax, uint64_t *zs,
                                        size_t zmax) {
  uint64_t *xse = xs + xmax;
  uint64_t *yse = ys + ymax;
  uint64_t *zse = zs + zmax;

  ssize_t i = -((ssize_t)xmax);
  ssize_t j = -((ssize_t)ymax);
  ssize_t k = -((ssize_t)zmax);

  uint64_t xi = xse[i], yj = yse[j];
  while (i & j & k) {
    uint64_t t = one_if_lt(xi, yj);
    yj = min(xi, yj);
    zse[k] = yj;
    i += t;
    xi = xse[i];
    t ^= 1; // 1 if y < x
    j += t;
    yj = yse[j];
    k += 1;
  }
  if (i == 0)
    memcpy(zse + k, yse + j, -8 * k);
  if (j == 0)
    memcpy(zse + k, xse + i, -8 * k);
}

void nonbranching_reverse(uint64_t *xs, size_t xmax, uint64_t *ys, size_t ymax,
                          uint64_t *zs, size_t zmax) {
  uint64_t *xse = xs + xmax;
  uint64_t *yse = ys + ymax;
  uint64_t *zse = zs + zmax;

  ssize_t i = -((ssize_t)xmax);
  ssize_t j = -((ssize_t)ymax);
  ssize_t k = -((ssize_t)zmax);

  uint64_t xi = xse[i], yj = yse[j];
  while (i & j & k) {
    uint64_t neg = (xi - yj) >> 63;
    yj = neg * xi + (1 - neg) * yj;
    zse[k] = yj;
    i += neg;
    xi = xse[i];
    neg ^= 1;
    j += neg;
    yj = yse[j];
    k += 1;
  }
  if (i == 0)
    memcpy(zse + k, yse + j, -8 * k);
  if (j == 0)
    memcpy(zse + k, xse + i, -8 * k);
}

void nonbranching_reverse_ternary(uint64_t *xs, size_t xmax, uint64_t *ys,
                                  size_t ymax, uint64_t *zs, size_t zmax) {
  uint64_t *xse = xs + xmax;
  uint64_t *yse = ys + ymax;
  uint64_t *zse = zs + zmax;

  ssize_t i = -((ssize_t)xmax);
  ssize_t j = -((ssize_t)ymax);
  ssize_t k = -((ssize_t)zmax);

  uint64_t xi = xse[i], yj = yse[j];
  while (i & j & k) {
    uint64_t ybig = (xi - yj) >> 63;
    yj = ybig ? xi : yj;
    zse[k] = yj;
    i += ybig;
    xi = xse[i];
    ybig ^= 1;
    j += ybig;
    yj = yse[j];
    k += 1;
  }
  if (i == 0)
    memcpy(zse + k, yse + j, -8 * k);
  if (j == 0)
    memcpy(zse + k, xse + i, -8 * k);
}

void asm_nb_rev(uint64_t *xs, size_t xmax, uint64_t *ys, size_t ymax,
                uint64_t *zs, size_t zmax) {
  uint64_t *xse = xs + xmax;
  uint64_t *yse = ys + ymax;
  uint64_t *zse = zs + zmax;

  ssize_t i = -((ssize_t)xmax);
  ssize_t j = -((ssize_t)ymax);
  ssize_t k = -((ssize_t)zmax);
  uint64_t xi = xse[i];
  uint64_t yj = yse[j];

  int64_t minxy;
  int64_t iinc;
  int64_t one = 1;
  int64_t u;

  asm("1: mov   %[minxy], %[yj];"
      "cmp   %[xi], %[yj];"
      "cmovl %[minxy], %[xi];"
      "mov qword ptr [%[zse]+8*%[k]], %[minxy];"
      "mov %[iinc], 0;"
      "cmovl %[iinc], %[one];"
      "add %[i], %[iinc];"
      "mov %[xi], qword ptr [%[xse]+8*%[i]];"
      "xor %[iinc], 1;"
      "add %[j], %[iinc];"
      "mov %[yj], qword ptr [%[yse]+8*%[j]];"
      "add %[k], 1;"
      "mov %[u], %[i];"
      "and %[u], %[j];"
      "test %[u], %[k];"
      "jnz 1b;"
      : [minxy] "=&r"(minxy), [xi] "+&r"(xi), [yj] "+&r"(yj),
        [iinc] "=&r"(iinc), [i] "+&r"(i), [j] "+&r"(j), [k] "+&r"(k),
        [u] "=&r"(u), [zse] "+&r"(zse)
      : [xse] "r"(xse), [yse] "r"(yse), [one] "r"(one)
      : "memory");

  if (i == 0)
    memcpy(zse + k, yse + j, -8 * k);
  if (j == 0)
    memcpy(zse + k, xse + i, -8 * k);
}

#define TIME(fun)                                                              \
  {                                                                            \
    time time1, time2;                                                         \
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);                           \
    fun;                                                                       \
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);                           \
    time d = diff(time1, time2);                                               \
    printf("%zu.%09zu\n", d.tv_sec, d.tv_nsec);                                \
  }

// Check that the resuling ZS is the same as TRUTH. If not, print of everything.
void check(void) {
  for (int i = 0; i < 2 * N; i++) {
    if (TRUTH[i] != ZS[i]) {
      printf("Mismatch!\n");
      printf("XS:\n");
      for (int i = 0; i < N; i++)
        printf("%4zu ", XS[i]);
      printf("\n");
      printf("YS:\n");
      for (int i = 0; i < N; i++)
        printf("%4zu ", YS[i]);
      printf("\n");
      printf("ZS:\n");
      for (int i = 0; i < 2 * N; i++)
        printf("%4zu ", ZS[i]);
      printf("\n");
      break;
    }
  }
}

#define SORT(variant)                                                \
void sort_##variant##2(uint64_t *xs, size_t n, uint64_t *buf, int *into_buf) {\
  switch (n) {                                                       \
  case 1:                                                            \
    *buf = *xs;                                                      \
  case 0:                                                            \
    *into_buf = 1;                                                   \
    return;                                                          \
  }                                                                  \
  size_t h = n / 2;                                                  \
  int a_ib, b_ib;                                                    \
  sort_##variant##2(xs, h, buf, &a_ib);                              \
  sort_##variant##2(xs + h, n - h, buf + h, &b_ib);                  \
  *into_buf = a_ib ^ 1;                                              \
  if (a_ib == 1) {                                                   \
    variant(buf, h, buf + h, n - h, xs, n);                          \
  } else {                                                           \
    variant(xs, h, xs + h, n - h, buf, n);                           \
  }                                                                  \
}                                                                    \
                                                                     \
void sort_##variant(uint64_t *xs, size_t n, uint64_t *buf) {       \
  int buf_contains_result;                                           \
  sort_##variant##2(xs, n, buf, &buf_contains_result);               \
  if (buf_contains_result) {                                         \
    memcpy(xs, buf, 8 * n);                                          \
  }                                                                  \
}

SORT(branching)
SORT(nonbranching_but_branching)
SORT(nonbranching)
SORT(nonbranching_but_branching_reverse)
SORT(nonbranching_reverse)
SORT(nonbranching_reverse_ternary)
SORT(asm_nb_rev)

#define TIME_SORT(v)\
  memset(ZS, 0, 8 * 2 * N);\
  printf("%s: ", #v);\
  TIME(v(XS, N, ZS));\
  for (int i = 0; i < N; i++) XS[i] = YS[i];

void sort_time(void) {
  for (int i = 0; i < 10; i++) {
    srand(i);
    for (int i = 0; i < N; i++) {
      XS[i] = RAND();
      YS[i] = XS[i];
    }

    TIME_SORT(sort_branching)
    TIME_SORT(sort_nonbranching_but_branching)
    TIME_SORT(sort_nonbranching)
    TIME_SORT(sort_nonbranching_but_branching_reverse)
    TIME_SORT(sort_nonbranching_reverse)
    TIME_SORT(sort_nonbranching_reverse_ternary)
    TIME_SORT(sort_asm_nb_rev)
  }
}

int main(void) {

  // sort_time();
  // return 0;

  fprintf(stderr, "Generating numbers and sorting them\n");
  prepare(0);
  fprintf(stderr, "Computing TRUTH:\n");
  branching(XS, N, YS, N, ZS, 2 * N);
  memcpy(TRUTH, ZS, 8 * 2 * N);

  fprintf(stderr, "Running:\n");
  for (int s = 0; s < 10; s++) {
    fprintf(stderr, "  seed=%d:\n", s);
    prepare(s);
    branching(XS, N, YS, N, ZS, 2 * N);
    memcpy(TRUTH, ZS, 8 * 2 * N);
    for (int i = 0; i < 10; i++) {

      memset(ZS, 0, 8 * 2 * N);
      printf("branching:                          ");
      TIME(branching(XS, N, YS, N, ZS, 2 * N));
      check();

      memset(ZS, 0, 8 * 2 * N);
      printf("nonbranching_but_branching:         ");
      TIME(nonbranching_but_branching(XS, N, YS, N, ZS, 2 * N));
      check();

      memset(ZS, 0, 8 * 2 * N);
      printf("nonbranching:                       ");
      TIME(nonbranching(XS, N, YS, N, ZS, 2 * N));
      check();

      memset(ZS, 0, 8 * 2 * N);
      printf("nonbranching_but_branching_reverse: ");
      TIME(nonbranching_but_branching_reverse(XS, N, YS, N, ZS, 2 * N));
      check();

      memset(ZS, 0, 8 * 2 * N);
      printf("nonbranching_reverse:               ");
      TIME(nonbranching_reverse(XS, N, YS, N, ZS, 2 * N));
      check();

      memset(ZS, 0, 8 * 2 * N);
      printf("nonbranching_reverse_ternary:       ");
      TIME(nonbranching_reverse_ternary(XS, N, YS, N, ZS, 2 * N));
      check();

      memset(ZS, 0, 8 * 2 * N);
      printf("asm_nb_rev:                         ");
      TIME(asm_nb_rev(XS, N, YS, N, ZS, 2 * N));
      check();
    }
  }

  return 0;
}