~ubuntu-branches/ubuntu/trusty/emscripten/trusty-proposed

« back to all changes in this revision

Viewing changes to system/lib/libc/musl/src/string/memccpy.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2014-01-19 14:12:40 UTC
  • mfrom: (1.2.4)
  • Revision ID: package-import@ubuntu.com-20140119141240-jg1l42cc158j59tn
Tags: 1.9.0~20140119~7dc8c2f-1
* New snapshot release (Closes: #733714)
* Provide sources for javascript and flash. Done in orig-tar.sh
  Available in third_party/websockify/include/web-socket-js/src/
  (Closes: #735903)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <string.h>
 
2
#include <stdlib.h>
 
3
#include <stdint.h>
 
4
#include <limits.h>
 
5
 
 
6
#define ALIGN (sizeof(size_t)-1)
 
7
#define ONES ((size_t)-1/UCHAR_MAX)
 
8
#define HIGHS (ONES * (UCHAR_MAX/2+1))
 
9
#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS)
 
10
 
 
11
void *memccpy(void *restrict dest, const void *restrict src, int c, size_t n)
 
12
{
 
13
        unsigned char *d = dest;
 
14
        const unsigned char *s = src;
 
15
        size_t *wd, k;
 
16
        const size_t *ws;
 
17
 
 
18
        c = (unsigned char)c;
 
19
        if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) {
 
20
                for (; ((uintptr_t)s & ALIGN) && n && (*d=*s)!=c; n--, s++, d++);
 
21
                if ((uintptr_t)s & ALIGN) goto tail;
 
22
                k = ONES * c;
 
23
                wd=(void *)d; ws=(const void *)s;
 
24
                for (; n>=sizeof(size_t) && !HASZERO(*ws^k);
 
25
                       n-=sizeof(size_t), ws++, wd++) *wd = *ws;
 
26
                d=(void *)wd; s=(const void *)ws;
 
27
        }
 
28
        for (; n && (*d=*s)!=c; n--, s++, d++);
 
29
tail:
 
30
        if (*s==c) return d+1;
 
31
        return 0;
 
32
}