changeset 3891:5e9e784a7988

libc: added CONFIG_LIBC_STRING_SMALL
author Alexandre Becoulet <alexandre.becoulet@free.fr>
date Sun, 22 Apr 2018 19:13:49 +0200
parents 775ee0c72c6d
children 774d560020d6
files libc/libc.config libc/string.c
diffstat 2 files changed, 22 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/libc/libc.config	Sun Apr 22 18:49:23 2018 +0200
+++ b/libc/libc.config	Sun Apr 22 19:13:49 2018 +0200
@@ -95,6 +95,12 @@
   depend CONFIG_MUTEK_CONTEXT_SCHED
 %config end
 
+%config CONFIG_LIBC_STRING_SMALL
+  parent CONFIG_LIBC
+  suggest_when CONFIG_COMPILE_OPTIMIZE=s
+  desc Use size optimized string function implementations
+%config end
+
 %config CONFIG_LIBC_FORMATTER
   parent CONFIG_LIBC
   flags auto
--- a/libc/string.c	Sun Apr 22 18:49:23 2018 +0200
+++ b/libc/string.c	Sun Apr 22 19:13:49 2018 +0200
@@ -41,8 +41,10 @@
 inline void * memset(void *dst, int_fast8_t _s, size_t count)
 {
   int8_t s = _s;
+  int8_t *a = dst;
+
+#ifndef CONFIG_LIBC_STRING_SMALL
   const reg_t v = (uint8_t)s * (reg_t)0x0101010101010101LL;
-  int8_t *a = dst;
   reg_t *r;
 
   /* align */
@@ -55,7 +57,11 @@
   for (r = (reg_t*)a; count; count -= sizeof(reg_t))
       *r++ = v;
 
-  for (a = (int8_t*)r; ucount; ucount--)
+  count = ucount;
+  a = (int8_t*)r;
+#endif
+
+  for (; count; count--)
       *a++ = s;
 
   return dst;
@@ -71,6 +77,7 @@
   reg_t *dst = _dst;
   const reg_t *src = _src;
 
+#ifndef CONFIG_LIBC_STRING_SMALL
   if (((uintptr_t)dst & reg_t_log2_m1) == 0 && ((uintptr_t)src & reg_t_log2_m1) == 0) {
     while (size >= sizeof(reg_t)) {
       *dst++ = *src++;
@@ -79,7 +86,7 @@
   } else {
     // In the end, we'll only use register copier loop if non-aligned
     // access is CPU-optimized
-#if (defined(HAS_CPU_ENDIAN_64_NA_STORE) && defined(HAS_CPU_ENDIAN_64_NA_LOAD)) \
+# if (defined(HAS_CPU_ENDIAN_64_NA_STORE) && defined(HAS_CPU_ENDIAN_64_NA_LOAD)) \
   || (CONFIG_CPU_NONALIGNED_ACCESS & 8)
     if (sizeof(reg_t) == 8) {
       while (size >= sizeof(reg_t)) {
@@ -87,8 +94,8 @@
         size -= sizeof(reg_t);
       }
     }
-#endif
-#if (defined(HAS_CPU_ENDIAN_32_NA_STORE) && defined(HAS_CPU_ENDIAN_32_NA_LOAD)) \
+# endif
+# if (defined(HAS_CPU_ENDIAN_32_NA_STORE) && defined(HAS_CPU_ENDIAN_32_NA_LOAD)) \
   || (CONFIG_CPU_NONALIGNED_ACCESS & 4)
     if (sizeof(reg_t) == 4) {
       while (size >= sizeof(reg_t)) {
@@ -96,8 +103,8 @@
         size -= sizeof(reg_t);
       }
     }
-#endif
-#if (defined(HAS_CPU_ENDIAN_16_NA_STORE) && defined(HAS_CPU_ENDIAN_16_NA_LOAD)) \
+# endif
+# if (defined(HAS_CPU_ENDIAN_16_NA_STORE) && defined(HAS_CPU_ENDIAN_16_NA_LOAD)) \
   || (CONFIG_CPU_NONALIGNED_ACCESS & 2)
     if (sizeof(reg_t) == 2) {
       while (size >= sizeof(reg_t)) {
@@ -105,8 +112,9 @@
         size -= sizeof(reg_t);
       }
     }
+# endif
+  }
 #endif
-  }
 
   uint8_t *cdst = (uint8_t*)dst;
   uint8_t *csrc = (uint8_t*)src;