2
0

README.bn 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. Welcome to my multiprecision math library! I'm a little bit proud
  2. of it, particularly its speed. If you have a machine for which
  3. assembly-language subroutines are available (you can probably guess
  4. from the filename), it will go even faster. Instructions for
  5. building the library with assembly subroutines are included later.
  6. Barring that, on many machines using GCC, the GNU C compiler, helps
  7. the speed significantly, because it not only supports "long long"
  8. 64-bit data types, but it can perform operations on them in line.
  9. Some other compilers that support "long long" generate inefficient
  10. code for working with them.
  11. For a description of what the library does, see bn.doc. For a description
  12. of how it's organized, see bnintern.doc. For the real nitty-gritty,
  13. read the code. I'm very curious what you all think of it. One thing I
  14. tried to do was to comment it better than most, although that is more
  15. apparent in the lower-level parts of the code (the lbn* files) that I
  16. wrote first rather than the higher levels, when I didn't need comments
  17. to explain what I was doing to myself.
  18. I can't put a full number theory course in the comments, so there
  19. are some parts that are just going to be confusing unless you
  20. have the background. I'd rather not answer questions that elementary.
  21. But really, I can't stop you from saying whatever you want. If you'd
  22. like to send some comments, good or bad, send me some mail.
  23. --
  24. -Colin <colin@nyx.net>
  25. ** How to build the library
  26. For the full details of how all the bits go together, see bnintern.doc.
  27. If you're on a Unix machine, run the "configure" script (generated from
  28. configure.in using GNU autoconf) and it will set up the basic C version
  29. automatically. Assembly-language support is still configured by hand.
  30. (Just edit the generated Makefile.) If you're on another machine,
  31. you'll have to do everything by hand, although it's still not hard.
  32. This library works in three word sizes: 16, 32 and 64 bits. The limiting
  33. factor is that it needs a double-word multiply, so even a 64-bit SPARC
  34. must use the 32-bit code, because it only produces a 64-bit multiply
  35. result. The DEC Alpha, MIPS R4000 (and up) and the upcoming 64-bit
  36. PowerPC, however, can use the 64-bit version, as they provide access to
  37. the high 64 bits of an integer multiply result.
  38. Trying to compile the file "sizetest.c" will produce an error telling
  39. you what word size is needed. In the absence of any assembly-language
  40. support routines, if your compiler has 64-bit longs or 64-bit long longs,
  41. you'll get the 32-bit version. Otherwise, you'll get the 16-bit version.
  42. The shipped archive contains only the 16-bit version of six important
  43. source files: bn16.c, bn16.h, lbn16.c, lbn16.h, bninit16.c and bntest16.c.
  44. The 32- and 64-bit versions are produced by a trivial replacement process
  45. from those. They are built automatically on demand by the Unix Makefile;
  46. other platforms will have to build them by hand if needed.
  47. If you have the Unix stream editor sed, you can do:
  48. sed -e s/32/64/g -e s/16/32/g bn16.c > bn32.c
  49. sed -e s/32/64/g -e s/16/32/g bn16.h > bn32.h
  50. sed -e s/32/64/g -e s/16/32/g lbn16.c > lbn32.c
  51. sed -e s/32/64/g -e s/16/32/g lbn16.h > lbn32.h
  52. sed -e s/32/64/g -e s/16/32/g bninit16.c > bninit32.c
  53. sed -e s/32/64/g -e s/16/32/g bntest16.c > bntest32.c
  54. If you don't, you'll have to use your favourite text editor and manually:
  55. Copy bn16.c to bn32.c
  56. Copy bn16.h to bn32.h
  57. Copy lbn16.c to lbn32.c
  58. Copy lbn16.h to lbn32.h
  59. Copy bninit16.c to bninit32.c
  60. Copy bntest16.c to bntest32.c
  61. Edit bn32.c, bn32.h, lbn32.c, lbn32.h, bninit32.c and bntest32.c (*32.?)
  62. Globally replace all "32" by "64"
  63. Globally replace all "16" by "32"
  64. I don't know of any non-Unix platforms that can use the 64-bit version,
  65. so you probably won't need it, but the process is siliar, just replace
  66. every "32" with "128" and every "16" with "64".
  67. Once you have all the word size versions you need you can compile them.
  68. If you're compiling a pure C version, or even a simple assembly-language
  69. version, there are some special auto-size-detecting files that will
  70. figure out (at compile time, using <limits.h> and the C preprocessor)
  71. the largest size that it can compile and #include it. To compile that
  72. version, you need to compile the following files:
  73. - bn.c
  74. - bn00.c
  75. - lbn00.c
  76. - lbnmem.c
  77. - legal.c
  78. The file "bntest00.c" (see README.bntest) is a low-level test program
  79. that will check the correct operation of the core low-level routines of
  80. the library. It needs only lbn00.c, lbnmem.c and legal.c.
  81. The file "germtest.c" is a simple program to generate Sophie Germain
  82. primes which demonstrates the library's use. This uses the full library.
  83. ** Adding assembly-language support routines
  84. It is possible to include some assembly-language primitives in this.
  85. For example, for the DEC Alpha primitives, you need to compile everything
  86. with the -DBNINCLUDE=lbnalpha.h flag (or somehow get the effect of
  87. "#define BNINCLUDE lbnalpha.h" in all of the code), and assemble and
  88. link in "lbnalpha.s".
  89. If you want to compile a specific version of the library, say the
  90. 32-bit version, you need to compile together the following files:
  91. - bn.c
  92. - bn32.c
  93. - bninit32.c
  94. - lbn32.c
  95. - lbnmem.c
  96. - legal.c
  97. Note the extra "bninit32.c" file. It contains only the function
  98. "bnInit()" which does nothing but call "bnInit_32()". This is included in
  99. "bn00.c", but is separated out here so that you can compile the library
  100. for two word sizes and replace the bnInit() function with one that will
  101. select a version to initialize at run time! That's described later.
  102. To include assembly-language support routines for a given processor
  103. (as an example, I'll use the mythical "DLX" processor), compile all the
  104. C files with -DBNINCLUDE=lbndlx.h and (or somehow get the effect of
  105. "#define BNINCLUDE lbndlx.h" when compiling all the .c files), and
  106. assemble and link in the lbndlx.s assembly-language file.
  107. The fun comes when you compile a version of the library for two
  108. word sizes. This is currently only supported for the 680x0 and 80x86
  109. processors, which come in 16- and 32-bit versions, but this also makes
  110. sense on MIPS and PowerPC processors that have 32- and 64-bit versions.
  111. To do this, you need to compile the library in two word sizes and include
  112. a custom bndlx.c file that defines a smart bnInit() which chooses between
  113. the two. For the 80x8 family (or the 680x0 family), you want to compile
  114. the following with -DBNINCLUDE=lbn8086.h (or -DBNINCLUDE=lbn68000.h):
  115. - bn.c
  116. - bn16.c
  117. - bn32.c
  118. - lbn16.c
  119. - lbn32.c
  120. - lbnmem.c
  121. - legal.c
  122. - lbn8086.asm (or lbn68000.c and lbn68020.c for the 680x0)
  123. - bn8086.c (or bn68000.c for the 680x0)
  124. The lbn8086.asm file contains the assembly-language subroutines.
  125. The lbn8086.h file contains declarations for them and the
  126. necessary information to call them instead of the C versions
  127. The bn8086.c file contains the single function bnInit(), which
  128. determines the word size of the processor when called and calls
  129. bnInit_16() or bnInit_32(), as appropriate.
  130. To summarize:
  131. To build a (not necessarily optimal) version on any machine, do the following,
  132. or get your favourite make(1)-like utility to do it:
  133. - Compile bn.c, bn00.c, lbn00.c, lbnmem.c and legal.c, with as much
  134. optimization as possible.
  135. - Link all of the .o files together
  136. To build an MS-DOS version that will run well on an 8088 and up,
  137. compile with -DBNINCLUDE=lbn8086.h:
  138. - Compile bn.c, bn16.c, bn32.c, lbn16.c, lbn32.c, lbnmem.c, legal.c and
  139. bn8086.c, with -DBNINCLUDE=lbn8086.h
  140. - Assemble lbn8086.asm
  141. - Link all of the .obj files together
  142. To build an 80x86 WIN16 version (16-bit segmented addressing, 32-bit processor):
  143. - Compile bn.c, bn00.c (or bn32.c and bninit32.c), lbn00.c (or lbn32.c),
  144. lbnmem.c and legal.c, with -DBNINCLUDE=lbn8086.h
  145. - Assemble lbn8086.asm
  146. - Link all of the .obj files together
  147. To build an 80x86 WIN32 version (32-bit flat model),
  148. - Compile bn.c, bn00.c (or bn32.c and bninit32.c), lbn00.c (or lbn32.c),
  149. lbnmem.c and legal.c, with -DBNINCLUDE=lbn80386.h
  150. - Assemble lbn80386.asm
  151. - Link all of the .obj files together
  152. To build a Mac 68K version that will work well on a 68000 and up,
  153. - Compile bn.c, bn16.c, bn32.c, lbn16.c, lbn32.c, lbnmem.c, legal.c and
  154. bn8086.c, with -DBNINCLUDE=lbn68000.h (On Metrowerks, you may have to
  155. build your own precompiled header to achieve this effect.)
  156. - Assemble lbn68000.c and lbn68020.c
  157. - Link all of the .o files together
  158. To build a Mac 68K version which will only work on a 68020 or better,
  159. - Compile bn.c, bn00.c (or bn32.c and bninit32.c), lbn00.c (or lbn32.c),
  160. lbnmem.c and legal.c, with -DBNINCLUDE=lbn68020.h (On Metrowerks, you
  161. may have to build your own precompiled header to achieve this effect.)
  162. - Assemble lbn68020.c
  163. - Link all of the .o files together
  164. To build a Mac PowerPC version,
  165. - Compile bn.c, bn00.c (or bn32.c and bninit32.c), lbn00.c (or lbn32.c),
  166. lbnmem.c and legal.c, with -DBNINCLUDE=lbnppc.h (On Metrowerks, you
  167. may have to build your own precompiled header to achieve this effect.)
  168. - Assemble lbnppc.c
  169. - Link all of the .o files together
  170. To build a Unix 80x86 version (32-bit flat model, AT&T assembler mnemonics),
  171. - Compile bn.c, bn00.c (or bn32.c and bninit32.c), lbn00.c (or lbn32.c),
  172. lbnmem.c and legal.c, with -DBNINCLUDE=lbn80386.h
  173. - Assemble lbn80386.s
  174. - Link all of the .o files together
  175. To build a DEC Alpha version (64-bit math; this *screams*),
  176. - Compile bn.c, bn00.c (or bn64.c and bninit64.c), lbn00.c (or lbn64.c),
  177. lbnmem.c and legal.c, with -DBNINCLUDE=lbnalpha.h
  178. - Assemble lbnalpha.s
  179. - Link all of the .o files together