From patchwork Fri Sep 1 09:17:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiufu Guo X-Patchwork-Id: 137375 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c792:0:b0:3f2:4152:657d with SMTP id b18csp758736vqu; Fri, 1 Sep 2023 02:18:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFZIMULigRdCaDDpacshLyGz038UQ74xEbNNFM5O5Mm5cs3LYyjapQ1nAT57GVX6xkPW/AJ X-Received: by 2002:aa7:c3d4:0:b0:522:2711:863 with SMTP id l20-20020aa7c3d4000000b0052227110863mr1565166edr.1.1693559918252; Fri, 01 Sep 2023 02:18:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693559918; cv=none; d=google.com; s=arc-20160816; b=BOWRViHQdDpRcCbLvvtDN/e6oGQyDUqzolPuc02EcniHFCzx54yxLbDhwG7DcTQ/E8 fV5uZoXV8v7anjXNaXtt+HNBKAGwyWiz5Ndw6gDBO8E8I12X8DrA1Mh4m3zBpmF+eoCM iVmcDZ2bD39m1sInpALT66vRRfmEpJJmuOS1GZ8ueeailkzUK4s7gBn1H/4f+Y4gcVez RfnZWbHB4AA5cwmhoNmhpwLAvw6/RsunAWhlft0Dt5UgMUCI/QyY8fSb7OxcS1UeorsG FC3K1EH2PTuIYzXzkpbGY/VjDZ8tR6uEvBhhkily41cBK0CEaeuhqA3ruIxh0xHuCXvk X+zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :mime-version:content-transfer-encoding:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=VDUO0w8pK0xcZk3fDoimS2O9em3TN7ur2wtbyMzWWdk=; fh=UmUX9czUiMMCVKqbgiUf4TbHn8KnplAAcpnEO35tia4=; b=ZXA9WEs/6ScWTIjZcDoDN+D72b/kKG6F542zgblFZB7JKk61s+foSXOomSl5s2Nx5g yo0DzglnFgGy2Ad8BMSkS/7QRGf59AjnmOgOIct7bWw5AkS1SykVBf1qFw8yfzwgZR3s xZpUIV54fY0lPahQ/vZWqyvdTD/rwf6GbUWdY/MrfjPfuAyt1BVEdtYc0Mk3/tPkVdC+ 6qvYkAeUv+J5k0X8BH5aTVaBD0EsEy8QTbsXyZANGpl72s6ABQUgxPkTwmntDnyBczLN aj7KEVmrkHeem2Dzu9z9ZguwVt2Z+NqgK4GnC/ZT2MSQYanwP4FrQAi/xZ9dt+SX/8iO YJzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=orZspQ0w; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id f6-20020aa7d846000000b0052a0f70ff9fsi2247249eds.581.2023.09.01.02.18.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Sep 2023 02:18:38 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=orZspQ0w; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CA1B13858C2B for ; Fri, 1 Sep 2023 09:18:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CA1B13858C2B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693559916; bh=VDUO0w8pK0xcZk3fDoimS2O9em3TN7ur2wtbyMzWWdk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=orZspQ0wTSOLPrlckleDe8WGuWLreeRxup35vX3qJ54+lSMH3C5BHECmMeF4t5PXa wMXS5npXWk2xdF1oNBrkfJWbiqtrIotLFlOJrdSR8wkHVhiXt0mbkHqby2Dg+v9Wlm HX6G7/lLgPOXYNozuwL6odc+uqn/qxuC1dWBXOoc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 841E93858D20; Fri, 1 Sep 2023 09:17:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 841E93858D20 Received: from pps.filterd (m0353729.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3819657U031988; Fri, 1 Sep 2023 09:17:39 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3sub4bbv9d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 01 Sep 2023 09:17:39 +0000 Received: from m0353729.ppops.net (m0353729.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 38196AU8032489; Fri, 1 Sep 2023 09:17:38 GMT Received: from ppma13.dal12v.mail.ibm.com (dd.9e.1632.ip4.static.sl-reverse.com [50.22.158.221]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3sub4bbv8v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 01 Sep 2023 09:17:38 +0000 Received: from pps.filterd (ppma13.dal12v.mail.ibm.com [127.0.0.1]) by ppma13.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 3818arFd014114; Fri, 1 Sep 2023 09:17:37 GMT Received: from smtprelay03.fra02v.mail.ibm.com ([9.218.2.224]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 3sqwxkk7ch-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 01 Sep 2023 09:17:37 +0000 Received: from smtpav04.fra02v.mail.ibm.com (smtpav04.fra02v.mail.ibm.com [10.20.54.103]) by smtprelay03.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 3819HYYm14483984 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 1 Sep 2023 09:17:34 GMT Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 9764820043; Fri, 1 Sep 2023 09:17:34 +0000 (GMT) Received: from smtpav04.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id BD11E2004B; Fri, 1 Sep 2023 09:17:32 +0000 (GMT) Received: from genoa.aus.stglabs.ibm.com (unknown [9.40.192.157]) by smtpav04.fra02v.mail.ibm.com (Postfix) with ESMTP; Fri, 1 Sep 2023 09:17:32 +0000 (GMT) To: gcc-patches@gcc.gnu.org Subject: [PATCH V6] Optimize '(X - N * M) / N' to 'X / N - M' if valid Date: Fri, 1 Sep 2023 17:17:31 +0800 Message-Id: <20230901091731.710880-1-guojiufu@linux.ibm.com> X-Mailer: git-send-email 2.25.1 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: jHy1toTGHPNvfZTAMloFjxQtuEXi2Vho X-Proofpoint-ORIG-GUID: KBJZDLua98OFnzG3um5QvLJITuW_nKdN X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-09-01_07,2023-08-31_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 adultscore=0 impostorscore=0 phishscore=0 mlxscore=0 spamscore=0 priorityscore=1501 mlxlogscore=946 bulkscore=0 suspectscore=0 clxscore=1015 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2308100000 definitions=main-2309010084 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jiufu Guo via Gcc-patches From: Jiufu Guo Reply-To: Jiufu Guo Cc: rguenther@suse.de, segher@kernel.crashing.org, richard.sandiford@arm.com, bergner@linux.ibm.com, linkw@gcc.gnu.org, dje.gcc@gmail.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775826284738441273 X-GMAIL-MSGID: 1775826284738441273 Hi, Integer expression "(X - N * M) / N" can be optimized to "X / N - M" with the below conditions: 1. There is no wrap/overflow/underflow. wrap/overflow/underflow breaks the arithmetic operation. 2. "X - N * M" and "X" are not of opposite sign. Here, the operation "/" would be "trunc_div", the fractional part is discarded towards zero. If "X - N * M" and "X" are in different signs, then trunc_div discards the fractional parts (of /N) in different directions. Compare the previous version: https://gcc.gnu.org/pipermail/gcc-patches/2023-July/624801.html This patch adds comments and update the pattern on "(t + C)" to be more tight. 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: * match.pd ((X - N * M) / N): New pattern. ((X + N * M) / N): New pattern. ((X + C) div_rshift N): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/pr108757-1.c: New test. * gcc.dg/pr108757-2.c: New test. * gcc.dg/pr108757.h: New test. --- gcc/match.pd | 78 ++++++++++ gcc/testsuite/gcc.dg/pr108757-1.c | 18 +++ gcc/testsuite/gcc.dg/pr108757-2.c | 19 +++ gcc/testsuite/gcc.dg/pr108757.h | 233 ++++++++++++++++++++++++++++++ 4 files changed, 348 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/match.pd b/gcc/match.pd index fa598d5ca2e470f9cc3b82469e77d743b12f107e..863bc7299cdefc622a7806a4d32e37268c50d453 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -959,6 +959,84 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) #endif )))) +#if GIMPLE +(for div (trunc_div exact_div) + /* Simplify (X + M*N) / N -> X / N + M. */ + (simplify + (div (plus:c@4 @0 (mult:c@3 @1 @2)) @2) + (with {value_range vr0, vr1, vr2, vr3, vr4;} + (if (INTEGRAL_TYPE_P (type) + && get_range_query (cfun)->range_of_expr (vr1, @1) + && get_range_query (cfun)->range_of_expr (vr2, @2) + /* "N*M" doesn't overflow. */ + && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2) + && get_range_query (cfun)->range_of_expr (vr0, @0) + && get_range_query (cfun)->range_of_expr (vr3, @3) + /* "X+(N*M)" doesn't overflow. */ + && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3) + && get_range_query (cfun)->range_of_expr (vr4, @4) + /* "X+N*M" is not with opposite sign as "X". */ + && (TYPE_UNSIGNED (type) + || (vr0.nonnegative_p () && vr4.nonnegative_p ()) + || (vr0.nonpositive_p () && vr4.nonpositive_p ()))) + (plus (div @0 @2) @1)))) + + /* Simplify (X - M*N) / N -> X / N - M. */ + (simplify + (div (minus@4 @0 (mult:c@3 @1 @2)) @2) + (with {value_range vr0, vr1, vr2, vr3, vr4;} + (if (INTEGRAL_TYPE_P (type) + && get_range_query (cfun)->range_of_expr (vr1, @1) + && get_range_query (cfun)->range_of_expr (vr2, @2) + /* "N * M" doesn't overflow. */ + && range_op_handler (MULT_EXPR).overflow_free_p (vr1, vr2) + && get_range_query (cfun)->range_of_expr (vr0, @0) + && get_range_query (cfun)->range_of_expr (vr3, @3) + /* "X - (N*M)" doesn't overflow. */ + && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3) + && get_range_query (cfun)->range_of_expr (vr4, @4) + /* "X-N*M" is not with opposite sign as "X". */ + && (TYPE_UNSIGNED (type) + || (vr0.nonnegative_p () && vr4.nonnegative_p ()) + || (vr0.nonpositive_p () && vr4.nonpositive_p ()))) + (minus (div @0 @2) @1))))) + +/* Simplify + (X + C) / N -> X / N + C / N where C is multiple of N. + (X + C) >> N -> X >> 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 shift = op == RSHIFT_EXPR; + #define plus_op1(v) (shift ? wi::rshift (v, n, TYPE_SIGN (type)) \ + : wi::div_trunc (v, n, TYPE_SIGN (type))) + #define exact_mod(v) (shift ? wi::ctz (v) >= n.to_shwi () \ + : wi::multiple_of_p (v, n, TYPE_SIGN (type))) + value_range vr0, vr1, vr3; + } + (if (INTEGRAL_TYPE_P (type) + && get_range_query (cfun)->range_of_expr (vr0, @0)) + (if (exact_mod (c) + && get_range_query (cfun)->range_of_expr (vr1, @1) + /* "X+C" doesn't overflow. */ + && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1) + && get_range_query (cfun)->range_of_expr (vr3, @3) + /* "X+C" and "X" are not of opposite sign. */ + && (TYPE_UNSIGNED (type) + || (vr0.nonnegative_p () && vr3.nonnegative_p ()) + || (vr0.nonpositive_p () && vr3.nonpositive_p ()))) + (plus (op @0 @2) { wide_int_to_tree (type, plus_op1 (c)); }) + (if (TYPE_UNSIGNED (type) && c.sign_mask () < 0 + && exact_mod (-c) + /* unsigned "X-(-C)" doesn't underflow. */ + && wi::geu_p (vr0.lower_bound (), -c)) + (plus (op @0 @2) { wide_int_to_tree (type, -plus_op1 (-c)); }))))))) +#endif + (for op (negate abs) /* Simplify cos(-x) and cos(|x|) -> cos(x). Similarly for cosh. */ (for coss (COS COSH) 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; +} +