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.

163 lines
4.8 KiB

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