1 Subject: [PATCH] AVR32-optimized string operations
3 Add hand-optimized AVR32-specific string operations. Some of them
4 need a bit more testing, though.
8 libc/string/avr32/Makefile | 40 +++++++++++
9 libc/string/avr32/bcopy.S | 15 ++++
10 libc/string/avr32/bzero.S | 12 +++
11 libc/string/avr32/memchr.S | 62 +++++++++++++++++
12 libc/string/avr32/memcmp.S | 50 +++++++++++++
13 libc/string/avr32/memcpy.S | 110 ++++++++++++++++++++++++++++++
14 libc/string/avr32/memmove.S | 114 +++++++++++++++++++++++++++++++
15 libc/string/avr32/memset.S | 60 ++++++++++++++++
16 libc/string/avr32/strcat.S | 95 ++++++++++++++++++++++++++
17 libc/string/avr32/strcmp.S | 80 ++++++++++++++++++++++
18 libc/string/avr32/strcpy.S | 63 +++++++++++++++++
19 libc/string/avr32/stringtest.c | 144 ++++++++++++++++++++++++++++++++++++++++
20 libc/string/avr32/strlen.S | 52 ++++++++++++++
21 libc/string/avr32/strncpy.S | 77 +++++++++++++++++++++
22 libc/string/avr32/test_memcpy.c | 66 ++++++++++++++++++
23 15 files changed, 1040 insertions(+)
25 Index: uClibc-0.9.28-avr32/libc/string/avr32/bcopy.S
26 ===================================================================
27 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
28 +++ uClibc-0.9.28-avr32/libc/string/avr32/bcopy.S 2006-10-19 15:05:52.000000000 +0200
31 + * Copyright (C) 2004 Atmel Norway
36 + .type bcopy, @function
39 + /* Swap the first two arguments */
44 + .size bcopy, . - bcopy
45 Index: uClibc-0.9.28-avr32/libc/string/avr32/bzero.S
46 ===================================================================
47 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
48 +++ uClibc-0.9.28-avr32/libc/string/avr32/bzero.S 2006-10-19 15:05:52.000000000 +0200
51 + * Copyright (C) 2004 Atmel Norway
56 + .type bzero, @function
62 Index: uClibc-0.9.28-avr32/libc/string/avr32/Makefile
63 ===================================================================
64 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
65 +++ uClibc-0.9.28-avr32/libc/string/avr32/Makefile 2006-10-19 15:05:52.000000000 +0200
67 +# Makefile for uClibc
69 +# Copyright (C) 2000-2003 Erik Andersen <andersen@uclibc.org>
71 +# This program is free software; you can redistribute it and/or modify it under
72 +# the terms of the GNU Library General Public License as published by the Free
73 +# Software Foundation; either version 2 of the License, or (at your option) any
76 +# This program is distributed in the hope that it will be useful, but WITHOUT
77 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
78 +# FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more
81 +# You should have received a copy of the GNU Library General Public License
82 +# along with this program; if not, write to the Free Software Foundation, Inc.,
83 +# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
86 +include $(TOPDIR)Rules.mak
88 +SSRC := bcopy.S bzero.S memcmp.S memcpy.S memmove.S
89 +SSRC += memset.S strcmp.S strlen.S
90 +# memchr.S, strcat.S, strcpy.S, strncpy.S is broken
91 +SOBJS := $(patsubst %.S,%.o, $(SSRC))
94 +OBJ_LIST:= ../../obj.string.$(TARGET_ARCH)
99 + echo $(addprefix string/$(TARGET_ARCH)/, $(OBJS)) > $@
102 + $(CC) $(ASFLAGS) -c $< -o $@
103 + $(STRIPTOOL) -x -R .note -R .comment $@
106 + $(RM) *.[oa] *~ core
107 Index: uClibc-0.9.28-avr32/libc/string/avr32/memchr.S
108 ===================================================================
109 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
110 +++ uClibc-0.9.28-avr32/libc/string/avr32/memchr.S 2006-10-19 15:05:52.000000000 +0200
113 + * Copyright (C) 2004 Atmel Norway
122 + .type memchr, @function
124 + or chr, chr, chr << 8
125 + or chr, chr, chr << 16
129 + brne .Lunaligned_str
139 + bfextu r9, r8, 24, 8
143 + bfextu r9, r8, 16, 8
147 + bfextu r9, r8, 8, 8
173 + rjmp .Laligned_search
174 Index: uClibc-0.9.28-avr32/libc/string/avr32/memcmp.S
175 ===================================================================
176 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
177 +++ uClibc-0.9.28-avr32/libc/string/avr32/memcmp.S 2006-10-20 10:42:09.000000000 +0200
180 + * Copyright (C) 2004 Atmel Norway.
189 + .type memcmp, @function
217 + bfextu r8, r9, 24, 8
219 + bfextu r8, r9, 16, 8
221 + bfextu r8, r9, 8, 8
225 + .size memcmp, . - memcmp
229 Index: uClibc-0.9.28-avr32/libc/string/avr32/memcpy.S
230 ===================================================================
231 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
232 +++ uClibc-0.9.28-avr32/libc/string/avr32/memcpy.S 2006-10-19 15:05:52.000000000 +0200
235 + * Copyright (C) 2004 Atmel Norway
238 +/* Don't use r12 as dst since we must return it unmodified */
245 + .type memcpy, @function
249 + .type __memcpy, @function
255 + /* If we have less than 32 bytes, don't do anything fancy */
257 + brge .Lmore_than_31
270 + /* Check alignment */
273 + brne .Lunaligned_src
276 + brne .Lunaligned_dst
280 + brlt .Lless_than_32
282 +1: /* Copy 32 bytes at a time */
291 + /* Copy 16 more bytes if possible */
293 + brlt .Lless_than_16
301 + /* Do the remaining as byte copies */
303 + add pc, pc, len << 2
312 + /* Make src cacheline-aligned. r8 = (src & 31) */
320 + /* If dst is word-aligned, we're ready to go */
324 + breq .Laligned_copy
327 + /* src is aligned, but dst is not. Expect bad performance */
336 + add pc, pc, len << 2
343 + .size memcpy, . - memcpy
344 Index: uClibc-0.9.28-avr32/libc/string/avr32/memmove.S
345 ===================================================================
346 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
347 +++ uClibc-0.9.28-avr32/libc/string/avr32/memmove.S 2006-10-19 15:05:52.000000000 +0200
350 + * Copyright (C) 2004 Atmel Norway
359 + .type memmove, @function
363 + .type __memmove, @function
374 + * The rest is basically the same as in memcpy.S except that
375 + * the direction is reversed.
378 + brge .Lmore_than_31
391 + /* Check alignment */
394 + brne .Lunaligned_src
397 + brne .Lunaligned_dst
401 + brlt .Lless_than_32
403 +1: /* Copy 32 bytes at a time */
412 + /* Copy 16 more bytes if possible */
414 + brlt .Lless_than_16
422 + /* Do the remaining as byte copies */
433 + /* Make src cacheline-aligned. r8 = (src & 31) */
440 + /* If dst is word-aligned, we're ready to go */
444 + breq .Laligned_copy
447 + /* src is aligned, but dst is not. Expect bad performance */
456 + add pc, pc, len << 2
463 Index: uClibc-0.9.28-avr32/libc/string/avr32/memset.S
464 ===================================================================
465 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
466 +++ uClibc-0.9.28-avr32/libc/string/avr32/memset.S 2006-10-20 10:42:15.000000000 +0200
469 + * Copyright (C) 2004 Atmel Norway.
478 + .type memset, @function
482 + .type __memset, @function
489 + brge .Llarge_memset
503 + bfins r8, r8, 16, 16
520 + /* If we are done, n == -8 and we'll skip all st.b insns below */
528 Index: uClibc-0.9.28-avr32/libc/string/avr32/strcat.S
529 ===================================================================
530 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
531 +++ uClibc-0.9.28-avr32/libc/string/avr32/strcat.S 2006-10-19 15:05:52.000000000 +0200
534 + * Copyright (C) 2004 Atmel Norway
542 + .type strcat, @function
547 + /* Make sure s1 is word-aligned */
552 + add pc, pc, r10 << 3
553 + sub r0, r0, 0 /* 4-byte nop */
564 + /* Find the end of the first string */
571 + bfextu r10, r8, 24, 8
575 + bfextu r10, r8, 16, 8
579 + bfextu r10, r8, 8, 8
585 + /* Now, append s2 */
603 + /* Copy one word at a time */
612 + /* Copy the remaining bytes */
613 + bfextu r10, r8, 24, 8
617 + bfextu r10, r8, 16, 8
621 + bfextu r10, r8, 8, 8
627 + .size strcat, . - strcat
628 Index: uClibc-0.9.28-avr32/libc/string/avr32/strcmp.S
629 ===================================================================
630 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
631 +++ uClibc-0.9.28-avr32/libc/string/avr32/strcmp.S 2006-10-19 15:05:52.000000000 +0200
634 + * Copyright (C) 2004 Atmel Norway.
643 + .type strcmp, @function
648 + brne .Lunaligned_s1
650 + brne .Lunaligned_s2
660 +2: bfextu r12, r8, 24, 8
661 + bfextu r11, r9, 24, 8
666 + bfextu r12, r8, 16, 8
667 + bfextu r11, r9, 16, 8
672 + bfextu r12, r8, 8, 8
673 + bfextu r11, r9, 8, 8
678 + bfextu r12, r8, 0, 8
679 + bfextu r11, r9, 0, 8
699 + * s1 and s2 can't both be aligned, and unaligned word loads
700 + * can trigger spurious exceptions if we cross a page boundary.
701 + * Do it the slow way...
713 Index: uClibc-0.9.28-avr32/libc/string/avr32/strcpy.S
714 ===================================================================
715 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
716 +++ uClibc-0.9.28-avr32/libc/string/avr32/strcpy.S 2006-10-19 15:05:52.000000000 +0200
719 + * Copyright (C) 2004 Atmel Norway
721 + * To reduce the size, this one might simply call strncpy with len = -1.
729 + .type strcpy, @function
736 + * Check alignment. If src is aligned but dst isn't, we can't
737 + * do much about it...
741 + brne .Lunaligned_src
751 + * Ok, r8 now contains the terminating '\0'. Copy the
752 + * remaining bytes individually.
754 + bfextu r10, r8, 24, 8
758 + bfextu r10, r8, 16, 8
762 + bfextu r10, r8, 8, 8
770 + /* Copy bytes until we're aligned */
772 + add pc, pc, r8 << 3
780 + rjmp .Laligned_copy
781 Index: uClibc-0.9.28-avr32/libc/string/avr32/stringtest.c
782 ===================================================================
783 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
784 +++ uClibc-0.9.28-avr32/libc/string/avr32/stringtest.c 2006-10-19 15:05:52.000000000 +0200
790 +#include <sys/mman.h>
792 +#define BUF_SIZE (8 * 1024)
795 +static char *buf1_ref;
798 +extern void *optimized_memcpy(void *dest, void *src, size_t len);
799 +extern void *optimized_memmove(void *dest, void *src, size_t len);
800 +extern char *optimized_strcpy(char *dest, char *src);
801 +extern char *optimized_strncpy(char *dest, char *src, size_t len);
803 +void dump_mismatch(char *buf, char *ref, size_t len)
807 + for (i = 0; i < len; i += 16) {
808 + if (memcmp(buf + i, ref + i, 16) == 0)
811 + printf("%4x buf:", i);
812 + for (j = i; j < (i + 16); j++)
813 + printf(" %02x", buf[j]);
815 + for (j = i; j < (i + 16); j++)
816 + printf(" %02x", ref[j]);
821 +static void test_memcpy(int src_offset, int dst_offset, int len)
823 + clock_t start, old, new;
826 + memset(buf1, 0x55, BUF_SIZE);
827 + memset(buf1_ref, 0x55, BUF_SIZE);
828 + memset(buf2, 0xaa, BUF_SIZE);
830 + printf("Testing memcpy with offsets %d => %d and len %d...",
831 + src_offset, dst_offset, len);
834 + for (i = 0; i < 8192; i++)
835 + optimized_memcpy(buf1 + dst_offset, buf2 + src_offset, len);
836 + new = clock() - start;
838 + for ( i = 0; i < 8192; i++)
839 + memcpy(buf1_ref + dst_offset, buf2 + src_offset, len);
840 + old = clock() - start;
842 + if (memcmp(buf1, buf1_ref, BUF_SIZE) == 0)
845 + printf("FAILED\n");
846 + dump_mismatch(buf1, buf1_ref, BUF_SIZE);
848 + printf("CPU time used: %d vs. %d\n", new, old);
851 +static void test_memmove(int src_offset, int dst_offset, int len)
853 + clock_t start, old, new;
855 + memset(buf1, 0x55, BUF_SIZE);
856 + memset(buf1_ref, 0x55, BUF_SIZE);
857 + memset(buf2, 0xaa, BUF_SIZE);
859 + printf("Testing memmove with offsets %d => %d and len %d...",
860 + src_offset, dst_offset, len);
863 + optimized_memmove(buf1 + dst_offset, buf2 + src_offset, len);
864 + new = clock() - start;
866 + memmove(buf1_ref + dst_offset, buf2 + src_offset, len);
867 + old = clock() - start;
869 + if (memcmp(buf1, buf1_ref, BUF_SIZE) == 0)
872 + printf("FAILED\n");
873 + dump_mismatch(buf1, buf1_ref, BUF_SIZE);
875 + printf("CPU time used: %d vs. %d\n", new, old);
878 +int main(int argc, char *argv[])
880 + buf2 = mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE,
881 + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
882 + if (buf2 == MAP_FAILED) {
883 + perror("Failed to allocate memory for buf2");
886 + buf1 = mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE,
887 + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
888 + if (buf1 == MAP_FAILED) {
889 + perror("Failed to allocate memory for buf1");
892 + buf1_ref = mmap(NULL, BUF_SIZE, PROT_READ | PROT_WRITE,
893 + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
894 + if (buf1_ref == MAP_FAILED) {
895 + perror("Failed to allocate memory for buf1_ref");
898 + printf("\n === MEMCPY ===\n\n");
900 + test_memcpy(0, 0, BUF_SIZE - 32);
901 + test_memcpy(0, 0, 1);
902 + test_memcpy(0, 0, 31);
903 + test_memcpy(0, 0, 32);
904 + test_memcpy(0, 0, 127);
905 + test_memcpy(0, 0, 128);
906 + test_memcpy(4, 4, BUF_SIZE - 32 - 4);
907 + test_memcpy(1, 1, BUF_SIZE - 32 - 1);
908 + test_memcpy(1, 1, 126);
909 + test_memcpy(0, 3, 128);
910 + test_memcpy(1, 4, 128);
911 + test_memcpy(0, 0, 0);
913 + printf("\n === MEMMOVE ===\n\n");
915 + test_memmove(0, 0, BUF_SIZE - 32);
916 + test_memmove(0, 0, 1);
917 + test_memmove(0, 0, 31);
918 + test_memmove(0, 0, 32);
919 + test_memmove(0, 0, BUF_SIZE - 33);
920 + test_memmove(0, 0, 128);
921 + test_memmove(4, 4, BUF_SIZE - 32 - 4);
922 + test_memmove(1, 1, BUF_SIZE - 32 - 1);
923 + test_memmove(1, 1, BUF_SIZE - 130);
924 + test_memmove(0, 3, BUF_SIZE - 128);
925 + test_memmove(1, 4, BUF_SIZE - 128);
926 + test_memmove(0, 0, 0);
930 Index: uClibc-0.9.28-avr32/libc/string/avr32/strlen.S
931 ===================================================================
932 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
933 +++ uClibc-0.9.28-avr32/libc/string/avr32/strlen.S 2006-10-19 15:05:52.000000000 +0200
936 + * Copyright (C) 2004 Atmel Norway
943 + .type strlen, @function
949 + brne .Lunaligned_str
956 + bfextu r9, r8, 24, 8
960 + bfextu r9, r8, 16, 8
964 + bfextu r9, r8, 8, 8
972 + add pc, pc, r9 << 3
973 + sub r0, r0, 0 /* 4-byte nop */
987 Index: uClibc-0.9.28-avr32/libc/string/avr32/strncpy.S
988 ===================================================================
989 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
990 +++ uClibc-0.9.28-avr32/libc/string/avr32/strncpy.S 2006-10-19 15:05:52.000000000 +0200
993 + * Copyright (C) 2004 Atmel Norway
1001 + .type strncpy, @function
1009 + * Check alignment. If src is aligned but dst isn't, we can't
1010 + * do much about it...
1014 + brne .Lunaligned_src
1029 + /* This is safe as long as src is word-aligned and r10 > 0 */
1033 + * Ok, r8 now contains the terminating '\0'. Copy the
1034 + * remaining bytes individually.
1036 + bfextu r11, r8, 24, 8
1042 + bfextu r11, r8, 16, 8
1048 + bfextu r11, r8, 8, 8
1058 + /* Copy bytes until we're aligned */
1063 +1: ld.ub r10, src++
1068 + rjmp .Laligned_copy
1069 Index: uClibc-0.9.28-avr32/libc/string/avr32/test_memcpy.c
1070 ===================================================================
1071 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1072 +++ uClibc-0.9.28-avr32/libc/string/avr32/test_memcpy.c 2006-10-19 15:05:52.000000000 +0200
1076 +#include <string.h>
1078 +#define BUF_SIZE 32768
1080 +static char buf1[BUF_SIZE] __attribute__((aligned(32)));
1081 +static char buf1_ref[BUF_SIZE] __attribute__((aligned(32)));
1082 +static char buf2[BUF_SIZE] __attribute__((aligned(32)));
1084 +extern void *new_memcpy(void *dest, void *src, size_t len);
1086 +void dump_mismatch(char *buf, char *ref, size_t len)
1090 + for (i = 0; i < len; i += 16) {
1091 + if (memcmp(buf + i, ref + i, 16) == 0)
1094 + printf("% 4x buf:", i);
1095 + for (j = i; j < (i + 16); j++)
1096 + printf(" %02x", buf[j]);
1097 + printf("\n ref:");
1098 + for (j = i; j < (i + 16); j++)
1099 + printf(" %02x", ref[j]);
1104 +void test(int src_offset, int dst_offset, int len)
1106 + memset(buf1, 0x55, sizeof(buf1));
1107 + memset(buf1_ref, 0x55, sizeof(buf1_ref));
1108 + memset(buf2, 0xaa, sizeof(buf2));
1110 + printf("Testing with offsets %d => %d and len %d...",
1111 + src_offset, dst_offset, len);
1113 + new_memcpy(buf1 + dst_offset, buf2 + src_offset, len);
1114 + memcpy(buf1_ref + dst_offset, buf2 + src_offset, len);
1116 + if (memcmp(buf1, buf1_ref, sizeof(buf1)) == 0)
1119 + printf("FAILED\n");
1120 + dump_mismatch(buf1, buf1_ref, sizeof(buf1));
1124 +int main(int argc, char *argv[])
1126 + test(0, 0, BUF_SIZE);
1132 + test(4, 4, BUF_SIZE - 4);
1133 + test(1, 1, BUF_SIZE - 1);