1 /*==============================================================================
  2 | 
  3 |  NAME     
  4 |
  5 |     Primpoly.h
  6 |
  7 |  DESCRIPTION   
  8 |
  9 |     Global header file for primitive polynomial routines.
 10 |     Constants, message strings, data types and algorithm control parameters.
 11 |
 12 |     User manual and technical documentation are described in detail in my web page at
 13 |     http://seanerikoconnor.freeservers.com/Mathematics/AbstractAlgebra/PrimitivePolynomials/overview.html
 14 |
 15 |  LEGAL
 16 |
 17 |     Primpoly Version 16.1 - A Program for Computing Primitive Polynomials.
 18 |     Copyright (C) 1999-2021 by Sean Erik O'Connor.  All Rights Reserved.
 19 |
 20 |     This program is free software: you can redistribute it and/or modify
 21 |     it under the terms of the GNU General Public License as published by
 22 |     the Free Software Foundation, either version 3 of the License, or
 23 |     (at your option) any later version.
 24 |
 25 |     This program is distributed in the hope that it will be useful,
 26 |     but WITHOUT ANY WARRANTY; without even the implied warranty of
 27 |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 28 |     GNU General Public License for more details.
 29 |
 30 |     You should have received a copy of the GNU General Public License
 31 |     along with this program.  If not, see http://www.gnu.org/licenses/.
 32 |     
 33 |     The author's address is seanerikoconnor!AT!gmail!DOT!com
 34 |     with !DOT! replaced by . and the !AT! replaced by @
 35 |
 36 ==============================================================================*/
 37 
 38 // Wrap this header file to prevent duplication if it is included
 39 // accidentally more than once.
 40 #ifndef PP_H 
 41 #define PP_H
 42 
 43 
 44 /*=============================================================================
 45  |
 46  | NAME
 47  |
 48  |     Debug macros.
 49  |
 50  +============================================================================*/
 51 
 52 // Let's do this always, so we can gather bug information from users.
 53 #define SELF_CHECK
 54 
 55 // These debugging flags are normally be defined in the makefile or build file,
 56 // but you could alternately define them here.
 57 #define DEBUG_ALL
 58 #undef  DEBUG_ALL
 59 
 60 #ifdef DEBUG_ALL
 61     // High level algorithmic debugging:
 62     #define DEBUG_PP_POLYNOMIAL
 63 
 64     // Debug the LALR(1) parser for polynomials and factor tables:
 65     #define DEBUG_PP_PARSER
 66 
 67     // Testing primality:
 68     #define DEBUG_PP_PRIMALITY_TESTING
 69 
 70     // Factoring into primes:
 71     #define DEBUG_PP_FACTOR
 72 
 73     // Arbitrary precision arithmetic debugging:
 74     #define DEBUG_PP_BIGINT
 75 
 76     // Modulo p arithmetic debugging:
 77     #define DEBUG_PP_ARITH
 78 #endif // DEBUG_ALL
 79 
 80 // Forces one or more unit tests to fail and generate test error messages.
 81 // Default is to leave undefined.
 82 //
 83 //    #define DEBUG_PP_FORCE_UNIT_TEST_FAIL
 84 //
 85 // Turn on to check memory exceptions.  Default is to leave it off.
 86 // This may crash some on machines with buggy C++ compilers and OS's.
 87 //
 88 //    #define DEBUG_PP_FORCE_MEMORY_OVERLOAD
 89 
 90 
 91 /*=============================================================================
 92  |
 93  | NAME
 94  |
 95  |     Basic integer types.
 96  |
 97  | DESCRIPTION
 98  |
 99  |     Define the basic integer types we will use for all modulus p calculations,
100  |     multiple precision arithmetic, polynomial operations and factoring.
101  |     Higher precision will decrease your computation time according to profiling.
102  |
103  +============================================================================*/
104 
105 // Microsoft Windows 7 64-bit + Visual C++ compiler or Cygwin.
106 #if defined( _MSC_VER ) || defined( __CYGWIN__ )
107     // 64-bit integer types.
108     typedef unsigned long long ppuint ;
109     typedef   signed long long ppsint ;
110 
111     // 32-bit integer types.
112     typedef unsigned int ppuint32 ;
113     typedef unsigned int ppsint32 ;
114 // Mac OS X or Ubuntu Linux.
115 #else
116     // 64-bit integer types.
117     typedef unsigned long int ppuint ;
118     typedef   signed long int ppsint ;
119 
120     // 32-bit integer types.
121     typedef unsigned int ppuint32 ;
122     typedef unsigned int ppsint32 ;
123 #endif
124 
125 // Check if we have at least 64-bit arithmetic.
126 static_assert( 8 * sizeof( ppuint ) >= 64 || 8 * sizeof( ppsint ) >= 64,
127                "Error:  basic integer types ppuint and ppsint must be at least 64-bits.  Sorry, you'll have to run on a computer with a 64-bit CPU." ) ;
128 
129 // Check if we have 32-bit arithmetic.
130 static_assert( 8 * sizeof( ppuint32 ) == 32 || 8 * sizeof( ppsint32 ) == 32,
131               "Error:  basic integer types ppuint32 and ppsint32 must be at least 32-bits. Redefine the types in Primpoly.h" ) ;
132 
133 
134 /*=============================================================================
135  |
136  | NAME
137  |
138  |     ReturnStatus
139  |
140  | DESCRIPTION
141  |
142  |     Enumerated type integer status fed back to the Unix shell from main().
143  |
144  +============================================================================*/
145 
146 enum class ReturnStatus
147 {
148     Success       = 0,
149     AskForHelp    = 1,
150     PNotPrime     = 2,
151     RangeError    = 3,
152     InternalError = 4,
153     Reserved      = 5
154 } ;
155 
156 
157 /*=============================================================================
158  |
159  | NAME
160  |
161  |     PrimpolyError
162  |
163  | DESCRIPTION
164  |
165  |     Top level error class used in main() and UnitTest.
166  |
167  +============================================================================*/
168 
169 class PrimpolyError : public runtime_error
170 {
171     public:
172         // Throw with error message, file name and line number.
173         PrimpolyError( const string & description, const string & file, const int & line )
174         : runtime_error( description + " in file " + file + " at line " + to_string(line) )
175         {
176         } ;
177 
178         // Throw with an error message.
179         PrimpolyError( const string & description )
180             : runtime_error( description )
181         {
182         } ;
183 
184         // Default throw with no error message.
185         PrimpolyError()
186             : runtime_error( "Polynomial error:  " )
187         {
188         } ;
189 
190 } ; // end class PrimpolyError
191 
192 
193 /*=============================================================================
194  |
195  | NAME
196  |
197  |     Message strings.
198  |
199  | DESCRIPTION
200  |
201  |     Messages to console which are used by main() only.
202  |
203  +============================================================================*/
204 
205 static const string writeToAuthorMessage
206 (
207     "Dear User,\n"
208     "    Sorry you got an error message.  Please email the author at\n"
209     "        seanerikoconnor!AT!gmail!DOT!com\n"
210     "    with !DOT! replaced by . and the !AT! replaced by @\n"
211     #ifdef SELF_CHECK
212     "    Please send me all console output from this program.\n"
213     "    Attach the unitTest.log file which should be located in the current directory.\n"
214     "    However, if the self-check failed, there won't be a log file.\n"
215     #else
216     "    It looks like you have the unit test self check compiled off."
217     "    Please set #define SELF_CHECK in the Primpoly.h header file, recompile and rerun."
218     #endif
219     "Thanks for your help,\n"
220     "Sean E. O'Connor\n"
221     "\n"
222 ) ;
223 
224 static const string confirmWarning
225 (
226     "Confirming polynomial is primitive with a brute force check.\n"
227     "WARNING:  You may wait an impossibly long time!\n"
228     "To abort, hit control-C in your console window to stop this program.\n"
229 ) ;
230 
231 #endif  //  End of wrapper for header.