Just let it overflow with {$R-}. Need power of two, of course and the correct bit size.Code: Pascal [Select][+][-]program ringbuffer;{$R-}var b:byte = 0;begin repeat writeln(b); inc(b); until 0=1;// silly, replace with signal hi/lo....end.Audio buffers trick.... That's how I used it in the past... And it is a 256 ringbuffer which is perfect for embedded.
Hi,Quote from: Thaddy on October 12, 2022, 10:30:45 amJust let it overflow with {$R-}. Need power of two, of course and the correct bit size.Code: Pascal [Select][+][-]program ringbuffer;{$R-}var b:byte = 0;begin repeat writeln(b); inc(b); until 0=1;// silly, replace with signal hi/lo....end.Audio buffers trick.... That's how I used it in the past... And it is a 256 ringbuffer which is perfect for embedded. I use the same technique for the ring buffer read/write indices ( i.e. see here for read ) but how the above can be a ring buffer with 256 slots ? Where the values are stored ? What am I missing ? Could you please elaborate ? regards,
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.
which is not the case.
Quote from: Thaddy on October 12, 2022, 06:11:38 pmNo, 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.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.
... even the bit-wise and isn't needed because it is achieved naturally by the limited length of the register ...
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 ... 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,