All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ttmathtypes.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TTMath Bignum Library
3  * and is distributed under the (new) BSD licence.
4  * Author: Tomasz Sowa <t.sowa@ttmath.org>
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 
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 
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 
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 
115 #ifndef TTMATH_FORCEASM
116 
117  #if !defined __i386__ && !defined _X86_ && !defined _M_IX86 && !defined __x86_64__ && !defined _M_X64
118 
130  #define TTMATH_NOASM
131  #endif
132 
133 
134 
135  #if !defined _MSC_VER && !defined __GNUC__
136 
140  #define TTMATH_NOASM
141  #endif
142 
143  #if defined __MIC__
144 
147  #define TTMATH_NOASM
148  #endif
149 
150 
151 #endif
152 
153 
154 namespace ttmath
155 {
156 
157 
158 #ifdef TTMATH_PLATFORM32
159 
163  typedef unsigned int uint;
164  typedef signed int sint;
165 
169  #ifdef _MSC_VER
170  // long long on MS Windows (Visual and GCC mingw compilers) have 64 bits
171  // stdint.h is not available on Visual Studio prior to VS 2010 version
172  typedef unsigned long long int ulint;
173  typedef signed long long int slint;
174  #else
175  // we do not use 'long' here because there is a difference in unix and windows
176  // environments: in unix 'long' has 64 bits but in windows it has only 32 bits
177  typedef uint64_t ulint;
178  typedef int64_t slint;
179  #endif
180 
184  #define TTMATH_BITS_PER_UINT 32u
185 
189  #define TTMATH_UINT_HIGHEST_BIT 2147483648u
190 
195  #define TTMATH_UINT_MAX_VALUE 4294967295u
196 
202  #define TTMATH_BUILTIN_VARIABLES_SIZE 256u
203 
209  #define TTMATH_BITS(min_bits) ((min_bits-1)/32 + 1)
210 
211 #else
212 
216  #ifdef _MSC_VER
217  /* in VC 'long' type has 32 bits, __int64 is VC extension */
218  typedef unsigned __int64 uint;
219  typedef signed __int64 sint;
220  #else
221  typedef unsigned long uint;
222  typedef signed long sint;
223  #endif
224 
232  #define TTMATH_BITS_PER_UINT 64ul
233 
237  #define TTMATH_UINT_HIGHEST_BIT 9223372036854775808ul
238 
243  #define TTMATH_UINT_MAX_VALUE 18446744073709551615ul
244 
250  #define TTMATH_BUILTIN_VARIABLES_SIZE 128ul
251 
257  #define TTMATH_BITS(min_bits) ((min_bits-1)/64 + 1)
258 
259 #endif
260 }
261 
262 
263 #if defined(TTMATH_MULTITHREADS) && !defined(TTMATH_MULTITHREADS_NOSYNC)
264  #if !defined(TTMATH_POSIX_THREADS) && !defined(TTMATH_WIN32_THREADS)
265 
266  #if defined(_WIN32)
267  #define TTMATH_WIN32_THREADS
268  #elif defined(unix) || defined(__unix__) || defined(__unix)
269  #define TTMATH_POSIX_THREADS
270  #endif
271 
272  #endif
273 #endif
274 
275 
276 
289 #define TTMATH_ARITHMETIC_MAX_LOOP 10000
290 
291 
292 
298 #ifdef TTMATH_DEBUG_LOG
299  // if TTMATH_DEBUG_LOG is defined then we should use the same size regardless of the compiler
300  #define TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE 3
301 #else
302  #ifdef __GNUC__
303  #define TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE 3
304  #else
305  #define TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE 5
306  #endif
307 #endif
308 
309 
317 #define TTMATH_GAMMA_BOUNDARY 2000
318 
319 
320 
321 
322 
323 namespace ttmath
324 {
325 
336  {
343  };
344 
345 
350  {
351  err_ok = 0,
378  };
379 
380 
385  struct Conv
386  {
392 
393 
399  bool scient;
400 
401 
409 
410 
424 
425 
439 
440 
448 
449 
455 
456 
466 
467 
475 
476 
482 
483 
486  uint group_exp; // not implemented yet
487 
488 
489 
490 
492  {
493  // default values
494  base = 10;
495  scient = false;
496  scient_from = 15;
497  base_round = true;
498  round = -1;
499  trim_zeroes = true;
500  comma = '.';
501  comma2 = ',';
502  group = 0;
503  group_digits = 3;
504  group_exp = 0;
505  }
506  };
507 
508 
509 
521  {
522  public:
523  virtual bool WasStopSignal() const volatile { return false; }
524  virtual ~StopCalculating(){}
525  };
526 
527 
535  {
536  const char * file;
537  int line;
538 
539  public:
540  ExceptionInfo() : file(0), line(0) {}
541  ExceptionInfo(const char * f, int l) : file(f), line(l) {}
542 
543  std::string Where() const
544  {
545  if( !file )
546  return "unknown";
547 
548  std::ostringstream result;
549  result << file << ":" << line;
550 
551  return result.str();
552  }
553  };
554 
555 
570  class ReferenceError : public std::logic_error, public ExceptionInfo
571  {
572  public:
573 
574  ReferenceError() : std::logic_error("reference error")
575  {
576  }
577 
578  ReferenceError(const char * f, int l) :
579  std::logic_error("reference error"), ExceptionInfo(f,l)
580  {
581  }
582 
583  std::string Where() const
584  {
585  return ExceptionInfo::Where();
586  }
587  };
588 
589 
602  class RuntimeError : public std::runtime_error, public ExceptionInfo
603  {
604  public:
605 
606  RuntimeError() : std::runtime_error("internal error")
607  {
608  }
609 
610  RuntimeError(const char * f, int l) :
611  std::runtime_error("internal error"), ExceptionInfo(f,l)
612  {
613  }
614 
615  std::string Where() const
616  {
617  return ExceptionInfo::Where();
618  }
619  };
620 
621 
622 
634  #if defined DEBUG || defined _DEBUG
635  #define TTMATH_DEBUG
636  #endif
637 
638 
639  #ifdef TTMATH_DEBUG
640 
641  #if defined(__FILE__) && defined(__LINE__)
642 
643  #define TTMATH_REFERENCE_ASSERT(expression) \
644  if( &(expression) == this ) throw ttmath::ReferenceError(__FILE__, __LINE__);
645 
646  #define TTMATH_ASSERT(expression) \
647  if( !(expression) ) throw ttmath::RuntimeError(__FILE__, __LINE__);
648 
649  #else
650 
651  #define TTMATH_REFERENCE_ASSERT(expression) \
652  if( &(expression) == this ) throw ReferenceError();
653 
654  #define TTMATH_ASSERT(expression) \
655  if( !(expression) ) throw RuntimeError();
656  #endif
657 
658  #else
659  #define TTMATH_REFERENCE_ASSERT(expression)
660  #define TTMATH_ASSERT(expression)
661  #endif
662 
663 
664 
665  #ifdef TTMATH_DEBUG_LOG
666  #define TTMATH_LOG(msg) PrintLog(msg, std::cout);
667  #define TTMATH_LOGC(msg, carry) PrintLog(msg, carry, std::cout);
668  #define TTMATH_VECTOR_LOG(msg, vector, len) PrintVectorLog(msg, std::cout, vector, len);
669  #define TTMATH_VECTOR_LOGC(msg, carry, vector, len) PrintVectorLog(msg, carry, std::cout, vector, len);
670  #else
671  #define TTMATH_LOG(msg)
672  #define TTMATH_LOGC(msg, carry)
673  #define TTMATH_VECTOR_LOG(msg, vector, len)
674  #define TTMATH_VECTOR_LOGC(msg, carry, vector, len)
675  #endif
676 
677 
678 
679 
680 } // namespace
681 
682 
683 #endif
684 
Definition: ttmathtypes.h:366
Definition: ttmathtypes.h:357
Definition: ttmathtypes.h:342
std::string Where() const
Definition: ttmathtypes.h:615
Definition: ttmathtypes.h:385
Definition: ttmathtypes.h:362
Definition: ttmathtypes.h:375
Definition: ttmathtypes.h:365
Definition: ttmathtypes.h:360
Definition: ttmathtypes.h:364
Definition: ttmathtypes.h:363
Definition: ttmathtypes.h:373
Definition: ttmathtypes.h:353
std::string Where() const
Definition: ttmathtypes.h:583
sint scient_from
Definition: ttmathtypes.h:408
bool scient
Definition: ttmathtypes.h:399
virtual ~StopCalculating()
Definition: ttmathtypes.h:524
int64_t slint
Definition: ttmathtypes.h:178
virtual bool WasStopSignal() const volatile
Definition: ttmathtypes.h:523
ReferenceError(const char *f, int l)
Definition: ttmathtypes.h:578
Definition: ttmathtypes.h:352
Definition: ttmathtypes.h:370
Definition: ttmathtypes.h:372
ReferenceError()
Definition: ttmathtypes.h:574
Definition: ttmathtypes.h:534
Definition: ttmathtypes.h:339
uint group_digits
Definition: ttmathtypes.h:481
Definition: ttmathtypes.h:356
Definition: ttmathtypes.h:371
Definition: ttmathtypes.h:359
uint base
Definition: ttmathtypes.h:391
ErrorCode
Definition: ttmathtypes.h:349
signed int sint
Definition: ttmathtypes.h:164
uint group
Definition: ttmathtypes.h:474
LibTypeCode
Definition: ttmathtypes.h:335
Definition: ttmathtypes.h:368
std::string Where() const
Definition: ttmathtypes.h:543
Definition: ttmathtypes.h:377
Definition: ttmathtypes.h:358
RuntimeError(const char *f, int l)
Definition: ttmathtypes.h:610
Definition: ttmathtypes.h:570
Definition: ttmathtypes.h:367
Conv()
Definition: ttmathtypes.h:491
Definition: ttmathtypes.h:340
uint comma
Definition: ttmathtypes.h:454
uint64_t ulint
Definition: ttmathtypes.h:177
Definition: ttmathtypes.h:376
Definition: ttmathtypes.h:341
Definition: ttmathtypes.h:338
Definition: ttmathtypes.h:337
Definition: ttmathtypes.h:520
Definition: ttmathtypes.h:602
ExceptionInfo()
Definition: ttmathtypes.h:540
bool base_round
Definition: ttmathtypes.h:423
Definition: ttmathtypes.h:369
unsigned int uint
Definition: ttmathtypes.h:163
ExceptionInfo(const char *f, int l)
Definition: ttmathtypes.h:541
Definition: ttmathtypes.h:354
Definition: ttmathtypes.h:351
uint group_exp
Definition: ttmathtypes.h:486
bool trim_zeroes
Definition: ttmathtypes.h:447
Definition: ttmathtypes.h:355
uint comma2
Definition: ttmathtypes.h:465
RuntimeError()
Definition: ttmathtypes.h:606
Definition: ttmathtypes.h:374
Definition: ttmathtypes.h:361
sint round
Definition: ttmathtypes.h:438