From bbeb2517c0bdf7477124f9a3acda0213ebe36529 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sat, 24 May 2014 10:38:02 -0400 Subject: [PATCH] Arithmetic exception flag and a plethora of tests. --- doc/gmqcc.1 | 5 +++++ fold.c | 3 +++ gmqcc.ini.example | 5 +++++ opts.def | 1 + tests/arithexcept.qc | 13 +++++++++++++ tests/arithexcept.tmpl | 4 ++++ tests/arithexcept_of.tmpl | 4 ++++ tests/arithexcept_uf.tmpl | 4 ++++ tests/inexact.qc | 8 ++++++++ tests/inexact.tmpl | 4 ++++ 10 files changed, 51 insertions(+) create mode 100644 tests/arithexcept.qc create mode 100644 tests/arithexcept.tmpl create mode 100644 tests/arithexcept_of.tmpl create mode 100644 tests/arithexcept_uf.tmpl create mode 100644 tests/inexact.qc create mode 100644 tests/inexact.tmpl diff --git a/doc/gmqcc.1 b/doc/gmqcc.1 index e88acd9..33a0eff 100644 --- a/doc/gmqcc.1 +++ b/doc/gmqcc.1 @@ -581,6 +581,11 @@ breaks decompilers, but causes the output file to be better compressible. In commutative instructions, always put the lower-numbered operand first. This shaves off 1 byte of entropy from all these instructions, reducing compressed size of the output file. +.It Fl f Ns Cm arithmetic-exceptions +Turn on arithmetic exception tests in the compiler. In constant expressions +which trigger exceptions like division by zero, overflow, underflow, etc, +the following flag will produce diagnostics for what triggered that +exception. .El .Sh OPTIMIZATIONS .Bl -tag -width Ds diff --git a/fold.c b/fold.c index 6d5d37a..98bbdb7 100644 --- a/fold.c +++ b/fold.c @@ -820,6 +820,9 @@ static bool fold_check_except_float(sfloat_t (*callback)(sfloat_state_t *, sfloa sfloat_cast_t ca; sfloat_cast_t cb; + if (!OPTS_FLAG(ARITHMETIC_EXCEPTIONS)) + return false; + s.roundingmode = SFLOAT_ROUND_NEAREST_EVEN; s.tiny = SFLOAT_TBEFORE; s.exceptionflags = 0; diff --git a/gmqcc.ini.example b/gmqcc.ini.example index a12628e..f7f3913 100644 --- a/gmqcc.ini.example +++ b/gmqcc.ini.example @@ -310,6 +310,11 @@ SORT_OPERANDS = false + #Turn on arithmetic exception tests in the compiler. In constant expressions + #which trigger exceptions like division by zero, overflow, underflow, etc, + #the following flag will produce diagnostics for what triggered that + #exception. + ARITHMETIC_EXCEPTIONS = false [warnings] #Generate a warning about variables which are declared but never diff --git a/opts.def b/opts.def index 5aa52ab..669e734 100644 --- a/opts.def +++ b/opts.def @@ -56,6 +56,7 @@ GMQCC_DEFINE_FLAG(UNSAFE_VARARGS) GMQCC_DEFINE_FLAG(TYPELESS_STORES) GMQCC_DEFINE_FLAG(SORT_OPERANDS) + GMQCC_DEFINE_FLAG(ARITHMETIC_EXCEPTIONS) #endif /* warning flags */ diff --git a/tests/arithexcept.qc b/tests/arithexcept.qc new file mode 100644 index 0000000..10476e6 --- /dev/null +++ b/tests/arithexcept.qc @@ -0,0 +1,13 @@ +const float huge = 340282346638528859811704183484516925440; // FLT_MAX + +#ifdef DIVBYZERO +const float a = 1.0 / 0.0; +#endif + +#ifdef OVERFLOW +const float a = huge * huge; +#endif + +#ifdef UNDERFLOW +const float a = 1 / huge; +#endif diff --git a/tests/arithexcept.tmpl b/tests/arithexcept.tmpl new file mode 100644 index 0000000..3055e98 --- /dev/null +++ b/tests/arithexcept.tmpl @@ -0,0 +1,4 @@ +I: arithexcept.qc +D: arithmetic exceptions (divide by zero) +T: -fail +C: -std=fteqcc -farithmetic-exceptions -DDIVBYZERO diff --git a/tests/arithexcept_of.tmpl b/tests/arithexcept_of.tmpl new file mode 100644 index 0000000..94b7d7d --- /dev/null +++ b/tests/arithexcept_of.tmpl @@ -0,0 +1,4 @@ +I: arithexcept.qc +D: arithmetic exceptions (overflow) +T: -fail +C: -std=ftqcc -farithmetic-exceptions -DOVERFLOW diff --git a/tests/arithexcept_uf.tmpl b/tests/arithexcept_uf.tmpl new file mode 100644 index 0000000..ed0a817 --- /dev/null +++ b/tests/arithexcept_uf.tmpl @@ -0,0 +1,4 @@ +I: arithexcept.qc +D: arithmetic exceptions (underflow) +T: -fail +C: -std=ftqcc -farithmetic-exceptions -DUNDERFLOW diff --git a/tests/inexact.qc b/tests/inexact.qc new file mode 100644 index 0000000..873d7cc --- /dev/null +++ b/tests/inexact.qc @@ -0,0 +1,8 @@ +void main() { + const float a = 1.0 / 3.0; + const float b = 0.3333333333333; + + if (a == b) { + // Should trigger warning + } +} diff --git a/tests/inexact.tmpl b/tests/inexact.tmpl new file mode 100644 index 0000000..9808f7d --- /dev/null +++ b/tests/inexact.tmpl @@ -0,0 +1,4 @@ +I: inexact.qc +D: inexact comparisons +T: -fail +C: -std=gmqcc -Winexact-compares -Wall -Werror -- 2.39.2