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

153 lines
4.6 KiB

25 years ago
  1. <HTML>
  2. <HEAD>
  3. <!-- Created by texi2html 1.56k from cln.texi on 14 January 2000 -->
  4. <TITLE>CLN, a Class Library for Numbers - 10. Internals</TITLE>
  5. </HEAD>
  6. <BODY>
  7. Go to the <A HREF="cln_1.html">first</A>, <A HREF="cln_9.html">previous</A>, <A HREF="cln_11.html">next</A>, <A HREF="cln_13.html">last</A> section, <A HREF="cln_toc.html">table of contents</A>.
  8. <P><HR><P>
  9. <H1><A NAME="SEC58" HREF="cln_toc.html#TOC58">10. Internals</A></H1>
  10. <H2><A NAME="SEC59" HREF="cln_toc.html#TOC59">10.1 Why C++ ?</A></H2>
  11. <P>
  12. Using C++ as an implementation language provides
  13. <UL>
  14. <LI>
  15. Efficiency: It compiles to machine code.
  16. <LI>
  17. Portability: It runs on all platforms supporting a C++ compiler. Because
  18. of the availability of GNU C++, this includes all currently used 32-bit and
  19. 64-bit platforms, independently of the quality of the vendor's C++ compiler.
  20. <LI>
  21. Type safety: The C++ compilers knows about the number types and complains if,
  22. for example, you try to assign a float to an integer variable. However,
  23. a drawback is that C++ doesn't know about generic types, hence a restriction
  24. like that <CODE>operation+ (const cl_MI&#38;, const cl_MI&#38;)</CODE> requires that both
  25. arguments belong to the same modular ring cannot be expressed as a compile-time
  26. information.
  27. <LI>
  28. Algebraic syntax: The elementary operations <CODE>+</CODE>, <CODE>-</CODE>, <CODE>*</CODE>,
  29. <CODE>=</CODE>, <CODE>==</CODE>, ... can be used in infix notation, which is more
  30. convenient than Lisp notation <SAMP>`(+ x y)'</SAMP> or C notation <SAMP>`add(x,y,&#38;z)'</SAMP>.
  31. </UL>
  32. <P>
  33. With these language features, there is no need for two separate languages,
  34. one for the implementation of the library and one in which the library's users
  35. can program. This means that a prototype implementation of an algorithm
  36. can be integrated into the library immediately after it has been tested and
  37. debugged. No need to rewrite it in a low-level language after having prototyped
  38. in a high-level language.
  39. <H2><A NAME="SEC60" HREF="cln_toc.html#TOC60">10.2 Memory efficiency</A></H2>
  40. <P>
  41. In order to save memory allocations, CLN implements:
  42. <UL>
  43. <LI>
  44. Object sharing: An operation like <CODE>x+0</CODE> returns <CODE>x</CODE> without copying
  45. it.
  46. <LI>
  47. Garbage collection: A reference counting mechanism makes sure that any
  48. number object's storage is freed immediately when the last reference to the
  49. object is gone.
  50. <LI>
  51. Small integers are represented as immediate values instead of pointers
  52. to heap allocated storage. This means that integers <CODE>&#62; -2^29</CODE>,
  53. <CODE>&#60; 2^29</CODE> don't consume heap memory, unless they were explicitly allocated
  54. on the heap.
  55. </UL>
  56. <H2><A NAME="SEC61" HREF="cln_toc.html#TOC61">10.3 Speed efficiency</A></H2>
  57. <P>
  58. Speed efficiency is obtained by the combination of the following tricks
  59. and algorithms:
  60. <UL>
  61. <LI>
  62. Small integers, being represented as immediate values, don't require
  63. memory access, just a couple of instructions for each elementary operation.
  64. <LI>
  65. The kernel of CLN has been written in assembly language for some CPUs
  66. (<CODE>i386</CODE>, <CODE>m68k</CODE>, <CODE>sparc</CODE>, <CODE>mips</CODE>, <CODE>arm</CODE>).
  67. <LI>
  68. On all CPUs, CLN uses the superefficient low-level routines from GNU
  69. GMP version 2.
  70. <LI>
  71. For large numbers, CLN uses, instead of the standard <CODE>O(N^2)</CODE>
  72. algorithm, the Karatsuba multiplication, which is an
  73. <CODE>O(N^1.6)</CODE>
  74. algorithm.
  75. <LI>
  76. For very large numbers (more than 12000 decimal digits), CLN uses
  77. Sch�nhage-Strassen
  78. multiplication, which is an asymptotically
  79. optimal multiplication algorithm.
  80. <LI>
  81. These fast multiplication algorithms also give improvements in the speed
  82. of division and radix conversion.
  83. </UL>
  84. <H2><A NAME="SEC62" HREF="cln_toc.html#TOC62">10.4 Garbage collection</A></H2>
  85. <P>
  86. All the number classes are reference count classes: They only contain a pointer
  87. to an object in the heap. Upon construction, assignment and destruction of
  88. number objects, only the objects' reference count are manipulated.
  89. <P>
  90. Memory occupied by number objects are automatically reclaimed as soon as
  91. their reference count drops to zero.
  92. <P>
  93. For number rings, another strategy is implemented: There is a cache of,
  94. for example, the modular integer rings. A modular integer ring is destroyed
  95. only if its reference count dropped to zero and the cache is about to be
  96. resized. The effect of this strategy is that recently used rings remain
  97. cached, whereas undue memory consumption through cached rings is avoided.
  98. <P><HR><P>
  99. Go to the <A HREF="cln_1.html">first</A>, <A HREF="cln_9.html">previous</A>, <A HREF="cln_11.html">next</A>, <A HREF="cln_13.html">last</A> section, <A HREF="cln_toc.html">table of contents</A>.
  100. </BODY>
  101. </HTML>