Changeset 6b080e54 in mainline


Ignore:
Timestamp:
2008-12-23T16:35:31Z (15 years ago)
Author:
Jiri Svoboda <jirik.svoboda@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
7a817d00
Parents:
47acd58
Message:

Faster memset() implementation in C library.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/libc/generic/string.c

    r47acd58 r6b080e54  
    4343#include <malloc.h>
    4444
    45 /* Dummy implementation of mem/ functions */
    46 
    47 void *memset(void *s, int c, size_t n)
    48 {
    49         char *os = s;
    50        
    51         while (n--)
    52                 *(os++) = c;
    53        
    54         return s;
     45/** Fill memory block with a constant value. */
     46void *memset(void *dest, int b, size_t n)
     47{
     48        char *pb;
     49        unsigned long *pw;
     50        size_t word_size;
     51        size_t n_words;
     52
     53        unsigned long pattern;
     54        size_t i;
     55        size_t fill;
     56
     57        /* Fill initial segment. */
     58        word_size = sizeof(unsigned long);
     59        fill = word_size - ((uintptr_t) dest & (word_size - 1));
     60        if (fill > n) fill = n;
     61
     62        pb = dest;
     63
     64        i = fill;
     65        while (i-- != 0)
     66                *pb++ = b;
     67
     68        /* Compute remaining size. */
     69        n -= fill;
     70        if (n == 0) return dest;
     71
     72        n_words = n / word_size;
     73        n = n % word_size;
     74        pw = (unsigned long *) pb;
     75
     76        /* Create word-sized pattern for aligned segment. */
     77        pattern = 0;
     78        i = word_size;
     79        while (i-- != 0)
     80                pattern = (pattern << 8) | (uint8_t) b;
     81
     82        /* Fill aligned segment. */
     83        i = n_words;
     84        while (i-- != 0)
     85                *pw++ = pattern;
     86
     87        pb = (char *) pw;
     88
     89        /* Fill final segment. */
     90        i = n;
     91        while (i-- != 0)
     92                *pb++ = b;
     93
     94        return dest;
    5595}
    5696
     
    115155
    116156        i = fill;
    117         while (i-- > 0)
     157        while (i-- != 0)
    118158                *dstb++ = *srcb++;
    119159
     
    133173        /* "Fast" copy. */
    134174        i = n_words;
    135         while (i-- > 0)
     175        while (i-- != 0)
    136176                *dstw++ = *srcw++;
    137177
     
    144184
    145185        i = n;
    146         while (i-- > 0)
     186        while (i-- != 0)
    147187                *dstb++ = *srcb++;
    148188
Note: See TracChangeset for help on using the changeset viewer.