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.
143 lines
7.6 KiB
HTML
143 lines
7.6 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>Binary to Radix (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="Binary to Radix (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="Radix-Conversion-Algorithms.html#Radix-Conversion-Algorithms" rel="up" title="Radix Conversion Algorithms">
|
|
<link href="Radix-to-Binary.html#Radix-to-Binary" rel="next" title="Radix to Binary">
|
|
<link href="Radix-Conversion-Algorithms.html#Radix-Conversion-Algorithms" rel="prev" title="Radix Conversion Algorithms">
|
|
<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="Binary-to-Radix"></a>
|
|
<div class="header">
|
|
<p>
|
|
Next: <a href="Radix-to-Binary.html#Radix-to-Binary" accesskey="n" rel="next">Radix to Binary</a>, Previous: <a href="Radix-Conversion-Algorithms.html#Radix-Conversion-Algorithms" accesskey="p" rel="prev">Radix Conversion Algorithms</a>, Up: <a href="Radix-Conversion-Algorithms.html#Radix-Conversion-Algorithms" accesskey="u" rel="up">Radix Conversion Algorithms</a> [<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
<hr>
|
|
<a name="Binary-to-Radix-1"></a>
|
|
<h4 class="subsection">15.6.1 Binary to Radix</h4>
|
|
|
|
<p>Conversions from binary to a power-of-2 radix use a simple and fast
|
|
<em>O(N)</em> bit extraction algorithm.
|
|
</p>
|
|
<p>Conversions from binary to other radices use one of two algorithms. Sizes
|
|
below <code>GET_STR_PRECOMPUTE_THRESHOLD</code> use a basic <em>O(N^2)</em> method.
|
|
Repeated divisions by <em>b^n</em> are made, where <em>b</em> is the radix and
|
|
<em>n</em> is the biggest power that fits in a limb. But instead of simply
|
|
using the remainder <em>r</em> from such divisions, an extra divide step is done
|
|
to give a fractional limb representing <em>r/b^n</em>. The digits of <em>r</em>
|
|
can then be extracted using multiplications by <em>b</em> rather than divisions.
|
|
Special case code is provided for decimal, allowing multiplications by 10 to
|
|
optimize to shifts and adds.
|
|
</p>
|
|
<p>Above <code>GET_STR_PRECOMPUTE_THRESHOLD</code> a sub-quadratic algorithm is used.
|
|
For an input <em>t</em>, powers <em>b^(n*2^i)</em> of the radix are
|
|
calculated, until a power between <em>t</em> and <em>sqrt(t)</em> is
|
|
reached. <em>t</em> is then divided by that largest power, giving a quotient
|
|
which is the digits above that power, and a remainder which is those below.
|
|
These two parts are in turn divided by the second highest power, and so on
|
|
recursively. When a piece has been divided down to less than
|
|
<code>GET_STR_DC_THRESHOLD</code> limbs, the basecase algorithm described above is
|
|
used.
|
|
</p>
|
|
<p>The advantage of this algorithm is that big divisions can make use of the
|
|
sub-quadratic divide and conquer division (see <a href="Divide-and-Conquer-Division.html#Divide-and-Conquer-Division">Divide and Conquer Division</a>), and big divisions tend to have less overheads than lots of
|
|
separate single limb divisions anyway. But in any case the cost of
|
|
calculating the powers <em>b^(n*2^i)</em> must first be overcome.
|
|
</p>
|
|
<p><code>GET_STR_PRECOMPUTE_THRESHOLD</code> and <code>GET_STR_DC_THRESHOLD</code> represent
|
|
the same basic thing, the point where it becomes worth doing a big division to
|
|
cut the input in half. <code>GET_STR_PRECOMPUTE_THRESHOLD</code> includes the cost
|
|
of calculating the radix power required, whereas <code>GET_STR_DC_THRESHOLD</code>
|
|
assumes that’s already available, which is the case when recursing.
|
|
</p>
|
|
<p>Since the base case produces digits from least to most significant but they
|
|
want to be stored from most to least, it’s necessary to calculate in advance
|
|
how many digits there will be, or at least be sure not to underestimate that.
|
|
For GMP the number of input bits is multiplied by <code>chars_per_bit_exactly</code>
|
|
from <code>mp_bases</code>, rounding up. The result is either correct or one too
|
|
big.
|
|
</p>
|
|
<p>Examining some of the high bits of the input could increase the chance of
|
|
getting the exact number of digits, but an exact result every time would not
|
|
be practical, since in general the difference between numbers 100… and
|
|
99… is only in the last few bits and the work to identify 99…
|
|
might well be almost as much as a full conversion.
|
|
</p>
|
|
<p>The <em>r/b^n</em> scheme described above for using multiplications to bring out
|
|
digits might be useful for more than a single limb. Some brief experiments
|
|
with it on the base case when recursing didn’t give a noticeable improvement,
|
|
but perhaps that was only due to the implementation. Something similar would
|
|
work for the sub-quadratic divisions too, though there would be the cost of
|
|
calculating a bigger radix power.
|
|
</p>
|
|
<p>Another possible improvement for the sub-quadratic part would be to arrange
|
|
for radix powers that balanced the sizes of quotient and remainder produced,
|
|
i.e. the highest power would be an <em>b^(n*k)</em> approximately equal to
|
|
<em>sqrt(t)</em>, not restricted to a <em>2^i</em> factor. That ought to
|
|
smooth out a graph of times against sizes, but may or may not be a net
|
|
speedup.
|
|
</p>
|
|
|
|
<hr>
|
|
<div class="header">
|
|
<p>
|
|
Next: <a href="Radix-to-Binary.html#Radix-to-Binary" accesskey="n" rel="next">Radix to Binary</a>, Previous: <a href="Radix-Conversion-Algorithms.html#Radix-Conversion-Algorithms" accesskey="p" rel="prev">Radix Conversion Algorithms</a>, Up: <a href="Radix-Conversion-Algorithms.html#Radix-Conversion-Algorithms" accesskey="u" rel="up">Radix Conversion Algorithms</a> [<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
|
|
</div>
|
|
|
|
|
|
|
|
</body>
|
|
</html>
|