lkml.org 
[lkml]   [2019]   [Feb]   [8]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
Subject[PATCH] lib/string.c: make strncmp() smaller
Space savings on x86_64:

add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-29 (-29)
Function old new delta
strncmp 67 38 -29

Space savings on arm:

add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-60 (-60)
Function old new delta
strncmp 116 56 -60

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---

lib/string.c | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)

--- a/lib/string.c
+++ b/lib/string.c
@@ -344,16 +344,14 @@ EXPORT_SYMBOL(strcmp);
*/
int strncmp(const char *cs, const char *ct, size_t count)
{
- unsigned char c1, c2;
+ while (count--) {
+ unsigned int c1 = *(unsigned char *)cs++;
+ unsigned int c2 = *(unsigned char *)ct++;

- while (count) {
- c1 = *cs++;
- c2 = *ct++;
if (c1 != c2)
- return c1 < c2 ? -1 : 1;
+ return c1 - c2;
if (!c1)
break;
- count--;
}
return 0;
}
\
 
 \ /
  Last update: 2019-02-08 08:34    [W:0.100 / U:0.132 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site