linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] lib/string.c: make strncmp() smaller
@ 2019-02-08  7:34 Alexey Dobriyan
  2019-02-08  9:31 ` Joe Perches
  0 siblings, 1 reply; 3+ messages in thread
From: Alexey Dobriyan @ 2019-02-08  7:34 UTC (permalink / raw)
  To: akpm; +Cc: linux-kernel

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;
 }

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] lib/string.c: make strncmp() smaller
  2019-02-08  7:34 [PATCH] lib/string.c: make strncmp() smaller Alexey Dobriyan
@ 2019-02-08  9:31 ` Joe Perches
  2019-02-09 13:02   ` Alexey Dobriyan
  0 siblings, 1 reply; 3+ messages in thread
From: Joe Perches @ 2019-02-08  9:31 UTC (permalink / raw)
  To: Alexey Dobriyan, akpm; +Cc: linux-kernel

On Fri, 2019-02-08 at 10:34 +0300, Alexey Dobriyan wrote:
> 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
[]
> --- 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;

This does change the return value for most comparisons.

Wasn't there a specific reason for -1 and 1?



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] lib/string.c: make strncmp() smaller
  2019-02-08  9:31 ` Joe Perches
@ 2019-02-09 13:02   ` Alexey Dobriyan
  0 siblings, 0 replies; 3+ messages in thread
From: Alexey Dobriyan @ 2019-02-09 13:02 UTC (permalink / raw)
  To: Joe Perches; +Cc: akpm, linux-kernel

On Fri, Feb 08, 2019 at 01:31:49AM -0800, Joe Perches wrote:
> On Fri, 2019-02-08 at 10:34 +0300, Alexey Dobriyan wrote:

> > -			return c1 < c2 ? -1 : 1;
> > +			return c1 - c2;
> 
> This does change the return value for most comparisons.

It does.

> Wasn't there a specific reason for -1 and 1?

Maybe. Spec says anything positive or negative works.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-02-09 13:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-08  7:34 [PATCH] lib/string.c: make strncmp() smaller Alexey Dobriyan
2019-02-08  9:31 ` Joe Perches
2019-02-09 13:02   ` Alexey Dobriyan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).