[Intel-gfx] [PATCH i-g-t 5/9] tools/intel_iosf_sb_*: Replace if ladder with an array and bsearch()

ville.syrjala at linux.intel.com ville.syrjala at linux.intel.com
Mon Mar 23 06:14:55 PDT 2015


From: Ville Syrjälä <ville.syrjala at linux.intel.com>

Replace the silly strcasecmp() if ladder with and array that maps the
unit names to port numbers. And keep the thing sorted so we can do
the lookup with bsearch() for extra speed :)

Signed-off-by: Ville Syrjälä <ville.syrjala at linux.intel.com>
---
 tools/intel_iosf_sb_read.c  | 59 ++++++++++++++++++++++++++++++---------------
 tools/intel_iosf_sb_write.c | 59 ++++++++++++++++++++++++++++++---------------
 2 files changed, 78 insertions(+), 40 deletions(-)

diff --git a/tools/intel_iosf_sb_read.c b/tools/intel_iosf_sb_read.c
index 03bf007..51b0d11 100644
--- a/tools/intel_iosf_sb_read.c
+++ b/tools/intel_iosf_sb_read.c
@@ -29,6 +29,44 @@
 #include <string.h>
 #include "intel_io.h"
 #include "intel_chipset.h"
+#include "drmtest.h"
+
+/* keep sorted by name for bsearch() */
+static const struct iosf_sb_port {
+	const char *name;
+	uint8_t port;
+} iosf_sb_ports[] = {
+	{ "bunit",   0x03, },
+	{ "cck",     0x14, },
+	{ "ccu",     0xa9, },
+	{ "dpio",    0x12, },
+	{ "dpio2",   0x1a, },
+	{ "flisdsi", 0x1b, },
+	{ "gpio_nc", 0x13, },
+	{ "nc",      0x11, },
+	{ "punit",   0x04, },
+};
+
+static int iosf_sb_port_compare(const void *a, const void *b)
+{
+	const char *name = a;
+	const struct iosf_sb_port *p = b;
+
+	return strcasecmp(name, p->name);
+}
+
+static int iosf_sb_port_parse(const char *name)
+{
+	const struct iosf_sb_port *p;
+
+	p = bsearch(name, iosf_sb_ports, ARRAY_SIZE(iosf_sb_ports),
+		    sizeof(iosf_sb_ports[0]),
+		    iosf_sb_port_compare);
+	if (p)
+		return p->port;
+
+	return strtoul(name, NULL, 16);
+}
 
 static void usage(const char *name)
 {
@@ -48,26 +86,7 @@ int main(int argc, char *argv[])
 		return 1;
 	}
 
-	if (!strcasecmp(argv[1], "bunit"))
-		port = 0x03;
-	else if (!strcasecmp(argv[1], "punit"))
-		port = 0x04;
-	else if (!strcasecmp(argv[1], "nc"))
-		port = 0x11;
-	else if (!strcasecmp(argv[1], "dpio"))
-		port = 0x12;
-	else if (!strcasecmp(argv[1], "gpio_nc"))
-		port = 0x13;
-	else if (!strcasecmp(argv[1], "cck"))
-		port = 0x14;
-	else if (!strcasecmp(argv[1], "ccu"))
-		port = 0xa9;
-	else if (!strcasecmp(argv[1], "dpio2"))
-		port = 0x1a;
-	else if (!strcasecmp(argv[1], "flisdsi"))
-		port = 0x1b;
-	else
-		port = strtoul(argv[1], NULL, 16);
+	port = iosf_sb_port_parse(argv[1]);
 
 	reg = strtoul(argv[2], NULL, 16);
 
diff --git a/tools/intel_iosf_sb_write.c b/tools/intel_iosf_sb_write.c
index 13c738f..f6aa8f1 100644
--- a/tools/intel_iosf_sb_write.c
+++ b/tools/intel_iosf_sb_write.c
@@ -28,6 +28,44 @@
 #include <string.h>
 #include "intel_io.h"
 #include "intel_chipset.h"
+#include "drmtest.h"
+
+/* keep sorted by name for bsearch() */
+static const struct iosf_sb_port {
+	const char *name;
+	uint8_t port;
+} iosf_sb_ports[] = {
+	{ "bunit",   0x03, },
+	{ "cck",     0x14, },
+	{ "ccu",     0xa9, },
+	{ "dpio",    0x12, },
+	{ "dpio2",   0x1a, },
+	{ "flisdsi", 0x1b, },
+	{ "gpio_nc", 0x13, },
+	{ "nc",      0x11, },
+	{ "punit",   0x04, },
+};
+
+static int iosf_sb_port_compare(const void *a, const void *b)
+{
+	const char *name = a;
+	const struct iosf_sb_port *p = b;
+
+	return strcasecmp(name, p->name);
+}
+
+static int iosf_sb_port_parse(const char *name)
+{
+	const struct iosf_sb_port *p;
+
+	p = bsearch(name, iosf_sb_ports, ARRAY_SIZE(iosf_sb_ports),
+		    sizeof(iosf_sb_ports[0]),
+		    iosf_sb_port_compare);
+	if (p)
+		return p->port;
+
+	return strtoul(name, NULL, 16);
+}
 
 static void usage(const char *name)
 {
@@ -47,26 +85,7 @@ int main(int argc, char** argv)
 		return 1;
 	}
 
-	if (!strcasecmp(argv[1], "bunit"))
-		port = 0x03;
-	else if (!strcasecmp(argv[1], "punit"))
-		port = 0x04;
-	else if (!strcasecmp(argv[1], "nc"))
-		port = 0x11;
-	else if (!strcasecmp(argv[1], "dpio"))
-		port = 0x12;
-	else if (!strcasecmp(argv[1], "gpio_nc"))
-		port = 0x13;
-	else if (!strcasecmp(argv[1], "cck"))
-		port = 0x14;
-	else if (!strcasecmp(argv[1], "ccu"))
-		port = 0xa9;
-	else if (!strcasecmp(argv[1], "dpio2"))
-		port = 0x1a;
-	else if (!strcasecmp(argv[1], "flisdsi"))
-		port = 0x1b;
-	else
-		port = strtoul(argv[1], NULL, 16);
+	port = iosf_sb_port_parse(argv[1]);
 
 	reg = strtoul(argv[2], NULL, 16);
 	val = strtoul(argv[3], NULL, 16);
-- 
2.0.5



More information about the Intel-gfx mailing list