s390x: add single-instruction math functions
[musl.git] / src / multibyte / mbtowc.c
blobc191bb038bbfdb05a81daf64c732de3b43b40424
1 #include <stdlib.h>
2 #include <wchar.h>
3 #include <errno.h>
4 #include "internal.h"
6 int mbtowc(wchar_t *restrict wc, const char *restrict src, size_t n)
8 unsigned c;
9 const unsigned char *s = (const void *)src;
10 wchar_t dummy;
12 if (!s) return 0;
13 if (!n) goto ilseq;
14 if (!wc) wc = &dummy;
16 if (*s < 0x80) return !!(*wc = *s);
17 if (MB_CUR_MAX==1) return (*wc = CODEUNIT(*s)), 1;
18 if (*s-SA > SB-SA) goto ilseq;
19 c = bittab[*s++-SA];
21 /* Avoid excessive checks against n: If shifting the state n-1
22 * times does not clear the high bit, then the value of n is
23 * insufficient to read a character */
24 if (n<4 && ((c<<(6*n-6)) & (1U<<31))) goto ilseq;
26 if (OOB(c,*s)) goto ilseq;
27 c = c<<6 | *s++-0x80;
28 if (!(c&(1U<<31))) {
29 *wc = c;
30 return 2;
33 if (*s-0x80u >= 0x40) goto ilseq;
34 c = c<<6 | *s++-0x80;
35 if (!(c&(1U<<31))) {
36 *wc = c;
37 return 3;
40 if (*s-0x80u >= 0x40) goto ilseq;
41 *wc = c<<6 | *s++-0x80;
42 return 4;
44 ilseq:
45 errno = EILSEQ;
46 return -1;