Re: [hackers] [slstatus][PATCH 1/2] wifi: use ipw2200 quadratic dBm->percent conversion

From: Страхиња Радић <sr_AT_strahinja.org>
Date: Wed, 31 Jul 2024 08:08:37 +0200

Дана 24/07/30 10:15PM, Joakim Sindholt написа:
> -#define RSSI_TO_PERC(rssi) \
> - rssi >= -50 ? 100 : \
> - (rssi <= -100 ? 0 : \
> - (2 * (rssi + 100)))
> +static inline int
> +RSSI_TO_PERC(int rssi)
> +{
> + static const int best = -20, worst = -85, delta = best-worst, square = delta*delta;
> + int q = (100*square-(best-rssi)*(15*delta+62*(best-rssi)))/square;
> + return q > 100 ? 100 : (q < 0 ? 0 : q);
> +}

1. Why replace a macro with a function? The "inline" keyword is just a
   suggestion to the compiler. In my tests, even with `-O3 -std=c99`,
   GCC 11.2.0 still emits CALL instructions for inline functions, while
   Clang/LLVM 16.0.6 doesn't. Macros are unambiguous.

2. When replacing a macro with a function, its name should be lowercased, eg.

        rssi_to_perc(int rssi)

3. While being "compressed" into three lines, the code is hard to read.
   Better:

        static inline int
        rssi_to_perc(const int rssi)
        {
                const int best = -20; /* enum { BEST, WORST }? */
                const int worst = -85;
                const int delta = best - worst;
                const int square = delta * delta;
                int q = (100 * square
                        - (best - rssi) * (15 * delta + 62 * (best - rssi)))
                                / square;
                return q > 100 ? 100 : (q < 0 ? 0 : q);
        }

4. While the formula in the proposed patch seems to be a quadratic
   approximation[1], it is worth checking out [2] and [3].


* * *

My opinion: Given the extra computing cost of non-constant
multiplication, as well as the introduced code complexity, I think it
would still be best to leave the linear approximation as default in the
mainline slstatus, and add the quadratic approximation from this
proposal as an (optional) patch.

What might be worth exploring is the linear approximation with a
steeper slope, represented in the last illustration at [2].


[1]: https://www.wolframalpha.com/input?i=plot+y+%3D+100+*+%283025+-+%28-85+-+x%29+*+%2815+*+-55+%2B+62+*+%28-20+-+x%29%29%29
[2]: https://gist.github.com/senseisimple/fe91413d6bb5d5e0994222ac59bc90c4
[3]: https://www.intuitibits.com/2016/03/23/dbm-to-percent-conversion/
Received on Wed Jul 31 2024 - 08:08:37 CEST

This archive was generated by hypermail 2.3.0 : Wed Jul 31 2024 - 08:12:37 CEST