Skip to content
This repository was archived by the owner on Sep 20, 2022. It is now read-only.

Commit e3f85f0

Browse files
author
Joakim Nohlgård
committed
spectrum-scanner: Run time performance improvements
Experiments show that using ldexpf, ilogbf yields significantly faster run time performance on Cortex-M0+ without floating point hardware.
1 parent 39a974e commit e3f85f0

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

spectrum-scanner/main.c

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2017 Eistec AB
2+
* Copyright (C) 2017-2018 Eistec AB
33
*
44
* This file is subject to the terms and conditions of the GNU Lesser
55
* General Public License v2.1. See the file LICENSE in the top level
@@ -61,13 +61,14 @@ void spectrum_scanner(unsigned long interval_us)
6161
/* Using expf(x) (natural exponent) gives quicker computations on Cortex-M0+,
6262
* compared to using powf(10, x). */
6363
/*
64-
* This was optimized by testing different combinations of expf, powf, logf, log10f:
64+
* This was optimized by testing different combinations of expf, powf, logf, log10f, ldexp, ilogbf:
6565
*
66-
* functions used | measurement iterations per 0.5 s on reference system (frdm-kw41z)
67-
* ------------------------------------------------------------------
68-
* expf, logf | 64
69-
* powf, log10f | 46
70-
* expf, log10f | 61
66+
* functions used | measurement iterations per 0.5 s on reference system (frdm-kw41z)
67+
* --------------------------------------------------------------------
68+
* expf, logf | 64
69+
* powf, log10f | 46
70+
* expf, log10f | 61
71+
* ldexpf, ilogbf | 82
7172
* no-op (baseline) | 83 (but the measurements are useless)
7273
*/
7374

@@ -105,11 +106,11 @@ void spectrum_scanner(unsigned long interval_us)
105106
}
106107
/* Convert dB to pseudo-energy before summing together the
107108
* measurements. "Pseudo" because we use the natural
108-
* exponential function e^x instead of computing 10^x which
109+
* exponential function 2^x instead of computing 10^x which
109110
* would be required if we needed the real measured energy.
110111
* There is no need to know the real energy level because we
111112
* will be converting back to dB again before printing. */
112-
ed_average[k][ch] += expf((float)level / 128.f);
113+
ed_average[k][ch] += ldexpf(1.f, level);
113114
}
114115
}
115116
++count;
@@ -125,10 +126,10 @@ void spectrum_scanner(unsigned long interval_us)
125126
print("] ", 2);
126127
for (unsigned int ch = IEEE802154_CHANNEL_MIN; ch <= IEEE802154_CHANNEL_MAX; ++ch) {
127128
/* Compute the average pseudo-energy and convert back to dB */
128-
ed_average[k][ch] = logf(ed_average[k][ch] / count) * 128.f;
129+
uint32_t ed = ilogbf(ed_average[k][ch] / count);
129130
print_u32_dec(ch);
130131
print(": ", 2);
131-
print_float(ed_average[k][ch], 4);
132+
print_s32_dec(ed);
132133
print(", ", 2);
133134
}
134135
print("\n", 1);

0 commit comments

Comments
 (0)