1/*==============================================================================
2|
3| NAME
4|
5| Primpoly.hpp
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.4 - A Program for Computing Primitive Polynomials.
18| Copyright (C) 1999-2025 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 // Highest level primitivity testing:
62 #define DEBUG_PP_PRIMITIVITY
63 //
64 // Show brute force primitivity check:
65 #define DEBUG_PP_SLOW_PRIMITIVITY_CHECK
66
67 // Polynomial calculations debugging:
68 #define DEBUG_PP_POLYNOMIAL
69
70 // Debug the LALR(1) parser for polynomials and factor tables:
71 #define DEBUG_PP_PARSER
72
73 // Testing primality:
74 #define DEBUG_PP_PRIMALITY_TESTING
75
76 // Factoring into primes:
77 #define DEBUG_PP_FACTOR
78
79 // Arbitrary precision arithmetic debugging:
80 #define DEBUG_PP_BIGINT
81
82 // Modulo p arithmetic debugging:
83 #define DEBUG_PP_ARITH
84#endif // DEBUG_ALL
85
86// Forces one or more unit tests to fail and generate test error messages.
87// Default is to leave undefined.
88//
89// #define DEBUG_PP_FORCE_UNIT_TEST_FAIL
90//
91// Turn on to check memory exceptions. Default is to leave it off.
92// This may crash some on machines with buggy C++ compilers and OS's.
93//
94// #define DEBUG_PP_FORCE_MEMORY_OVERLOAD
95
96
97/*=============================================================================
98 |
99 | NAME
100 |
101 | Basic integer types.
102 |
103 | DESCRIPTION
104 |
105 | Define the basic integer types we will use for all modulus p calculations,
106 | multiple precision arithmetic, polynomial operations and factoring.
107 | Higher precision will decrease your computation time according to profiling.
108 |
109 +============================================================================*/
110
111// Microsoft Windows 7 64-bit + Visual C++ compiler or Cygwin.
112#if defined( _MSC_VER ) || defined( __CYGWIN__ )
113 // 64-bit integer types.
114 typedef unsigned long long ppuint ;
115 typedef signed long long ppsint ;
116
117 // 32-bit integer types.
118 typedef unsigned int ppuint32 ;
119 typedef unsigned int ppsint32 ;
120// Mac OS X or Ubuntu Linux.
121#else
122 // 64-bit integer types.
123 typedef unsigned long int ppuint ;
124 typedef signed long int ppsint ;
125
126 // 32-bit integer types.
127 typedef unsigned int ppuint32 ;
128 typedef unsigned int ppsint32 ;
129#endif
130
131// Check if we have at least 64-bit arithmetic.
132static_assert( 8 * sizeof( ppuint ) >= 64 || 8 * sizeof( ppsint ) >= 64,
133 "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." ) ;
134
135// Check if we have 32-bit arithmetic.
136static_assert( 8 * sizeof( ppuint32 ) == 32 || 8 * sizeof( ppsint32 ) == 32,
137 "Error: basic integer types ppuint32 and ppsint32 must be at least 32-bits. Redefine the types in Primpoly.hpp" ) ;
138
139
140/*=============================================================================
141 |
142 | NAME
143 |
144 | ReturnStatus
145 |
146 | DESCRIPTION
147 |
148 | Enumerated type integer status fed back to the Unix shell from main().
149 |
150 +============================================================================*/
151
152enum class ReturnStatus
153{
154 Success = 0,
155 AskForHelp = 1,
156 PNotPrime = 2,
157 RangeError = 3,
158 InternalError = 4,
159 Reserved = 5
160} ;
161
162
163/*=============================================================================
164 |
165 | NAME
166 |
167 | PrimpolyError
168 |
169 | DESCRIPTION
170 |
171 | Top level error class used in main() and UnitTest.
172 |
173 +============================================================================*/
174
175class PrimpolyError : public runtime_error
176{
177 public:
178 // Throw with error message, file name and line number.
179 PrimpolyError( const string & description, const string & file, const int & line )
180 : runtime_error( description + " in file " + file + " at line " + to_string(line) )
181 {
182 } ;
183
184 // Throw with an error message.
185 PrimpolyError( const string & description )
186 : runtime_error( description )
187 {
188 } ;
189
190 // Default throw with no error message.
191 PrimpolyError()
192 : runtime_error( "Polynomial error: " )
193 {
194 } ;
195
196} ; // end class PrimpolyError
197
198
199/*=============================================================================
200 |
201 | NAME
202 |
203 | Message strings.
204 |
205 | DESCRIPTION
206 |
207 | Messages to console which are used by main() only.
208 |
209 +============================================================================*/
210
211static const string writeToAuthorMessage
212(
213 "Dear User,\n"
214 " Sorry you got an error message. Please email the author at\n"
215 " seanerikoconnor!AT!gmail!DOT!com\n"
216 " with !DOT! replaced by . and the !AT! replaced by @\n"
217 #ifdef SELF_CHECK
218 " Please send me all console output from this program.\n"
219 " Attach the unitTest.log file which should be located in the current directory.\n"
220 " However, if the self-check failed, there won't be a log file.\n"
221 #else
222 " It looks like you have the unit test self check compiled off."
223 " Please set #define SELF_CHECK in the Primpoly.hpp header file, recompile and rerun."
224 #endif
225 "Thanks for your help,\n"
226 "Sean E. O'Connor\n"
227 "\n"
228) ;
229
230static const string confirmWarning
231(
232 "Confirming polynomial is primitive with a brute force check.\n"
233 "WARNING: You may wait an impossibly long time!\n"
234 "To abort, hit control-C in your console window to stop this program.\n"
235) ;
236
237#endif // End of wrapper for header.