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