Re: [PATCH] ath9k: Use swap() instead of open coding it

From: Joe Perches
Date: Mon Jul 04 2022 - 12:32:20 EST


On Mon, 2022-07-04 at 16:55 +0200, Toke Høiland-Jørgensen wrote:
> "Tan Zhongjun" <tanzhongjun@xxxxxxxxxxx> writes:
>
> > Use swap() instead of open coding it
> >
> > Signed-off-by: Tan Zhongjun <tanzhongjun@xxxxxxxxxxx>
>
> Please don't send HTML email, the mailing lists will drop that. Also, an
> identical patch was submitted back in February and an issue was pointed
> out which your patch also suffers from:
>
> https://lore.kernel.org/r/a2400dd73f6ea8672bb6e50124cc3041c0c43d6d.1644838854.git.yang.guang5@xxxxxxxxxx

Perhaps instead use sort instead of a bubble sort.

Something like:
---
drivers/net/wireless/ath/ath9k/calib.c | 35 ++++++++++++++++++----------------
1 file changed, 19 insertions(+), 16 deletions(-)

diff --git a/drivers/net/wireless/ath/ath9k/calib.c b/drivers/net/wireless/ath/ath9k/calib.c
index 0422a33395b77..4e298925049e8 100644
--- a/drivers/net/wireless/ath/ath9k/calib.c
+++ b/drivers/net/wireless/ath/ath9k/calib.c
@@ -17,29 +17,32 @@
#include "hw.h"
#include "hw-ops.h"
#include <linux/export.h>
+#include <linux/sort.h>

/* Common calibration code */

+static int cmp_int16_t(const void *a, const void *b)
+{
+ int16_t a1 = *(int16_t *)a;
+ int16_t b1 = *(int16_t *)b;
+
+ if (a1 < b1)
+ return -1;
+ if (a1 > b1)
+ return 1;
+ return 0;
+}

static int16_t ath9k_hw_get_nf_hist_mid(int16_t *nfCalBuffer)
{
int16_t nfval;
- int16_t sort[ATH9K_NF_CAL_HIST_MAX];
- int i, j;
-
- for (i = 0; i < ATH9K_NF_CAL_HIST_MAX; i++)
- sort[i] = nfCalBuffer[i];
-
- for (i = 0; i < ATH9K_NF_CAL_HIST_MAX - 1; i++) {
- for (j = 1; j < ATH9K_NF_CAL_HIST_MAX - i; j++) {
- if (sort[j] > sort[j - 1]) {
- nfval = sort[j];
- sort[j] = sort[j - 1];
- sort[j - 1] = nfval;
- }
- }
- }
- nfval = sort[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];
+ int16_t sorted[ATH9K_NF_CAL_HIST_MAX];
+
+ memcpy(sorted, nfCalBuffer, sizeof(int16_t) * ATH9K_NF_CAL_HIST_MAX);
+
+ sort(sorted, ARRAY_SIZE(sorted), sizeof(int16_t), cmp_int16_t, NULL);
+
+ nfval = sorted[(ATH9K_NF_CAL_HIST_MAX - 1) >> 1];

return nfval;
}