Forum > Embedded

A circular / ring buffer for embedded

<< < (5/5)

marcov:

--- Quote from: Thaddy on October 12, 2022, 06:16:23 pm ---which is not the case.

--- End quote ---

Coincidence or by design?

alpine:

--- Quote from: Thaddy on October 12, 2022, 06:11:38 pm ---No, modulo is on most CPU's an expensive operation. Using overflow is NOT an expensive operation.
I will add a simple sine wave later  to demonstrate its use. But it is very easy anyway.

--- End quote ---
No?
FYI modulo of 2^n is quite a light operation and is actually bit-wise and with 2^n-1. In your example, that you called an overflow, even the bit-wise and isn't needed because it is achieved naturally by the limited length of the register - the carry just drops into the status flags and gets ignored. Regardless of the size of the type/register, unsigned and two's complement integers always naturally form a commutative ring. See also modular arithmetics.

d.ioannidis:
Hi,

--- Quote from: y.ivanov on October 12, 2022, 07:32:35 pm ---
--- Quote from: Thaddy on October 12, 2022, 06:11:38 pm ---No, modulo is on most CPU's an expensive operation. Using overflow is NOT an expensive operation.
I will add a simple sine wave later  to demonstrate its use. But it is very easy anyway.

--- End quote ---
No?
FYI modulo of 2^n is quite a light operation and is actually bit-wise and with 2^n-1. In your example, that you called an overflow, even the bit-wise and isn't needed because it is achieved naturally by the limited length of the register - the carry just drops into the status flags and gets ignored. Regardless of the size of the type/register, unsigned and two's complement integers always naturally form a commutative ring. See also modular arithmetics.

--- End quote ---

the spsc_ringbuffer code is exactly what you describe .

Stripped down is effectively :

EDIT: wrong quote ....

regards,

d.ioannidis:
Hi,

--- Quote from: y.ivanov on October 12, 2022, 07:32:35 pm ---... even the bit-wise and isn't needed because it is achieved naturally by the limited length of the register ...

--- End quote ---

the bit-wise AND is needed in the case the buffer size is smaller of the index type max value.

i.e. If the index are type of byte and the buffer size is 64 instead of 128 the "index AND (size - 1)" clamps the value into the buffer size range .

At least this is how I understand it.

regards,

alpine:

--- Quote from: d.ioannidis on October 12, 2022, 08:03:08 pm ---Hi,

--- Quote from: y.ivanov on October 12, 2022, 07:32:35 pm ---... even the bit-wise and isn't needed because it is achieved naturally by the limited length of the register ...

--- End quote ---

the bit-wise AND is needed in the case the buffer size is smaller of the index type max value.

i.e. If the index are type of byte and the buffer size is 64 instead of 128 the "index AND (size - 1)" clamps the value into the buffer size range .

At least this is how I understand it.

regards,

--- End quote ---
Right, (x mod 2^n) is same as (x and (2^n-1)). In the case when we're incrementing a BYTE with a value of \$FF, the carry from the seventh bit to the eight (which is not present) just vanishes, it is same as to make (\$10000 and \$FFFF) - the result is zero.