y-cruncher Multi-Precision Library

A Multi-threaded Multi-Precision Arithmetic Library

(Powered by y-cruncher)

(Last updated: March 7, 2016)

 

Shortcuts:

YMP Library:

Large Number Objects:

Low-Level Programming:

BigFloatR - Large Floating-Point (no RAII)

 

BigFloatR is a "raw" subclass of BigFloat.

 

 

Raw classes are bare metal objects that do not own the memory they reside in. While they are extremely difficult to use, they also provide the greatest degree of control and flexibility. For this reason, raw classes are the most performant object if used correctly.

 

More on "raw" classes.

 

 

BigFloatR is used extensively in y-cruncher.

 

 

 

 

 

 

 

 

 

 

 

 

Function List:

 

Inherited from BigFloat:

Constructors:

Setters:

Basic Arithmetic:

Large Multiplication:

Common Transform Interface Use Cases:

Functions Library:

 

Constructors:

 

Since BigFloatR does not own the buffers that it holds, they are all constructed from either raw pointers, or aliased from existing bignum objects.

 

 

Default:

BigFloatR<u32_t>::BigFloatR ();

BigFloatR<u64_t>::BigFloatR ();

 

Construct a new object in state 1 with everything uninitialized.

 

Construct from Raw Pointer:

BigFloatR<u32_t>::BigFloatR (u32_t* ptr, upL_t size);

BigFloatR<u64_t>::BigFloatR (u64_t* ptr, upL_t size);

 

void BigFloatR<u32_t>::replace (u32_t* ptr, upL_t size);

void BigFloatR<u64_t>::replace (u64_t* ptr, upL_t size);

 

Construct a new object in state 2 who's internal buffer is {ptr, size}.

 

The replace() function is the same thing, but it replaces the current object rather than constructing a new one.

For the replace(), the current object can be in any state before the call. It will be in state 2 after the call.

 

Alias/Overlap to bottom of existing BigFloatR object:

BigFloatR<u32_t>::BigFloatR (const BigFloatR<u32_t>& x, upL_t L);

BigFloatR<u64_t>::BigFloatR (const BigFloatR<u64_t>& x, upL_t L);

 

Construct a new object in state 2 who's internal buffer aliases with the bottom L words of the buffer of x. The new object will have a buffer size of L.

 

Alias/Overlap to middle of existing BigFloatR object:

BigFloatR<u32_t>::BigFloatR (const BigFloatR<u32_t>& x, upL_t s, upL_t L);

BigFloatR<u64_t>::BigFloatR (const BigFloatR<u64_t>& x, upL_t s, upL_t L);

 

Construct a new object in state 2 who's internal buffer aliases the buffer range [s, s + L) of x. The new object will have a buffer size of L.

 

Alias/Overlap to end of existing BigFloatR object:

BigFloatR<u32_t>::BigFloatR (upL_t L, const BigFloatR<u32_t>& x);

BigFloatR<u64_t>::BigFloatR (upL_t L, const BigFloatR<u64_t>& x);

 

Construct a new object in state 2 who's internal buffer aliases the upper L words of the buffer of x. The new object will have a buffer size of L.

 

Consume a Bignum Object:

BigFloatR<u32_t>::BigFloatR (BasicIntR<u32_t>& x);

BigFloatR<u64_t>::BigFloatR (BasicIntR<u64_t>& x);

 

BigFloatR<u32_t>::BigFloatR (ExactFloatR<u32_t>& x);

BigFloatR<u64_t>::BigFloatR (ExactFloatR<u64_t>& x);

 

void BigFloatR<u32_t>::replace (BasicIntR<u32_t>& x);

void BigFloatR<u64_t>::replace (BasicIntR<u64_t>& x);

 

void BigFloatR<u32_t>::replace (ExactFloatR<u32_t>& x);

void BigFloatR<u64_t>::replace (ExactFloatR<u64_t>& x);

 

Construct a new object in state 3 by "consuming" x. The new object takes over x's buffer and will have a numerical value of x.

x must be in state 3. After this call, x goes to state 1.

 

The replace() function is the same thing, but it replaces the current object rather than constructing a new one.

For the replace(), the current object can be in any state before the call. It will be in state 3 after the call.

 

These constructors are primarily used for converting one raw object type to another type.

 

Setters:

 

Set to Zero:

void BigFloatR<u32_t>::set_zero ();

void BigFloatR<u64_t>::set_zero ();

Description:

Set the current object to a value of zero.

Notes:

 


Set to Small Integer:

void BigFloatR<u32_t>::set_uW (u32_t x);

void BigFloatR<u64_t>::set_uW (u64_t x);

 

void BigFloatR<u32_t>::set_uL (uiL_t x);

void BigFloatR<u64_t>::set_uL (uiL_t x);

Description:

Set the current object to a value of x.

Notes:

 


Set to Double-Precision:

void BigFloatR<u32_t>::set_double (double x, siL_t exp);

void BigFloatR<u64_t>::set_double (double x, siL_t exp);

Description:

Set the current object to a value of x * baseexp.

 

Be aware of the usual floating-point issues. This does not "magically" increase the accuracy of the number beyond 53 bits.

The primary use of this function is a starting point for Newton's Method.

Notes:

 


Set to BigInt:

void BigFloatR<u32_t>::set_BigInt (const BigFloat<u32_t>& x);

void BigFloatR<u64_t>::set_BigInt (const BigFloat<u64_t>& x);

Description:

Set the current object to a value of x.

Notes:

 


Set to BigFloat:

void BigFloatR<u32_t>::set_BigFloat (const BigFloat<u32_t>& x, upL_t p);

void BigFloatR<u64_t>::set_BigFloat (const BigFloat<u64_t>& x, upL_t p);

Description:

Set the current object to a value of x. The precision will be truncated to p.

This acts as a copy-assignment with precision truncation.

Notes:

 

Basic Arithmetic:

 

Negate:

void BigFloatR<u32_t>::negate ();

void BigFloatR<u64_t>::negate ();

Description:

Negates this object.

Performance:

Notes:

 


Strip Trailing Zeros:

void BigFloatR<u32_t>::strip_tzs_all ();

void BigFloatR<u64_t>::strip_tzs_all ();

Description:

Remove all trailing zeros from the representation of the current object.

This reduces the length of the number which may increase performance of subsequent operations on this object.

Performance:

Notes:

 


Multiply by Power-of-Two:

void BigFloatR<u32_t>::operator<<= (siL_t pow);

void BigFloatR<u64_t>::operator<<= (siL_t pow);

Description:

Multiplies this object by 2pow. Negative pow implies by division power-of-two.

Performance:

Notes:

 


Single-Word Multiply (In-Place):

void BigFloatR<u32_t>::operator*= (u32_t x);

void BigFloatR<u64_t>::operator*= (u64_t x);

Description:

Multiplies this object by x.

Performance:

Notes:

 


Single-Word Multiply (Out-of-Place):

void BigFloatR<u32_t>::set_mul_uW (const BigFloat<u32_t>& A, u32_t x);

void BigFloatR<u64_t>::set_mul_uW (const BigFloat<u64_t>& A, u64_t x);

Description:

Computes A * x and sets the value of the current object to the result.

Performance:

Notes:

 


In-Place Add a Word:

void BigFloatR<u32_t>::add_ip (u32_t x, siL_t mag);

void BigFloatR<u64_t>::add_ip (u64_t x, siL_t mag);

Description:

Adds x * basemag to the current object.

Performance:

Notes:

 


Addition/Subtraction:

void BigFloatR<u32_t>::set_add (const BigFloat<u32_t>& A, const BigFloat<u32_t>& B, upL_t p);

void BigFloatR<u64_t>::set_add (const BigFloat<u64_t>& A, const BigFloat<u64_t>& B, upL_t p);

 

void BigFloatR<u32_t>::set_sub (const BigFloat<u32_t>& A, const BigFloat<u32_t>& B, upL_t p);

void BigFloatR<u64_t>::set_sub (const BigFloat<u64_t>& A, const BigFloat<u64_t>& B, upL_t p);

Description:

Computes A + B or A - B and sets the value of the current object to the result.

Performance:

Notes:

 

Large Multiplication:

 

Large Multiplication:

void BigFloatR<u32_t>::set_sqr (const BasicParameters& mp, const BigFloat<u32_t>& A, upL_t p);

void BigFloatR<u64_t>::set_sqr (const BasicParameters& mp, const BigFloat<u64_t>& A, upL_t p);

 

void BigFloatR<u32_t>::set_mul (const BasicParameters& mp, const BigFloat<u32_t>& A, const BigFloat<u32_t>& B, upL_t p);

void BigFloatR<u64_t>::set_mul (const BasicParameters& mp, const BigFloat<u64_t>& A, const BigFloat<u64_t>& B, upL_t p);

Description:

Computes A2 or A * B and sets the value of the current object to the result.

Performance:

BasicParameter (mp) Required Fields:

Notes:

 

Common Transform Interface Use Cases:

 

The transform-only interface is currently not public. But some of the common use-cases have been abstracted out into the public interface.

 


In-Place Large Multiplication:

void BigFloatR<u32_t>::set_sqr_ip (const BasicParameters& mp, const BigFloat<u32_t>& A, upL_t p);

void BigFloatR<u64_t>::set_sqr_ip (const BasicParameters& mp, const BigFloat<u64_t>& A, upL_t p);

 

void BigFloatR<u32_t>::set_mul_ip (const BasicParameters& mp, const BigFloat<u32_t>& A, const BigFloat<u32_t>& B, upL_t p);

void BigFloatR<u64_t>::set_mul_ip (const BasicParameters& mp, const BigFloat<u64_t>& A, const BigFloat<u64_t>& B, upL_t p);

Description:

Identical to set_sqr() and set_mul(), but it allows aliasing and overlapping between the current object and the input operands A and B.

The parameter mp is the low-level multiplication parameters struct.

 

This function may be slower for small products. The performance will be the same for large products that use FFT.

 


Chained Multiply (AB-BC) with Transform Reuse:

static void BigFloatR<u32_t>::set_mul_AB_BC (

    const BasicParameters& mp,

    BigFloatR<u32_t>& AB, upL_t pAB,

    BigFloatR<u32_t>& BC, upL_t pBC,

    const BigFloat<u32_t>& A, const BigFloat<u32_t>& B, const BigFloat<u32_t>& C

);

static void BigFloatR<u64_t>::set_mul_AB_BC (

    const BasicParameters& mp,

    BigFloatR<u64_t>& AB, upL_t pAB,

    BigFloatR<u64_t>& BC, upL_t pBC,

    const BigFloat<u64_t>& A, const BigFloat<u64_t>& B, const BigFloat<u64_t>& C

);

Description:

Same as calling:

AB.set_mul_ip(mp, A, B, pAB);

BC.set_mul_ip(mp, B, C, pBC);

For large inputs, this function may be faster than calling two separate multiplies.

Internally, this function will reuse the forward FFT for B if it determines that it may be beneficial to do so.

Aliasing/Overlap Rules:

Notes:

 

Functions Library:

 

Addition, subtraction, and multiplication are the only operations that are supported by the class itself. Everything else is implemented separately in a functions library.

All functions here are thin wrappers over native implementations in y-cruncher. So they have access to optimizations that are only possible via the internal interface.

 


Object to Hexadecimal String:

const char* to_string_hex (char* buffer, const BigFloat<u32_t>& x, upL_t digits);

const char* to_string_hex (char* buffer, const BigFloat<u64_t>& x, upL_t digits);

 

uiL_t BigFloat_to_string_hex_sizes<u32_t> (upL_t digits);

uiL_t BigFloat_to_string_hex_sizes<u64_t> (upL_t digits);

Description:

Converts x to a string representation in base 16 with at most digits significant digits.

 

The output is written to buffer. The returned pointer is a C-string with the output. The returned pointer is guaranteed to point to somewhere in buffer, but not necessarily at the beginning.

 

BigFloat_to_string_hex_sizes() returns the minimum size of the buffer needed to call to_string_hex().

 

The rounding behavior is unspecified. But the current implementation truncates towards zero.

The function will auto-select between floating and scientific notation.

Performance:

Notes:

 


Object to Decimal String:

const char* to_string_dec (const BasicParameters& mp, char* buffer, const BigFloat<u32_t>& x, upL_t digits);

const char* to_string_dec (const BasicParameters& mp, char* buffer, const BigFloat<u64_t>& x, upL_t digits);

 

uiL_t BigFloat_to_string_dec_sizes<u32_t> (uiL_t& Msize, upL_t digits, upL_t tds = 1);

uiL_t BigFloat_to_string_dec_sizes<u64_t> (uiL_t& Msize, upL_t digits, upL_t tds = 1);

Description:

Converts x to a string representation in base 10 with at most digits significant digits.

 

The output is written to buffer. The returned pointer is a C-string with the output. The returned pointer is guaranteed to point to somewhere in buffer, but not necessarily at the beginning.

 

The rounding behavior is unspecified. But the current implementation usually truncates towards zero.

The function will auto-select between floating and scientific notation.

 

BigFloat_to_string_dec_sizes() computes the minimum buffer sizes required to call to_string_hex().

Performance:

Notes:

 


Reciprocal:

void rcp (const BasicParameters& mp, BigFloatR<u32_t>& T, const BigFloat<u32_t>& A, upL_t p, u32_t* P, upL_t PL);

void rcp (const BasicParameters& mp, BigFloatR<u32_t>& T, const BigFloat<u64_t>& A, upL_t p, u64_t* P, upL_t PL);

 

void rcp_sizes<u32_t> (uiL_t& Tsize, uiL_t& Psize, uiL_t& Msize, uiL_t p, upL_t tds);

void rcp_sizes<u64_t> (uiL_t& Tsize, uiL_t& Psize, uiL_t& Msize, uiL_t p, upL_t tds);

Description:

Computes 1 / A. The result is stored into T overwriting it.

 

{P, PL} is additional scratch memory.

 

rcp_sizes() computes the minimum buffer sizes required to call rcp().

The sizing function is an increasing function for both parameters p and tds.

Performance:

BasicParameter (mp) Required Fields:

Notes:

 


Division:

void div (const BasicParameters& mp, BigFloatR<u32_t>& T, const BigFloat<u32_t>& N, const BigFloat<u32_t>& D, upL_t p, u32_t* P, upL_t PL);

void div (const BasicParameters& mp, BigFloatR<u32_t>& T, const BigFloat<u32_t>& N, const BigFloat<u32_t>& D, upL_t p, u64_t* P, upL_t PL);

 

void div_sizes<u32_t> (uiL_t& Tsize, uiL_t& Psize, uiL_t& Msize, uiL_t p, upL_t tds);

void div_sizes<u64_t> (uiL_t& Tsize, uiL_t& Psize, uiL_t& Msize, uiL_t p, upL_t tds);

Description:

Computes N / D. The result is stored into T overwriting it.

 

{P, PL} is additional scratch memory.

 

rcp_sizes() computes the minimum buffer sizes required to call rcp().

The sizing function is an increasing function for both parameters p and tds.

Performance:

BasicParameter (mp) Required Fields:

Notes:

 


Inverse Square Root (single word):

void invsqrt_uW (const BasicParameters& mp, BigFloatR<u32_t>& T, u32_t x, upL_t p, u32_t* P, upL_t PL);

void invsqrt_uW (const BasicParameters& mp, BigFloatR<u64_t>& T, u64_t x, upL_t p, u64_t* P, upL_t PL);

 

void invsqrt_uW_sizes<u32_t> (uiL_t& Tsize, uiL_t& Psize, uiL_t& Msize, uiL_t p, upL_t tds);

void invsqrt_uW_sizes<u64_t> (uiL_t& Tsize, uiL_t& Psize, uiL_t& Msize, uiL_t p, upL_t tds);

Description:

Computes 1 / sqrt(x). The result is stored into T overwriting it.

 

{P, PL} is additional scratch memory.

 

invsqrt_uW_sizes() computes the minimum buffer sizes required to call invsqrt_uW().

The sizing function is an increasing function for both parameters p and tds.

Performance:

BasicParameter (mp) Required Fields:

Notes:

 


Power Function (integer power):

void pow_uL (const BasicParameters& mp, BigFloatR<u32_t>& T, u32_t x, uiL_t pow, upL_t p);

void pow_uL (const BasicParameters& mp, BigFloatR<u64_t>& T, u64_t x, uiL_t pow, upL_t p);

 

void pow_uL_sizes<u32_t> (uiL_t& Tsize, uiL_t& Msize, uiL_t p, upL_t tds);

void pow_uL_sizes<u64_t> (uiL_t& Tsize, uiL_t& Msize, uiL_t p, upL_t tds);

 

void pow_sL (const BasicParameters& mp, BigFloatR<u32_t>& T, u32_t x, siL_t pow, upL_t p, u32_t* P, upL_t PL);

void pow_sL (const BasicParameters& mp, BigFloatR<u64_t>& T, u64_t x, siL_t pow, upL_t p, u32_t* P, upL_t PL);

 

void pow_sL_sizes<u32_t> (uiL_t& Tsize, uiL_t& Psize, uiL_t& Msize, uiL_t p, upL_t tds);

void pow_sL_sizes<u64_t> (uiL_t& Tsize, uiL_t& Psize, uiL_t& Msize, uiL_t p, upL_t tds);

Description:

Computes xpow. The result is stored into T overwriting it.

 

{P, PL} is additional scratch memory.

 

The respective sizing functions are provided:

The sizing function is an increasing function for both parameters p and tds.

 

 

Currently, there is only support for integerinteger. This will likely be extended to BigFloatinteger in a later version.

Support for BigFloatBigFloat will be more difficult as that requires exp() which requires log() which requires AGM() which requires sqrt(). And none of these are currently supported.

Performance:

BasicParameter (mp) Required Fields:

Notes: