// =============================================================================
// Create 2 identical uint64[]s, mirror the first 32 bits in the last, sort one
// with qsort and the other with RadixSort11110000() and compare the results
int test_uint64_32_sort(EV_TIME_STR *tsa) {
uint64_t *la1, *la2;
int ii, lcount=3615232, zmem=0, debug=0, ucount=1, ri, er12=0, ep=63;
time_t tt; // This Time!
float rssec=2.0f; // RadixSort_SEConds elapsed time from Radixsort()
time_event(E_GENERATE_RANDOM_ARRAY, tsa, E_TIME_EVENT, 1, 0);
srand((unsigned) time(&tt));
la1=(uint64_t *)alloc_check((lcount)*8, zmem=0, "LLong_ara_1", debug);
la2=(uint64_t *)alloc_check((lcount)*8, zmem=0, "LLong_ara_2", debug);
for(ii=0; ii < lcount; ii++) { // Look only SHORT range
ri = rand(); // Random int
la1[ii] = ri << 32 + (0XFFFFFFFF - ri); // Reflect val in lower 32
}
// Make identical copies in the other []
time_event(E_MEMCPY_DOUBLE_ARRAY, tsa, E_TIME_EVENT, 1, 0);
memcpy((void *) la2, (void *) la1, lcount * sizeof(uint64_t));
time_event(E_QSORT_UINT64_ARRAY, tsa, E_TIME_EVENT, 1, 0);
qsort(la1, lcount, sizeof(uint64_t), comp_uint6411110000);
time_event(E_RADIX_SORT64_11110000, tsa, E_TIME_EVENT, 1, 0);
radixSort11110000(la2, lcount, &rssec, &ucount);
time_event(E_SPLIT_RGBJ_MEM, tsa, E_TIME_EVENT, 1, 0);
for(ii=er12=0; ii < lcount; ii++) {
if(la1[ii] != la2[ii]) {
er12++;
if((--ep) > 0) {
printf("II %d) Er%d, l1=0X%016llX, l2=0X%016llX\n",
ii, ep, la1[ii], la2[ii]); FF_SO; }
} // Count Error Mismatches
if(!(ii%100000)) {
printf("II %d) l1=0X%016llX, l2=0X%016llX\n",
ii, la1[ii], la2[ii]); FF_SO; }
}
printf("T63S: Er1/2 = %d \n", er12); FF_SO;
if(ucount) {
printf("T63S: RadixSort time = %.3f ms, unique=%d = %.3f%%\n",
rssec*1.0E3f, ucount, (100.0f * ucount / lcount)); FF_SO;
}
free_bb(la1); free_bb(la2);
}
// =============================================================================
// Based on original code from https://ideone.com/JHI0d9
// and suggestions from Ian Abbott
// https://stackoverflow.com/questions/47080353/radix-sort-c-code-to-sort-uint64-t-looking-only-at-32-msb-bits
// Hacked code may be unsuitable for any use and should not be used by anyone
// Sort uint64[] by looking ONLY at the
uint64_t *radixSort11110000(uint64_t *arrayA, uint32_t size) {
register uint64_t *array=arrayA; // Slam arg into Register!
register uint64_t std; // STanDard to compare others for uniqueness
register int dist=0; // Distinct, unique values found if *UCount arg is TRUE
register int ii; // Loop control
int64_t rtime, mtns; // Time in NanoSeconds!!!
rscounts4_t counts;
memset(&counts, 0, 256 * 4 * sizeof(uint32_t));
uint64_t * cpy = (uint64_t *)malloc(size * sizeof(uint64_t));
uint32_t o4=0, o3=0, o2=0, o1=0;
uint32_t t4, t3, t2, t1;
register uint32_t x;
// calculate counts
for(x = 0; x < size; x++) {
t4 = (array[x] >> 32) & 0xff;
t3 = (array[x] >> 40) & 0xff;
t2 = (array[x] >> 48) & 0xff;
t1 = (array[x] >> 56) & 0xff;
counts.c4[t4]++;
counts.c3[t3]++;
counts.c2[t2]++;
counts.c1[t1]++;
}
// convert counts to offsets
for(x = 0; x < 256; x++) {
t4 = o4 + counts.c4[x];
t3 = o3 + counts.c3[x];
t2 = o2 + counts.c2[x];
t1 = o1 + counts.c1[x];
counts.c4[x] = o4;
counts.c3[x] = o3;
counts.c2[x] = o2;
counts.c1[x] = o1;
o4 = t4;
o3 = t3;
o2 = t2;
o1 = t1;
}
// radix
for(x = 0; x < size; x++) {
t4 = (array[x] >> 32) & 0xff;
cpy[counts.c4[t4]] = array[x];
counts.c4[t4]++; }
for(x = 0; x < size; x++) {
t3 = (cpy[x] >> 40) & 0xff;
array[counts.c3[t3]] = cpy[x];
counts.c3[t3]++; }
for(x = 0; x < size; x++) {
t2 = (array[x] >> 48) & 0xff;
cpy[counts.c2[t2]] = array[x];
counts.c2[t2]++; }
for(x = 0; x < size; x++) {
t1 = (cpy[x] >> 56) & 0xff;
array[counts.c1[t1]] = cpy[x];
counts.c1[t1]++; }
free(cpy);
return array;
} // End radixSort_11110000().
// #############################################################################
// From: http://ideone.com/JHI0d9
// RadixSort---
typedef union {
struct {
uint32_t c4[256];
uint32_t c3[256];
uint32_t c2[256];
uint32_t c1[256];
};
uint32_t counts[256 * 4];
} rscounts4_t;
// =============================================================================
// Compare only the MSB 4 bytes of a uint64 by masking each with
// 0XFFFFFFFF00000000 before comparison
int comp_uint6411110000(const void *a, const void *b) {
return (
(
( ( *( (uint64_t *)a ) ) & 0XFFFFFFFF00000000ULL ) >
( ( *( (uint64_t *)b ) ) & 0XFFFFFFFF00000000ULL )
)
-
(
( ( *( (uint64_t *)a ) ) & 0XFFFFFFFF00000000ULL ) <
( ( *( (uint64_t *)b ) ) & 0XFFFFFFFF00000000ULL )
)
);
} // End Comp_Uint64_11110000().
// Both sorted arrays were identical.
// T63S: Er1/2 = 0
// TE: Top 90% events in desc order (7/312):
// Time= 0.157 sec = 71.282%, QSORT_UINT64_ARRAY , hits=1
// Time= 0.029 sec = 13.119%, RADIX_SORT64_11110000, hits=1
// Time= 0.026 sec = 11.872%, GENERATE_RANDOM_ARRAY, hits=1
// mult 71.282 /13.119 -> 5.433493
// The RadixSort is over 5x faster that the qsort
Perhaps the QSort comparitor could be handled more efficiently
gcc -Ofast -ffast-math -march=native -m64 -fopenmp -funroll-loops -flto // Time=1.543 sec = 24.289%, Event RADIX_SORT_64 , hits=3, 0.514 sec each
// Time=2.746 sec = 43.212%, Event QSORT_DOUBLE_ARRAY , hits=3, 0.915 sec each
// Time=1.522 sec = 24.120%, Event RADIX_SORT_64 , hits=3, 0.507 sec each
// Time=2.743 sec = 43.454%, Event QSORT_DOUBLE_ARRAY , hits=3, 0.914 sec each
– PatrickPhotog Nov 02 '17 at 21:46