Debian Codegraph @

extern/ttmath/ttmathtypes.h

raw
1 /*
2 * This file is a part of TTMath Bignum Library
3 * and is distributed under the (new) BSD licence.
4 * Author: Tomasz Sowa <[email protected]>
5 */
6
7 /*
8 * Copyright (c) 2006-2012, Tomasz Sowa
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 * * Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 *
17 * * Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * * Neither the name Tomasz Sowa nor the names of contributors to this
22 * project may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35 * THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38
39 #ifndef headerfilettmathtypes
40 #define headerfilettmathtypes
41
42 /*!
43 \file ttmathtypes.h
44 \brief constants used in the library
45
46 As our library is written in header files (templates) we cannot use
47 constants like 'const int' etc. because we should have some source files
48 *.cpp to define this variables. Only what we can have are constants
49 defined by #define preprocessor macros.
50
51 All macros are preceded by TTMATH_ prefix
52 */
53
54
55 #include <stdexcept>
56 #include <sstream>
57 #include <vector>
58
59 #ifndef _MSC_VER
60 #include <stdint.h>
61 // for uint64_t and int64_t on a 32 bit platform
62 #endif
63
64
65
66 /*!
67 the version of the library
68
69 TTMATH_PRERELEASE_VER is either zero or one
70 zero means that this is the release version of the library
71 (one means something like beta)
72 */
73 #define TTMATH_MAJOR_VER 0
74 #define TTMATH_MINOR_VER 9
75 #define TTMATH_REVISION_VER 3
76
77 #define TTMATH_PRERELEASE_VER 0
78
79
80
81 /*!
82 you can define a platform explicitly by defining either
83 TTMATH_PLATFORM32 or TTMATH_PLATFORM64 macro
84 */
85 #if !defined TTMATH_PLATFORM32 && !defined TTMATH_PLATFORM64
86
87 #if !defined _M_X64 && !defined __x86_64__
88
89 /*
90 other platforms than x86 and amd64 are not recognized at the moment
91 so you should set TTMATH_PLATFORMxx manually
92 */
93
94 // we're using a 32bit platform
95 #define TTMATH_PLATFORM32
96
97 #else
98
99 // we're using a 64bit platform
100 #define TTMATH_PLATFORM64
101
102 #endif
103
104 #endif
105
106
107 /*!
108 asm version of the library is available by default only for:
109 x86 and amd64 platforms and for Microsoft Visual and GCC compilers
110
111 but you can force using asm version (the same asm as for Microsoft Visual)
112 by defining TTMATH_FORCEASM macro
113 you have to be sure that your compiler accept such an asm format
114 */
115 #ifndef TTMATH_FORCEASM
116
117 #if !defined __i386__ && !defined _X86_ && !defined _M_IX86 && !defined __x86_64__ && !defined _M_X64
118 /*!
119 x86 architecture:
120 __i386__ defined by GNU C
121 _X86_ defined by MinGW32
122 _M_IX86 defined by Visual Studio, Intel C/C++, Digital Mars and Watcom C/C++
123
124 amd64 architecture:
125 __x86_64__ defined by GNU C, CLANG (LLVM) and Sun Studio
126 _M_X64 defined by Visual Studio
127
128 asm version is available only for x86 or amd64 platforms
129 */
130 #define TTMATH_NOASM
131 #endif
132
133
134
135 #if !defined _MSC_VER && !defined __GNUC__
136 /*!
137 another compilers than MS VC or GCC or CLANG (LLVM) by default use no asm version
138 (CLANG defines __GNUC__ too)
139 */
140 #define TTMATH_NOASM
141 #endif
142
143 #endif
144
145 #define TTMATH_NOASM
146
147 namespace ttmath
148 {
149
150
151 #ifdef TTMATH_PLATFORM32
152
153 /*!
154 on 32bit platforms one word (uint, sint) will be equal 32bits
155 */
156 typedef unsigned int uint;
157 typedef signed int sint;
158
159 /*!
160 on 32 bit platform ulint and slint will be equal 64 bits
161 */
162 #ifdef _MSC_VER
163 // long long on MS Windows (Visual and GCC mingw compilers) have 64 bits
164 // stdint.h is not available on Visual Studio prior to VS 2010 version
165 typedef unsigned long long int ulint;
166 typedef signed long long int slint;
167 #else
168 // we do not use 'long' here because there is a difference in unix and windows
169 // environments: in unix 'long' has 64 bits but in windows it has only 32 bits
170 typedef uint64_t ulint;
171 typedef int64_t slint;
172 #endif
173
174 /*!
175 how many bits there are in the uint type
176 */
177 #define TTMATH_BITS_PER_UINT 32u
178
179 /*!
180 the mask for the highest bit in the unsigned 32bit word (2^31)
181 */
182 #define TTMATH_UINT_HIGHEST_BIT 2147483648u
183
184 /*!
185 the max value of the unsigned 32bit word (2^32 - 1)
186 (all bits equal one)
187 */
188 #define TTMATH_UINT_MAX_VALUE 4294967295u
189
190 /*!
191 the number of words (32bit words on 32bit platform)
192 which are kept in built-in variables for a Big<> type
193 (these variables are defined in ttmathbig.h)
194 */
195 #define TTMATH_BUILTIN_VARIABLES_SIZE 256u
196
197 /*!
198 this macro returns the number of machine words
199 capable to hold min_bits bits
200 e.g. TTMATH_BITS(128) returns 4
201 */
202 #define TTMATH_BITS(min_bits) ((min_bits-1)/32 + 1)
203
204 #else
205
206 /*!
207 on 64bit platforms one word (uint, sint) will be equal 64bits
208 */
209 #ifdef _MSC_VER
210 /* in VC 'long' type has 32 bits, __int64 is VC extension */
211 typedef unsigned __int64 uint;
212 typedef signed __int64 sint;
213 #else
214 typedef unsigned long uint;
215 typedef signed long sint;
216 #endif
217
218 /*!
219 on 64bit platforms we do not define ulint and slint
220 */
221
222 /*!
223 how many bits there are in the uint type
224 */
225 #define TTMATH_BITS_PER_UINT 64ul
226
227 /*!
228 the mask for the highest bit in the unsigned 64bit word (2^63)
229 */
230 #define TTMATH_UINT_HIGHEST_BIT 9223372036854775808ul
231
232 /*!
233 the max value of the unsigned 64bit word (2^64 - 1)
234 (all bits equal one)
235 */
236 #define TTMATH_UINT_MAX_VALUE 18446744073709551615ul
237
238 /*!
239 the number of words (64bit words on 64bit platforms)
240 which are kept in built-in variables for a Big<> type
241 (these variables are defined in ttmathbig.h)
242 */
243 #define TTMATH_BUILTIN_VARIABLES_SIZE 128ul
244
245 /*!
246 this macro returns the number of machine words
247 capable to hold min_bits bits
248 e.g. TTMATH_BITS(128) returns 2
249 */
250 #define TTMATH_BITS(min_bits) ((min_bits-1)/64 + 1)
251
252 #endif
253 }
254
255
256 #if defined(TTMATH_MULTITHREADS) && !defined(TTMATH_MULTITHREADS_NOSYNC)
257 #if !defined(TTMATH_POSIX_THREADS) && !defined(TTMATH_WIN32_THREADS)
258
259 #if defined(_WIN32)
260 #define TTMATH_WIN32_THREADS
261 #elif defined(unix) || defined(__unix__) || defined(__unix)
262 #define TTMATH_POSIX_THREADS
263 #endif
264
265 #endif
266 #endif
267
268
269
270 /*!
271 this variable defines how many iterations are performed
272 during some kind of calculating when we're making any long formulas
273 (for example Taylor series)
274
275 it's used in ExpSurrounding0(...), LnSurrounding1(...), Sin0pi05(...), etc.
276
277 note! there'll not be so many iterations, iterations are stopped when
278 there is no sense to continue calculating (for example when the result
279 still remains unchanged after adding next series and we know that the next
280 series are smaller than previous ones)
281 */
282 #define TTMATH_ARITHMETIC_MAX_LOOP 10000
283
284
285
286 /*!
287 this is a limit when calculating Karatsuba multiplication
288 if the size of a vector is smaller than TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE
289 the Karatsuba algorithm will use standard schoolbook multiplication
290 */
291 #ifdef TTMATH_DEBUG_LOG
292 // if TTMATH_DEBUG_LOG is defined then we should use the same size regardless of the compiler
293 #define TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE 3
294 #else
295 #ifdef __GNUC__
296 #define TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE 3
297 #else
298 #define TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE 5
299 #endif
300 #endif
301
302
303 /*!
304 this is a special value used when calculating the Gamma(x) function
305 if x is greater than this value then the Gamma(x) will be calculated using
306 some kind of series
307
308 don't use smaller values than about 100
309 */
310 #define TTMATH_GAMMA_BOUNDARY 2000
311
312
313
314
315
316 namespace ttmath
317 {
318
319 /*!
320 lib type codes:
321 asm_vc_32 - with asm code designed for Microsoft Visual C++ (32 bits)
322 asm_gcc_32 - with asm code designed for GCC (32 bits)
323 asm_vc_64 - with asm for VC (64 bit)
324 asm_gcc_64 - with asm for GCC (64 bit)
325 no_asm_32 - pure C++ version (32 bit) - without any asm code
326 no_asm_64 - pure C++ version (64 bit) - without any asm code
327 */
328 enum LibTypeCode
329 {
330 asm_vc_32 = 0,
331 asm_gcc_32,
332 asm_vc_64,
333 asm_gcc_64,
334 no_asm_32,
335 no_asm_64
336 };
337
338
339 /*!
340 error codes
341 */
342 enum ErrorCode
343 {
344 err_ok = 0,
345 err_nothing_has_read,
346 err_unknown_character,
347 err_unexpected_final_bracket,
348 err_stack_not_clear,
349 err_unknown_variable,
350 err_division_by_zero,
351 err_interrupt,
352 err_overflow,
353 err_unknown_function,
354 err_unknown_operator,
355 err_unexpected_semicolon_operator,
356 err_improper_amount_of_arguments,
357 err_improper_argument,
358 err_unexpected_end,
359 err_internal_error,
360 err_incorrect_name,
361 err_incorrect_value,
362 err_variable_exists,
363 err_variable_loop,
364 err_functions_loop,
365 err_must_be_only_one_value,
366 err_object_exists,
367 err_unknown_object,
368 err_still_calculating,
369 err_in_short_form_used_function,
370 err_percent_from
371 };
372
373
374 /*!
375 this struct is used when converting to/from a string
376 /temporarily only in Big::ToString() and Big::FromString()/
377 */
378 struct Conv
379 {
380 /*!
381 base (radix) on which the value will be shown (or read)
382 default: 10
383 */
384 uint base;
385
386
387 /*!
388 used only in Big::ToString()
389 if true the value will be always shown in the scientific mode, e.g: 123e+30
390 default: false
391 */
392 bool scient;
393
394
395 /*!
396 used only in Big::ToString()
397 if scient is false then the value will be printed in the scientific mode
398 only if the exponent is greater than scien_from
399 default: 15
400 */
401 sint scient_from;
402
403
404 /*!
405 if 'base_round' is true and 'base' is different from 2, 4, 8, or 16
406 and the result value is not an integer then we make an additional rounding
407 (after converting the last digit from the result is skipped)
408 default: true
409
410 e.g.
411 Conv c;
412 c.base_round = false;
413 Big<1, 1> a = "0.1"; // decimal input
414 std::cout << a.ToString(c) << std::endl; // the result is: 0.099999999
415 */
416 bool base_round;
417
418
419 /*!
420 used only in Big::ToString()
421 tells how many digits after comma are possible
422 default: -1 which means all digits are printed
423
424 set it to zero if you want integer value only
425
426 for example when the value is:
427 12.345678 and 'round' is 4
428 then the result will be
429 12.3457 (the last digit was rounded)
430 */
431 sint round;
432
433
434 /*!
435 if true that not mattered digits in the mantissa will be cut off
436 (zero characters at the end -- after the comma operator)
437 e.g. 1234,78000 will be: 1234,78
438 default: true
439 */
440 bool trim_zeroes;
441
442
443 /*!
444 the main comma operator (used when reading and writing)
445 default is a dot '.'
446 */
447 uint comma;
448
449
450 /*!
451 additional comma operator (used only when reading)
452 if you don't want it just set it to zero
453 default is a comma ','
454
455 this allowes you to convert from a value:
456 123.45 as well as from 123,45
457 */
458 uint comma2;
459
460
461 /*!
462 it sets the character which is used for grouping
463 if group=' ' then: 1234,56789 will be printed as: 1 234,567 89
464
465 if you don't want grouping just set it to zero (which is default)
466 */
467 uint group;
468
469
470 /*!
471 how many digits should be grouped (it is used if 'group' is non zero)
472 default: 3
473 */
474 uint group_digits;
475
476
477 /*!
478 */
479 uint group_exp; // not implemented yet
480
481
482
483
484 Conv()
485 {
486 // default values
487 base = 10;
488 scient = false;
489 scient_from = 15;
490 base_round = true;
491 round = -1;
492 trim_zeroes = true;
493 comma = '.';
494 comma2 = ',';
495 group = 0;
496 group_digits = 3;
497 group_exp = 0;
498 }
499 };
500
501
502
503 /*!
504 this simple class can be used in multithreading model
505 (you can write your own class derived from this one)
506
507 for example: in some functions like Factorial()
508 /at the moment only Factorial/ you can give a pointer to
509 the 'stop object', if the method WasStopSignal() of this
510 object returns true that means we should break the calculating
511 and return
512 */
513 class StopCalculating
514 {
515 public:
516 virtual bool WasStopSignal() const volatile { return false; }
517 virtual ~StopCalculating(){}
518 };
519
520
521 /*!
522 a small class which is useful when compiling with gcc
523
524 object of this type holds the name and the line of a file
525 in which the macro TTMATH_ASSERT or TTMATH_REFERENCE_ASSERT was used
526 */
527 class ExceptionInfo
528 {
529 const char * file;
530 int line;
531
532 public:
533 ExceptionInfo() : file(0), line(0) {}
534 ExceptionInfo(const char * f, int l) : file(f), line(l) {}
535
536 std::string Where() const
537 {
538 if( !file )
539 return "unknown";
540
541 std::ostringstream result;
542 result << file << ":" << line;
543
544 return result.str();
545 }
546 };
547
548
549 /*!
550 A small class used for reporting 'reference' errors
551
552 In the library is used macro TTMATH_REFERENCE_ASSERT which
553 can throw an exception of this type
554
555 ** from version 0.9.2 this macro is removed from all methods
556 in public interface so you don't have to worry about it **
557
558 If you compile with gcc you can get a small benefit
559 from using method Where() (it returns std::string) with
560 the name and the line of a file where the macro TTMATH_REFERENCE_ASSERT
561 was used)
562 */
563 class ReferenceError : public std::logic_error, public ExceptionInfo
564 {
565 public:
566
567 ReferenceError() : std::logic_error("reference error")
568 {
569 }
570
571 ReferenceError(const char * f, int l) :
572 std::logic_error("reference error"), ExceptionInfo(f,l)
573 {
574 }
575
576 std::string Where() const
577 {
578 return ExceptionInfo::Where();
579 }
580 };
581
582
583 /*!
584 a small class used for reporting errors
585
586 in the library is used macro TTMATH_ASSERT which
587 (if the condition in it is false) throw an exception
588 of this type
589
590 if you compile with gcc you can get a small benefit
591 from using method Where() (it returns std::string) with
592 the name and the line of a file where the macro TTMATH_ASSERT
593 was used)
594 */
595 class RuntimeError : public std::runtime_error, public ExceptionInfo
596 {
597 public:
598
599 RuntimeError() : std::runtime_error("internal error")
600 {
601 }
602
603 RuntimeError(const char * f, int l) :
604 std::runtime_error("internal error"), ExceptionInfo(f,l)
605 {
606 }
607
608 std::string Where() const
609 {
610 return ExceptionInfo::Where();
611 }
612 };
613
614
615
616 /*!
617 TTMATH_DEBUG
618 this macro enables further testing during writing your code
619 you don't have to define it in a release mode
620
621 if this macro is set then macros TTMATH_ASSERT and TTMATH_REFERENCE_ASSERT
622 are set as well and these macros can throw an exception if a condition in it
623 is not fulfilled (look at the definition of TTMATH_ASSERT and TTMATH_REFERENCE_ASSERT)
624
625 TTMATH_DEBUG is set automatically if DEBUG or _DEBUG are defined
626 */
627 #if defined DEBUG || defined _DEBUG
628 #define TTMATH_DEBUG
629 #endif
630
631
632 #ifdef TTMATH_DEBUG
633
634 #if defined(__FILE__) && defined(__LINE__)
635
636 #define TTMATH_REFERENCE_ASSERT(expression) \
637 if( &(expression) == this ) throw ttmath::ReferenceError(__FILE__, __LINE__);
638
639 #define TTMATH_ASSERT(expression) \
640 if( !(expression) ) throw ttmath::RuntimeError(__FILE__, __LINE__);
641
642 #else
643
644 #define TTMATH_REFERENCE_ASSERT(expression) \
645 if( &(expression) == this ) throw ReferenceError();
646
647 #define TTMATH_ASSERT(expression) \
648 if( !(expression) ) throw RuntimeError();
649 #endif
650
651 #else
652 #define TTMATH_REFERENCE_ASSERT(expression)
653 #define TTMATH_ASSERT(expression)
654 #endif
655
656
657
658 #ifdef TTMATH_DEBUG_LOG
659 #define TTMATH_LOG(msg) PrintLog(msg, std::cout);
660 #define TTMATH_LOGC(msg, carry) PrintLog(msg, carry, std::cout);
661 #define TTMATH_VECTOR_LOG(msg, vector, len) PrintVectorLog(msg, std::cout, vector, len);
662 #define TTMATH_VECTOR_LOGC(msg, carry, vector, len) PrintVectorLog(msg, carry, std::cout, vector, len);
663 #else
664 #define TTMATH_LOG(msg)
665 #define TTMATH_LOGC(msg, carry)
666 #define TTMATH_VECTOR_LOG(msg, vector, len)
667 #define TTMATH_VECTOR_LOGC(msg, carry, vector, len)
668 #endif
669
670
671
672
673 } // namespace
674
675
676 #endif
677