From: Martin Liska Date: Wed, 20 Jul 2016 07:00:50 +0000 (+0200) Subject: Add sreal to selftests X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=dcbdb17aebb7fac8b3e455774728c044dd711e37;p=gcc.git Add sreal to selftests * selftest-run-tests.c (selftest::run_tests): New function. * selftest.h (sreal_c_tests): Declare. * sreal.c (sreal_verify_basics): New function. (verify_aritmetics): Likewise. (sreal_verify_arithmetics): Likewise. (verify_shifting): Likewise. (sreal_verify_shifting): Likewise. (void sreal_c_tests): Likewise. * gcc.dg/plugin/plugin.exp: Remove sreal test. * gcc.dg/plugin/sreal-test-1.c: Remove. * gcc.dg/plugin/sreal_plugin.c: Remove. From-SVN: r238508 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index d3dfcc895a6..2d767c8e7fe 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,14 @@ +2016-07-20 Martin Liska + + * selftest-run-tests.c (selftest::run_tests): New function. + * selftest.h (sreal_c_tests): Declare. + * sreal.c (sreal_verify_basics): New function. + (verify_aritmetics): Likewise. + (sreal_verify_arithmetics): Likewise. + (verify_shifting): Likewise. + (sreal_verify_shifting): Likewise. + (void sreal_c_tests): Likewise. + 2016-07-19 Jakub Jelinek PR rtl-optimization/71916 diff --git a/gcc/selftest-run-tests.c b/gcc/selftest-run-tests.c index bddf0b2d9a9..bb004cc5167 100644 --- a/gcc/selftest-run-tests.c +++ b/gcc/selftest-run-tests.c @@ -49,6 +49,7 @@ selftest::run_tests () pretty_print_c_tests (); wide_int_cc_tests (); ggc_tests_c_tests (); + sreal_c_tests (); /* Mid-level data structures. */ input_c_tests (); diff --git a/gcc/selftest.h b/gcc/selftest.h index 967e76bd2c8..c8053862767 100644 --- a/gcc/selftest.h +++ b/gcc/selftest.h @@ -86,6 +86,7 @@ extern void pretty_print_c_tests (); extern void rtl_tests_c_tests (); extern void spellcheck_c_tests (); extern void spellcheck_tree_c_tests (); +extern void sreal_c_tests (); extern void tree_c_tests (); extern void tree_cfg_c_tests (); extern void vec_c_tests (); diff --git a/gcc/sreal.c b/gcc/sreal.c index a7c9c12d662..9c43b4eed21 100644 --- a/gcc/sreal.c +++ b/gcc/sreal.c @@ -52,6 +52,7 @@ along with GCC; see the file COPYING3. If not see #include #include "coretypes.h" #include "sreal.h" +#include "selftest.h" /* Print the content of struct sreal. */ @@ -233,3 +234,114 @@ sreal::operator/ (const sreal &other) const r.normalize (); return r; } + +#if CHECKING_P + +namespace selftest { + +/* Selftests for sreals. */ + +/* Verify basic sreal operations. */ + +static void +sreal_verify_basics (void) +{ + sreal minimum = INT_MIN; + sreal maximum = INT_MAX; + + sreal seven = 7; + sreal minus_two = -2; + sreal minus_nine = -9; + + ASSERT_EQ (INT_MIN, minimum.to_int ()); + ASSERT_EQ (INT_MAX, maximum.to_int ()); + + ASSERT_FALSE (minus_two < minus_two); + ASSERT_FALSE (seven < seven); + ASSERT_TRUE (seven > minus_two); + ASSERT_TRUE (minus_two < seven); + ASSERT_TRUE (minus_two != seven); + ASSERT_EQ (minus_two, -2); + ASSERT_EQ (seven, 7); + ASSERT_EQ ((seven << 10) >> 10, 7); + ASSERT_EQ (seven + minus_nine, -2); +} + +/* Helper function that performs basic arithmetics and comparison + of given arguments A and B. */ + +static void +verify_aritmetics (int64_t a, int64_t b) +{ + ASSERT_EQ (a, -(-(sreal (a))).to_int ()); + ASSERT_EQ (a < b, sreal (a) < sreal (b)); + ASSERT_EQ (a <= b, sreal (a) <= sreal (b)); + ASSERT_EQ (a == b, sreal (a) == sreal (b)); + ASSERT_EQ (a != b, sreal (a) != sreal (b)); + ASSERT_EQ (a > b, sreal (a) > sreal (b)); + ASSERT_EQ (a >= b, sreal (a) >= sreal (b)); + ASSERT_EQ (a + b, (sreal (a) + sreal (b)).to_int ()); + ASSERT_EQ (a - b, (sreal (a) - sreal (b)).to_int ()); + ASSERT_EQ (b + a, (sreal (b) + sreal (a)).to_int ()); + ASSERT_EQ (b - a, (sreal (b) - sreal (a)).to_int ()); +} + +/* Verify arithmetics for interesting numbers. */ + +static void +sreal_verify_arithmetics (void) +{ + int values[] = {-14123413, -7777, -17, -10, -2, 0, 17, 139, 1234123}; + unsigned c = sizeof (values) / sizeof (int); + + for (unsigned i = 0; i < c; i++) + for (unsigned j = 0; j < c; j++) + { + int a = values[i]; + int b = values[j]; + + verify_aritmetics (a, b); + } +} + +/* Helper function that performs various shifting test of a given + argument A. */ + +static void +verify_shifting (int64_t a) +{ + sreal v = a; + + for (unsigned i = 0; i < 16; i++) + ASSERT_EQ (a << i, (v << i).to_int()); + + a = a << 16; + v = v << 16; + + for (unsigned i = 0; i < 16; i++) + ASSERT_EQ (a >> i, (v >> i).to_int()); +} + +/* Verify shifting for interesting numbers. */ + +static void +sreal_verify_shifting (void) +{ + int values[] = {0, 17, 32, 139, 1024, 55555, 1234123}; + unsigned c = sizeof (values) / sizeof (int); + + for (unsigned i = 0; i < c; i++) + verify_shifting (values[i]); +} + +/* Run all of the selftests within this file. */ + +void sreal_c_tests () +{ + sreal_verify_basics (); + sreal_verify_arithmetics (); + sreal_verify_shifting (); +} + +} // namespace selftest +#endif /* CHECKING_P */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 47ca5262864..7f42fad4064 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2016-07-20 Martin Liska + + * gcc.dg/plugin/plugin.exp: Remove sreal test. + * gcc.dg/plugin/sreal-test-1.c: Remove. + * gcc.dg/plugin/sreal_plugin.c: Remove. + 2016-07-19 Thomas Koenig PR fortran/71902 diff --git a/gcc/testsuite/gcc.dg/plugin/plugin.exp b/gcc/testsuite/gcc.dg/plugin/plugin.exp index f039c8de35f..faebb75d114 100644 --- a/gcc/testsuite/gcc.dg/plugin/plugin.exp +++ b/gcc/testsuite/gcc.dg/plugin/plugin.exp @@ -59,7 +59,6 @@ set plugin_test_list [list \ { selfassign.c self-assign-test-1.c self-assign-test-2.c } \ { ggcplug.c ggcplug-test-1.c } \ { one_time_plugin.c one_time-test-1.c } \ - { sreal_plugin.c sreal-test-1.c } \ { start_unit_plugin.c start_unit-test-1.c } \ { finish_unit_plugin.c finish_unit-test-1.c } \ { wide-int_plugin.c wide-int-test-1.c } \ diff --git a/gcc/testsuite/gcc.dg/plugin/sreal-test-1.c b/gcc/testsuite/gcc.dg/plugin/sreal-test-1.c deleted file mode 100644 index 1bce2cc0a35..00000000000 --- a/gcc/testsuite/gcc.dg/plugin/sreal-test-1.c +++ /dev/null @@ -1,8 +0,0 @@ -/* Test that pass is inserted and invoked once. */ -/* { dg-do compile } */ -/* { dg-options "-O" } */ - -int main (int argc, char **argv) -{ - return 0; -} diff --git a/gcc/testsuite/gcc.dg/plugin/sreal_plugin.c b/gcc/testsuite/gcc.dg/plugin/sreal_plugin.c deleted file mode 100644 index f1138163c5d..00000000000 --- a/gcc/testsuite/gcc.dg/plugin/sreal_plugin.c +++ /dev/null @@ -1,170 +0,0 @@ -/* Plugin that process internal tests for sreal. */ -#include "config.h" -#include "gcc-plugin.h" -#include "system.h" -#include "coretypes.h" -#include "tree.h" -#include "tm.h" -#include "toplev.h" -#include "hash-table.h" -#include "vec.h" -#include "ggc.h" -#include "basic-block.h" -#include "tree-ssa-alias.h" -#include "internal-fn.h" -#include "gimple-fold.h" -#include "tree-eh.h" -#include "gimple-expr.h" -#include "is-a.h" -#include "gimple.h" -#include "tree-pass.h" -#include "intl.h" -#include "context.h" -#include "sreal.h" - -int plugin_is_GPL_compatible; - -namespace { - -static void assert (bool c) -{ - if (!c) - abort (); -} - -const pass_data pass_data_sreal_pass = -{ - GIMPLE_PASS, /* type */ - "sreal", /* name */ - OPTGROUP_NONE, /* optinfo_flags */ - TV_NONE, /* tv_id */ - PROP_gimple_any, /* properties_required */ - 0, /* properties_provided */ - 0, /* properties_destroyed */ - 0, /* todo_flags_start */ - 0, /* todo_flags_finish */ -}; - -class sreal_pass : public gimple_opt_pass -{ -public: - sreal_pass(gcc::context *ctxt) - : gimple_opt_pass(pass_data_sreal_pass, ctxt) - {} - - /* opt_pass methods: */ - virtual bool gate (function *); - virtual unsigned int execute (function *); - -private: - void check_sreal (); - - static void verify_aritmetics (int a, int b); - static void verify_shifting (int a); -}; // class one_pass - -} // anon namespace - -void -sreal_pass::verify_aritmetics (int a, int b) -{ - assert (a == -(-(sreal (a))).to_int ()); - assert ((a < b) == (sreal (a) < sreal (b))); - assert ((a <= b) == (sreal (a) <= sreal (b))); - assert ((a == b) == (sreal (a) == sreal (b))); - assert ((a != b) == (sreal (a) != sreal (b))); - assert ((a > b) == (sreal (a) > sreal (b))); - assert ((a >= b) == (sreal (a) >= sreal (b))); - assert ((a + b) == (sreal (a) + sreal (b)).to_int ()); - assert ((a - b) == (sreal (a) - sreal (b)).to_int ()); - assert ((b + a) == (sreal (b) + sreal (a)).to_int ()); - assert ((b - a) == (sreal (b) - sreal (a)).to_int ()); -} - -void -sreal_pass::verify_shifting (int a) -{ - sreal v = a; - - for (unsigned i = 0; i < 16; i++) - assert ((a << i) == (v << i).to_int()); - - a = a << 16; - v = v << 16; - - for (unsigned i = 0; i < 16; i++) - assert ((a >> i) == (v >> i).to_int()); -} - -void -sreal_pass::check_sreal () -{ - sreal minimum = INT_MIN; - sreal maximum = INT_MAX; - sreal seven = 7; - sreal minus_two = -2; - sreal minus_nine = -9; - - assert (minimum.to_int () == INT_MIN); - assert (maximum.to_int () == INT_MAX); - - assert (!(minus_two < minus_two)); - assert (!(seven < seven)); - assert (seven > minus_two); - assert (minus_two < seven); - assert (minus_two != seven); - assert (minus_two == minus_two); - assert (seven == seven); - - assert (seven == ((seven << 10) >> 10)); - - assert ((seven + minus_two) == 5); - assert ((seven + minus_nine) == -2); - - for (int a = -100; a < 100; a++) - for (int b = -100; b < 100; b++) - { - verify_aritmetics (a, b); - verify_aritmetics (INT_MIN + 100, b); - verify_aritmetics (INT_MAX - 100, b); - } - - srand (123456); - - for (int i = 0; i < 1000 * 1000; i++) - { - verify_aritmetics (rand () % 10, rand () % 1000000); - verify_aritmetics (rand () % 100, rand () % 10000); - } - - for (int a = -100; a < 100; a++) - verify_shifting (a); -} - -bool sreal_pass::gate (function *) -{ - return true; -} - -unsigned int -sreal_pass::execute (function *) -{ - check_sreal (); - - return 0; -} - -int plugin_init (struct plugin_name_args *plugin_info, - struct plugin_gcc_version *version) -{ - struct register_pass_info p; - - p.pass = new sreal_pass (g); - p.reference_pass_name = "cfg"; - p.ref_pass_instance_number = 1; - p.pos_op = PASS_POS_INSERT_AFTER; - - register_callback ("sreal", PLUGIN_PASS_MANAGER_SETUP, NULL, &p); - - return 0; -}