Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I am not sure, if this works as you expect... maybe the macros and wrappers generate a copy under the hood?

use Inline C; use strict; use warnings; my $x = "012\xf0ABC\x0f\x00\x01\xff"; print unpack("H*", $x ), "\n"; # 303132f04142430f0001ff flipbits( $x, length $x ); print unpack("H*", $x ), "\n"; # cfcecd0fbebdbcf0fffe00 __END__ __C__ void flipbits(char* a, int len ) { for ( ; len-- ; ++a ) *a ^= 0xff; }

Update: Switching to 32bit blocks gave a huge improvement on my 32bit setup...

void flipbits32(char* a, int len ) { int blocks = len >> 2; if ( sizeof blocks == 4 ) { /* try 32bit blocks */ for ( ; blocks-- ; a += 4 ) *( (unsigned int*) a ) ^= 0xffffffffU +; /* TODO: add treatment for last 1..3 bytes if len is not a multipl +e of 4 */ } else { /* fallback to 8bit */ for ( ; len-- ; ++a ) *a ^= 0xff; } }
Benchmark:
Rate str inline inline32 str 69.1/s -- -37% -69% flipbits 110/s 59% -- -51% flipbits32 223/s 222% 103% --

Potential next steps which are also less general, but that's the price to pay:


In reply to Re: In-place bitwise NOT? by Perlbotics
in thread In-place bitwise NOT? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (8)
    As of 2015-07-29 11:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (263 votes), past polls