]>
git.ipfire.org Git - thirdparty/glibc.git/blob - 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.
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.
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.
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
22 #include "string-endian.h"
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. */
27 __strnlen (const char *s
, size_t maxlen
)
29 /* When maxlen is 0, can't read any bytes or it might cause a page fault. */
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));
38 /* Read and MASK the first word. */
39 uint64_t v
= *p
| MASK (s_int
);
42 while ((bits
= __insn_v1cmpeqi (v
, 0)) == 0)
44 if (bytes_read
>= maxlen
)
46 /* Read maxlen bytes and didn't find the terminator. */
50 bytes_read
+= sizeof (v
);
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
);
57 weak_alias (__strnlen
, strnlen
)
58 libc_hidden_def (strnlen
)