]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/tile/tilegx/strnlen.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / tile / tilegx / strnlen.c
1 /* Copyright (C) 2013-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
17
18 #include <string.h>
19 #include <stdint.h>
20 #include "string-endian.h"
21
22 /* Find the length of S, but scan at most MAXLEN characters. If no
23 '\0' terminator is found in that many characters, return MAXLEN. */
24 size_t
25 __strnlen (const char *s, size_t maxlen)
26 {
27 /* When maxlen is 0, can't read any bytes or it might cause a page fault. */
28 if (maxlen == 0)
29 return 0;
30
31 /* Get an aligned pointer. */
32 const uintptr_t s_int = (uintptr_t) s;
33 const uint64_t *p = (const uint64_t *) (s_int & -8);
34 size_t bytes_read = sizeof (*p) - (s_int & (sizeof (*p) - 1));
35
36 /* Read and MASK the first word. */
37 uint64_t v = *p | MASK (s_int);
38
39 uint64_t bits;
40 while ((bits = __insn_v1cmpeqi (v, 0)) == 0)
41 {
42 if (bytes_read >= maxlen)
43 {
44 /* Read maxlen bytes and didn't find the terminator. */
45 return maxlen;
46 }
47 v = *++p;
48 bytes_read += sizeof (v);
49 }
50
51 /* Found '\0', check it is not larger than maxlen */
52 size_t len = ((const char *) p) + (CFZ (bits) >> 3) - s;
53 return (len < maxlen ? len : maxlen);
54 }
55 weak_alias (__strnlen, strnlen)
56 libc_hidden_def (strnlen)