[PATCH 3/4] wifi: rtw88: rtw8822c: switch to the kernel's sort() library

From: Arsenii Pashchenko

Date: Wed Aug 12 2026 - 10:46:55 EST


Replace the custom, nested bubble-sort logic in rtw8822c_dac_iq_sort()
and __rtw8822c_dac_iq_sort() with a call to the kernel's optimized
heapsort implementation.

Implement a concise, localized s32 comparator. Since the previous
patch already guarantees that input values are properly sign-extended
into signed arrays, the custom circular index sorting rules completely
disappear, trimming down lines of code.

Signed-off-by: Arsenii Pashchenko <ulijg308@xxxxxxxxx>
---
drivers/net/wireless/realtek/rtw88/rtw8822c.c | 28 ++++++-------------
1 file changed, 8 insertions(+), 20 deletions(-)

diff --git a/drivers/net/wireless/realtek/rtw88/rtw8822c.c b/drivers/net/wireless/realtek/rtw88/rtw8822c.c
index d06986d09..642ac2e2b 100644
--- a/drivers/net/wireless/realtek/rtw88/rtw8822c.c
+++ b/drivers/net/wireless/realtek/rtw88/rtw8822c.c
@@ -4,6 +4,7 @@

#include <linux/module.h>
#include <linux/bitops.h>
+#include <linux/sort.h>
#include "main.h"
#include "coex.h"
#include "fw.h"
@@ -186,31 +187,18 @@ static void rtw8822c_rf_minmax_cmp(struct rtw_dev *rtwdev, s32 value_s32,
}
}

-static void __rtw8822c_dac_iq_sort(struct rtw_dev *rtwdev, s32 *v1_s32, s32 *v2_s32)
+static int rtw8822c_dac_iq_cmp_s32(const void *a, const void *b)
{
- u32 *v1 = (u32 *)v1_s32;
- u32 *v2 = (u32 *)v2_s32;
- if (*v1 >= 0x200 && *v2 >= 0x200) {
- if (*v1 > *v2)
- swap(*v1, *v2);
- } else if (*v1 < 0x200 && *v2 < 0x200) {
- if (*v1 > *v2)
- swap(*v1, *v2);
- } else if (*v1 < 0x200 && *v2 >= 0x200) {
- swap(*v1, *v2);
- }
+ s32 va = *(const s32 *)a;
+ s32 vb = *(const s32 *)b;
+
+ return (va > vb) - (va < vb);
}

static void rtw8822c_dac_iq_sort(struct rtw_dev *rtwdev, s32 *iv, s32 *qv)
{
- u32 i, j;
-
- for (i = 0; i < DACK_SN_8822C - 1; i++) {
- for (j = 0; j < (DACK_SN_8822C - 1 - i) ; j++) {
- __rtw8822c_dac_iq_sort(rtwdev, &iv[j], &iv[j + 1]);
- __rtw8822c_dac_iq_sort(rtwdev, &qv[j], &qv[j + 1]);
- }
- }
+ sort(iv, DACK_SN_8822C, sizeof(s32), rtw8822c_dac_iq_cmp_s32, NULL);
+ sort(qv, DACK_SN_8822C, sizeof(s32), rtw8822c_dac_iq_cmp_s32, NULL);
}

static void rtw8822c_dac_iq_offset(struct rtw_dev *rtwdev, s32 *vec_s32, u32 *val)
--
2.55.0