From 38e3c58cba951ff43f8314edfd0adb8082d81f8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Syrj=C3=A4l=C3=A4?= Date: Thu, 5 Feb 2015 19:44:40 +0200 Subject: [PATCH] tools/intel_iosf_sb_*: Replace if ladder with an array and bsearch() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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ä --- 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 03bf0076..51b0d114 100644 --- a/tools/intel_iosf_sb_read.c +++ b/tools/intel_iosf_sb_read.c @@ -29,6 +29,44 @@ #include #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 13c738f1..f6aa8f1c 100644 --- a/tools/intel_iosf_sb_write.c +++ b/tools/intel_iosf_sb_write.c @@ -28,6 +28,44 @@ #include #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);