+\r
+\r
+typedef struct bucket {\r
+ uint32_t *head;\r
+ uint32_t *bp;\r
+} bucket_t;\r
+\r
+typedef bucket_t bucket_array_t[2][0x100];\r
+\r
+typedef struct bucket_info {\r
+ struct {\r
+ uint32_t *head, *tail;\r
+ } bucket_info[2][0x100];\r
+ uint32_t numbuckets;\r
+ } bucket_info_t;\r
+ \r
+\r
+static void bucket_sort_intersect(uint32_t* const estart, uint32_t* const estop,\r
+ uint32_t* const ostart, uint32_t* const ostop,\r
+ bucket_info_t *bucket_info, bucket_array_t bucket)\r
+{\r
+ uint32_t *p1, *p2;\r
+ uint32_t *start[2];\r
+ uint32_t *stop[2];\r
+ \r
+ start[0] = estart;\r
+ stop[0] = estop;\r
+ start[1] = ostart;\r
+ stop[1] = ostop;\r
+ \r
+ // init buckets to be empty\r
+ for (uint32_t i = 0; i < 2; i++) {\r
+ for (uint32_t j = 0x00; j <= 0xff; j++) {\r
+ bucket[i][j].bp = bucket[i][j].head;\r
+ }\r
+ }\r
+ \r
+ // sort the lists into the buckets based on the MSB (contribution bits)\r
+ for (uint32_t i = 0; i < 2; i++) { \r
+ for (p1 = start[i]; p1 <= stop[i]; p1++) {\r
+ uint32_t bucket_index = (*p1 & 0xff000000) >> 24;\r
+ *(bucket[i][bucket_index].bp++) = *p1;\r
+ }\r
+ }\r
+\r
+ \r
+ // write back intersecting buckets as sorted list.\r
+ // fill in bucket_info with head and tail of the bucket contents in the list and number of non-empty buckets.\r
+ uint32_t nonempty_bucket;\r
+ for (uint32_t i = 0; i < 2; i++) {\r
+ p1 = start[i];\r
+ nonempty_bucket = 0;\r
+ for (uint32_t j = 0x00; j <= 0xff; j++) {\r
+ if (bucket[0][j].bp != bucket[0][j].head && bucket[1][j].bp != bucket[1][j].head) { // non-empty intersecting buckets only\r
+ bucket_info->bucket_info[i][nonempty_bucket].head = p1;\r
+ for (p2 = bucket[i][j].head; p2 < bucket[i][j].bp; *p1++ = *p2++);\r
+ bucket_info->bucket_info[i][nonempty_bucket].tail = p1 - 1;\r
+ nonempty_bucket++;\r
+ }\r
+ }\r
+ bucket_info->numbuckets = nonempty_bucket;\r
+ }\r
+}\r
+\r
+\r