source: git/ntl/doc/tour-impl.html @ de6a29

spielwiese
Last change on this file since de6a29 was de6a29, checked in by Hans Schönemann <hannes@…>, 19 years ago
* hannes: NTL-5.4 git-svn-id: file:///usr/local/Singular/svn/trunk@8693 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 11.7 KB
Line 
1<html>
2<head>
3<title>
4A Tour of NTL: NTL Implementation and Portability  </title>
5</head>
6
7<body bgcolor="#fff9e6">
8<center>
9<a href="tour-tips.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
10 <a href="tour.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a> 
11<a href="tour-gmp.html"> <img src="arrow3.gif" alt="[Next]" align=bottom></a>
12</center>
13
14<h1> 
15<p align=center>
16A Tour of NTL: NTL Implementation and Portability
17</p>
18</h1>
19
20<p> <hr> <p>
21
22NTL is designed to be portable, fast,
23and relatively easy to use and extend.
24
25<p>
26To make NTL portable, no assembly code is used (well, almost none, see below).
27This is highly desirable, as architectures are constantly
28changing and evolving, and maintaining assembly
29code is quite costly.
30By avoiding assembly code, NTL should remain usable,
31with virtually no maintenance, for many years.
32
33<p>
34
35<h3>Minimal platform requirements</h3>
36
37When the configuration flags <tt>NTL_CLEAN_INT</tt>
38and <tt>NTL_CLEAN_PTR</tt> are both <i>on</i> (this is not the default,
39see below),
40NTL makes two requirements
41of its platform,
42neither of which are guaranteed by the <tt>C++</tt> language
43definition, but are essentially universal:
44
45<ol>
46<li>
47<tt>int</tt> and <tt>long</tt> quantities, respectively,
48are represented using a 2's complement
49representation whose width is equal to the width of <tt>unsigned int</tt>
50and <tt>unsigned long</tt>, respectively.
51<li>
52Double precision floating point
53conforms to the IEEE floating point standard.
54</ol>
55
56<p>
57NTl makes very conservative requirements of the <tt>C/C++</tt> compiler:
58<ul>
59<li>
60it is assumed that the <tt>C</tt> compiler conforms to the original
61ANSI <tt>C</tt> standard,
62<li>
63it is assumed that the <tt>C++</tt> compiler supports all of the
64language features described in the <i>second</i> edition of Stroustrup's book,
65minus exceptions, templates, and derived types.
66</ul>
67
68
69<p>
70
71<h3>The <tt>NTL_CLEAN_INT</tt> flag</h3>
72
73<p>
74
75The configuration flag <tt>NTL_CLEAN_INT</tt> 
76is currently <i>off</i> by default.
77
78<p>
79When this flag is off, NTL makes another requirement of its platform;
80namely, that arithmetic operations on the type <tt>long</tt>
81do not overflow, but simply "wrap around" modulo the word size.
82This behavior is <i>not</i> guaranteed by the <tt>C++</tt> standard,
83and yet it is essentially universally implemented.
84In fact, most compilers will go out of their way to ensure this behavior,
85since it is a very reasonable behavior, and since many programs
86implicitly rely on this behavior.
87
88<p>
89Making this "wrap around" assumption can lead to slightly more efficient code
90on some platforms.
91It seems fairly unlikely that one would ever have to turn the
92<tt>NTL_CLEAN_INT</tt> flag <i>on</i>, but it seems a good idea
93to make this possible, and at the very least
94to identify and isolate the code that
95relies on this assumption.
96
97
98
99
100<p>
101Actually, with <tt>NTL_CLEAN_INT</tt> off, it is also assumed
102that right shifts of signed integers are consistent,
103in the sense that if it is sometimes an arithmetic shift,
104then it is always an arithmetic shift (the installation
105scripts check if right shift appears to be arithmetic, and if so,
106this assumption is made elsewhere).
107
108<p>
109It is hard to imagine that there is a platform existing today
110(or in the foreseeable future) where these assumptions
111are not meet.
112However,
113 as of version 5.4 of NTL, all of the most
114performance-critical code now works almost as well
115with  <tt>NTL_CLEAN_INT</tt> set as without.
116The differences are not very significant (maybe 10%).
117Therefore, there is hardly any reason to not set this flag.
118Also, note that the only code affected by this flag
119is the traditional long integer package (which, if you use
120GMP as the primary long integer package, is not involved),
121and the single-precision modular multiplication routines
122defined in <tt>ZZ.h</tt>.
123
124<p>
125
126<h3>The <tt>NTL_CLEAN_PTR</tt> flag</h3>
127
128<p>
129
130The configuration flag <tt>NTL_CLEAN_PTR</tt> 
131is currently <i>off</i> by default.
132
133<p>
134When this flag is off, NTL makes another requirement of its platform;
135namely, that the address space is "flat", and in particular,
136that one can test if an object pointed to by a pointer <tt>p</tt>
137is located in a array of objects <tt>v[0..n-1]</tt> by testing
138if <tt>p &gt;= v</tt> and <tt>p &lt; v + n</tt>.
139The <tt>C++</tt> standard does not guarantee that such a test will
140work;   the only way to perform this test in a standard-conforming way
141is to iteratively test if <tt>p == v</tt>, <tt>p == v+1</tt>, etc.
142
143<p>
144This assumption of a "flat" address space is essentially universally
145valid, and making this assumption leads to some more efficient code.
146For this reason, the <tt>NTL_CLEAN_PTR</tt> is <i>off</i> by default,
147but one can always turn it on, and in fact, the overall performance
148penalty should be negligible for most applications.
149
150
151
152<h3>Some floating point issues</h3>
153
154
155<p>
156NTL uses floating point arithmetic in a number of places,
157including a number of exact computations, where one might
158not expect to see floating point.
159Relying on floating point may seem prone to errors,
160but with the guarantees provided by the IEEE standard,
161one can prove the correctness of the NTL code that uses floating point.
162
163<p>
164Briefly, the IEEE floating point standard says that basic arithmetic operations
165on doubles should work <i>as if</i> the operation were performed with infinite
166precision, and then rounded to <tt>p</tt> bits,
167where <tt>p</tt> is the precision (typically, <tt>p = 53</tt>).
168
169
170<p>
171Throughout most of NTL, correctness follows from weaker assumptions,
172namely
173<p>
174<ul>
175<li>
176basic arithmetic operations and conversion from integral types
177produce results with a <i>relative error</i> of
178<tt>2^{-p + 1}</tt> (assuming no overflow),
179<li>
180multiplication by powers of 2 produce <i>exact</i> results (assuming no overflow),
181<li>
182basic arithmetic operations on integers represented as doubles and conversions from integral types
183to doubles produce <i>exact</i> results, provided the inputs and outputs
184are less than <tt>2^p</tt> in absolute value,
185<li>
186if <tt>y/2 &lt;= x &lt;= 2y</tt>, then <tt>x-y</tt> is computed exactly.
187</ul>
188Also, NTL allows the compiler to compute <tt>z = x/y</tt> as
189<tt>t = 1/y</tt>, <tt>z = t*x</tt>.
190
191<p>
192One big problem with the IEEE standard is that it allows intermediate
193quantities to be computed in a higher precision than the standard
194double precision.
195This "looseness" in the standard is a substantial impediment to
196creating portable software.
197Most platforms today implement the "strict" IEEE standard, with no
198excess precision.
199One notable exception -- the 800 pound gorilla, so to speak --
200is the Intel x86.
201
202<p>
203NTL goes out of its way to ensure that its code is correct with
204both "strict" and "loose" IEEE floating point.
205This is achieved in a portable fashion throughout NTL, except
206for the <tt>quad_float</tt> module, where some desperate hacks,
207including assembly code, may be used
208to try to work around problems created by "loose" IEEE floating point
209<a href="quad_float.txt">[more details]</a>.
210But note that even if the <tt>quad_float</tt> package does not work correctly
211because of these problems, the only other routines that are affected
212are the <tt>LLL_QP</tt> routines in the <tt>LLL</tt> module -- the
213rest of NTL should work fine.
214
215
216
217<p>
218Mostly, NTL does not
219 require that the IEEE floating point
220special quantities "infinity"
221and "not a number" are implemented correctly.
222This is certainly the case for core code where
223floating point arithmetic is used for exact (but fast)
224computations, as the numbers involved never get too big (or small).
225However, the behavior of
226certain explicit floating point computations
227(e.g., the <tt>xdouble</tt> and <tt>quad_float</tt> classes,
228and the floating point versions of LLL) will be
229much more predictable and reliable if "infinity"
230and "not a number" are implemented correctly.
231
232
233<p>
234<h3>Implementing long integer arithmetic</h3>
235<p>
236There are three basic strategies for implementing long integer arithmetic.
237
238<p>
239The <i>default</i> strategy is implemented in the
240<i>traditional long integer arithmetic package</i>.
241This package is derived from the LIP package originally developed by
242A. K. Lenstra, although it has evolved quite a bit within NTL.
243This package uses no assembly code and is very portable.
244
245<p>
246The <i>second</i> strategy is to use the Gnu Multi-Precision Package (GMP)
247as a <i>supplemental long integer arithmetic package</i>.
248In this strategy, the representation of long integers is identical
249to that in he traditional long integer package.
250This representation is incompatible with the GMP representation,
251and on-the-fly conversions are done between the two representations
252(only when this is sensible).
253This strategy typically yields better performance, but requires
254that GMP is installed on your platform.
255
256<p>
257The <i>third</i> strategy is to use GMP as the
258<i>primary long integer arithmetic package</i>.
259In this strategy, the representation of long integers is in a
260form compatible with GMP.
261This strategy typically yields the best performance,
262but requires
263that GMP is installed on your platform, and also
264introduces some minor backward incompatibilities in the programming
265interface.
266
267<p>
268<a href="tour-gmp.html">Go here</a> for more details on the use
269of GMP with NTL.
270
271<p>
272<h3>Algorithms</h3>
273<p>
274NTL makes fairly consistent use of asymptotically fast algorithms.
275
276<p>
277Long integer multiplication is implemented using the classical
278algorithm, crossing over to Karatsuba for very big numbers.
279Long integer division is currently only implemented using
280the classical algorithm -- unless you use NTL with GMP (version 3 or later)
281as either a supplemental or primary long integer package,
282which
283employs an algorithm that is about twice as slow as multiplication
284for very large numbers.
285<p>
286Polynomial multiplication and division is carried out
287using a combination of the classical algorithm, Karatsuba,
288the FFT using small primes, and the FFT using the Schoenhagge-Strassen
289approach.
290The choice of algorithm depends on the coefficient domain.
291<p>
292Many algorithms employed throughout NTL are inventions
293of the author (<a href="http://www.shoup.net">Victor Shoup</a>)
294and his colleagues
295<a href="http://math-www.uni-paderborn.de/~aggathen/joachim.html">Joachim von zur Gathen</a>
296and
297<a href="http://www4.ncsu.edu/~kaltofen">Erich Kaltofen</a>,
298as well as <a href="mailto:abbott@dima.unige.it">John Abbott</a>
299and
300<a href="http://www.loria.fr/~zimmerma">Paul Zimmermann</a>.
301
302<p>
303<h3>
304Some of NTL's imperfections
305</h3>
306<p>
307
308NTL is not a "perfect" library.
309Here are some limitations of NTL that a "perfect" library would not have:
310<p>
311<ul>
312<li>
313NTL is neither thread-safe nor re-entrant, and making it so
314would require a fundamental redesign.
315<p>
316
317<li>
318NTL provides only a very crude form of error handling:
319print an error message and abort.
320For most NTL users, this is quite sufficient.
321The alternative would be to have NTL throw exceptions.
322Writing code that handles exceptions correctly is quite difficult.
323The easy part is throwing and catching exceptions.
324The hard part is writing code <i>through which</i> an exception
325can be safely and correctly thrown.
326Retrofitting NTL to throw exceptions at this late date
327would be quite difficult and error prone, and I do not think
328that there is much demand for it.
329
330<p>
331
332<li>
333NTL does not release all of its resources.
334There are some routines which for efficiency reasons will
335allocate some memory and never give it back to the system,
336so as to avoid re-allocations on subsequent calls.
337The amount of memory "stolen" by NTL in this way is fairly reasonable,
338and I have heard no complaints yet about its effects.
339
340</ul>
341
342
343<p>
344
345<center>
346<a href="tour-tips.html"><img src="arrow1.gif" alt="[Previous]" align=bottom></a>
347 <a href="tour.html"><img src="arrow2.gif" alt="[Up]" align=bottom></a> 
348<a href="tour-gmp.html"> <img src="arrow3.gif" alt="[Next]" align=bottom></a>
349</center>
350
351
352</body>
353</html>
Note: See TracBrowser for help on using the repository browser.