9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] [PATCH] silence drawterm runestrchr while(assign) warning
@ 2022-05-22 23:04 Amavect
  2022-05-30  1:02 ` [9front] " Amavect
  2022-05-30  5:15 ` [9front] " ori
  0 siblings, 2 replies; 3+ messages in thread
From: Amavect @ 2022-05-22 23:04 UTC (permalink / raw)
  To: 9front

[-- Attachment #1: Type: text/plain, Size: 1359 bytes --]

Hi,

I want to shut up drawterm compile warnings without resorting to -Wno.
I'll rationalize this with reducing function sizes by bytes,
"improving readability", and micro optimizations for kencc.

Here are some basic transformations on strchr and runestrchr.
It reduces 9front function byte sizes on the arches I tested
(amd64 0x4f->0x43, arm 0x50->0x40, power 0x68->0x5c).
It shuts up a gcc warning about runestrchr while(c1 = *s++)
(when compiled without -Wno-parentheses)
Attached are patches for 9front and drawterm.
Below are the full functions for criticism.

Thanks,
Amavect


char*
strchr(char *s, int c)
{
	char r;

	if(c == 0)
		while(*s++)
			;
	else
		while((r = *s++) != c)
			if(r == 0)
				return 0;
	return s-1;
}


Rune*
runestrchr(Rune *s, Rune c)
{
	Rune r;

	if(c == 0)
		while(*s++)
			;
	else
		while((r = *s++) != c)
			if(r == 0)
				return 0;
	return s-1;
}


unit test:

#include <u.h>
#include <libc.h>

void
main(void)
{
	char *v = "foo bar ss";
	char *e;
	Rune *c = L"foo βαρ ß";
	Rune *t;

	e = strchr(v, L'z');
	assert(e == nil);
	e = strchr(v, L'a');
	assert(e == v+5);
	e = strchr(v, 0);
	assert(e == v+10);

	t = runestrchr(c, L'z');
	assert(t == nil);
	t = runestrchr(c, L'α');
	assert(t == c+5);
	t = runestrchr(c, 0);
	assert(t == c+9);
}

[-- Attachment #2.1: Type: text/plain, Size: 318 bytes --]

from postmaster@9front:
The following attachment had content that we can't
prove to be harmless.  To avoid possible automatic
execution, we changed the content headers.
The original header was:

	Content-Type: text/x-patch
	Content-Transfer-Encoding: 7bit
	Content-Disposition: attachment; filename=strchr.9front.patch

[-- Attachment #2.2: strchr.9front.patch.suspect --]
[-- Type: application/octet-stream, Size: 1063 bytes --]

From: Amavect <amavect@gmail.com>
Date: Sun, 22 May 2022 22:38:11 +0000
Subject: [PATCH] shorten strchr and runestrchr

---
diff f87c5f7f43ce3ab4e160b9985b30eb548ee75646 345def163036bc7b6f9048d43aac1b7d0a3bac69
--- a/sys/src/libc/port/runestrchr.c	Fri May 20 01:58:13 2022
+++ b/sys/src/libc/port/runestrchr.c	Sun May 22 17:38:11 2022
@@ -4,17 +4,14 @@
 Rune*
 runestrchr(Rune *s, Rune c)
 {
-	Rune c0 = c;
-	Rune c1;
+	Rune r;
 
-	if(c == 0) {
+	if(c == 0)
 		while(*s++)
 			;
-		return s-1;
-	}
-
-	while(c1 = *s++)
-		if(c1 == c0)
-			return s-1;
-	return 0;
+	else
+		while((r = *s++) != c)
+			if(r == 0)
+				return 0;
+	return s-1;
 }
--- a/sys/src/libc/port/strchr.c	Fri May 20 01:58:13 2022
+++ b/sys/src/libc/port/strchr.c	Sun May 22 17:38:11 2022
@@ -4,17 +4,14 @@
 char*
 strchr(char *s, int c)
 {
-	char c0 = c;
-	char c1;
+	char r;
 
-	if(c == 0) {
+	if(c == 0)
 		while(*s++)
 			;
-		return s-1;
-	}
-
-	while(c1 = *s++)
-		if(c1 == c0)
-			return s-1;
-	return 0;
+	else
+		while((r = *s++) != c)
+			if(r == 0)
+				return 0;
+	return s-1;
 }


[-- Attachment #3.1: Type: text/plain, Size: 350 bytes --]

from postmaster@9front:
The following attachment had content that we can't
prove to be harmless.  To avoid possible automatic
execution, we changed the content headers.
The original header was:

	Content-Type: text/x-patch
	Content-Transfer-Encoding: 7bit
	Content-Disposition: attachment;
 filename=0001-silence-runestrchr-while-assign-warning.patch

[-- Attachment #3.2: 0001-silence-runestrchr-while-assign-warning.patch.suspect --]
[-- Type: application/octet-stream, Size: 750 bytes --]

 From ff49e28ad04453061925882a1b2b7a1aec7d88eb Mon Sep 17 00:00:00 2001
From: Amavect <amavect@gmail.com>
Date: Sun, 22 May 2022 17:48:37 -0500
Subject: [PATCH] silence runestrchr while(assign) warning

---
 libc/runestrchr.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

diff --git a/libc/runestrchr.c b/libc/runestrchr.c
index af7fc4e..e4c757d 100644
--- a/libc/runestrchr.c
+++ b/libc/runestrchr.c
@@ -4,17 +4,14 @@
 Rune*
 runestrchr(Rune *s, Rune c)
 {
-	Rune c0 = c;
-	Rune c1;
+	Rune r;
 
-	if(c == 0) {
+	if(c == 0)
 		while(*s++)
 			;
-		return s-1;
-	}
-
-	while(c1 = *s++)
-		if(c1 == c0)
-			return s-1;
-	return 0;
+	else
+		while((r = *s++) != c)
+			if(r == 0)
+				return 0;
+	return s-1;
 }
-- 
2.36.0



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

* [9front] Re: [PATCH] silence drawterm runestrchr while(assign) warning
  2022-05-22 23:04 [9front] [PATCH] silence drawterm runestrchr while(assign) warning Amavect
@ 2022-05-30  1:02 ` Amavect
  2022-05-30  5:15 ` [9front] " ori
  1 sibling, 0 replies; 3+ messages in thread
From: Amavect @ 2022-05-30  1:02 UTC (permalink / raw)
  To: 9front

On Sun, 22 May 2022 18:04:36 -0500
Amavect <amavect@gmail.com> wrote:

> I want to shut up drawterm compile warnings without resorting to -Wno.
> I'll rationalize this with reducing function sizes by bytes,
> "improving readability", and micro optimizations for kencc.

Pinging this again.

Thanks,
Amavect

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

* Re: [9front] [PATCH] silence drawterm runestrchr while(assign) warning
  2022-05-22 23:04 [9front] [PATCH] silence drawterm runestrchr while(assign) warning Amavect
  2022-05-30  1:02 ` [9front] " Amavect
@ 2022-05-30  5:15 ` ori
  1 sibling, 0 replies; 3+ messages in thread
From: ori @ 2022-05-30  5:15 UTC (permalink / raw)
  To: 9front

Quoth Amavect <amavect@gmail.com>:
> Hi,
> 
> I want to shut up drawterm compile warnings without resorting to -Wno.
> I'll rationalize this with reducing function sizes by bytes,
> "improving readability", and micro optimizations for kencc.
> 
> Here are some basic transformations on strchr and runestrchr.
> It reduces 9front function byte sizes on the arches I tested
> (amd64 0x4f->0x43, arm 0x50->0x40, power 0x68->0x5c).
> It shuts up a gcc warning about runestrchr while(c1 = *s++)
> (when compiled without -Wno-parentheses)
> Attached are patches for 9front and drawterm.
> Below are the full functions for criticism.

Looks fine to me, thanks.

Committed locally, will push tomorrow after I take a second,
well rested look.

Will also toss your unit tests into the regress test repo.

> Thanks,
> Amavect
> 
> 
> char*
> strchr(char *s, int c)
> {
> 	char r;
> 
> 	if(c == 0)
> 		while(*s++)
> 			;
> 	else
> 		while((r = *s++) != c)
> 			if(r == 0)
> 				return 0;
> 	return s-1;
> }
> 
> 
> Rune*
> runestrchr(Rune *s, Rune c)
> {
> 	Rune r;
> 
> 	if(c == 0)
> 		while(*s++)
> 			;
> 	else
> 		while((r = *s++) != c)
> 			if(r == 0)
> 				return 0;
> 	return s-1;
> }
> 
> 
> unit test:
> 
> #include <u.h>
> #include <libc.h>
> 
> void
> main(void)
> {
> 	char *v = "foo bar ss";
> 	char *e;
> 	Rune *c = L"foo βαρ ß";
> 	Rune *t;
> 
> 	e = strchr(v, L'z');
> 	assert(e == nil);
> 	e = strchr(v, L'a');
> 	assert(e == v+5);
> 	e = strchr(v, 0);
> 	assert(e == v+10);
> 
> 	t = runestrchr(c, L'z');
> 	assert(t == nil);
> 	t = runestrchr(c, L'α');
> 	assert(t == c+5);
> 	t = runestrchr(c, 0);
> 	assert(t == c+9);
> }
> 


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

end of thread, other threads:[~2022-05-30  5:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-22 23:04 [9front] [PATCH] silence drawterm runestrchr while(assign) warning Amavect
2022-05-30  1:02 ` [9front] " Amavect
2022-05-30  5:15 ` [9front] " ori

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).