Skip to content

Commit

Permalink
Use stable loop power calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
klange committed Mar 19, 2024
1 parent 179b4e4 commit 72b59b7
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/obj_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,6 @@ static struct SortSlice powersort_extend_and_reverse_right(struct SortSlice begi
return j;
}

#if defined(__TINYC__) || (defined(_MSC_VER) && !defined(__clang__))
static int __builtin_clz(unsigned int x) {
int i = 31;
while (!(x & (1 << i)) && i >= 0) i--;
return 31-i;
}
#endif

/**
* @brief Calculate power.
*
Expand All @@ -546,11 +538,23 @@ static int __builtin_clz(unsigned int x) {
*/
static size_t powersort_power(size_t begin, size_t end, size_t beginA, size_t beginB, size_t endB) {
size_t n = end - begin;
unsigned long l2 = beginA + beginB - 2 * begin;
unsigned long r2 = beginB + endB - 2 * begin;
unsigned int a = (unsigned int)((l2 << 30) / n);
unsigned int b = (unsigned int)((r2 << 30) / n);
return __builtin_clz(a ^ b);
unsigned long l = beginA - begin + beginB - begin;
unsigned long r = beginB - begin + endB - begin;
size_t common = 0;
int digitA = l >= n;
int digitB = r >= n;
while (digitA == digitB) {
common++;
if (digitA) {
l -= n;
r -= n;
}
l <<= 1;
r <<= 1;
digitA = l >= n;
digitB = r >= n;
}
return common + 1;
}

/**
Expand Down

0 comments on commit 72b59b7

Please sign in to comment.