#ifndef BIGINTEGER_H #define BIGINTEGER_H #include "BigUnsigned.hh" /* A BigInteger object represents a signed integer of size limited only by * available memory. BigUnsigneds support most mathematical operators and can * be converted to and from most primitive integer types. * * A BigInteger is just an aggregate of a BigUnsigned and a sign. (It is no * longer derived from BigUnsigned because that led to harmful implicit * conversions.) */ class BigInteger { public: typedef BigUnsigned::Blk Blk; typedef BigUnsigned::Index Index; typedef BigUnsigned::CmpRes CmpRes; static const CmpRes less = BigUnsigned::less , equal = BigUnsigned::equal , greater = BigUnsigned::greater; // Enumeration for the sign of a BigInteger. enum Sign { negative = -1, zero = 0, positive = 1 }; protected: Sign sign; BigUnsigned mag; public: // Constructs zero. BigInteger() : sign(zero), mag() {} // Copy constructor BigInteger(const BigInteger &x) : sign(x.sign), mag(x.mag) {}; // Assignment operator void operator=(const BigInteger &x); // Constructor that copies from a given array of blocks with a sign. BigInteger(const Blk *b, Index blen, Sign s); // Nonnegative constructor that copies from a given array of blocks. BigInteger(const Blk *b, Index blen) : mag(b, blen) { sign = mag.isZero() ? zero : positive; } // Constructor from a BigUnsigned and a sign BigInteger(const BigUnsigned &x, Sign s); // Nonnegative constructor from a BigUnsigned BigInteger(const BigUnsigned &x) : mag(x) { sign = mag.isZero() ? zero : positive; } // Constructors from primitive integer types BigInteger(unsigned long x); BigInteger( long x); BigInteger(unsigned int x); BigInteger( int x); BigInteger(unsigned short x); BigInteger( short x); /* Converters to primitive integer types * The implicit conversion operators caused trouble, so these are now * named. */ unsigned long toUnsignedLong () const; long toLong () const; unsigned int toUnsignedInt () const; int toInt () const; unsigned short toUnsignedShort() const; short toShort () const; protected: // Helper template X convertToUnsignedPrimitive() const; template X convertToSignedPrimitive() const; public: // ACCESSORS Sign getSign() const { return sign; } /* The client can't do any harm by holding a read-only reference to the * magnitude. */ const BigUnsigned &getMagnitude() const { return mag; } // Some accessors that go through to the magnitude Index getLength() const { return mag.getLength(); } Index getCapacity() const { return mag.getCapacity(); } Blk getBlock(Index i) const { return mag.getBlock(i); } bool isZero() const { return sign == zero; } // A bit special // COMPARISONS // Compares this to x like Perl's <=> CmpRes compareTo(const BigInteger &x) const; // Ordinary comparison operators bool operator ==(const BigInteger &x) const { return sign == x.sign && mag == x.mag; } bool operator !=(const BigInteger &x) const { return !operator ==(x); }; bool operator < (const BigInteger &x) const { return compareTo(x) == less ; } bool operator <=(const BigInteger &x) const { return compareTo(x) != greater; } bool operator >=(const BigInteger &x) const { return compareTo(x) != less ; } bool operator > (const BigInteger &x) const { return compareTo(x) == greater; } // OPERATORS -- See the discussion in BigUnsigned.hh. void add (const BigInteger &a, const BigInteger &b); void subtract(const BigInteger &a, const BigInteger &b); void multiply(const BigInteger &a, const BigInteger &b); /* See the comment on BigUnsigned::divideWithRemainder. Semantics * differ from those of primitive integers when negatives and/or zeros * are involved. */ void divideWithRemainder(const BigInteger &b, BigInteger &q); void negate(const BigInteger &a); /* Bitwise operators are not provided for BigIntegers. Use * getMagnitude to get the magnitude and operate on that instead. */ BigInteger operator +(const BigInteger &x) const; BigInteger operator -(const BigInteger &x) const; BigInteger operator *(const BigInteger &x) const; BigInteger operator /(const BigInteger &x) const; BigInteger operator %(const BigInteger &x) const; BigInteger operator -() const; void operator +=(const BigInteger &x); void operator -=(const BigInteger &x); void operator *=(const BigInteger &x); void operator /=(const BigInteger &x); void operator %=(const BigInteger &x); void flipSign(); // INCREMENT/DECREMENT OPERATORS void operator ++( ); void operator ++(int); void operator --( ); void operator --(int); }; // NORMAL OPERATORS /* These create an object to hold the result and invoke * the appropriate put-here operation on it, passing * this and x. The new object is then returned. */ inline BigInteger BigInteger::operator +(const BigInteger &x) const { BigInteger ans; ans.add(*this, x); return ans; } inline BigInteger BigInteger::operator -(const BigInteger &x) const { BigInteger ans; ans.subtract(*this, x); return ans; } inline BigInteger BigInteger::operator *(const BigInteger &x) const { BigInteger ans; ans.multiply(*this, x); return ans; } inline BigInteger BigInteger::operator /(const BigInteger &x) const { if (x.isZero()) throw "BigInteger::operator /: division by zero"; BigInteger q, r; r = *this; r.divideWithRemainder(x, q); return q; } inline BigInteger BigInteger::operator %(const BigInteger &x) const { if (x.isZero()) throw "BigInteger::operator %: division by zero"; BigInteger q, r; r = *this; r.divideWithRemainder(x, q); return r; } inline BigInteger BigInteger::operator -() const { BigInteger ans; ans.negate(*this); return ans; } /* * ASSIGNMENT OPERATORS * * Now the responsibility for making a temporary copy if necessary * belongs to the put-here operations. See Assignment Operators in * BigUnsigned.hh. */ inline void BigInteger::operator +=(const BigInteger &x) { add(*this, x); } inline void BigInteger::operator -=(const BigInteger &x) { subtract(*this, x); } inline void BigInteger::operator *=(const BigInteger &x) { multiply(*this, x); } inline void BigInteger::operator /=(const BigInteger &x) { if (x.isZero()) throw "BigInteger::operator /=: division by zero"; /* The following technique is slightly faster than copying *this first * when x is large. */ BigInteger q; divideWithRemainder(x, q); // *this contains the remainder, but we overwrite it with the quotient. *this = q; } inline void BigInteger::operator %=(const BigInteger &x) { if (x.isZero()) throw "BigInteger::operator %=: division by zero"; BigInteger q; // Mods *this by x. Don't care about quotient left in q. divideWithRemainder(x, q); } // This one is trivial inline void BigInteger::flipSign() { sign = Sign(-sign); } #endif 09 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
/*
    ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/**
 * @file    templates/halconf.h
 * @brief   HAL configuration header.
 * @details HAL configuration file, this file allows to enable or disable the
 *          various device drivers from your application. You may also use
 *          this file in order to override the device drivers default settings.
 *
 * @addtogroup HAL_CONF
 * @{
 */

#ifndef _HALCONF_H_
#define _HALCONF_H_

#include "mcuconf.h"

/**
 * @brief   Enables the PAL subsystem.
 */
#if !defined(HAL_USE_PAL) || defined(__DOXYGEN__)
#define HAL_USE_PAL                 TRUE
#endif

/**
 * @brief   Enables the ADC subsystem.
 */
#if !defined(HAL_USE_ADC) || defined(__DOXYGEN__)
#define HAL_USE_ADC                 FALSE
#endif

/**
 * @brief   Enables the CAN subsystem.
 */
#if !defined(HAL_USE_CAN) || defined(__DOXYGEN__)
#define HAL_USE_CAN                 FALSE
#endif

/**
 * @brief   Enables the DAC subsystem.
 */
#if !defined(HAL_USE_DAC) || defined(__DOXYGEN__)
#define HAL_USE_DAC                 FALSE
#endif

/**
 * @brief   Enables the EXT subsystem.
 */
#if !defined(HAL_USE_EXT) || defined(__DOXYGEN__)
#define HAL_USE_EXT                 FALSE
#endif

/**
 * @brief   Enables the GPT subsystem.
 */
#if !defined(HAL_USE_GPT) || defined(__DOXYGEN__)
#define HAL_USE_GPT                 FALSE
#endif

/**
 * @brief   Enables the I2C subsystem.
 */
#if !defined(HAL_USE_I2C) || defined(__DOXYGEN__)
#define HAL_USE_I2C                 FALSE
#endif

/**
 * @brief   Enables the I2S subsystem.
 */
#if !defined(HAL_USE_I2S) || defined(__DOXYGEN__)
#define HAL_USE_I2S                 FALSE
#endif

/**
 * @brief   Enables the ICU subsystem.
 */
#if !defined(HAL_USE_ICU) || defined(__DOXYGEN__)
#define HAL_USE_ICU                 TRUE
#endif

/**
 * @brief   Enables the MAC subsystem.
 */
#if !defined(HAL_USE_MAC) || defined(__DOXYGEN__)
#define HAL_USE_MAC                 FALSE
#endif

/**
 * @brief   Enables the MMC_SPI subsystem.
 */
#if !defined(HAL_USE_MMC_SPI) || defined(__DOXYGEN__)
#define HAL_USE_MMC_SPI             FALSE
#endif

/**
 * @brief   Enables the PWM subsystem.
 */
#if !defined(HAL_USE_PWM) || defined(__DOXYGEN__)
#define HAL_USE_PWM                 TRUE
#endif

/**
 * @brief   Enables the RTC subsystem.
 */
#if !defined(HAL_USE_RTC) || defined(__DOXYGEN__)
#define HAL_USE_RTC                 FALSE
#endif

/**
 * @brief   Enables the SDC subsystem.
 */
#if !defined(HAL_USE_SDC) || defined(__DOXYGEN__)
#define HAL_USE_SDC                 FALSE
#endif

/**
 * @brief   Enables the SERIAL subsystem.
 */
#if !defined(HAL_USE_SERIAL) || defined(__DOXYGEN__)
#define HAL_USE_SERIAL              FALSE
#endif

/**
 * @brief   Enables the SERIAL over USB subsystem.
 */
#if !defined(HAL_USE_SERIAL_USB) || defined(__DOXYGEN__)
#define HAL_USE_SERIAL_USB          FALSE
#endif

/**
 * @brief   Enables the SPI subsystem.
 */
#if !defined(HAL_USE_SPI) || defined(__DOXYGEN__)
#define HAL_USE_SPI                 FALSE
#endif

/**
 * @brief   Enables the UART subsystem.
 */
#if !defined(HAL_USE_UART) || defined(__DOXYGEN__)
#define HAL_USE_UART                FALSE
#endif

/**
 * @brief   Enables the USB subsystem.
 */
#if !defined(HAL_USE_USB) || defined(__DOXYGEN__)
#define HAL_USE_USB                 FALSE
#endif

/*===========================================================================*/
/* ADC driver related settings.                                              */
/*===========================================================================*/

/**
 * @brief   Enables synchronous APIs.
 * @note    Disabling this option saves both code and data space.
 */
#if !defined(ADC_USE_WAIT) || defined(__DOXYGEN__)
#define ADC_USE_WAIT                TRUE
#endif

/**
 * @brief   Enables the @p adcAcquireBus() and @p adcReleaseBus() APIs.
 * @note    Disabling this option saves both code and data space.
 */
#if !defined(ADC_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__)
#define ADC_USE_MUTUAL_EXCLUSION    TRUE
#endif

/*===========================================================================*/
/* CAN driver related settings.                                              */
/*===========================================================================*/

/**
 * @brief   Sleep mode related APIs inclusion switch.
 */
#if !defined(CAN_USE_SLEEP_MODE) || defined(__DOXYGEN__)
#define CAN_USE_SLEEP_MODE          TRUE
#endif

/*===========================================================================*/
/* I2C driver related settings.                                              */
/*===========================================================================*/

/**
 * @brief   Enables the mutual exclusion APIs on the I2C bus.
 */
#if !defined(I2C_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__)
#define I2C_USE_MUTUAL_EXCLUSION    TRUE
#endif

/*===========================================================================*/
/* MAC driver related settings.                                              */
/*===========================================================================*/

/**
 * @brief   Enables an event sources for incoming packets.
 */
#if !defined(MAC_USE_ZERO_COPY) || defined(__DOXYGEN__)
#define MAC_USE_ZERO_COPY           FALSE
#endif

/**
 * @brief   Enables an event sources for incoming packets.
 */
#if !defined(MAC_USE_EVENTS) || defined(__DOXYGEN__)
#define MAC_USE_EVENTS              TRUE
#endif

/*===========================================================================*/
/* MMC_SPI driver related settings.                                          */
/*===========================================================================*/

/**
 * @brief   Delays insertions.
 * @details If enabled this options inserts delays into the MMC waiting
 *          routines releasing some extra CPU time for the threads with
 *          lower priority, this may slow down the driver a bit however.
 *          This option is recommended also if the SPI driver does not
 *          use a DMA channel and heavily loads the CPU.
 */
#if !defined(MMC_NICE_WAITING) || defined(__DOXYGEN__)
#define MMC_NICE_WAITING            TRUE
#endif

/*===========================================================================*/
/* SDC driver related settings.                                              */
/*===========================================================================*/

/**
 * @brief   Number of initialization attempts before rejecting the card.
 * @note    Attempts are performed at 10mS intervals.
 */
#if !defined(SDC_INIT_RETRY) || defined(__DOXYGEN__)
#define SDC_INIT_RETRY              100
#endif

/**
 * @brief   Include support for MMC cards.
 * @note    MMC support is not yet implemented so this option must be kept
 *          at @p FALSE.
 */
#if !defined(SDC_MMC_SUPPORT) || defined(__DOXYGEN__)
#define SDC_MMC_SUPPORT             FALSE
#endif

/**
 * @brief   Delays insertions.
 * @details If enabled this options inserts delays into the MMC waiting
 *          routines releasing some extra CPU time for the threads with
 *          lower priority, this may slow down the driver a bit however.
 */
#if !defined(SDC_NICE_WAITING) || defined(__DOXYGEN__)
#define SDC_NICE_WAITING            TRUE
#endif

/*===========================================================================*/
/* SERIAL driver related settings.                                           */
/*===========================================================================*/

/**
 * @brief   Default bit rate.
 * @details Configuration parameter, this is the baud rate selected for the
 *          default configuration.
 */
#if !defined(SERIAL_DEFAULT_BITRATE) || defined(__DOXYGEN__)
#define SERIAL_DEFAULT_BITRATE      38400
#endif

/**
 * @brief   Serial buffers size.
 * @details Configuration parameter, you can change the depth of the queue
 *          buffers depending on the requirements of your application.
 * @note    The default is 64 bytes for both the transmission and receive
 *          buffers.
 */
#if !defined(SERIAL_BUFFERS_SIZE) || defined(__DOXYGEN__)
#define SERIAL_BUFFERS_SIZE         16
#endif

/*===========================================================================*/
/* SERIAL_USB driver related setting.                                        */
/*===========================================================================*/

/**
 * @brief   Serial over USB buffers size.
 * @details Configuration parameter, the buffer size must be a multiple of
 *          the USB data endpoint maximum packet size.
 * @note    The default is 64 bytes for both the transmission and receive
 *          buffers.
 */
#if !defined(SERIAL_USB_BUFFERS_SIZE) || defined(__DOXYGEN__)
#define SERIAL_USB_BUFFERS_SIZE     256
#endif

/*===========================================================================*/
/* SPI driver related settings.                                              */
/*===========================================================================*/

/**
 * @brief   Enables synchronous APIs.
 * @note    Disabling this option saves both code and data space.
 */
#if !defined(SPI_USE_WAIT) || defined(__DOXYGEN__)
#define SPI_USE_WAIT                TRUE
#endif

/**
 * @brief   Enables the @p spiAcquireBus() and @p spiReleaseBus() APIs.
 * @note    Disabling this option saves both code and data space.
 */
#if !defined(SPI_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__)
#define SPI_USE_MUTUAL_EXCLUSION    TRUE
#endif

#endif /* _HALCONF_H_ */

/** @} */