Skip to content

Commit

Permalink
gmpints: add CHECK_INT macro for debugging
Browse files Browse the repository at this point in the history
To turn this on, #define DEBUG_GMP 1
  • Loading branch information
fingolfin committed Jan 4, 2017
1 parent 81a0c31 commit 0377638
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/gmpints.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ extern "C" {
/* macro for swapping two variables of a given type. Poor man's C++ macro. */
#define SWAP(T, a, b) do { T SWAP_TMP = a; a = b; b = SWAP_TMP; } while (0)

/* debugging */
#ifndef DEBUG_GMP
#define DEBUG_GMP 0
#endif

#if defined(__GNUC__)
# define CURRENT_FUNCTION __PRETTY_FUNCTION__
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)
# define CURRENT_FUNCTION __func__
#elif defined(_MSC_VER)
# define CURRENT_FUNCTION __FUNCTION__
#else
# define CURRENT_FUNCTION "<unknown>"
#endif

#if DEBUG_GMP
#define CHECK_INT(op) IS_NORMALIZED_AND_REDUCED(op, CURRENT_FUNCTION, __LINE__)
#else
#define CHECK_INT(op) do { } while(0);
#endif


/* macros to save typing later :) */
#define VAL_LIMB0(obj) (*ADDR_INT(obj))
Expand Down Expand Up @@ -297,6 +318,38 @@ Obj GMP_REDUCE( Obj gmp )
return gmp;
}

#if DEBUG_GMP
static int IS_NORMALIZED_AND_REDUCED( Obj gmp, const char *func, int line )
{
TypGMPSize size;
if (IS_INTOBJ( gmp )) {
return 1;
}
for ( size = SIZE_INT(gmp); size != (TypGMPSize)1; size-- ) {
if ( ADDR_INT(gmp)[(size - 1)] != 0 ) {
break;
}
}
if ( size < SIZE_INT(gmp) ) {
Pr("WARNING: non-normalized gmp value (%s:%d)\n",(Int)func,line);
}
if ( SIZE_INT(gmp) == 1) {
if ( ( VAL_LIMB0(gmp) < ((1L<<NR_SMALL_INT_BITS)) ) ||
( IS_INTNEG(gmp) &&
( VAL_LIMB0(gmp) == (1L<<NR_SMALL_INT_BITS) ) ) ) {
if ( IS_INTNEG(gmp) ) {
Pr("WARNING: non-reduced negative gmp value (%s:%d)\n",(Int)func,line);
return 0;
}
else {
Pr("WARNING: non-reduced positive gmp value (%s:%d)\n",(Int)func,line);
return 0;
}
}
}
return 1;
}
#endif

/****************************************************************************
**
Expand Down Expand Up @@ -795,6 +848,9 @@ Obj FuncSTRING_INT( Obj self, Obj integer )
*/
Int EqInt ( Obj gmpL, Obj gmpR )
{
CHECK_INT(gmpL);
CHECK_INT(gmpR);

/* compare two small integers */
if ( ARE_INTOBJS( gmpL, gmpR ) )
return gmpL == gmpR;
Expand Down Expand Up @@ -823,6 +879,9 @@ Int LtInt ( Obj gmpL, Obj gmpR )
{
Int res;

CHECK_INT(gmpL);
CHECK_INT(gmpR);

/* compare two small integers */
if ( ARE_INTOBJS( gmpL, gmpR ) )
return (Int)gmpL < (Int)gmpR;
Expand Down Expand Up @@ -873,6 +932,9 @@ static Obj SumOrDiffInt ( Obj gmpL, Obj gmpR, Int sign )
Int resneg; /* set to 1 if result will be negative */
TypLimb carry; /* hold any carry or borrow */

CHECK_INT(gmpL);
CHECK_INT(gmpR);

twosmall = 0;
onesmall = 0;
swapped = 0;
Expand Down Expand Up @@ -1167,6 +1229,8 @@ Obj AInvInt ( Obj gmp )
{
Obj inv;

CHECK_INT(gmp);

/* handle small integer */
if ( IS_INTOBJ( gmp ) ) {

Expand Down Expand Up @@ -1528,6 +1592,9 @@ Obj PowInt ( Obj gmpL, Obj gmpR )
Int i;
Obj pow;

CHECK_INT(gmpL);
CHECK_INT(gmpR);

if ( gmpR == INTOBJ_INT(0) ) {
pow = INTOBJ_INT(1);
}
Expand Down

0 comments on commit 0377638

Please sign in to comment.