From c1cd11726546017d7bf733bd8670a8c77f6a2347 Mon Sep 17 00:00:00 2001 From: Grzegorz Wawiorko <35483345+gwawiork@users.noreply.github.com> Date: Thu, 11 Apr 2019 13:10:45 +0200 Subject: [PATCH] Make wimpy factor argument parsing logic common (#155) --- test_common/harness/parseParameters.cpp | 24 +++++++++++++++++++ test_common/harness/parseParameters.h | 3 +++ .../conversions/test_conversions.c | 20 +--------------- test_conformance/half/main.c | 20 +--------------- test_conformance/math_brute_force/main.c | 19 +-------------- test_conformance/select/test_select.c | 20 +--------------- 6 files changed, 31 insertions(+), 75 deletions(-) diff --git a/test_common/harness/parseParameters.cpp b/test_common/harness/parseParameters.cpp index 9c227a14..02ef94bd 100644 --- a/test_common/harness/parseParameters.cpp +++ b/test_common/harness/parseParameters.cpp @@ -127,3 +127,27 @@ int parseCustomParam (int argc, const char *argv[], const char *ignore) } return argc; } + +bool is_power_of_two(int number) +{ + return number && !(number & (number - 1)); +} + +extern void parseWimpyReductionFactor(const char *&arg, int &wimpyReductionFactor) +{ + const char *arg_temp = strchr(&arg[1], ']'); + if (arg_temp != 0) + { + int new_factor = atoi(&arg[1]); + arg = arg_temp; // Advance until ']' + if (is_power_of_two(new_factor)) + { + log_info("\n Wimpy reduction factor changed from %d to %d \n", wimpyReductionFactor, new_factor); + wimpyReductionFactor = new_factor; + } + else + { + log_info("\n WARNING: Incorrect wimpy reduction factor %d, must be power of 2. The default value will be used.\n", new_factor); + } + } +} diff --git a/test_common/harness/parseParameters.h b/test_common/harness/parseParameters.h index 13171332..4fb69a4c 100644 --- a/test_common/harness/parseParameters.h +++ b/test_common/harness/parseParameters.h @@ -16,6 +16,7 @@ #ifndef _parseParameters_h #define _parseParameters_h +#include "compat.h" #include extern bool gOfflineCompiler; @@ -34,4 +35,6 @@ extern OfflineCompilerOutputType gOfflineCompilerOutputType; extern int parseCustomParam (int argc, const char *argv[], const char *ignore = 0 ); +extern void parseWimpyReductionFactor(const char *&arg, int &wimpyReductionFactor); + #endif // _parseParameters_h diff --git a/test_conformance/conversions/test_conversions.c b/test_conformance/conversions/test_conversions.c index 2f277df7..a5a0f70c 100644 --- a/test_conformance/conversions/test_conversions.c +++ b/test_conformance/conversions/test_conversions.c @@ -441,25 +441,7 @@ static int ParseArgs( int argc, const char **argv ) gWimpyMode ^= 1; break; case '[': - // wimpy reduction factor can be set with the option -[2^n] - // Default factor is 128, and n practically can be from 1 to 12 - { - const char *arg_temp = strchr(&arg[1], ']'); - if (arg_temp != 0) - { - int new_factor = atoi(&arg[1]); - arg = arg_temp; // Advance until ']' - if (new_factor && !(new_factor & (new_factor - 1))) - { - vlog(" WimpyReduction factor changed from %d to %d \n", gWimpyReductionFactor, new_factor); - gWimpyReductionFactor = new_factor; - } - else - { - vlog(" Error in WimpyReduction factor, must be power of 2 \n"); - } - } - } + parseWimpyReductionFactor(arg, gWimpyReductionFactor); break; case 'z': gForceFTZ ^= 1; diff --git a/test_conformance/half/main.c b/test_conformance/half/main.c index d3b73b34..93eb65d5 100644 --- a/test_conformance/half/main.c +++ b/test_conformance/half/main.c @@ -251,25 +251,7 @@ static int ParseArgs( int argc, const char **argv ) gWimpyMode = true; break; case '[': - // wimpy reduction factor can be set with the option -[2^n] - // Default factor is 512, and n practically can be from 1 to 12 - { - const char *arg_temp = strchr(&arg[1], ']'); - if (arg_temp != 0) - { - int new_factor = atoi(&arg[1]); - arg = arg_temp; // Advance until ']' - if (new_factor && !(new_factor & (new_factor - 1))) - { - vlog(" WimpyReduction factor changed from %d to %d \n", gWimpyReductionFactor, new_factor); - gWimpyReductionFactor = new_factor; - } - else - { - vlog(" Error in WimpyReduction factor, must be power of 2 \n"); - } - } - } + parseWimpyReductionFactor( arg, gWimpyReductionFactor); break; default: vlog_error( " <-- unknown flag: %c (0x%2.2x)\n)", *arg, *arg ); diff --git a/test_conformance/math_brute_force/main.c b/test_conformance/math_brute_force/main.c index d09d5baa..3d9ef5d4 100644 --- a/test_conformance/math_brute_force/main.c +++ b/test_conformance/math_brute_force/main.c @@ -1039,24 +1039,7 @@ static int ParseArgs( int argc, const char **argv ) break; case '[': - // wimpy reduction factor can be set with the option -[2^n] - // Default factor is 32, and n practically can be from 1 to 10 - { - const char *arg_temp = strchr(&arg[1], ']'); - if( arg_temp != 0) - { - int new_factor = atoi(&arg[1]); - arg=arg_temp; // Advance until ']' - if(new_factor && !(new_factor & (new_factor - 1))) - { - vlog( " WimpyReduction factor changed from %d to %d \n",gWimpyReductionFactor, new_factor); - gWimpyReductionFactor = new_factor; - }else - { - vlog( " Error in WimpyReduction factor %d, must be power of 2 \n",gWimpyReductionFactor); - } - } - } + parseWimpyReductionFactor(arg, gWimpyReductionFactor); break; case 'z': diff --git a/test_conformance/select/test_select.c b/test_conformance/select/test_select.c index 5cd088fc..0224394c 100644 --- a/test_conformance/select/test_select.c +++ b/test_conformance/select/test_select.c @@ -605,25 +605,7 @@ int main(int argc, char* argv[]) s_wimpy_mode = true; break; case '[': - // wimpy reduction factor can be set with the option -[2^n] - // Default factor is 256, and n practically can be from 1 to 12 - { - const char *arg_temp = strchr(&arg[1], ']'); - if (arg_temp != 0) - { - int new_factor = atoi(&arg[1]); - arg = arg_temp; // Advance until ']' - if (new_factor && !(new_factor & (new_factor - 1))) - { - vlog(" WimpyReduction factor changed from %d to %d \n", s_wimpy_reduction_factor, new_factor); - s_wimpy_reduction_factor = new_factor; - } - else - { - vlog(" Error in WimpyReduction factor must be power of 2 \n"); - } - } - } + parseWimpyReductionFactor(arg, s_wimpy_reduction_factor); break; default: break;