Patchwork [08/13] cpuid: simplify CPUID flag search function

login
register
mail settings
Submitter Andre Przywara
Date Feb. 2, 2010, 10:08 a.m.
Message ID <1265105301-3455-9-git-send-email-andre.przywara@amd.com>
Download mbox | patch
Permalink /patch/44270/
State New
Headers show

Comments

Andre Przywara - Feb. 2, 2010, 10:08 a.m.
avoid code duplication and handle the CPUID flag name search in a
loop.

Signed-off-by: Andre Przywara <andre.przywara@amd.com>
---
 target-i386/cpuid.c |   38 +++++++++++++-------------------------
 1 files changed, 13 insertions(+), 25 deletions(-)

Patch

diff --git a/target-i386/cpuid.c b/target-i386/cpuid.c
index 3f56c50..635c2f4 100644
--- a/target-i386/cpuid.c
+++ b/target-i386/cpuid.c
@@ -90,34 +90,22 @@  static void add_flagname_to_bitmaps(const char *flagname, uint32_t *features,
                                     uint32_t *ext3_features,
                                     uint32_t *kvm_features)
 {
-    int i;
+    int i, j;
     int found = 0;
-
-    for ( i = 0 ; i < 32 ; i++ )
-        if (feature_name[i] && !strcmp (flagname, feature_name[i])) {
-            *features |= 1 << i;
-            found = 1;
-        }
-    for ( i = 0 ; i < 32 ; i++ )
-        if (ext_feature_name[i] && !strcmp (flagname, ext_feature_name[i])) {
-            *ext_features |= 1 << i;
-            found = 1;
-        }
-    for ( i = 0 ; i < 32 ; i++ )
-        if (ext2_feature_name[i] && !strcmp (flagname, ext2_feature_name[i])) {
-            *ext2_features |= 1 << i;
-            found = 1;
-        }
-    for ( i = 0 ; i < 32 ; i++ )
-        if (ext3_feature_name[i] && !strcmp (flagname, ext3_feature_name[i])) {
-            *ext3_features |= 1 << i;
-            found = 1;
-        }
-    for ( i = 0 ; i < 32 ; i++ )
-        if (kvm_feature_name[i] && !strcmp (flagname, kvm_feature_name[i])) {
-            *kvm_features |= 1 << i;
-            found = 1;
+    const char ** feature_names[5] = {feature_name, ext_feature_name,
+                                      ext2_feature_name, ext3_feature_name,
+                                      kvm_feature_name};
+    uint32_t* feature_flags[5] = {features, ext_features, ext2_features,
+                                  ext3_features, kvm_features};
+
+    for (j = 0; j < 5; j++) {
+        for ( i = 0 ; i < 32 ; i++ ) {
+            if (feature_names[j][i] && !strcmp(flagname, feature_names[j][i])) {
+                *feature_flags[j] |= 1 << i;
+                found = 1;
+            }
         }
+    }
 
     if (!found) {
         fprintf(stderr, "CPU feature %s not found\n", flagname);