You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
7.0 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- This manual describes how to install and use the GNU multiple precision
arithmetic library, version 6.1.0.
Copyright 1991, 1993-2015 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document under
the terms of the GNU Free Documentation License, Version 1.3 or any later
version published by the Free Software Foundation; with no Invariant Sections,
with the Front-Cover Texts being "A GNU Manual", and with the Back-Cover
Texts being "You have freedom to copy and modify this GNU Manual, like GNU
software". A copy of the license is included in
GNU Free Documentation License. -->
<!-- Created by GNU Texinfo 6.4, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Exact Division (GNU MP 6.1.0)</title>
<meta name="description" content="How to install and use the GNU multiple precision arithmetic library, version 6.1.0.">
<meta name="keywords" content="Exact Division (GNU MP 6.1.0)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="Division-Algorithms.html#Division-Algorithms" rel="up" title="Division Algorithms">
<link href="Exact-Remainder.html#Exact-Remainder" rel="next" title="Exact Remainder">
<link href="Block_002dWise-Barrett-Division.html#Block_002dWise-Barrett-Division" rel="prev" title="Block-Wise Barrett Division">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en">
<a name="Exact-Division"></a>
<div class="header">
<p>
Next: <a href="Exact-Remainder.html#Exact-Remainder" accesskey="n" rel="next">Exact Remainder</a>, Previous: <a href="Block_002dWise-Barrett-Division.html#Block_002dWise-Barrett-Division" accesskey="p" rel="prev">Block-Wise Barrett Division</a>, Up: <a href="Division-Algorithms.html#Division-Algorithms" accesskey="u" rel="up">Division Algorithms</a> &nbsp; [<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Exact-Division-1"></a>
<h4 class="subsection">15.2.5 Exact Division</h4>
<p>A so-called exact division is when the dividend is known to be an exact
multiple of the divisor. Jebelean&rsquo;s exact division algorithm uses this
knowledge to make some significant optimizations (see <a href="References.html#References">References</a>).
</p>
<p>The idea can be illustrated in decimal for example with 368154 divided by
543. Because the low digit of the dividend is 4, the low digit of the
quotient must be 8. This is arrived at from <em>4*7 mod 10</em>, using the fact 7 is the modular inverse of 3 (the low digit of
the divisor), since <em>3*7
&equiv; 1 mod 10</em>. So <em>8*543=4344</em> can be
subtracted from the dividend leaving 363810. Notice the low digit has become
zero.
</p>
<p>The procedure is repeated at the second digit, with the next quotient digit 7
(<em>7 &equiv; 1*7 mod 10</em>), subtracting
<em>7*543=3801</em>, leaving 325800. And finally at
the third digit with quotient digit 6 (<em>8*7
mod 10</em>), subtracting <em>6*543=3258</em> leaving 0.
So the quotient is 678.
</p>
<p>Notice however that the multiplies and subtractions don&rsquo;t need to extend past
the low three digits of the dividend, since that&rsquo;s enough to determine the
three quotient digits. For the last quotient digit no subtraction is needed
at all. On a 2NxN division like this one, only about half the work of
a normal basecase division is necessary.
</p>
<p>For an NxM exact division producing Q=N-M quotient limbs, the
saving over a normal basecase division is in two parts. Firstly, each of the
Q quotient limbs needs only one multiply, not a 2x1 divide and
multiply. Secondly, the crossproducts are reduced when <em>Q&gt;M</em> to
<em>Q*M-M*(M+1)/2</em>, or when <em>Q&lt;=M</em> to <em>Q*(Q-1)/2</em>. Notice the savings are complementary. If Q is big then many
divisions are saved, or if Q is small then the crossproducts reduce to a small
number.
</p>
<p>The modular inverse used is calculated efficiently by <code>binvert_limb</code> in
<samp>gmp-impl.h</samp>. This does four multiplies for a 32-bit limb, or six for a
64-bit limb. <samp>tune/modlinv.c</samp> has some alternate implementations that
might suit processors better at bit twiddling than multiplying.
</p>
<p>The sub-quadratic exact division described by Jebelean in &ldquo;Exact Division
with Karatsuba Complexity&rdquo; is not currently implemented. It uses a
rearrangement similar to the divide and conquer for normal division
(see <a href="Divide-and-Conquer-Division.html#Divide-and-Conquer-Division">Divide and Conquer Division</a>), but operating from low to high. A
further possibility not currently implemented is &ldquo;Bidirectional Exact Integer
Division&rdquo; by Krandick and Jebelean which forms quotient limbs from both the
high and low ends of the dividend, and can halve once more the number of
crossproducts needed in a 2NxN division.
</p>
<p>A special case exact division by 3 exists in <code>mpn_divexact_by3</code>,
supporting Toom-3 multiplication and <code>mpq</code> canonicalizations. It forms
quotient digits with a multiply by the modular inverse of 3 (which is
<code>0xAA..AAB</code>) and uses two comparisons to determine a borrow for the next
limb. The multiplications don&rsquo;t need to be on the dependent chain, as long as
the effect of the borrows is applied, which can help chips with pipelined
multipliers.
</p>
<hr>
<div class="header">
<p>
Next: <a href="Exact-Remainder.html#Exact-Remainder" accesskey="n" rel="next">Exact Remainder</a>, Previous: <a href="Block_002dWise-Barrett-Division.html#Block_002dWise-Barrett-Division" accesskey="p" rel="prev">Block-Wise Barrett Division</a>, Up: <a href="Division-Algorithms.html#Division-Algorithms" accesskey="u" rel="up">Division Algorithms</a> &nbsp; [<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>