GEOS  3.8.0dev
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 3-Clause BSD Licence.
4  * Author: Tomasz Sowa <t.sowa@ttmath.org>
5  */
6 
7 /*
8  * Copyright (c) 2006-2017, 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 
82 #define TTMATH_MINOR_VER 9
83 
91 #define TTMATH_REVISION_VER 4
92 
102 #define TTMATH_PRERELEASE_VER 1
103 
104 
105 
110 #if !defined TTMATH_PLATFORM32 && !defined TTMATH_PLATFORM64
111 
112  #if !defined _M_X64 && !defined __x86_64__
113 
114  /*
115  other platforms than x86 and amd64 are not recognized at the moment
116  so you should set TTMATH_PLATFORMxx manually
117  */
118 
119  // we're using a 32bit platform
120  #define TTMATH_PLATFORM32
121 
122  #else
123 
124  // we're using a 64bit platform
125  #define TTMATH_PLATFORM64
126 
127  #endif
128 
129 #endif
130 
131 
140 #ifndef TTMATH_FORCEASM
141 
142  #if !defined __i386__ && !defined _X86_ && !defined _M_IX86 && !defined __x86_64__ && !defined _M_X64
143 
155  #define TTMATH_NOASM
156  #endif
157 
158 
159 
160  #if !defined _MSC_VER && !defined __GNUC__
161 
165  #define TTMATH_NOASM
166  #endif
167 
168  /* 32-bit gcc < 5 doesn't like to build the ASM with PIC enabled as */
169  /* we do for GEOS */
170  #if defined __GNUC__ && __GNUC__ < 5 && !defined __x86_64__ && !defined _M_X64
171  #define TTMATH_NOASM
172  #endif
173 
174 #endif
175 
176 
177 namespace ttmath
178 {
179 
180 
181 #ifdef TTMATH_PLATFORM32
182 
186  typedef unsigned int uint;
187  typedef signed int sint;
188 
192  #ifdef _MSC_VER
193  // long long on MS Windows (Visual and GCC mingw compilers) have 64 bits
194  // stdint.h is not available on Visual Studio prior to VS 2010 version
195  typedef unsigned long long int ulint;
196  typedef signed long long int slint;
197  #else
198  // we do not use 'long' here because there is a difference in unix and windows
199  // environments: in unix 'long' has 64 bits but in windows it has only 32 bits
200  typedef uint64_t ulint;
201  typedef int64_t slint;
202  #endif
203 
207  #define TTMATH_BITS_PER_UINT 32u
208 
212  #define TTMATH_UINT_HIGHEST_BIT 2147483648u
213 
218  #define TTMATH_UINT_MAX_VALUE 4294967295u
219 
225  #define TTMATH_BUILTIN_VARIABLES_SIZE 256u
226 
232  #define TTMATH_BITS(min_bits) ((min_bits-1)/32 + 1)
233 
234 #else
235 
236  #ifdef _MSC_VER
237  /* in VC 'long' type has 32 bits, __int64 is VC extension */
238  typedef unsigned __int64 uint;
239  typedef signed __int64 sint;
240  #else
241  #ifdef __MINGW64__
242  //Mingw64 64-bit patch from https://www.ttmath.org/forum/patch_for_building_64_bit_using_windows_mingw64_gcc
243  typedef uint64_t uint;
244  typedef int64_t sint;
245  #else
246 
249  typedef unsigned long uint;
253  typedef signed long sint;
254  #endif
255 
256  #endif
257 
265  #define TTMATH_BITS_PER_UINT 64ul
266 
270  #define TTMATH_UINT_HIGHEST_BIT 9223372036854775808ul
271 
276  #define TTMATH_UINT_MAX_VALUE 18446744073709551615ul
277 
283  #define TTMATH_BUILTIN_VARIABLES_SIZE 128ul
284 
290  #define TTMATH_BITS(min_bits) ((min_bits-1)/64 + 1)
291 
292 #endif
293 }
294 
295 
296 #if defined(TTMATH_MULTITHREADS) && !defined(TTMATH_MULTITHREADS_NOSYNC)
297  #if !defined(TTMATH_POSIX_THREADS) && !defined(TTMATH_WIN32_THREADS)
298 
299  #if defined(_WIN32)
300  #define TTMATH_WIN32_THREADS
301  #elif defined(unix) || defined(__unix__) || defined(__unix)
302  #define TTMATH_POSIX_THREADS
303  #endif
304 
305  #endif
306 #endif
307 
308 
309 
322 #define TTMATH_ARITHMETIC_MAX_LOOP 10000
323 
324 
325 
331 #ifdef TTMATH_DEBUG_LOG
332  // if TTMATH_DEBUG_LOG is defined then we should use the same size regardless of the compiler
333  #define TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE 3
334 #else
335  #ifdef __GNUC__
336  #define TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE 3
337  #else
338  #define TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE 5
339  #endif
340 #endif
341 
342 
350 #define TTMATH_GAMMA_BOUNDARY 2000
351 
352 
353 
354 
355 
356 namespace ttmath
357 {
358 
369  {
370  asm_vc_32 = 0,
371  asm_gcc_32,
372  asm_vc_64,
373  asm_gcc_64,
374  no_asm_32,
375  no_asm_64
376  };
377 
378 
383  {
384  err_ok = 0,
385  err_nothing_has_read,
386  err_unknown_character,
387  err_unexpected_final_bracket,
388  err_stack_not_clear,
389  err_unknown_variable,
390  err_division_by_zero,
391  err_interrupt,
392  err_overflow,
393  err_unknown_function,
394  err_unknown_operator,
395  err_unexpected_semicolon_operator,
396  err_improper_amount_of_arguments,
397  err_improper_argument,
398  err_unexpected_end,
399  err_internal_error,
400  err_incorrect_name,
401  err_incorrect_value,
402  err_variable_exists,
403  err_variable_loop,
404  err_functions_loop,
405  err_must_be_only_one_value,
406  err_object_exists,
407  err_unknown_object,
408  err_still_calculating,
409  err_in_short_form_used_function,
410  err_percent_from
411  };
412 
413 
418  struct Conv
419  {
425 
426 
432  bool scient;
433 
434 
442 
443 
458 
459 
472  sint round;
473 
474 
482 
483 
489 
490 
500 
501 
509 
510 
516 
517 
520  uint group_exp; // not implemented yet
521 
522 
523 
524 
525  Conv()
526  {
527  // default values
528  base = 10;
529  scient = false;
530  scient_from = 15;
531  base_round = true;
532  round = -1;
533  trim_zeroes = true;
534  comma = '.';
535  comma2 = ',';
536  group = 0;
537  group_digits = 3;
538  group_exp = 0;
539  }
540  };
541 
542 
543 
555  {
556  public:
557  virtual bool WasStopSignal() const volatile { return false; }
558  virtual ~StopCalculating(){}
559  };
560 
561 
569  {
570  const char * file;
571  int line;
572 
573  public:
574  ExceptionInfo() : file(0), line(0) {}
575  ExceptionInfo(const char * f, int l) : file(f), line(l) {}
576 
577  std::string Where() const
578  {
579  if( !file )
580  return "unknown";
581 
582  std::ostringstream result;
583  result << file << ":" << line;
584 
585  return result.str();
586  }
587  };
588 
589 
604  class ReferenceError : public std::logic_error, public ExceptionInfo
605  {
606  public:
607 
608  ReferenceError() : std::logic_error("reference error")
609  {
610  }
611 
612  ReferenceError(const char * f, int l) :
613  std::logic_error("reference error"), ExceptionInfo(f,l)
614  {
615  }
616 
617  std::string Where() const
618  {
619  return ExceptionInfo::Where();
620  }
621  };
622 
623 
636  class RuntimeError : public std::runtime_error, public ExceptionInfo
637  {
638  public:
639 
640  RuntimeError() : std::runtime_error("internal error")
641  {
642  }
643 
644  RuntimeError(const char * f, int l) :
645  std::runtime_error("internal error"), ExceptionInfo(f,l)
646  {
647  }
648 
649  std::string Where() const
650  {
651  return ExceptionInfo::Where();
652  }
653  };
654 
655 
656 
668  #if defined DEBUG || defined _DEBUG
669  #define TTMATH_DEBUG
670  #endif
671 
672 
673  #ifdef TTMATH_DEBUG
674 
675  #if defined(__FILE__) && defined(__LINE__)
676 
677  #define TTMATH_REFERENCE_ASSERT(expression) \
678  if( &(expression) == this ) throw ttmath::ReferenceError(__FILE__, __LINE__);
679 
680  #define TTMATH_ASSERT(expression) \
681  if( !(expression) ) throw ttmath::RuntimeError(__FILE__, __LINE__);
682 
683  #else
684 
685  #define TTMATH_REFERENCE_ASSERT(expression) \
686  if( &(expression) == this ) throw ReferenceError();
687 
688  #define TTMATH_ASSERT(expression) \
689  if( !(expression) ) throw RuntimeError();
690  #endif
691 
692  #else
693  #define TTMATH_REFERENCE_ASSERT(expression)
694  #define TTMATH_ASSERT(expression)
695  #endif
696 
697 
698 
699  #ifdef TTMATH_DEBUG_LOG
700  #define TTMATH_LOG(msg) PrintLog(msg, std::cout);
701  #define TTMATH_LOGC(msg, carry) PrintLog(msg, carry, std::cout);
702  #define TTMATH_VECTOR_LOG(msg, vector, len) PrintVectorLog(msg, std::cout, vector, len);
703  #define TTMATH_VECTOR_LOGC(msg, carry, vector, len) PrintVectorLog(msg, carry, std::cout, vector, len);
704  #else
705  #define TTMATH_LOG(msg)
706  #define TTMATH_LOGC(msg, carry)
707  #define TTMATH_VECTOR_LOG(msg, vector, len)
708  #define TTMATH_VECTOR_LOGC(msg, carry, vector, len)
709  #endif
710 
711 
712 
713 
714 } // namespace
715 
716 
717 #endif
718 
sint round
Definition: ttmathtypes.h:472
Definition: ttmathtypes.h:554
sint scient_from
Definition: ttmathtypes.h:441
Definition: ttmathtypes.h:568
bool base_round
Definition: ttmathtypes.h:457
Definition: ttmathtypes.h:636
ErrorCode
Definition: ttmathtypes.h:382
a namespace for the TTMath library
Definition: ttmath.h:62
LibTypeCode
Definition: ttmathtypes.h:368
uint comma
Definition: ttmathtypes.h:488
bool trim_zeroes
Definition: ttmathtypes.h:481
uint64_t ulint
Definition: ttmathtypes.h:200
uint comma2
Definition: ttmathtypes.h:499
Definition: ttmathtypes.h:604
bool scient
Definition: ttmathtypes.h:432
uint group
Definition: ttmathtypes.h:508
unsigned int uint
Definition: ttmathtypes.h:186
Definition: ttmathtypes.h:418
uint base
Definition: ttmathtypes.h:424
uint group_digits
Definition: ttmathtypes.h:515