From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 36F773857711; Tue, 11 Jul 2023 09:11:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 36F773857711 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linux.ibm.com Received: from pps.filterd (m0353728.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 36B975xK003698; Tue, 11 Jul 2023 09:11:27 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : content-transfer-encoding : mime-version; s=pp1; bh=IJOkpS/D+zxGxr+4pR4HVrrFOm6JnVlg5f4XrdHBrI0=; b=fxynidAqmMAlQraXCXcA4c9MYJ4PG5lzYsCLpNJ/b1GgoGDrEnhELUKYMk9Z47y3Z+DT 4Adru6mKM6wJGBTXNz83Zzgr89R94vk8Ea0E+BqwwzrLns3QMcmEbk/4LXvYqqMGEBY1 WNbT6oY5ojBiM4LB12NkEvn0QxOxW45kia7wRcvIZiIidy8Sh6ZywVvQ393dpCv0Qq3+ Z/+2VWj+9KjiR0fI12Iyrcl3uy/+eCbaRly+jAqLQDjFC3xRUsiu1uXuUi48ms7hZiAb g+BUoT2U1Q7NGQzeD44CBpGCYBwwD/hdIJb4qfl+fs2+YpeMA0wiaro42CBtmUCAcZJ/ MA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3rs3vd8g4x-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 11 Jul 2023 09:11:27 +0000 Received: from m0353728.ppops.net (m0353728.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 36B98J1v010785; Tue, 11 Jul 2023 09:11:27 GMT Received: from ppma05fra.de.ibm.com (6c.4a.5195.ip4.static.sl-reverse.com [149.81.74.108]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3rs3vd8g3j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 11 Jul 2023 09:11:26 +0000 Received: from pps.filterd (ppma05fra.de.ibm.com [127.0.0.1]) by ppma05fra.de.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 36B4eu0B013369; Tue, 11 Jul 2023 09:04:19 GMT Received: from smtprelay05.fra02v.mail.ibm.com ([9.218.2.225]) by ppma05fra.de.ibm.com (PPS) with ESMTPS id 3rpye5990p-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 11 Jul 2023 09:04:19 +0000 Received: from smtpav07.fra02v.mail.ibm.com (smtpav07.fra02v.mail.ibm.com [10.20.54.106]) by smtprelay05.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 36B94Gsc18088672 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 11 Jul 2023 09:04:16 GMT Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id EACD02004D; Tue, 11 Jul 2023 09:04:15 +0000 (GMT) Received: from smtpav07.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 128FF20043; Tue, 11 Jul 2023 09:04:14 +0000 (GMT) Received: from ltcden2-lp1.aus.stglabs.ibm.com (unknown [9.3.90.43]) by smtpav07.fra02v.mail.ibm.com (Postfix) with ESMTP; Tue, 11 Jul 2023 09:04:13 +0000 (GMT) From: Jiufu Guo To: gcc-patches@gcc.gnu.org Cc: amacleod@redhat.com, aldyh@redhat.com, rguenther@suse.de, jeffreyalaw@gmail.com, richard.sandiford@arm.com, segher@kernel.crashing.org, dje.gcc@gmail.com, linkw@gcc.gnu.org, bergner@linux.ibm.com, guojiufu@linux.ibm.com Subject: [PATCH V4] Optimize '(X - N * M) / N' to 'X / N - M' if valid Date: Tue, 11 Jul 2023 17:04:13 +0800 Message-Id: <20230711090413.3587421-1-guojiufu@linux.ibm.com> X-Mailer: git-send-email 2.39.3 X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: XHSkvhQ8c5HxRHOWbWBdl1LWn5RNQDG2 X-Proofpoint-GUID: pHRUYQE0B5GV23vvEZNZPyebu11FJuY3 Content-Transfer-Encoding: 8bit X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-07-11_04,2023-07-06_02,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 mlxscore=0 impostorscore=0 adultscore=0 malwarescore=0 bulkscore=0 mlxlogscore=999 phishscore=0 spamscore=0 lowpriorityscore=0 priorityscore=1501 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2305260000 definitions=main-2307110080 X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,GIT_PATCH_0,KAM_LOTSOFHASH,KAM_SHORT,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Hi, Integer expression "(X - N * M) / N" can be optimized to "X / N - M" if there is no wrap/overflow/underflow and "X - N * M" has the same sign with "X". Compare the previous version: https://gcc.gnu.org/pipermail/gcc-patches/2023-June/623028.html - The APIs for checking overflow of range operation are moved to other files: range-op and gimple-range. - Improve the patterns with '(X + C)' for unsigned type. Bootstrap & regtest pass on ppc64{,le} and x86_64. Is this patch ok for trunk? BR, Jeff (Jiufu Guo) PR tree-optimization/108757 gcc/ChangeLog: * gimple-range.cc (arith_without_overflow_p): New function. (same_sign_p): New function. * gimple-range.h (arith_without_overflow_p): New declare. (same_sign_p): New declare. * match.pd ((X - N * M) / N): New pattern. ((X + N * M) / N): New pattern. ((X + C) div_rshift N): New pattern. * range-op.cc (plus_without_overflow_p): New function. (minus_without_overflow_p): New function. (mult_without_overflow_p): New function. * range-op.h (plus_without_overflow_p): New declare. (minus_without_overflow_p): New declare. (mult_without_overflow_p): New declare. * value-query.h (get_range): New function * value-range.cc (irange::nonnegative_p): New function. (irange::nonpositive_p): New function. * value-range.h (irange::nonnegative_p): New declare. (irange::nonpositive_p): New declare. gcc/testsuite/ChangeLog: * gcc.dg/pr108757-1.c: New test. * gcc.dg/pr108757-2.c: New test. * gcc.dg/pr108757.h: New test. --- gcc/gimple-range.cc | 50 +++++++ gcc/gimple-range.h | 2 + gcc/match.pd | 64 ++++++++ gcc/range-op.cc | 77 ++++++++++ gcc/range-op.h | 4 + gcc/value-query.h | 10 ++ gcc/value-range.cc | 12 ++ gcc/value-range.h | 2 + gcc/testsuite/gcc.dg/pr108757-1.c | 18 +++ gcc/testsuite/gcc.dg/pr108757-2.c | 19 +++ gcc/testsuite/gcc.dg/pr108757.h | 233 ++++++++++++++++++++++++++++++ 11 files changed, 491 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/pr108757-1.c create mode 100644 gcc/testsuite/gcc.dg/pr108757-2.c create mode 100644 gcc/testsuite/gcc.dg/pr108757.h diff --git a/gcc/gimple-range.cc b/gcc/gimple-range.cc index 01e62d3ff3901143bde33dc73c0debf41d0c0fdd..620fe32e85e5fe3847a933554fc656b2939cf02d 100644 --- a/gcc/gimple-range.cc +++ b/gcc/gimple-range.cc @@ -926,3 +926,53 @@ assume_query::dump (FILE *f) } fprintf (f, "------------------------------\n"); } + +/* Return true if the operation "X CODE Y" in type does not overflow + underflow or wrap with value range info, otherwise return false. */ + +bool +arith_without_overflow_p (tree_code code, tree x, tree y, tree type) +{ + gcc_assert (INTEGRAL_TYPE_P (type)); + + if (TYPE_OVERFLOW_UNDEFINED (type)) + return true; + + value_range vr0; + value_range vr1; + if (!(get_range (vr0, x) && get_range (vr1, y))) + return false; + + switch (code) + { + case PLUS_EXPR: + return plus_without_overflow_p (vr0, vr1, type); + case MINUS_EXPR: + return minus_without_overflow_p (vr0, vr1, type); + case MULT_EXPR: + return mult_without_overflow_p (vr0, vr1, type); + default: + gcc_unreachable (); + } + + return false; +} + +/* Return true if "X" and "Y" have the same sign or zero. */ + +bool +same_sign_p (tree x, tree y, tree type) +{ + gcc_assert (INTEGRAL_TYPE_P (type)); + + if (TYPE_UNSIGNED (type)) + return true; + + value_range vr0; + value_range vr1; + if (!(get_range (vr0, x) && get_range (vr1, y))) + return false; + + return (vr0.nonnegative_p () && vr1.nonnegative_p ()) + || (vr0.nonpositive_p () && vr1.nonpositive_p ()); +} diff --git a/gcc/gimple-range.h b/gcc/gimple-range.h index 6587e4923ff44e10826a697ecced237a0ad23c88..84eac87392b642ed3305011415c804f5b319e09f 100644 --- a/gcc/gimple-range.h +++ b/gcc/gimple-range.h @@ -101,5 +101,7 @@ protected: gori_compute m_gori; }; +bool arith_without_overflow_p (tree_code code, tree x, tree y, tree type); +bool same_sign_p (tree x, tree y, tree type); #endif // GCC_GIMPLE_RANGE_H diff --git a/gcc/match.pd b/gcc/match.pd index 8543f777a28e4f39b2b2a40d0702aed88786bbb3..87e990c5b1ebbd116d7d7efdba62347d3a967cdd 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -942,6 +942,70 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) #endif )))) +#if GIMPLE +(for div (trunc_div exact_div) + /* Simplify (t + M*N) / N -> t / N + M. */ + (simplify + (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2) + (if (INTEGRAL_TYPE_P (type) + && arith_without_overflow_p (MULT_EXPR, @1, @2, type) + && arith_without_overflow_p (PLUS_EXPR, @0, @3, type) + && same_sign_p (@0, @4, type)) + (plus (div @0 @2) @1))) + + /* Simplify (t - M*N) / N -> t / N - M. */ + (simplify + (div (minus@4 @0 (mult:c@3 @1 @2)) @2) + (if (INTEGRAL_TYPE_P (type) + && arith_without_overflow_p (MULT_EXPR, @1, @2, type) + && arith_without_overflow_p (MINUS_EXPR, @0, @3, type) + && same_sign_p (@0, @4, type)) + (minus (div @0 @2) @1)))) + +/* Simplify + (t + C) / N -> t / N + C / N where C is multiple of N. + (t + C) >> N -> t >> N + C>>N if low N bits of C is 0. */ +(for op (trunc_div exact_div rshift) + (simplify + (op (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2) + (with + { + wide_int c = wi::to_wide (@1); + wide_int n = wi::to_wide (@2); + bool is_rshift = op == RSHIFT_EXPR; + bool neg_c = false; + bool ok = false; + if (INTEGRAL_TYPE_P (type)) + { + ok = is_rshift ? wi::ctz (c) >= n.to_shwi () + : wi::multiple_of_p (c, n, TYPE_SIGN (type)); + ok = ok && arith_without_overflow_p (PLUS_EXPR, @0, @1, type) + && same_sign_p (@0, @3, type); + + /* Try check 'X + C' as 'X - -C' for unsigned. */ + if (!ok && TYPE_UNSIGNED (type) && c.sign_mask () < 0) + { + neg_c = true; + c = -c; + ok = is_rshift ? wi::ctz (c) >= n.to_shwi () + : wi::multiple_of_p (c, n, UNSIGNED); + value_range vr; + ok = ok && get_range (vr, @0) + && wi::geu_p (vr.lower_bound (), c); + } + } + } + (if (ok) + (with + { + wide_int m; + m = is_rshift ? wi::rshift (c, n, TYPE_SIGN (type)) + : wi::div_trunc (c, n, TYPE_SIGN (type)); + m = neg_c ? -m : m; + } + (plus (op @0 @2) { wide_int_to_tree(type, m); })))))) +#endif + (for op (negate abs) /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */ (for coss (COS COSH) diff --git a/gcc/range-op.cc b/gcc/range-op.cc index cb584314f4cfc93aeec50c7f888829997a7dc8eb..487e781a0cdfa39416589983a246327957c14d54 100644 --- a/gcc/range-op.cc +++ b/gcc/range-op.cc @@ -4311,6 +4311,83 @@ range_op_table::initialize_integral_ops () } +bool +plus_without_overflow_p (value_range &vr0, value_range &vr1, tree type) +{ + wi::overflow_type ovf; + signop sgn = TYPE_SIGN (type); + wide_int wmax0 = vr0.upper_bound (); + wide_int wmax1 = vr1.upper_bound (); + wi::add (wmax0, wmax1, sgn, &ovf); + if (ovf != wi::OVF_NONE) + return false; + + if (TYPE_UNSIGNED (type)) + return true; + + wide_int wmin0 = vr0.lower_bound (); + wide_int wmin1 = vr1.lower_bound (); + wi::add (wmin0, wmin1, sgn, &ovf); + if (ovf != wi::OVF_NONE) + return false; + + return true; +} + +bool +minus_without_overflow_p (value_range &vr0, value_range &vr1, tree type) +{ + wi::overflow_type ovf; + signop sgn = TYPE_SIGN (type); + wide_int wmin0 = vr0.lower_bound (); + wide_int wmax1 = vr1.upper_bound (); + wi::sub (wmin0, wmax1, sgn, &ovf); + if (ovf != wi::OVF_NONE) + return false; + + if (TYPE_UNSIGNED (type)) + return true; + + wide_int wmax0 = vr0.upper_bound (); + wide_int wmin1 = vr1.lower_bound (); + wi::sub (wmax0, wmin1, sgn, &ovf); + if (ovf != wi::OVF_NONE) + return false; + + return true; +} + +bool +mult_without_overflow_p (value_range &vr0, value_range &vr1, tree type) +{ + wi::overflow_type ovf; + signop sgn = TYPE_SIGN (type); + wide_int wmax0 = vr0.upper_bound (); + wide_int wmax1 = vr1.upper_bound (); + wi::mul (wmax0, wmax1, sgn, &ovf); + if (ovf != wi::OVF_NONE) + return false; + + if (TYPE_UNSIGNED (type)) + return true; + + wide_int wmin0 = vr0.lower_bound (); + wide_int wmin1 = vr1.lower_bound (); + wi::mul (wmin0, wmin1, sgn, &ovf); + if (ovf != wi::OVF_NONE) + return false; + + wi::mul (wmin0, wmax1, sgn, &ovf); + if (ovf != wi::OVF_NONE) + return false; + + wi::mul (wmax0, wmin1, sgn, &ovf); + if (ovf != wi::OVF_NONE) + return false; + + return true; +} + #if CHECKING_P #include "selftest.h" diff --git a/gcc/range-op.h b/gcc/range-op.h index af94c2756a74f710aa50aec1ac3b3de5eeb43a8e..1697ce5464c0cc8fbfb86234137ba7cc23e10979 100644 --- a/gcc/range-op.h +++ b/gcc/range-op.h @@ -220,6 +220,10 @@ protected: range_operator *m_operator; }; +extern bool plus_without_overflow_p (value_range &, value_range &, tree); +extern bool minus_without_overflow_p (value_range &, value_range &, tree); +extern bool mult_without_overflow_p (value_range &, value_range &, tree); + // Cast the range in R to TYPE if R supports TYPE. inline bool diff --git a/gcc/value-query.h b/gcc/value-query.h index d10c3eac1e2e6c477cbab026942a45b4fcc2ddce..cf488c7ea9bfc423fd3de9d2c743f71175804ae2 100644 --- a/gcc/value-query.h +++ b/gcc/value-query.h @@ -137,6 +137,16 @@ get_range_query (const struct function *fun) return (fun && fun->x_range_query) ? fun->x_range_query : &global_ranges; } +/* Return true if there is range for "X" expression at "S" statement, + and the range is not varying and not undefined. */ + +inline bool +get_range (vrange &r, tree x, gimple *s = NULL) +{ + return get_range_query (cfun)->range_of_expr (r, x, s) && !r.varying_p () + && !r.undefined_p (); +} + // Query the global range of NAME in function F. Default to cfun. extern void gimple_range_global (vrange &v, tree name, struct function *f = cfun); diff --git a/gcc/value-range.cc b/gcc/value-range.cc index 011bdbdeae62845b5627a8dfa0d261370df7e5db..5ae4e044194326c24aab6babf8e0df3e4b313e98 100644 --- a/gcc/value-range.cc +++ b/gcc/value-range.cc @@ -313,6 +313,18 @@ add_vrange (const vrange &v, inchash::hash &hstate, } //namespace inchash +bool +irange::nonnegative_p () const +{ + return wi::ge_p (lower_bound (), 0, TYPE_SIGN (type ())); +} + +bool +irange::nonpositive_p () const +{ + return wi::le_p (upper_bound (), 0, TYPE_SIGN (type ())); +} + bool irange::supports_type_p (const_tree type) const { diff --git a/gcc/value-range.h b/gcc/value-range.h index 0170188201bc9b1c4e117661c9a62819dc1547c5..a12dea514e48cdd04a3cee5dc6304637e501d399 100644 --- a/gcc/value-range.h +++ b/gcc/value-range.h @@ -280,6 +280,8 @@ public: virtual bool singleton_p (tree *result = NULL) const override; bool singleton_p (wide_int &) const; bool contains_p (const wide_int &) const; + bool nonnegative_p () const; + bool nonpositive_p () const; // In-place operators. virtual bool union_ (const vrange &) override; diff --git a/gcc/testsuite/gcc.dg/pr108757-1.c b/gcc/testsuite/gcc.dg/pr108757-1.c new file mode 100644 index 0000000000000000000000000000000000000000..7908f4bdcb8e225fe311b668efbe8f6db525b4d5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr108757-1.c @@ -0,0 +1,18 @@ +/* PR tree-optimization/108757 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +#include +#define N 5 +#define M 3 +#define GAP 0 +typedef unsigned int UINT; +typedef int INT; +#define UMAX UINT_MAX +#define IMAX INT_MAX +#define IMIN INT_MIN +#include "pr108757.h" + +/* { dg-final { scan-tree-dump-not " = x_\[0-9\]+\\(D\\) \\+ " "optimized" } } * +/* { dg-final { scan-tree-dump-not " = x_\[0-9\]+\\(D\\) \\- " "optimized" } } */ +/* { dg-final { scan-tree-dump-not " = b_\[0-9\]+ \\+ " "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/pr108757-2.c b/gcc/testsuite/gcc.dg/pr108757-2.c new file mode 100644 index 0000000000000000000000000000000000000000..82bebd09944f0be2e0690c604723bfe6182acec3 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr108757-2.c @@ -0,0 +1,19 @@ +/* PR tree-optimization/108757 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized -fwrapv" } */ + +#include +#define N 4 +#define M 3 +#define GAP 2 +typedef unsigned int UINT; +typedef int INT; +#define UMAX UINT_MAX +#define IMAX INT_MAX +#define IMIN INT_MIN +#include "pr108757.h" + +/* { dg-final { scan-tree-dump-times " = x_\[0-9\]+\\(D\\) \\+ " 16 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " = x_\[0-9\]+\\(D\\) \\- " 3 "optimized" } } */ +/* { dg-final { scan-tree-dump-times " \\+ x_\[0-9\]+\\(D\\)" 3 "optimized" } } */ + diff --git a/gcc/testsuite/gcc.dg/pr108757.h b/gcc/testsuite/gcc.dg/pr108757.h new file mode 100644 index 0000000000000000000000000000000000000000..5550199c1ef952f54eaa83087cec25e992057c34 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr108757.h @@ -0,0 +1,233 @@ +#define NOINLINE __attribute__ ((noinline)) +UINT NOINLINE +opt_u1 (UINT x) +{ + if (x < (M * N) - GAP) + return 0; + UINT a = x - (M * N); + UINT b = a / N; + return b + M; +} + +UINT NOINLINE +opt_u2 (UINT x) +{ + if (x > (UMAX - (M * N) + GAP)) + return 0; + UINT a = x + (M * N); + UINT b = a / N; + return b - M; +} + +INT NOINLINE +opt_s1 (INT x) +{ + if (x < (M * N) - GAP) + return 0; + INT a = x - (M * N); + INT b = a / N; + return b + M; +} + +INT NOINLINE +opt_s2 (INT x) +{ + if (x < IMIN + (M * N) - GAP || x > 0) + return 0; + INT a = x - (M * N); + INT b = a / N; + return b + M; +} + +INT NOINLINE +opt_s3 (INT x) +{ + if (x < (M * N) - GAP) + return 0; + INT a = x - (M * N); + INT b = a / -N; + return b + -M; +} + +INT NOINLINE +opt_s4 (INT x) +{ + if (x < IMIN + (M * N) - GAP || x > 0) + return 0; + INT a = x - (M * N); + INT b = a / -N; + return b + -M; +} + +INT NOINLINE +opt_s5 (INT x) +{ + if (x > (-M * N) + GAP) + return 0; + INT a = x - (-M * N); + INT b = a / N; + return b + -M; +} + +INT NOINLINE +opt_s6 (INT x) +{ + if (x > IMAX - (M * N) + GAP || x < 0) + return 0; + INT a = x - (-M * N); + INT b = a / N; + return b + -M; +} + +INT NOINLINE +opt_s7 (INT x) +{ + if (x > (M * -N) + GAP) + return 0; + INT a = x - (M * -N); + INT b = a / -N; + return b + M; +} + +INT NOINLINE +opt_s8 (INT x) +{ + if (x > IMAX - (M * N) + GAP || x < 0) + return 0; + INT a = x - (M * -N); + INT b = a / -N; + return b + M; +} + +UINT NOINLINE +opt_u3 (UINT x) +{ + if (x < (M << N) - GAP) + return 0; + UINT a = x - (M << N); + UINT b = a >> N; + return b + M; +} + +UINT NOINLINE +opt_u4 (UINT x) +{ + if (x > (UMAX - (M << N)) + GAP) + return 0; + UINT a = x + (M << N); + UINT b = a >> N; + return b - M; +} + +INT NOINLINE +opt_s9 (INT x) +{ + if (x < (M << N) - GAP) + return 0; + INT a = x - (M << N); + INT b = a >> N; + return b + M; +} + +INT NOINLINE +opt_s10 (INT x) +{ + if (x < IMIN + (M << N) - GAP || x > 0) + return 0; + INT a = x - (M << N); + INT b = a >> N; + return b + M; +} + +INT NOINLINE +opt_s11 (INT x) +{ + if (x > (-M << N) + GAP) + return 0; + INT a = x - (-M << N); + INT b = a >> N; + return b + -M; +} + +INT NOINLINE +opt_s12 (INT x) +{ + if (x > IMAX - (M << N) + GAP || x < 0) + return 0; + INT a = x - (-M << N); + INT b = a >> N; + return b + -M; +} + +UINT NOINLINE +opt_u5 (UINT x, UINT n, UINT m) +{ + if (n > N || m > M) + return 0; + if (x < (M*N) - GAP) + return 0; + UINT a = x - (m * n); + UINT b = a / n; + return b + m; +} + +UINT NOINLINE +opt_u6 (UINT x, UINT n, UINT m) +{ + if (n > N || m > M) + return 0; + if (x > (UMAX - M*N) + GAP) + return 0; + UINT a = x + (m * n); + UINT b = a / n; + return b - m; +} + +INT NOINLINE +opt_s13 (INT x, INT n, INT m) +{ + if (n > N || m > M || n < 0 || m < 0) + return 0; + if (x < (M*N) - GAP) + return 0; + INT a = x - (m * n); + INT b = a / n; + return b + m; +} + +INT NOINLINE +opt_s14 (INT x, INT n, INT m) +{ + if (n > N || m > M || n < 0 || m < 0) + return 0; + if (x > -M*N + GAP) + return 0; + INT a = x + (m * n); + INT b = a / n; + return b - m; +} + +INT +opt_s15 (INT x, INT n, INT m) +{ + if (n > 0 || m > 0 || n < -N || m < -M) + return 0; + if (x < (M*N) - GAP) + return 0; + INT a = x - (m * n); + INT b = a / n; + return b + m; +} + +INT NOINLINE +opt_s16 (INT x, INT n, INT m) +{ + if (n > 0 || m > 0 || n < -N || m < -M) + return 0; + if (x < 0 || x > (IMAX - M*N) + GAP) + return 0; + INT a = x + (m * n); + INT b = a / n; + return b - m; +} + -- 2.39.3