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

    /* Construct a word's worth of the byte value we need to set. */
    for (i = 3; (1<<i) < __WORDSIZE; ++i)
        x |= x << (1<<i);
    /* At this point i is the power of 2 which is equal to the word size. */

    /* 1<<i would be the number of bits per word, therefore (1<<i)/8 == 1<<(i-3)
     * is the number of bytes per word.
     */
    bytes_per_word = 1<<(i-3);

    /* Check sz is bytes_per_word-byte-aligned. */
    assert(!(sz & (bytes_per_word-1)));
    sz >>= i-3;

    while (sz--)
        *p++ = x;
    return s;
}
/* Now let's do an architecture-independent word-by-word memset. You would never
 * want to implement this in practice, as you are relying on the compiler to
 * optimise the statically determinable loops that could be manually written
 * much more efficiently if you know the target architecture, but this is a
 * useful thought exercise. Note that GCC defines the handy constant __WORDSIZE
 * that tells us the size (in bits) of words on this architecture.
 */
Now let's do an architecture-independent word-by-word memset. You would never
want to implement this in practice, as you are relying on the compiler to
optimise the statically determinable loops that could be manually written
much more efficiently if you know the target architecture, but this is a
useful thought exercise. Note that GCC defines the handy constant __WORDSIZE
that tells us the size (in bits) of words on this architecture.
Now let ' s do an architecture - independent word - by - word memset . You would never want to implement this in practice as you are relying on the compiler to optimise the statically determinable loops that could be manually written much more efficiently if you know the target architecture but this is a useful thought exercise . Note that GCC defines the handy constant __WORDSIZE that tells us the size ( in bits ) of words on this architecture .
void* wordwise_memset(void* s, int c, size_t sz) {
    uintptr_t* p = (uintptr_t*)s;
    uintptr_t x = c & 0xff;
    int i;
    int bytes_per_word;
    for (i = 3; (1<<i) < __WORDSIZE; ++i)
        x |= x << (1<<i);
    bytes_per_word = 1<<(i-3);
    assert(!(sz & (bytes_per_word-1)));
    sz >>= i-3;
    while (sz--)
        *p++ = x;
    return s;
}
void * wordwise_memset ( void * s , int c , size_t sz ) { uintptr_t * p = ( uintptr_t * ) s ; uintptr_t x = c & 0xff ; int i ; int bytes_per_word ; for ( i = 3 ; ( 1 << i ) < __WORDSIZE ; ++ i ) x |= x << ( 1 << i ) ; bytes_per_word = 1 << ( i - 3 ) ; assert ( ! ( sz & ( bytes_per_word - 1 ) ) ) ; sz >>= i - 3 ; while ( sz -- ) * p ++ = x ; return s ; }
Now let's do an architecture-independent word-by-word memset.
Now let ' s do an architecture - independent word - by - word memset .
/* Construct a word's worth of the byte value we need to set. */ /* At this point i is the power of 2 which is equal to the word size. */ /* 1<<i would be the number of bits per word, therefore (1<<i)/8 == 1<<(i-3)
     * is the number of bytes per word.
     */ /* Check sz is bytes_per_word-byte-aligned. */