in reply to [OT] Swapping buffers in place.

I think the method I proposed above would look something like this, if rendered in a manner similar to the snippet in the OP -- but please just think of this as pseudo-code (and take it with a good bit of salt), since I'm not as fluent or comfortable as I once was with the more C-like syntax you used up there, and I don't have the means to test this.
void swapBufferChunks( U8 *buf, U32 start1, U32 start2, U32 bufsize ) +{ register U64 *p1 = (U64*)&buf[ start1 ]; register U64 *p2 = (U64*)&buf[ start2 ]; register U64 temp; shift_size = p2 - p1; end_buf = p1 + bufsize; # assuming bufsize is byte count item_count = bufsize / 8; # assuming elements are 8-bytes each iter_count = 0; bgn_ofs = p1; temp = *p1; while ( iter_count < item_count ) { if ( p2 = bgn_ofs ) { *p1 = temp; iter_count++; last if ( iter_count == item_count ); # break out of while + loop as soon as this is true p1++; p2++; bgn_ofs = p1; temp = *p1; } *p1 = *p2; iter_count++; p1 = p2; p2 += shift_size; p2 -= bufsize if ( p2 >= end_buf ); } }
(updated include a line inside the while loop to increment iter_count - rather a stupid thing to forget.)

(Updating again to mention that my snippet is bound to have a problem when it hits the last iteration; I haven't tried to fix that in the snippet (left as an exercise for the reader) -- hint: must increment iter_count every time a value is assigned to *p1, and must exit the while loop as soon as iter_count reaches the value of item_count.

(final update: last line used to say ... if ( p2 > end_buf ) which was bound to be wrong. Hell, while I'm at. might as well fix the last-iteration problem too. Still, this sort of thing needs really careful testing, which I haven't done.)

Replies are listed 'Best First'.
Re^2: [OT] Swapping buffers in place.
by BrowserUk (Patriarch) on Mar 01, 2015 at 10:41 UTC

    Yes. I think you filled in the last piece o fthe puzzle for me...I'll get back once I've coded and tested it. Thank you!


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked