macros.h File Reference

Various CPP macros. More...

Go to the source code of this file.

Defines

#define _TIFA_MACROS_H_
#define MPN_NORMALIZE(dest, nlimbs)
#define SIZ(x)   ((x)->_mp_size)
#define ABSIZ(x)   (ABS(SIZ(x)))
#define MPZ_TO_ABS(x)   (SIZ(x) = ABSIZ(x))
#define PTR(x)   ((x)->_mp_d)
#define ALLOC(x)   ((x)->_mp_alloc)
#define MPZ_LIMB_VALUE(x, i)   ( PTR(x)[(i)] & GMP_NUMB_MASK )
#define MPZ_LAST_LIMB_VALUE(x)   ( PTR(x)[SIZ(x) - 1] & GMP_NUMB_MASK )
#define MAX(a, b)   ( ((a) > (b)) ? (a) : (b) )
#define MIN(a, b)   ( ((a) < (b)) ? (a) : (b) )
#define ABS(a)   ( ((a) < 0) ? (-(a)) : (a) )
#define IS_POWER_OF_2_UI(ui)   ( ((ui) & ((ui) - 1)) == 0 )
#define IS_EVEN(ui)   (((ui) & 1) == 0)
#define IS_ODD(ui)   (((ui) & 1) != 0)
#define ARE_EVEN(uia, uib)   ((((uia) | (uib)) & 1) == 0)
#define ARE_ODD(uia, uib)   ((((uia) | (uib)) & 1) != 0)
#define BIT(N, i)   ( ((N) & (1<<(i))) ? 1 : 0 )
#define DUFF_DEVICE(COUNT, STATEMENT,...)
#define MPZ_IS_SQUARE(X)   (0 != mpz_perfect_square_p(X))
#define NMILLER_RABIN   32
#define MPZ_IS_PRIME(X)   (0 != mpz_probab_prime_p((X), NMILLER_RABIN))
#define MPN_ADD(A, B, C)
#define MPN_ADD_CS(A, B, C)
#define MPN_SUB(A, B, C)
#define MPN_SUB_N(A, B, C)
#define MPN_TDIV_QR(Q, R, N, D)
#define MPN_MUL(A, B, C)
#define MPN_MUL_N(A, B, C)
#define MPN_MUL_CS(A, B, C)
#define MPN_MUL_CS_S(A, B, C)
#define DECLARE_MPZ_SWAP_VARS
#define MPZ_SWAP(A, B)
#define TIFA_DEBUG_MSG(...)


Detailed Description

Various CPP macros.

Author:
Jerome Milan
Date:
Fri Jun 10 2011
Version:
2011-06-10
Defines some C preprocessor macros that should be kept internal to the TIFA library to avoid poluting client code.

Definition in file macros.h.


Define Documentation

#define _TIFA_MACROS_H_

Standard include guard.

Definition at line 36 of file macros.h.

#define ABS (  )     ( ((a) < 0) ? (-(a)) : (a) )

Standard macro returning the absolute value of a.

Note:
As usual, be careful of possible side effects when using this kind of macro. The standard disclaimers apply.

Definition at line 204 of file macros.h.

#define ABSIZ (  )     (ABS(SIZ(x)))

Macro from the GMP library: Returns the absolute value of SIZ(x).

Returns the absolute value of SIZ(x) that is to say the number of mp_limbs_t integers needed to represent the value of x.

Note:
This macro is the ABSIZ macro from the GMP library. It is redistributed under the GNU LGPL license.

Definition at line 115 of file macros.h.

#define ALLOC (  )     ((x)->_mp_alloc)

Macro from the GMP library: Returns the _mp_alloc field of an mpz_t integer.

Returns the _mp_alloc field of an mpz_t integer, that is to say the size (in units of mp_limb_t) of the x->_mp_d array.

Note:
This macro is the ALLOC macro from the GMP library. It is redistributed under the GNU LGPL license.

Definition at line 154 of file macros.h.

#define ARE_EVEN ( uia,
uib   )     ((((uia) | (uib)) & 1) == 0)

Macro returning True if both of the unsigned integers uia and uib are even, False otherwise.

Definition at line 237 of file macros.h.

#define ARE_ODD ( uia,
uib   )     ((((uia) | (uib)) & 1) != 0)

Macro returning True if both of the unsigned integers uia and uib are odd, False otherwise.

Definition at line 244 of file macros.h.

#define BIT ( N,
 )     ( ((N) & (1<<(i))) ? 1 : 0 )

Macro returning the value of the i-th least significant bit of N. BIT(N, 0) returns the least significant bit of N.

Definition at line 251 of file macros.h.

#define DECLARE_MPZ_SWAP_VARS

Value:

mp_ptr __TMPPTR__MACROS_H__a9b3c01__;                           \
    mp_size_t __TMPSIZ__MACROS_H__a9b3c01__;
Macro declaring local variables needed by the MPZ_SWAP macro. Should be called once prior to any use of the MPZ_SWAP macro.

Warning:
Declares the variables __TMPPTR__MACROS_H__a9b3c01__ and __TMPSIZ__MACROS_H__a9b3c01__ . Hopefully their names are fancy enough to avoid any local conflict.

Definition at line 562 of file macros.h.

#define DUFF_DEVICE ( COUNT,
STATEMENT,
...   ) 

Value:

do {                                            \
        long int __count__ = (COUNT);               \
        long int __niter__ = (__count__ + 7) >> 3;  \
        switch (__count__ & 7) {                    \
            case 0: do { STATEMENT; __VA_ARGS__;    \
            case 7:      STATEMENT; __VA_ARGS__;    \
            case 6:      STATEMENT; __VA_ARGS__;    \
            case 5:      STATEMENT; __VA_ARGS__;    \
            case 4:      STATEMENT; __VA_ARGS__;    \
            case 3:      STATEMENT; __VA_ARGS__;    \
            case 2:      STATEMENT; __VA_ARGS__;    \
            case 1:      STATEMENT; __VA_ARGS__;    \
                         __niter__--;               \
                    } while (__niter__ > 0);        \
        }                                           \
    } while (0)
Implements the so-called "Duff device" which is a fairly well-known (and so ugly looking!) loop unrolling technique. COUNT is the number of times to perform the operations given by STATEMENTS.

Warning:
COUNT should be strictly positive. Using COUNT equals to zero will yield wrong results.
Note:
This macro was actually inspired (borrowed? stolen?) from the example given in the article "A Reusable Duff Device" written by Ralf Holly.
See also:
Tom Duff's comments about this technique at http://www.lysator.liu.se/c/duffs-device.html (URL last accessed on Thu 17 Feb 2011)

"A Reusable Duff Device", Ralf Holly, Dr. Dobb's Journal, August 2005. Available online at: http://www.drdobbs.com/high-performance-computing/184406208 (URL last accessed on Thu 17 Feb 2011)

Definition at line 277 of file macros.h.

#define IS_EVEN ( ui   )     (((ui) & 1) == 0)

Macro returning True if the unsigned integer ui is even, False otherwise.

Definition at line 223 of file macros.h.

#define IS_ODD ( ui   )     (((ui) & 1) != 0)

Macro returning True if the unsigned integer ui is odd, False otherwise.

Definition at line 230 of file macros.h.

#define IS_POWER_OF_2_UI ( ui   )     ( ((ui) & ((ui) - 1)) == 0 )

Macro returning a non-zero value if the unsigned integer ui is a power of 2.

Note:
As usual, be careful of possible side effects when using this kind of macro. The standard disclaimers apply.

Definition at line 216 of file macros.h.

Referenced by ceil_log2().

#define MAX ( a,
 )     ( ((a) > (b)) ? (a) : (b) )

Standard macro returning the maximum of a and b.

Note:
As usual, be careful of possible side effects when using this kind of macro. The standard disclaimers apply.

Definition at line 180 of file macros.h.

#define MIN ( a,
 )     ( ((a) < (b)) ? (a) : (b) )

Standard macro returning the minimum of a and b.

Note:
As usual, be careful of possible side effects when using this kind of macro. The standard disclaimers apply.

Definition at line 192 of file macros.h.

#define MPN_ADD ( A,
B,
 ) 

Value:

do {                                                            \
       if (mpn_add(PTR(A), PTR(B), SIZ(B), PTR(C), SIZ(C))) {       \
           SIZ(A) = SIZ(B);                                         \
           MPN_NORMALIZE(PTR(A), SIZ(A));                           \
           PTR(A)[SIZ(A)] = 1;                                      \
           SIZ(A)++;                                                \
       } else {                                                     \
           SIZ(A) = SIZ(B);                                         \
           MPN_NORMALIZE(PTR(A), SIZ(A));                           \
       }                                                            \
    } while (0)
Syntaxic sugar macro wrapping a call to mpn_add. Performs size normalization on the result and takes care of the possible carry out. Does not perform any reallocation: the user should make sure the result has enough space to accomodate the possible carry out.

Takes as parameters three mpz_t (and not arrays of mp_limb_t). SIZ(B) should be greater than or equal to SIZ(C).

Warning:
B and C should be both positive or the result will be unpredictable.
See also:
The GMP documentation for more information on the mpn_add function.

Definition at line 339 of file macros.h.

#define MPN_ADD_CS ( A,
B,
 ) 

Value:

do {                                                            \
       if (SIZ(B) > SIZ(C)) {                                       \
           MPN_ADD(A, B, C);                                        \
       } else {                                                     \
           MPN_ADD(A, C, B);                                        \
       }                                                            \
    } while (0)
Syntaxic sugar macro wrapping a call to mpn_add. Performs size normalization on the result, takes care of the possible carry out, and Checks the Sizes of the operands to call mpn_add with the proper parameters' order. However, does not perform any reallocation: the user should make sure the result has enough space to accomodate the possible carry out.

Takes as parameters three mpz_t (and not arrays of mp_limb_t). B and C can be used interchangeably.

Warning:
B and C should be both positive or the result will be unpredictable.
See also:
The GMP documentation for more information on the mpn_add function.

Definition at line 371 of file macros.h.

#define MPN_MUL ( A,
B,
 ) 

Value:

do {                                                            \
       mpn_mul(PTR(A), PTR(B), SIZ(B), PTR(C), SIZ(C));             \
       SIZ(A) = SIZ(B) + SIZ(C);                                    \
       MPN_NORMALIZE(PTR(A), SIZ(A));                               \
    } while (0)
Syntaxic sugar macro wrapping a call to mpn_mul. Performs size normalization on the result.

Takes as parameters three mpz_t (and not arrays of mp_limb_t). SIZ(B) should be greater than or equal to SIZ(C).

Warning:
B and C should be both positive or the result will be unpredictable.
See also:
The GMP documentation for more information on the mpn_mul function.

Definition at line 466 of file macros.h.

#define MPN_MUL_CS ( A,
B,
 ) 

Value:

do {                                                            \
       if (SIZ(B) > SIZ(C)) {                                       \
           mpn_mul(PTR(A), PTR(B), SIZ(B), PTR(C), SIZ(C));         \
       } else {                                                     \
           mpn_mul(PTR(A), PTR(C), SIZ(C), PTR(B), SIZ(B));         \
       }                                                            \
       SIZ(A) = SIZ(B) + SIZ(C);                                    \
       MPN_NORMALIZE(PTR(A), SIZ(A));                               \
    } while (0)
Syntaxic sugar macro wrapping a call to mpn_mul. Performs size normalization on the result, and Checks the Sizes of the operands to call mpn_mul with the proper parameters' order.

Takes as parameters three mpz_t (and not arrays of mp_limb_t). B and C can be used interchangeably.

Warning:
B and C should be both positive or the result will be unpredictable.
See also:
The GMP documentation for more information on the mpn_mul function.

Definition at line 511 of file macros.h.

#define MPN_MUL_CS_S ( A,
B,
 ) 

Value:

do {                                                            \
       if (ABSIZ(B) > ABSIZ(C)) {                                   \
           mpn_mul(PTR(A), PTR(B), ABSIZ(B), PTR(C), ABSIZ(C));     \
       } else {                                                     \
           mpn_mul(PTR(A), PTR(C), ABSIZ(C), PTR(B), ABSIZ(B));     \
       }                                                            \
       SIZ(A) = ABSIZ(B) + ABSIZ(C);                                \
       MPN_NORMALIZE(PTR(A), SIZ(A));                               \
       if ((SIZ(B) ^ SIZ(C)) < 0) {                                 \
            SIZ(A) = -SIZ(A);                                       \
       }                                                            \
    } while (0)
Syntaxic sugar macro wrapping a call to mpn_mul. Performs size normalization on the result, and Checks the Sizes and the Signs of the operands to call mpn_mul with the proper parameters' order.

Takes as parameters three mpz_t (and not arrays of mp_limb_t). B and C can be used interchangeably.

Note:
B and C are allowed to be negative.
See also:
The GMP documentation for more information on the mpn_mul function.

Definition at line 537 of file macros.h.

#define MPN_MUL_N ( A,
B,
 ) 

Value:

do {                                                            \
       mpn_mul_n(PTR(A), PTR(B), PTR(C), SIZ(B));                   \
       SIZ(A) = SIZ(B) << 1;                                        \
       MPN_NORMALIZE(PTR(A), SIZ(A));                               \
    } while (0)
Syntaxic sugar macro wrapping a call to mpn_mul_n. Performs size normalization on the result.

Takes as parameters three mpz_t (and not arrays of mp_limb_t). SIZ(B) and SIZ(C) should be the same.

Warning:
B and C should be both positive or the result will be unpredictable.
See also:
The GMP documentation for more information on the mpn_mul_n function.

Definition at line 488 of file macros.h.

#define MPN_NORMALIZE ( dest,
nlimbs   ) 

Value:

do {                                                         \
        while (((nlimbs) > 0) && ((dest)[(nlimbs) - 1] == 0)) {  \
            (nlimbs)--;                                          \
        }                                                        \
    } while (0)
Macro from the GMP library: Computes the effective size of an MPN number.

Given dest, a pointer to an array of nlimbs mp_limbs_t integers giving the representation of a multi-precision integer n, computes the absolute value of the effective size of n, i.e the number of significant mp_size_t integers needed to represent n and modifies the value of nlimbs accordingly.

Note:
This macro is originally the MPN_NORMALIZE macro from the GMP library. It has been slightly modified.

Definition at line 79 of file macros.h.

#define MPN_SUB ( A,
B,
 ) 

Value:

do {                                                            \
       mpn_sub(PTR(A), PTR(B), SIZ(B), PTR(C), SIZ(C));             \
       SIZ(A) = SIZ(B);                                             \
       MPN_NORMALIZE(PTR(A), SIZ(A));                               \
    } while (0)
Syntaxic sugar macro wrapping a call to mpn_sub. Performs size normalization on the result but does not take care of the possible borrow out.

Takes as parameters three mpz_t (and not arrays of mp_limb_t). B should be greater than or equal to C.

Warning:
B and C should be both positive or the result will be unpredictable.
See also:
The GMP documentation for more information on the mpn_sub function.

Definition at line 396 of file macros.h.

#define MPN_SUB_N ( A,
B,
 ) 

Value:

do {                                                            \
       mpn_sub_n(PTR(A), PTR(B), PTR(C), SIZ(B));                   \
       SIZ(A) = SIZ(B);                                             \
       MPN_NORMALIZE(PTR(A), SIZ(A));                               \
    } while (0)
Syntaxic sugar macro wrapping a call to mpn_sub_n. Performs size normalization on the result but does not take care of the possible borrow out.

Takes as parameters three mpz_t (and not arrays of mp_limb_t). B should be greater than or equal to C.

Warning:
B and C should be both positive or the result will be unpredictable.
See also:
The GMP documentation for more information on the mpn_sub_n function.

Definition at line 419 of file macros.h.

#define MPN_TDIV_QR ( Q,
R,
N,
 ) 

Value:

do {                                                                     \
        if (SIZ(N) >= SIZ(D)) {                                              \
            mpn_tdiv_qr(PTR(Q), PTR(R), 0,  PTR(N), SIZ(N), PTR(D), SIZ(D)); \
            SIZ(Q) = SIZ(N) - SIZ(D) + 1;                                    \
            MPN_NORMALIZE(PTR(Q), SIZ(Q));                                   \
            SIZ(R) = SIZ(D);                                                 \
            MPN_NORMALIZE(PTR(R), SIZ(R));                                   \
        }                                                                    \
    } while (0)
Syntaxic sugar macro wrapping a call to mpn_tdiv_qr. Performs size normalization on both the quotient and remainder.

Takes as parameters four mpz_t (and not arrays of mp_limb_t).

Warning:
N and D should be both positive or the result will be unpredictable.
See also:
The GMP documentation for more information on the mpn_tdiv_qr function.

Definition at line 440 of file macros.h.

#define MPZ_IS_PRIME (  )     (0 != mpz_probab_prime_p((X), NMILLER_RABIN))

Syntaxic sugar macro wrapping a call to mpz_probab_prime_p. "Returns" true if and only if the mpz_t X is (probably) prime.

Takes as parameter an mpz_t.

Definition at line 320 of file macros.h.

#define MPZ_IS_SQUARE (  )     (0 != mpz_perfect_square_p(X))

Syntaxic sugar macro wrapping a call to mpz_perfect_square_p . "Returns" true if and only if the mpz_t X is a perfect square.

Takes as parameter an mpz_t.

Definition at line 303 of file macros.h.

#define MPZ_LAST_LIMB_VALUE (  )     ( PTR(x)[SIZ(x) - 1] & GMP_NUMB_MASK )

Returns the value of the most significant limb of the mpz_t integer x. Is equivalent to MPZ_LIMB_VALUE(x, SIZ(x) - 1).

Definition at line 169 of file macros.h.

#define MPZ_LIMB_VALUE ( x,
 )     ( PTR(x)[(i)] & GMP_NUMB_MASK )

Returns the value of the i-th limb of the mpz_t integer x. The least significant limb is given by i = 0.

Definition at line 162 of file macros.h.

#define MPZ_SWAP ( A,
 ) 

Value:

do {                                                            \
        __TMPPTR__MACROS_H__a9b3c01__ = PTR(A);                     \
        __TMPSIZ__MACROS_H__a9b3c01__ = SIZ(A);                     \
        PTR(A) = PTR(B);                                            \
        SIZ(A) = SIZ(B);                                            \
        PTR(B) = __TMPPTR__MACROS_H__a9b3c01__;                     \
        SIZ(B) = __TMPSIZ__MACROS_H__a9b3c01__;                     \
    } while (0)
Macro swapping the values of the two mpz_t A and B.

Warning:
The macro DECLARE_MPZ_SWAP_VARS should be called once before using MPZ_SWAP.

Definition at line 575 of file macros.h.

#define MPZ_TO_ABS (  )     (SIZ(x) = ABSIZ(x))

Sets the mpz_t x to its absolute value.

Definition at line 123 of file macros.h.

#define NMILLER_RABIN   32

Number of Miller-Rabin iterations to perform for each compositeness test.

Definition at line 310 of file macros.h.

#define PTR (  )     ((x)->_mp_d)

Macro from the GMP library: Returns the _mp_d field of an mpz_t integer.

Returns the _mp_d field of an mpz_t integer, that is to say a pointer to an array of mp_limbs_t integers giving the representation of the value of x.

Note:
This macro is the PTR macro from the GMP library. It is redistributed under the GNU LGPL license.

Definition at line 139 of file macros.h.

#define SIZ (  )     ((x)->_mp_size)

Macro from the GMP library: Returns the _mp_size field of an mpz_t integer.

Returns the _mp_size field of the variable x of type mpz_t, that is to say the number of mp_limbs_t integers needed to represent the value of x. The sign of the returned value is given by the sign of x's value.

Note:
This macro is the SIZ macro from the GMP library. It is redistributed under the GNU LGPL license.

Definition at line 100 of file macros.h.

#define TIFA_DEBUG_MSG ( ...   ) 

Macro printing debug message with filename and line number.

Warning:
The symbol ENABLE_TIFA_DEBUG_MSG should be defined to non zero before including this file.

Definition at line 601 of file macros.h.


Generated on Fri Jun 17 11:10:12 2011 for TIFA by Doxygen 1.5.5