That works, and also this looks good, although you can't step back more than
the mod size else it goes negative.

To step back n units, where n < RING_SIZE:

        ((ringpos - n + RING_SIZE) % RING_SIZE))

To prove it to myself, I wrote a test program that looped through the values,
printed them out, and I counted them all. It is attached (see next comment for
rationale).

Clearly this has been a thick-headed week for me.
Thanks for looking over my shoulder!

Aaron


Ilja Booij <[EMAIL PROTECTED]> said:

> 
> Ilja Booij wrote:
> > Hi,
> > 
> > the ringbuf stuff is severly broken. C module arithmetic can be pretty 
> > akward:
> > 
> > (ringpos - 1) % RING_SIZE, with ringpos = 0 and RING_SIZE = 6:
> > 
> > -1 % 6 = -1
> > 
> > WTF? why isn't it 5?
> > 
> > I've just made a simple function to fix this, but I'm looking for a more 
> > elegant solution.
> 
> If you think compiler macros are elegant, then the elegant solution is 
> here. :)
> 
> I've added a macro MOD(x,y) to pipe.c which calculates the modulo of x 
> with respect y. It also works when x is negative.
> 
> Ilja
> _______________________________________________
> Dbmail-dev mailing list
> Dbmail-dev@dbmail.org
> http://twister.fastxs.net/mailman/listinfo/dbmail-dev
> 



-- 



#include <stdio.h>

#define RING_SIZE 6

int main() {
	int ringpos = 0;

	for (ringpos = 0; ringpos <= RING_SIZE; ringpos++ ) {
		printf("ringpos = %d\n", ringpos);
		printf( "  less 1: %d\n", ((ringpos - 1 + RING_SIZE) % RING_SIZE));
		printf( "  less 2: %d\n", ((ringpos - 2 + RING_SIZE) % RING_SIZE));
		printf( "  less 3: %d\n", ((ringpos - 3 + RING_SIZE) % RING_SIZE));
		printf( "  less 4: %d\n", ((ringpos - 4 + RING_SIZE) % RING_SIZE));
		printf( "  less 5: %d\n", ((ringpos - 5 + RING_SIZE) % RING_SIZE));
		printf( "  less 6: %d\n", ((ringpos - 6 + RING_SIZE) % RING_SIZE));
		printf( "  less 7: %d\n", ((ringpos - 7 + RING_SIZE) % RING_SIZE));
		printf( "  less 8: %d\n", ((ringpos - 8 + RING_SIZE) % RING_SIZE));
		printf( "  less 9: %d\n", ((ringpos - 9 + RING_SIZE) % RING_SIZE));
	}

}

Reply via email to