Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tile: optimize strnlen using SIMD instructions

Using strlen as a model, add length checking to create strnlen.

Signed-off-by: Ken Steele <ken@tilera.com>
Signed-off-by: Chris Metcalf <cmetcalf@tilera.com>

authored by

Ken Steele and committed by
Chris Metcalf
5916700c c53c70a9

+98 -1
+2
arch/tile/include/asm/string.h
··· 21 21 #define __HAVE_ARCH_MEMMOVE 22 22 #define __HAVE_ARCH_STRCHR 23 23 #define __HAVE_ARCH_STRLEN 24 + #define __HAVE_ARCH_STRNLEN 24 25 25 26 extern __kernel_size_t strlen(const char *); 27 + extern __kernel_size_t strnlen(const char *, __kernel_size_t); 26 28 extern char *strchr(const char *s, int c); 27 29 extern void *memchr(const void *s, int c, size_t n); 28 30 extern void *memset(void *, int, __kernel_size_t);
+1 -1
arch/tile/lib/Makefile
··· 4 4 5 5 lib-y = cacheflush.o checksum.o cpumask.o delay.o uaccess.o \ 6 6 memmove.o memcpy_$(BITS).o memchr_$(BITS).o memset_$(BITS).o \ 7 - strchr_$(BITS).o strlen_$(BITS).o 7 + strchr_$(BITS).o strlen_$(BITS).o strnlen_$(BITS).o 8 8 9 9 ifeq ($(CONFIG_TILEGX),y) 10 10 CFLAGS_REMOVE_memcpy_user_64.o = -fno-omit-frame-pointer
+47
arch/tile/lib/strnlen_32.c
··· 1 + /* 2 + * Copyright 2013 Tilera Corporation. All Rights Reserved. 3 + * 4 + * This program is free software; you can redistribute it and/or 5 + * modify it under the terms of the GNU General Public License 6 + * as published by the Free Software Foundation, version 2. 7 + * 8 + * This program is distributed in the hope that it will be useful, but 9 + * WITHOUT ANY WARRANTY; without even the implied warranty of 10 + * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or 11 + * NON INFRINGEMENT. See the GNU General Public License for 12 + * more details. 13 + */ 14 + 15 + #include <linux/types.h> 16 + #include <linux/string.h> 17 + #include <linux/module.h> 18 + 19 + size_t strnlen(const char *s, size_t count) 20 + { 21 + /* Get an aligned pointer. */ 22 + const uintptr_t s_int = (uintptr_t) s; 23 + const uint32_t *p = (const uint32_t *)(s_int & -4); 24 + size_t bytes_read = sizeof(*p) - (s_int & (sizeof(*p) - 1)); 25 + size_t len; 26 + uint32_t v, bits; 27 + 28 + /* Avoid page fault risk by not reading any bytes when count is 0. */ 29 + if (count == 0) 30 + return 0; 31 + 32 + /* Read first word, but force bytes before the string to be nonzero. */ 33 + v = *p | ((1 << ((s_int << 3) & 31)) - 1); 34 + 35 + while ((bits = __insn_seqb(v, 0)) == 0) { 36 + if (bytes_read >= count) { 37 + /* Read COUNT bytes and didn't find the terminator. */ 38 + return count; 39 + } 40 + v = *++p; 41 + bytes_read += sizeof(v); 42 + } 43 + 44 + len = ((const char *) p) + (__insn_ctz(bits) >> 3) - s; 45 + return (len < count ? len : count); 46 + } 47 + EXPORT_SYMBOL(strnlen);
+48
arch/tile/lib/strnlen_64.c
··· 1 + /* 2 + * Copyright 2013 Tilera Corporation. All Rights Reserved. 3 + * 4 + * This program is free software; you can redistribute it and/or 5 + * modify it under the terms of the GNU General Public License 6 + * as published by the Free Software Foundation, version 2. 7 + * 8 + * This program is distributed in the hope that it will be useful, but 9 + * WITHOUT ANY WARRANTY; without even the implied warranty of 10 + * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or 11 + * NON INFRINGEMENT. See the GNU General Public License for 12 + * more details. 13 + */ 14 + 15 + #include <linux/types.h> 16 + #include <linux/string.h> 17 + #include <linux/module.h> 18 + #include "string-endian.h" 19 + 20 + size_t strnlen(const char *s, size_t count) 21 + { 22 + /* Get an aligned pointer. */ 23 + const uintptr_t s_int = (uintptr_t) s; 24 + const uint64_t *p = (const uint64_t *)(s_int & -8); 25 + size_t bytes_read = sizeof(*p) - (s_int & (sizeof(*p) - 1)); 26 + size_t len; 27 + uint64_t v, bits; 28 + 29 + /* Avoid page fault risk by not reading any bytes when count is 0. */ 30 + if (count == 0) 31 + return 0; 32 + 33 + /* Read and MASK the first word. */ 34 + v = *p | MASK(s_int); 35 + 36 + while ((bits = __insn_v1cmpeqi(v, 0)) == 0) { 37 + if (bytes_read >= count) { 38 + /* Read COUNT bytes and didn't find the terminator. */ 39 + return count; 40 + } 41 + v = *++p; 42 + bytes_read += sizeof(v); 43 + } 44 + 45 + len = ((const char *) p) + (CFZ(bits) >> 3) - s; 46 + return (len < count ? len : count); 47 + } 48 + EXPORT_SYMBOL(strnlen);