misc/memset.c
void* wordwise_unaligned_memset(void* s, int c, size_t sz) {
    uintptr_t* p;
    uintptr_t x = c & 0xff;
    byte* pp = (byte*)s;
    byte xx = c & 0xff;
    size_t tail;
    int i;
    int bytes_per_word;

    for (i = 3; (1<<i) < __WORDSIZE; ++i)
        x |= x << (1<<i);
    bytes_per_word = 1<<(i-3);

    /* Prologue. */
    while (((unsigned int)pp & (bytes_per_word-1)) && sz--)
        *pp++ = xx;
    tail = sz & (bytes_per_word-1);
    p = (uintptr_t*)pp;

    /* Main loop. */
    sz >>= i-3;
    while (sz--)
        *p++ = x;

    /* Epilogue. */
    pp = (byte*)p;
    while (tail--)
        *pp++ = xx;

    return s;
}
/* Let's take the final logical step and implement an architecture-independent
 * memset that can cope with unaligned pointers and sizes. Note, the same
 * caveat as for wordwise_memset applies; you wouldn't write code like this in
 * real life.
 */
Let's take the final logical step and implement an architecture-independent
memset that can cope with unaligned pointers and sizes. Note, the same
caveat as for wordwise_memset applies; you wouldn't write code like this in
real life.
Let ' s take the final logical step and implement an architecture - independent memset that can cope with unaligned pointers and sizes . Note the same caveat as for wordwise_memset applies ; you wouldn ' t write code like this in real life .
void* wordwise_unaligned_memset(void* s, int c, size_t sz) {
    uintptr_t* p;
    uintptr_t x = c & 0xff;
    byte* pp = (byte*)s;
    byte xx = c & 0xff;
    size_t tail;
    int i;
    int bytes_per_word;
    for (i = 3; (1<<i) < __WORDSIZE; ++i)
        x |= x << (1<<i);
    bytes_per_word = 1<<(i-3);
    while (((unsigned int)pp & (bytes_per_word-1)) && sz--)
        *pp++ = xx;
    tail = sz & (bytes_per_word-1);
    p = (uintptr_t*)pp;
    sz >>= i-3;
    while (sz--)
        *p++ = x;
    pp = (byte*)p;
    while (tail--)
        *pp++ = xx;
    return s;
}
void * wordwise_unaligned_memset ( void * s , int c , size_t sz ) { uintptr_t * p ; uintptr_t x = c & 0xff ; byte * pp = ( byte * ) s ; byte xx = c & 0xff ; size_t tail ; int i ; int bytes_per_word ; for ( i = 3 ; ( 1 << i ) < __WORDSIZE ; ++ i ) x |= x << ( 1 << i ) ; bytes_per_word = 1 << ( i - 3 ) ; while ( ( ( unsigned int ) pp & ( bytes_per_word - 1 ) ) && sz -- ) * pp ++ = xx ; tail = sz & ( bytes_per_word - 1 ) ; p = ( uintptr_t * ) pp ; sz >>= i - 3 ; while ( sz -- ) * p ++ = x ; pp = ( byte * ) p ; while ( tail -- ) * pp ++ = xx ; return s ; }
Let's take the final logical step and implement an architecture-independent
 memset that can cope with unaligned pointers and sizes.
Let ' s take the final logical step and implement an architecture - independent memset that can cope with unaligned pointers and sizes .
/* Prologue. */ /* Main loop. */ /* Epilogue. */