I've always wanted an opportunity to use Duff's device :
void bduggan_duff(SV *sv1, SV *sv2) { /* slightly modified avar_c_inplace */ char *sv1p, *sv2p; STRLEN sv1len, sv2len; SV *sv1_sv; STRLEN i; if (!SvROK(sv1) || !SvPOK((SV*)SvRV(sv1)) || !SvPOK(sv2)) { croak("Usage: avar_cee(\$s1, $s2)"); } sv1_sv = (SV*)SvRV(sv1); sv1p = SvPV(sv1_sv, sv1len); sv2p = SvPV(sv2, sv2len); if (sv1len != sv2len) { croak("The given strings must be of the same length"); } i = 0; switch (sv2len % 8) { case 0 : do { if (sv1p[i] == '\0') sv1p[i] = sv2p[i]; i++; case 1 : if (sv1p[i] == '\0') sv1p[i] = sv2p[i]; i++; case 2 : if (sv1p[i] == '\0') sv1p[i] = sv2p[i]; i++; case 3 : if (sv1p[i] == '\0') sv1p[i] = sv2p[i]; i++; case 4 : if (sv1p[i] == '\0') sv1p[i] = sv2p[i]; i++; case 5 : if (sv1p[i] == '\0') sv1p[i] = sv2p[i]; i++; case 6 : if (sv1p[i] == '\0') sv1p[i] = sv2p[i]; i++; case 7 : if (sv1p[i] == '\0') sv1p[i] = sv2p[i]; i++; } while (i<sv2len); } } void bduggan_duff2(SV *sv1, SV *sv2) { /* slightly modified avar_tye_c_inplace */ char *sv1p, *sv2p; + + STRLEN sv1len, sv2len; SV *sv1_sv; STRLEN i; if (!SvROK(sv1) || !SvPOK((SV*)SvRV(sv1)) || !SvPOK(sv2)) { croak("Usage: avar_cee(\$s1, $s2)"); } sv1_sv = (SV*)SvRV(sv1); sv1p = SvPV(sv1_sv, sv1len); sv2p = SvPV(sv2, sv2len); if (sv1len != sv2len) { croak("The given strings must be of the same length"); } char *pSrc = sv2p + sv2len; char *pDst = sv1p + sv2len; switch (sv2len % 8) { case 0 : do { if (!*pDst) { *pDst = *pSrc; } --pSrc; --pDst; case 1 : if (!*pDst) { *pDst = *pSrc; } --pSrc; --pDst; case 2 : if (!*pDst) { *pDst = *pSrc; } --pSrc; --pDst; case 3 : if (!*pDst) { *pDst = *pSrc; } --pSrc; --pDst; case 4 : if (!*pDst) { *pDst = *pSrc; } --pSrc; --pDst; case 5 : if (!*pDst) { *pDst = *pSrc; } --pSrc; --pDst; case 6 : if (!*pDst) { *pDst = *pSrc; } --pSrc; --pDst; case 7 : if (!*pDst) { *pDst = *pSrc; } --pSrc; --pDst; } while (sv2p <= pSrc); }
Sadly (or maybe not), they can't compete with memchr. With gcc 3.3.3 the first one seems to come in second place. With gcc 4.1.2, the second one does.
-Brian

In reply to Re: Challenge: CPU-optimized byte-wise or-equals (for a meter of beer) by bduggan
in thread Challenge: CPU-optimized byte-wise or-equals (for a meter of beer) by dragonchild

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.