Library for big numbers from http://www.ttmath.org/

Dependents:   PIDHeater82 Conceptcontroller_v_1_0 AlarmClockApp COG4050_adxl355_tilt ... more

TTMath is a small library which allows one to perform arithmetic operations with big unsigned integer, big signed integer and big floating point numbers. It provides standard mathematical operations like adding, subtracting, multiplying, dividing.

TTMath is BSD Licensed (new/modified BSD)

For more information about ttmath see http://www.ttmath.org/

Committer:
stevep
Date:
Tue Jul 30 18:43:48 2013 +0000
Revision:
0:04a9f72bbca7
v0.9.3 of ttmath

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stevep 0:04a9f72bbca7 1 /*
stevep 0:04a9f72bbca7 2 * This file is a part of TTMath Bignum Library
stevep 0:04a9f72bbca7 3 * and is distributed under the (new) BSD licence.
stevep 0:04a9f72bbca7 4 * Author: Tomasz Sowa <t.sowa@ttmath.org>
stevep 0:04a9f72bbca7 5 */
stevep 0:04a9f72bbca7 6
stevep 0:04a9f72bbca7 7 /*
stevep 0:04a9f72bbca7 8 * Copyright (c) 2006-2009, Tomasz Sowa
stevep 0:04a9f72bbca7 9 * All rights reserved.
stevep 0:04a9f72bbca7 10 *
stevep 0:04a9f72bbca7 11 * Redistribution and use in source and binary forms, with or without
stevep 0:04a9f72bbca7 12 * modification, are permitted provided that the following conditions are met:
stevep 0:04a9f72bbca7 13 *
stevep 0:04a9f72bbca7 14 * * Redistributions of source code must retain the above copyright notice,
stevep 0:04a9f72bbca7 15 * this list of conditions and the following disclaimer.
stevep 0:04a9f72bbca7 16 *
stevep 0:04a9f72bbca7 17 * * Redistributions in binary form must reproduce the above copyright
stevep 0:04a9f72bbca7 18 * notice, this list of conditions and the following disclaimer in the
stevep 0:04a9f72bbca7 19 * documentation and/or other materials provided with the distribution.
stevep 0:04a9f72bbca7 20 *
stevep 0:04a9f72bbca7 21 * * Neither the name Tomasz Sowa nor the names of contributors to this
stevep 0:04a9f72bbca7 22 * project may be used to endorse or promote products derived
stevep 0:04a9f72bbca7 23 * from this software without specific prior written permission.
stevep 0:04a9f72bbca7 24 *
stevep 0:04a9f72bbca7 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
stevep 0:04a9f72bbca7 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
stevep 0:04a9f72bbca7 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
stevep 0:04a9f72bbca7 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
stevep 0:04a9f72bbca7 29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
stevep 0:04a9f72bbca7 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
stevep 0:04a9f72bbca7 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
stevep 0:04a9f72bbca7 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
stevep 0:04a9f72bbca7 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
stevep 0:04a9f72bbca7 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
stevep 0:04a9f72bbca7 35 * THE POSSIBILITY OF SUCH DAMAGE.
stevep 0:04a9f72bbca7 36 */
stevep 0:04a9f72bbca7 37
stevep 0:04a9f72bbca7 38
stevep 0:04a9f72bbca7 39
stevep 0:04a9f72bbca7 40 #ifndef headerfilettmaththreads
stevep 0:04a9f72bbca7 41 #define headerfilettmaththreads
stevep 0:04a9f72bbca7 42
stevep 0:04a9f72bbca7 43 #include "ttmathtypes.h"
stevep 0:04a9f72bbca7 44
stevep 0:04a9f72bbca7 45 #ifdef TTMATH_WIN32_THREADS
stevep 0:04a9f72bbca7 46 #include <windows.h>
stevep 0:04a9f72bbca7 47 #include <cstdio>
stevep 0:04a9f72bbca7 48 #endif
stevep 0:04a9f72bbca7 49
stevep 0:04a9f72bbca7 50 #ifdef TTMATH_POSIX_THREADS
stevep 0:04a9f72bbca7 51 #include <pthread.h>
stevep 0:04a9f72bbca7 52 #endif
stevep 0:04a9f72bbca7 53
stevep 0:04a9f72bbca7 54
stevep 0:04a9f72bbca7 55
stevep 0:04a9f72bbca7 56 /*!
stevep 0:04a9f72bbca7 57 \file ttmaththreads.h
stevep 0:04a9f72bbca7 58 \brief Some objects used in multithreads environment
stevep 0:04a9f72bbca7 59 */
stevep 0:04a9f72bbca7 60
stevep 0:04a9f72bbca7 61
stevep 0:04a9f72bbca7 62 /*
stevep 0:04a9f72bbca7 63 this is a simple skeleton of a program in multithreads environment:
stevep 0:04a9f72bbca7 64
stevep 0:04a9f72bbca7 65 #define TTMATH_MULTITHREADS
stevep 0:04a9f72bbca7 66 #include<ttmath/ttmath.h>
stevep 0:04a9f72bbca7 67
stevep 0:04a9f72bbca7 68 TTMATH_MULTITHREADS_HELPER
stevep 0:04a9f72bbca7 69
stevep 0:04a9f72bbca7 70 int main()
stevep 0:04a9f72bbca7 71 {
stevep 0:04a9f72bbca7 72 [...]
stevep 0:04a9f72bbca7 73 }
stevep 0:04a9f72bbca7 74
stevep 0:04a9f72bbca7 75 make sure that macro TTMATH_MULTITHREADS is defined and (somewhere in *.cpp file)
stevep 0:04a9f72bbca7 76 use TTMATH_MULTITHREADS_HELPER macro (outside of any classes/functions/namespaces scope)
stevep 0:04a9f72bbca7 77 */
stevep 0:04a9f72bbca7 78
stevep 0:04a9f72bbca7 79
stevep 0:04a9f72bbca7 80 namespace ttmath
stevep 0:04a9f72bbca7 81 {
stevep 0:04a9f72bbca7 82
stevep 0:04a9f72bbca7 83
stevep 0:04a9f72bbca7 84 #ifdef TTMATH_WIN32_THREADS
stevep 0:04a9f72bbca7 85
stevep 0:04a9f72bbca7 86 /*
stevep 0:04a9f72bbca7 87 we use win32 threads
stevep 0:04a9f72bbca7 88 */
stevep 0:04a9f72bbca7 89
stevep 0:04a9f72bbca7 90
stevep 0:04a9f72bbca7 91 /*!
stevep 0:04a9f72bbca7 92 in multithreads environment you should use TTMATH_MULTITHREADS_HELPER macro
stevep 0:04a9f72bbca7 93 somewhere in *.cpp file
stevep 0:04a9f72bbca7 94
stevep 0:04a9f72bbca7 95 (at the moment in win32 this macro does nothing)
stevep 0:04a9f72bbca7 96 */
stevep 0:04a9f72bbca7 97 #define TTMATH_MULTITHREADS_HELPER
stevep 0:04a9f72bbca7 98
stevep 0:04a9f72bbca7 99
stevep 0:04a9f72bbca7 100 /*!
stevep 0:04a9f72bbca7 101 objects of this class are used to synchronize
stevep 0:04a9f72bbca7 102 */
stevep 0:04a9f72bbca7 103 class ThreadLock
stevep 0:04a9f72bbca7 104 {
stevep 0:04a9f72bbca7 105 HANDLE mutex_handle;
stevep 0:04a9f72bbca7 106
stevep 0:04a9f72bbca7 107
stevep 0:04a9f72bbca7 108 void CreateName(char * buffer) const
stevep 0:04a9f72bbca7 109 {
stevep 0:04a9f72bbca7 110 #ifdef _MSC_VER
stevep 0:04a9f72bbca7 111 #pragma warning (disable : 4996)
stevep 0:04a9f72bbca7 112 // warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead.
stevep 0:04a9f72bbca7 113 #endif
stevep 0:04a9f72bbca7 114
stevep 0:04a9f72bbca7 115 sprintf(buffer, "TTMATH_LOCK_%ul", (unsigned long)GetCurrentProcessId());
stevep 0:04a9f72bbca7 116
stevep 0:04a9f72bbca7 117 #ifdef _MSC_VER
stevep 0:04a9f72bbca7 118 #pragma warning (default : 4996)
stevep 0:04a9f72bbca7 119 #endif
stevep 0:04a9f72bbca7 120 }
stevep 0:04a9f72bbca7 121
stevep 0:04a9f72bbca7 122
stevep 0:04a9f72bbca7 123 public:
stevep 0:04a9f72bbca7 124
stevep 0:04a9f72bbca7 125 bool Lock()
stevep 0:04a9f72bbca7 126 {
stevep 0:04a9f72bbca7 127 char buffer[50];
stevep 0:04a9f72bbca7 128
stevep 0:04a9f72bbca7 129 CreateName(buffer);
stevep 0:04a9f72bbca7 130 mutex_handle = CreateMutexA(0, false, buffer);
stevep 0:04a9f72bbca7 131
stevep 0:04a9f72bbca7 132 if( mutex_handle == 0 )
stevep 0:04a9f72bbca7 133 return false;
stevep 0:04a9f72bbca7 134
stevep 0:04a9f72bbca7 135 WaitForSingleObject(mutex_handle, INFINITE);
stevep 0:04a9f72bbca7 136
stevep 0:04a9f72bbca7 137 return true;
stevep 0:04a9f72bbca7 138 }
stevep 0:04a9f72bbca7 139
stevep 0:04a9f72bbca7 140
stevep 0:04a9f72bbca7 141 ThreadLock()
stevep 0:04a9f72bbca7 142 {
stevep 0:04a9f72bbca7 143 mutex_handle = 0;
stevep 0:04a9f72bbca7 144 }
stevep 0:04a9f72bbca7 145
stevep 0:04a9f72bbca7 146
stevep 0:04a9f72bbca7 147 ~ThreadLock()
stevep 0:04a9f72bbca7 148 {
stevep 0:04a9f72bbca7 149 if( mutex_handle != 0 )
stevep 0:04a9f72bbca7 150 {
stevep 0:04a9f72bbca7 151 ReleaseMutex(mutex_handle);
stevep 0:04a9f72bbca7 152 CloseHandle(mutex_handle);
stevep 0:04a9f72bbca7 153 }
stevep 0:04a9f72bbca7 154 }
stevep 0:04a9f72bbca7 155 };
stevep 0:04a9f72bbca7 156
stevep 0:04a9f72bbca7 157 #endif // #ifdef TTMATH_WIN32_THREADS
stevep 0:04a9f72bbca7 158
stevep 0:04a9f72bbca7 159
stevep 0:04a9f72bbca7 160
stevep 0:04a9f72bbca7 161
stevep 0:04a9f72bbca7 162
stevep 0:04a9f72bbca7 163 #ifdef TTMATH_POSIX_THREADS
stevep 0:04a9f72bbca7 164
stevep 0:04a9f72bbca7 165 /*
stevep 0:04a9f72bbca7 166 we use posix threads
stevep 0:04a9f72bbca7 167 */
stevep 0:04a9f72bbca7 168
stevep 0:04a9f72bbca7 169
stevep 0:04a9f72bbca7 170 /*!
stevep 0:04a9f72bbca7 171 in multithreads environment you should use TTMATH_MULTITHREADS_HELPER macro
stevep 0:04a9f72bbca7 172 somewhere in *.cpp file
stevep 0:04a9f72bbca7 173 (this macro defines a pthread_mutex_t object used by TTMath library)
stevep 0:04a9f72bbca7 174 */
stevep 0:04a9f72bbca7 175 #define TTMATH_MULTITHREADS_HELPER \
stevep 0:04a9f72bbca7 176 namespace ttmath \
stevep 0:04a9f72bbca7 177 { \
stevep 0:04a9f72bbca7 178 pthread_mutex_t ttmath_mutex = PTHREAD_MUTEX_INITIALIZER; \
stevep 0:04a9f72bbca7 179 }
stevep 0:04a9f72bbca7 180
stevep 0:04a9f72bbca7 181
stevep 0:04a9f72bbca7 182 /*!
stevep 0:04a9f72bbca7 183 ttmath_mutex will be defined by TTMATH_MULTITHREADS_HELPER macro
stevep 0:04a9f72bbca7 184 */
stevep 0:04a9f72bbca7 185 extern pthread_mutex_t ttmath_mutex;
stevep 0:04a9f72bbca7 186
stevep 0:04a9f72bbca7 187
stevep 0:04a9f72bbca7 188 /*!
stevep 0:04a9f72bbca7 189 objects of this class are used to synchronize
stevep 0:04a9f72bbca7 190 */
stevep 0:04a9f72bbca7 191 class ThreadLock
stevep 0:04a9f72bbca7 192 {
stevep 0:04a9f72bbca7 193 public:
stevep 0:04a9f72bbca7 194
stevep 0:04a9f72bbca7 195 bool Lock()
stevep 0:04a9f72bbca7 196 {
stevep 0:04a9f72bbca7 197 if( pthread_mutex_lock(&ttmath_mutex) != 0 )
stevep 0:04a9f72bbca7 198 return false;
stevep 0:04a9f72bbca7 199
stevep 0:04a9f72bbca7 200 return true;
stevep 0:04a9f72bbca7 201 }
stevep 0:04a9f72bbca7 202
stevep 0:04a9f72bbca7 203
stevep 0:04a9f72bbca7 204 ~ThreadLock()
stevep 0:04a9f72bbca7 205 {
stevep 0:04a9f72bbca7 206 pthread_mutex_unlock(&ttmath_mutex);
stevep 0:04a9f72bbca7 207 }
stevep 0:04a9f72bbca7 208 };
stevep 0:04a9f72bbca7 209
stevep 0:04a9f72bbca7 210 #endif // #ifdef TTMATH_POSIX_THREADS
stevep 0:04a9f72bbca7 211
stevep 0:04a9f72bbca7 212
stevep 0:04a9f72bbca7 213
stevep 0:04a9f72bbca7 214
stevep 0:04a9f72bbca7 215 #if !defined(TTMATH_POSIX_THREADS) && !defined(TTMATH_WIN32_THREADS)
stevep 0:04a9f72bbca7 216
stevep 0:04a9f72bbca7 217 /*!
stevep 0:04a9f72bbca7 218 we don't use win32 and pthreads
stevep 0:04a9f72bbca7 219 */
stevep 0:04a9f72bbca7 220
stevep 0:04a9f72bbca7 221 /*!
stevep 0:04a9f72bbca7 222 */
stevep 0:04a9f72bbca7 223 #define TTMATH_MULTITHREADS_HELPER
stevep 0:04a9f72bbca7 224
stevep 0:04a9f72bbca7 225
stevep 0:04a9f72bbca7 226 /*!
stevep 0:04a9f72bbca7 227 objects of this class are used to synchronize
stevep 0:04a9f72bbca7 228 actually we don't synchronize, the method Lock() returns always 'false'
stevep 0:04a9f72bbca7 229 */
stevep 0:04a9f72bbca7 230 class ThreadLock
stevep 0:04a9f72bbca7 231 {
stevep 0:04a9f72bbca7 232 public:
stevep 0:04a9f72bbca7 233
stevep 0:04a9f72bbca7 234 bool Lock()
stevep 0:04a9f72bbca7 235 {
stevep 0:04a9f72bbca7 236 return false;
stevep 0:04a9f72bbca7 237 }
stevep 0:04a9f72bbca7 238 };
stevep 0:04a9f72bbca7 239
stevep 0:04a9f72bbca7 240
stevep 0:04a9f72bbca7 241 #endif // #if !defined(TTMATH_POSIX_THREADS) && !defined(TTMATH_WIN32_THREADS)
stevep 0:04a9f72bbca7 242
stevep 0:04a9f72bbca7 243
stevep 0:04a9f72bbca7 244
stevep 0:04a9f72bbca7 245
stevep 0:04a9f72bbca7 246
stevep 0:04a9f72bbca7 247 } // namespace
stevep 0:04a9f72bbca7 248
stevep 0:04a9f72bbca7 249 #endif
stevep 0:04a9f72bbca7 250
stevep 0:04a9f72bbca7 251