···11+/*22+ * Copyright 2013 Tilera Corporation. All Rights Reserved.33+ *44+ * This program is free software; you can redistribute it and/or55+ * modify it under the terms of the GNU General Public License66+ * as published by the Free Software Foundation, version 2.77+ *88+ * This program is distributed in the hope that it will be useful, but99+ * WITHOUT ANY WARRANTY; without even the implied warranty of1010+ * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or1111+ * NON INFRINGEMENT. See the GNU General Public License for1212+ * more details.1313+ */1414+1515+#include <linux/types.h>1616+#include <linux/string.h>1717+#include <linux/module.h>1818+1919+size_t strnlen(const char *s, size_t count)2020+{2121+ /* Get an aligned pointer. */2222+ const uintptr_t s_int = (uintptr_t) s;2323+ const uint32_t *p = (const uint32_t *)(s_int & -4);2424+ size_t bytes_read = sizeof(*p) - (s_int & (sizeof(*p) - 1));2525+ size_t len;2626+ uint32_t v, bits;2727+2828+ /* Avoid page fault risk by not reading any bytes when count is 0. */2929+ if (count == 0)3030+ return 0;3131+3232+ /* Read first word, but force bytes before the string to be nonzero. */3333+ v = *p | ((1 << ((s_int << 3) & 31)) - 1);3434+3535+ while ((bits = __insn_seqb(v, 0)) == 0) {3636+ if (bytes_read >= count) {3737+ /* Read COUNT bytes and didn't find the terminator. */3838+ return count;3939+ }4040+ v = *++p;4141+ bytes_read += sizeof(v);4242+ }4343+4444+ len = ((const char *) p) + (__insn_ctz(bits) >> 3) - s;4545+ return (len < count ? len : count);4646+}4747+EXPORT_SYMBOL(strnlen);
+48
arch/tile/lib/strnlen_64.c
···11+/*22+ * Copyright 2013 Tilera Corporation. All Rights Reserved.33+ *44+ * This program is free software; you can redistribute it and/or55+ * modify it under the terms of the GNU General Public License66+ * as published by the Free Software Foundation, version 2.77+ *88+ * This program is distributed in the hope that it will be useful, but99+ * WITHOUT ANY WARRANTY; without even the implied warranty of1010+ * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or1111+ * NON INFRINGEMENT. See the GNU General Public License for1212+ * more details.1313+ */1414+1515+#include <linux/types.h>1616+#include <linux/string.h>1717+#include <linux/module.h>1818+#include "string-endian.h"1919+2020+size_t strnlen(const char *s, size_t count)2121+{2222+ /* Get an aligned pointer. */2323+ const uintptr_t s_int = (uintptr_t) s;2424+ const uint64_t *p = (const uint64_t *)(s_int & -8);2525+ size_t bytes_read = sizeof(*p) - (s_int & (sizeof(*p) - 1));2626+ size_t len;2727+ uint64_t v, bits;2828+2929+ /* Avoid page fault risk by not reading any bytes when count is 0. */3030+ if (count == 0)3131+ return 0;3232+3333+ /* Read and MASK the first word. */3434+ v = *p | MASK(s_int);3535+3636+ while ((bits = __insn_v1cmpeqi(v, 0)) == 0) {3737+ if (bytes_read >= count) {3838+ /* Read COUNT bytes and didn't find the terminator. */3939+ return count;4040+ }4141+ v = *++p;4242+ bytes_read += sizeof(v);4343+ }4444+4545+ len = ((const char *) p) + (CFZ(bits) >> 3) - s;4646+ return (len < count ? len : count);4747+}4848+EXPORT_SYMBOL(strnlen);