Remove powerpc, sparc fdim inlines (bug 22987).
[glibc.git] / sysdeps / tile / strnlen.c
blobc7c72818dffd9a01c59b4f08c1143e133c84e197
1 /* Copyright (C) 2013-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
18 #include <string.h>
19 #include <stdint.h>
20 #include "string-endian.h"
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)
27 /* When maxlen is 0, can't read any bytes or it might cause a page fault. */
28 if (maxlen == 0)
29 return 0;
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));
36 /* Read and MASK the first word. */
37 uint64_t v = *p | MASK (s_int);
39 uint64_t bits;
40 while ((bits = v1cmpeqi (v, 0)) == 0)
42 if (bytes_read >= maxlen)
44 /* Read maxlen bytes and didn't find the terminator. */
45 return maxlen;
47 v = *++p;
48 bytes_read += sizeof (v);
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);
55 libc_hidden_def (__strnlen)
56 weak_alias (__strnlen, strnlen)
57 libc_hidden_def (strnlen)