in reply to Re^3: [OT] The interesting problem of comparing bit-strings.
in thread [OT] The interesting problem of comparing bit-strings.
As you are the third respondent that has suggested bit-masks of one form or another, either:
Both distinct possibilities.
As I see it, my problem isn't how to isolate the bits of the strings that I need to compare -- __shiftleft128() does that easily and efficiently.
The problem is one of how to construct/combine the nested loops in order to perform the iteration of the bits and quads in the proper way.
Looking up a basic (ie. not the gcc version which incredibly complicated) strstr() function I found this:
char *strstr( char *haystack, char *needle ) { char *hp; char *np; if( !*needle ) return haystack; // null needle; retur +n haystack (strange POSIX specification requirement!) while( *haystack ) { // until we reach the + end of the haystack hp = haystack; // copy of the haysta +ck pointer np = needle; // copy of the needle + pointer do { if( !*np ) return haystack; // end of needle; mat +ch completed; return current value of haystack pointer } while( *hp++ == *np++ ); // increment both poi +nter (copies) while the chars match ++haystack; // Got here means a m +ismatch; base pointer to next character. } return 0; // Got here means we +hit the terminating null of the haystack; no match; return null. }
If my bitstrings where always 64-bit aligned, I could just substitute U64 for char everywhere, and that would be that; but of course it isn't.
Essentially, there are five operations I need to encapsulate:
For 2 & 3 I thought to write a function:
U64 nextQuad( U64 **p, U8 o ) { // p: pointer to poin +ter to the 'current' quad; o: value of the unused bits at the beginni +ng. U64 hi = *( ++*p ) // increment the poin +ter at *p, and set hi to the value it points at. , lo = *( *p+1 ); // and lo to the valu +e + 1 it points at return __shiftleft128( lo, hi, o ); // return the value f +rom the higher location with o bits from lo shifted in from the right +. }
I think that would work, but for efficiency, you'd want to keep hi and lo in registers between calls, and you can't take the address of regiters, so I'd be relying on the compiler to recognise the importance of doing so.
For one, things get more complicated. Now I need a pointer to the offset as well:
U64 nextBit( U64 **p, U8 *o ) { // p: pointer to poin +ter to current quad; o: pointer to current offset U64 hi, lo; if( ++( *o ) == 64 ) { // increment the offs +et and detect transitions across quad boundaries. *o = 0; // reset the offset. ++*p; // increment the quad + pointer. } hi = **p; // Get the 'current' + (unshifted) quad value lo = *( *p + 1 ); // And the next return __shiftleft128( lo, hi, *o ); // return the value +from the higher location with *o bits from lo shifted in from the rig +ht. }
For 4 & 5, I need to calculate whether I've reached the end from the current values of the quad pointer & associated offset.
Putting it all together and getting it to compile is defeating me at the moment.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: [OT] The interesting problem of comparing bit-strings. (More info.)
by Krambambuli (Curate) on Mar 25, 2015 at 10:31 UTC | |
by BrowserUk (Patriarch) on Mar 25, 2015 at 13:34 UTC | |
by BrowserUk (Patriarch) on Mar 25, 2015 at 11:49 UTC | |
|
Re^5: [OT] The interesting problem of comparing bit-strings. (More info.)
by marinersk (Priest) on Mar 25, 2015 at 11:11 UTC | |
by BrowserUk (Patriarch) on Mar 25, 2015 at 11:48 UTC | |
by marinersk (Priest) on Mar 25, 2015 at 15:52 UTC | |
by marinersk (Priest) on Mar 25, 2015 at 20:19 UTC |