summaryrefslogtreecommitdiff
path: root/pp.c
diff options
context:
space:
mode:
authorTomasz Konojacki <me@xenu.pl>2018-10-15 05:24:27 +0200
committerTony Cook <tony@develop-help.com>2018-11-02 14:53:42 +1100
commitf196658042490a6287fc178f0bc20fd5558ac54b (patch)
tree9d76a58641f2aa0058af1375101d22fb35e31159 /pp.c
parent191f8909fa4eca1db16a91ada42dd4a065c04890 (diff)
downloadperl-f196658042490a6287fc178f0bc20fd5558ac54b.tar.gz
pp_divide: use modulo instead of multiplication
On most architectures with hardware integer division (like x86 or aarch64), division instruction returns both the remainder and the quotient. It means that performing modulo operation immediately after division using the same operands is 100% free. Essentially this commit changes "div" and "mul" into a single "div" instruction, which results in minor speed up. [perl #133511]
Diffstat (limited to 'pp.c')
-rw-r--r--pp.c5
1 files changed, 4 insertions, 1 deletions
diff --git a/pp.c b/pp.c
index 33eac6040d..cfa343fbbb 100644
--- a/pp.c
+++ b/pp.c
@@ -1503,8 +1503,11 @@ PP(pp_divide)
#endif
) {
/* Integer division can't overflow, but it can be imprecise. */
+
+ /* Modern compilers optimize division followed by
+ * modulo into a single div instruction */
const UV result = left / right;
- if (result * right == left) {
+ if (left % right == 0) {
SP--; /* result is valid */
if (left_non_neg == right_non_neg) {
/* signs identical, result is positive. */