• The fastest strlen() ever

    From Bonita Montero@3:633/10 to All on Wed Nov 19 15:02:01 2025
    size_t xstrlen( char const *s )
    {
    ÿ ÿ char const *begin = s;
    ÿ ÿ __m512i zero = _mm512_setzero_si512();
    ÿ ÿ uintptr_t
    ÿ ÿ ÿ ÿ up = (uintptr_t)s,
    ÿ ÿ ÿ ÿ base = up & ~(uintptr_t)63;
    ÿ ÿ unsigned skip = (unsigned)(up - base);
    ÿ ÿ s = (char *)base;
    ÿ ÿ uint64_t matches = _mm512_cmpeq_epi8_mask( _mm512_load_si512( (void
    *)s ), zero );
    ÿ ÿ matches = matches >> skip;
    ÿ ÿ if( matches )
    ÿ ÿ ÿ ÿ return countr_zero( matches );
    ÿ ÿ s += 64;
    ÿ ÿ for( ; ; s += 64 )
    ÿ ÿ ÿ ÿ if( matches = _mm512_cmpeq_epi8_mask( _mm512_load_si512( (void
    *)s ), zero ); matches )
    ÿ ÿ ÿ ÿ ÿ ÿ return s - begin + countr_zero( matches );
    }

    Everything with AVX-512.



    --- PyGate Linux v1.5
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)