Main Page | Modules | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

mp::mpi Class Reference
[Signed integers]

User class that provides an interface to use signed integers with the MP++ library. More...

#include <mpi.h>

Inheritance diagram for mp::mpi:

mp::mpi_ mp::mpn_ List of all members.

Pseudo-random number generator

void prng (size_type n)
 Generate a pseudo-random number.
static void prng_seed (limb_long_type seed)
 Set the PRNG seed.
static void prng_seed (const mpn_ &seed)
 Set the PRNG seed.

Public Types

typedef mp::int_type int_type
 Type for integer conversion.
typedef mp::size_type size_type
 Type of index registers.
typedef mp::limb_type limb_type
 Type of normal limbs.
typedef mp::limb_long_type limb_long_type
 Type of double-long limbs.
typedef mp::radix_type radix_type
 Type of radix identifier.

Public Member Functions

 mpi (const mpi_ &n)
 mpi ()
 mpi (int_type n, size_type size=0)
 mpi (const mpi &n, size_type size=0)
 mpi (const std::string &s, radix_type b=0)
void prime (size_type n)
 Set to a random prime number.
bool isPrime (int n=_MP_RM_TESTS)
 Primality testing.
int_type toInt () const
 Conversion to standard signed integer type.
int compare (const mpi_ &n) const
 Compare two MPIs.
void checkNeg ()
 Makes sure no -0 condition can exist.
void setUse ()
 Sets the local use_ variable after an operation.
Conversion operators
 operator int_type () const
 operator std::string () const
Assignment overloads
const mpioperator= (int_type n)
const mpioperator= (const mpi &n)
const mpioperator= (const std::string &s)
Bit shifting assignment overloads
const mpioperator<<= (size_type y)
const mpioperator>>= (size_type y)
Bitwise assignment overloads
const mpioperator &= (const mpi &n)
const mpioperator|= (const mpi &n)
const mpioperator^= (const mpi &n)
mpi operator~ ()
Increment & decrement overloads
mpioperator++ ()
const mpi operator++ (int)
mpioperator-- ()
const mpi operator-- (int)
Arithmetic assignment overloads
const mpioperator+= (const mpi &n)
const mpioperator-= (const mpi &n)
const mpioperator *= (const mpi &n)
const mpioperator/= (const mpi &n)
const mpioperator%= (const mpi &n)
Initialization and basic alteration
void init (int_type n, size_type size=0)
 Initialization to signed integer.
void init (const mpi_ &n, size_type size=0)
 Initialization to an MPI.
void clear ()
 Resets the number.
void resize (size_type size)
 Resizes to size bits.
void swap (mpi_ &n)
 Swaps this with n.
Initialization and basic alteration
void init (limb_type n, size_type size=0)
 Initialization to single limb.
void init (const mpn_ &n, size_type size=0)
 Initialization to MPN.
void swap (mpn_ &n)
 Swaps this with n.
Information retreval
bool isNeg () const
 Is the number negative?
size_type size () const
 Find the number of bits in use.
String conversion
void setString (const std::string &s, radix_type b=0)
 String input.
std::string getString (radix_type b=string_base) const
 String output.
Bit shifting
void shift_l (size_type n)
 Left shift.
void shift_r (size_type n)
 Right shift.
Debugging output
void print () const
 Binary dump to stdout.
Algebraic functions
void add (const mpi_ &n)
 Addition of an MPI.
void sub (const mpi_ &n)
 Subtraction of an MPI.
void mul (const mpi_ &n)
 Multiplication by an MPI.
void div (const mpi_ &n)
 Division by an MPI.
void mod (const mpi_ &n)
 Modulus by an MPI.
Algebraic functions
void add (const mpn_ &n)
 Addition of an MPN.
void sub (const mpn_ &n)
 Subtraction of an MPN.
void mul (const mpn_ &n)
 Multiplication by an MPN.
void div_mod (mpn_ &n)
 Division by an MPN.
Additional arithmetic operations
void negate ()
 Flips the sign register, AKA negates.
void abs ()
 Clears the sign register, AKA takes the absolute value.
void sqr ()
 Squares the number efficiantly.
mpi_ pow (int_type e) const
 Exponentation.
mpi_ pow_mod (const mpi_ &e, const mpi_ &m) const
 Performs modular exponentiation.
Comparison
int compare (const mpn_ &n) const
 Compare two MPNs.
Information retreval
bool isZero () const
 Is the number zero?
size_type low_zeros () const
 Returns number of consecutive zero limbs, starting at the lowest limb.
Raw accessors
Data format is

raw_type getRaw () const
 Raw data output.
void setRaw (const raw_type &data)
 Raw data input.
Limb retreval overloads
Subscript operator is overloaded to allow users to interact with the individual limbs of a number in a safe way.

const limb_typeoperator[] (size_type i) const
 Const limb retreval.
limb_typeoperator[] (size_type i)
 Limb retreval and writing.
Bitwise functions
size_type popcount () const
 Population count.
void bitwise (const mpn_ &y, int type)
 Binary bitwise operations.
void bitwise_not ()
 Bitwise not operation.

Static Public Member Functions

static const byte capabilities ()
 A bitmask of this class' numerical and functional capabilities.
static void setDefaultBase (radix_type b)
 Sets the default base for string conversion.
static radix_type getDefaultBase ()
 Gets the default base for string conversion.
static void interrupt ()
 Interrupt the next main (math, string, PRNG) operation that runs.

Static Public Attributes

static const size_type limb_size_bits = _MP_SIZE / 2
 Size of a single limb (in bits).
static const limb_type limb_full = (1 << limb_size_bits) - 1
 A limb with all bits set.
static const size_type size_max = INT_MAX
 The largest positive size_type value.
static const std::string string_digits = _MP_STR_DIGIT
 The digits used in string conversion.

Protected Member Functions

void onInterrupt () const
 Activated when interrupt register is set.
Algebraic functions with single limbs
These are much faster than the normal algebraic functions, and are called anyway if the argument fits within a single limb.

void add_l (limb_type n)
 Addition of a single limb.
void sub_l (limb_type n)
 Subtraction of a single limb.
void mul_l (limb_type n)
 Multiplacation by a single limb.
void div_mod_l (limb_type &n)
 Division by a single limb.

Protected Attributes

bool neg_
 negativity register (true if negative).
size_type alloc_
 Number of limbs allocated, pointed to by data.
size_type use_
 Number of limbs used (always <= alloc).
limb_typedata_
 Pointer to the data.

Static Protected Attributes

static radix_type string_base = _MP_STR_BASE
 Default base for string conversion.
static bool interrupted
 Interrupt register.

Detailed Description

User class that provides an interface to use signed integers with the MP++ library.

All standard arithmetic, logical, conversion, and comparison operators are overloaded, so no knowledge of the underlying functions is required.

Most overloads function just as if operating on standard signed integer types, except that there is no chance (within the available heap) of overflow.


Member Typedef Documentation

typedef mp::int_type mp::mpi_::int_type [inherited]
 

Type for integer conversion.

typedef mp::limb_long_type mp::mpn_::limb_long_type [inherited]
 

Type of double-long limbs.

typedef mp::limb_type mp::mpn_::limb_type [inherited]
 

Type of normal limbs.

typedef mp::radix_type mp::mpn_::radix_type [inherited]
 

Type of radix identifier.

typedef mp::size_type mp::mpn_::size_type [inherited]
 

Type of index registers.


Constructor & Destructor Documentation

mp::mpi::mpi const mpi_ n  )  [inline]
 

mp::mpi::mpi  )  [inline]
 

mp::mpi::mpi int_type  n,
size_type  size = 0
[inline]
 

mp::mpi::mpi const mpi n,
size_type  size = 0
[inline]
 

mp::mpi::mpi const std::string &  s,
radix_type  b = 0
[inline]
 


Member Function Documentation

void mp::mpi_::abs  )  [inherited]
 

Clears the sign register, AKA takes the absolute value.

void mp::mpn_::add const mpn_ n  )  [inherited]
 

Addition of an MPN.

Parameters:
n Number to add

void mp::mpi_::add const mpi_ n  )  [inherited]
 

Addition of an MPI.

Parameters:
n Number to add

void mp::mpn_::add_l limb_type  n  )  [protected, inherited]
 

Addition of a single limb.

Parameters:
n Limb to add

void mp::mpn_::bitwise const mpn_ y,
int  type
[inherited]
 

Binary bitwise operations.

Parameters:
y Second parameter of the operation
type Type of bitwise operation to perform
  • 1: AND
  • 2: OR
  • 3: XOR

void mp::mpn_::bitwise_not  )  [inherited]
 

Bitwise not operation.

Operates on chunks of bytes, so that a not of a not is equal to the origional.

const byte mp::mpi_::capabilities  )  [static, inherited]
 

A bitmask of this class' numerical and functional capabilities.

Reimplemented from mp::mpn_.

Reimplemented in mp::mpf_.

void mp::mpi_::checkNeg  )  [inherited]
 

Makes sure no -0 condition can exist.

void mp::mpi_::clear  )  [inherited]
 

Resets the number.

Reimplemented from mp::mpn_.

Reimplemented in mp::mpf_.

int mp::mpn_::compare const mpn_ n  )  const [inherited]
 

Compare two MPNs.

Parameters:
n Number to compare with
Returns:
sign(this - n)

int mp::mpi_::compare const mpi_ n  )  const [inherited]
 

Compare two MPIs.

Parameters:
n Number to compare with
Returns:
sign(this - n)

void mp::mpi_::div const mpi_ n  )  [inherited]
 

Division by an MPI.

Parameters:
n Number to divide by. If 0, a range_error exception is thrown.

void mp::mpn_::div_mod mpn_ n  )  [inherited]
 

Division by an MPN.

Parameters:
n Divisor and remainder storage. If less than 0, a range_error is thrown.

void mp::mpn_::div_mod_l limb_type n  )  [protected, inherited]
 

Division by a single limb.

Parameters:
n Divisor and remainder storage. If less than 0, a range_error is thrown.

radix_type mp::mpi_::getDefaultBase  )  [static, inherited]
 

Gets the default base for string conversion.

Returns:
Current default string base

raw_type mp::mpn_::getRaw  )  const [inherited]
 

Raw data output.

string mp::mpi_::getString radix_type  b = string_base  )  const [inherited]
 

String output.

Converts number to a string in base b.

Parameters:
b Base of the output string. If less than 2 or greater than the number of available conversion digits, an out_of_range exception is thrown.
Returns:
Output string

Reimplemented from mp::mpn_.

Reimplemented in mp::mpf_.

void mp::mpn_::init const mpn_ n,
size_type  size = 0
[inherited]
 

Initialization to MPN.

MPN is truncated or padded with zeros in the to fit the size.

Parameters:
n Number to which this is initialized
size Size of the result, in limbs. If 0, size is set to 1. If less than 0, an out_of_range exception is thrown.

void mp::mpn_::init limb_type  n,
size_type  size = 0
[inherited]
 

Initialization to single limb.

MPN is padded with zeros to fit the size.

Parameters:
n Number to which this is initialized
size Size of the result, in limbs. If 0, size is set to 1. If less than 0, an out_of_range exception is thrown.

void mp::mpi_::init const mpi_ n,
size_type  size = 0
[inherited]
 

Initialization to an MPI.

MPI is truncated or padded with zeros to fit the size.

Parameters:
n Number to which this is initialized. If self is argument, a runtime_error is thrown.
size Minimum size of the result, in bits. If 0, size is set to the input's size. If less than 0, an out_of_range exception is thrown.

Reimplemented in mp::mpf_.

void mp::mpi_::init int_type  n,
size_type  size = 0
[inherited]
 

Initialization to signed integer.

MPI is padded with zeros to fit the size.

Parameters:
n Number to which this is initialized
size Minimum size of the result, in bits. If 0, size is set to mpi_::limb_size. If less than 0, an out_of_range exception is thrown.

Reimplemented in mp::mpf_.

void mp::mpn_::interrupt  )  [static, inherited]
 

Interrupt the next main (math, string, PRNG) operation that runs.

bool mp::mpi_::isNeg  )  const [inherited]
 

Is the number negative?

Returns:
True if the number is negative

bool mp::mpi::isPrime int  n = _MP_RM_TESTS  ) 
 

Primality testing.

Uses the Rabin-Miller probabilistic test to determine if a number is likely prime or not.

Parameters:
n Number of iterations of the probabilistic test. If less than 1, an out_of_range exception is thrown.
Returns:
True if the number is likely prime

bool mp::mpn_::isZero  )  const [inherited]
 

Is the number zero?

Returns:
True if the number is zero

mpn_::size_type mp::mpn_::low_zeros  )  const [inherited]
 

Returns number of consecutive zero limbs, starting at the lowest limb.

void mp::mpi_::mod const mpi_ n  )  [inherited]
 

Modulus by an MPI.

Parameters:
n Number to divide by. If 0, a range_error exception is thrown.

void mp::mpn_::mul const mpn_ n  )  [inherited]
 

Multiplication by an MPN.

Parameters:
n Number to multiply by

void mp::mpi_::mul const mpi_ n  )  [inherited]
 

Multiplication by an MPI.

Parameters:
n Number to multiply by

void mp::mpn_::mul_l limb_type  n  )  [protected, inherited]
 

Multiplacation by a single limb.

Parameters:
n Limb to multiply by

void mp::mpi_::negate  )  [inherited]
 

Flips the sign register, AKA negates.

void mp::mpn_::onInterrupt  )  const [protected, inherited]
 

Activated when interrupt register is set.

Throws a runtime_error to break the execution of a main (math, string handling, PRNG) function.

const mpi & mp::mpi::operator &= const mpi n  ) 
 

const mpi & mp::mpi::operator *= const mpi n  ) 
 

mp::mpi::operator int_type  )  const [inline]
 

mp::mpi::operator std::string  )  const [inline]
 

const mpi & mp::mpi::operator%= const mpi n  ) 
 

const mpi mp::mpi::operator++ int   ) 
 

mpi & mp::mpi::operator++  ) 
 

const mpi & mp::mpi::operator+= const mpi n  ) 
 

const mpi mp::mpi::operator-- int   ) 
 

mpi & mp::mpi::operator--  ) 
 

const mpi & mp::mpi::operator-= const mpi n  ) 
 

const mpi & mp::mpi::operator/= const mpi n  ) 
 

const mpi & mp::mpi::operator<<= size_type  y  ) 
 

const mpi & mp::mpi::operator= const std::string &  s  ) 
 

const mpi & mp::mpi::operator= const mpi n  ) 
 

const mpi & mp::mpi::operator= int_type  n  ) 
 

const mpi & mp::mpi::operator>>= size_type  y  ) 
 

mpn_::limb_type & mp::mpn_::operator[] size_type  i  )  [inherited]
 

Limb retreval and writing.

Parameters:
i Index of limb. If less than 0, an out_of_range exception is thrown.

const mpn_::limb_type & mp::mpn_::operator[] size_type  i  )  const [inherited]
 

Const limb retreval.

Parameters:
i Index of limb. If less than 0, an out_of_range exception is thrown.

const mpi & mp::mpi::operator^= const mpi n  ) 
 

const mpi & mp::mpi::operator|= const mpi n  ) 
 

mpi mp::mpi::operator~  ) 
 

mpn_::size_type mp::mpn_::popcount  )  const [inherited]
 

Population count.

Returns:
Number of set bits

mpi_ mp::mpi_::pow int_type  e  )  const [inherited]
 

Exponentation.

Takes the number to a basic signed integer power. The exponant is limited because the number would overflow if allowed to be any larger.

Parameters:
e Exponant in powering operation

mpi_ mp::mpi_::pow_mod const mpi_ e,
const mpi_ m
const [inherited]
 

Performs modular exponentiation.

The result is equivalent to the expression: (this ^ p) m

Parameters:
e Exponant in powering operation
m Divisor in modulus operation

void mp::mpi::prime size_type  n  ) 
 

Set to a random prime number.

Parameters:
n Minimum size, in bits, of result

void mp::mpi_::print  )  const [inherited]
 

Binary dump to stdout.

Includes sign and size information.

Reimplemented from mp::mpn_.

Reimplemented in mp::mpf_.

void mp::mpn_::prng size_type  n  )  [inherited]
 

Generate a pseudo-random number.

Parameters:
n Number of random bits to generate

void mp::mpn_::prng_seed const mpn_ seed  )  [static, inherited]
 

Set the PRNG seed.

Parameters:
seed Seed for the PRNG

void mp::mpn_::prng_seed limb_long_type  seed  )  [static, inherited]
 

Set the PRNG seed.

Parameters:
seed Seed for the PRNG

void mp::mpi_::resize size_type  size  )  [inherited]
 

Resizes to size bits.

Adds upper zeros or destroys upper bits as necessary.

Parameters:
size Size of the result, in bits

Reimplemented from mp::mpn_.

void mp::mpi_::setDefaultBase radix_type  b  )  [static, inherited]
 

Sets the default base for string conversion.

Parameters:
b Base to set as default. If less than 2 or greater than the number of available conversion digits, an out_of_range exception is thrown.

void mp::mpn_::setRaw const raw_type data  )  [inherited]
 

Raw data input.

void mp::mpi_::setString const std::string &  s,
radix_type  b = 0
[inherited]
 

String input.

Converts the input string in base b to an MPI.

Parameters:
s Input string
b Base of the input string. If zero, base is guessed using the first few digits of the input string. If less than 2 or greater than the number of available conversion digits, an out_of_range exception is thrown.

Reimplemented from mp::mpn_.

Reimplemented in mp::mpf_.

void mp::mpn_::setUse  )  [inherited]
 

Sets the local use_ variable after an operation.

Not necessary for users unless twiddling with individual limbs.

void mp::mpi_::shift_l size_type  n  )  [inherited]
 

Left shift.

Non-destructively shifts left n bits, AKA "multiplication by 2 ^\a n".

Parameters:
n Number of bits to shift. If less than 0, right shift.

Reimplemented from mp::mpn_.

void mp::mpi_::shift_r size_type  n  )  [inherited]
 

Right shift.

Destructively shifts right n bits, AKA "integer division by 2 ^\a n".

Parameters:
n Number of bits to shift. If less than 0, left shift.

Reimplemented from mp::mpn_.

mpi_::size_type mp::mpi_::size  )  const [inherited]
 

Find the number of bits in use.

Returns:
The minimum number of bits required to store the number

Reimplemented from mp::mpn_.

void mp::mpi_::sqr  )  [inherited]
 

Squares the number efficiantly.

Reimplemented from mp::mpn_.

Reimplemented in mp::mpf_.

void mp::mpn_::sub const mpn_ n  )  [inherited]
 

Subtraction of an MPN.

Parameters:
n Number to subtract

void mp::mpi_::sub const mpi_ n  )  [inherited]
 

Subtraction of an MPI.

Parameters:
n Number to subtract

void mp::mpn_::sub_l limb_type  n  )  [protected, inherited]
 

Subtraction of a single limb.

Parameters:
n Limb to subtract

void mp::mpn_::swap mpn_ n  )  [inherited]
 

Swaps this with n.

This swaps the internals of the number, so it is very fast. A fast move can be done by clear()ing the destination before swap()ing.

Parameters:
n Number to swap with

void mp::mpi_::swap mpi_ n  )  [inherited]
 

Swaps this with n.

Function swaps the internals of the number, so it is very fast. A fast move can be done by clear()ing the destination before swap()ing.

Parameters:
n Number to swap with

int_type mp::mpi_::toInt  )  const [inherited]
 

Conversion to standard signed integer type.

If number is too large to fit in standard integer an overflow_error exception is thrown.

Returns:
Standard signed integer representation of the number

Reimplemented in mp::mpf_.


Member Data Documentation

size_type mp::mpn_::alloc_ [protected, inherited]
 

Number of limbs allocated, pointed to by data.

limb_type* mp::mpn_::data_ [protected, inherited]
 

Pointer to the data.

bool mp::mpn_::interrupted [static, protected, inherited]
 

Interrupt register.

const mpn_::limb_type mp::mpn_::limb_full = (1 << limb_size_bits) - 1 [static, inherited]
 

A limb with all bits set.

const mpn_::size_type mp::mpn_::limb_size_bits = _MP_SIZE / 2 [static, inherited]
 

Size of a single limb (in bits).

bool mp::mpi_::neg_ [protected, inherited]
 

negativity register (true if negative).

const mpn_::size_type mp::mpn_::size_max = INT_MAX [static, inherited]
 

The largest positive size_type value.

radix_type mp::mpi_::string_base = _MP_STR_BASE [static, protected, inherited]
 

Default base for string conversion.

const string mp::mpn_::string_digits = _MP_STR_DIGIT [static, inherited]
 

The digits used in string conversion.

size_type mp::mpn_::use_ [protected, inherited]
 

Number of limbs used (always <= alloc).


The documentation for this class was generated from the following files:
Generated on Fri Feb 4 16:17:55 2005 for LibMP++ by  doxygen 1.4.1