util/spkmodem-recv: optimise ring buffer pos calc

instead of computing next every time, just advance
two indexes. another performance optimisation on
older machines, especially old compilers, because
it reduces the amount of logical branching.

the old code was pretty much just advancing two
indexes in lockstep, when getting the next pulse,
but recalculating one of them based on the other,
each time.

this is yet another hangover from the old GNU code
that i forked three years ago.

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-12 04:44:44 +00:00
parent 54936a45a5
commit 44c6b453bc
+11 -7
View File
@@ -41,6 +41,8 @@ struct decoder_state {
unsigned char pulse[MAX_SAMPLES];
int ringpos;
int sep_pos;
int freq_data;
int freq_separator;
int sample_count;
@@ -89,6 +91,9 @@ main(int argc, char **argv)
memset(&st, 0, sizeof(st));
st.ascii_bit = 7;
st.ringpos = 0;
st.sep_pos = SAMPLES_PER_FRAME;
argv0 = argv[0];
while (1) {
@@ -140,15 +145,10 @@ static void
decode_pulse(struct decoder_state *st)
{
size_t n;
int next;
next = st->ringpos + SAMPLES_PER_FRAME;
if (next >= MAX_SAMPLES)
next -= MAX_SAMPLES;
st->freq_data -= st->pulse[st->ringpos];
st->freq_data += st->pulse[next];
st->freq_separator -= st->pulse[next];
st->freq_data += st->pulse[st->sep_pos];
st->freq_separator -= st->pulse[st->sep_pos];
n = fread(&st->frame[st->ringpos], sizeof(st->frame[0]), 1, stdin);
if (n != 1) {
@@ -166,6 +166,10 @@ decode_pulse(struct decoder_state *st)
if (st->ringpos >= MAX_SAMPLES)
st->ringpos = 0;
st->sep_pos++;
if (st->sep_pos >= MAX_SAMPLES)
st->sep_pos = 0;
st->sample_count++;
}