Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add platform dependent implementation of set_int and get_int; adjust …
…nqp::isbig_I accordingly
  • Loading branch information
moritz committed Nov 13, 2011
1 parent 506177f commit bee86c9
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 9 deletions.
39 changes: 39 additions & 0 deletions 3rdparty/libtommath/bn_mp_get_long.c
@@ -0,0 +1,39 @@
#include <tommath.h>
#ifdef BN_MP_GET_INT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/

/* get the lower unsigned long of an mp_int, platform dependent */
unsigned long mp_get_long(mp_int * a)
{
int i;
unsigned long res;

if (a->used == 0) {
return 0;
}

/* get number of digits of the lsb we have to read */
i = MIN(a->used,(int)((sizeof(unsigned long)*CHAR_BIT+DIGIT_BIT-1)/DIGIT_BIT))-1;

/* get most significant digit of result */
res = DIGIT(a,i);

while (--i >= 0) {
res = (res << DIGIT_BIT) | DIGIT(a,i);
}
return res;
}
#endif
48 changes: 48 additions & 0 deletions 3rdparty/libtommath/bn_mp_set_long.c
@@ -0,0 +1,48 @@
#include <tommath.h>
#ifdef BN_MP_SET_INT_C
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/

/* set a platform dependent unsigned long int */
int mp_set_long (mp_int * a, unsigned long b)
{
int x, res;

mp_zero (a);

/* set four bits at a time */
for (x = 0; x < sizeof(unsigned long) * 2; x++) {
/* shift the number up four bits */
if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) {
return res;
}

/* OR in the top four bits of the source */
a->dp[0] |= (b >> ((sizeof(unsigned long)) * 8 - 4)) & 15;

/* shift the source up to the next four bits */
b <<= 4;

/* ensure that digits are not clamped off */
a->used += 1;
}
mp_clamp (a);
return MP_OKAY;
}
#endif

/* $Source$ */
/* $Revision$ */
/* $Date$ */
6 changes: 6 additions & 0 deletions 3rdparty/libtommath/tommath.h
Expand Up @@ -231,9 +231,15 @@ void mp_set(mp_int *a, mp_digit b);
/* set a 32-bit const */
int mp_set_int(mp_int *a, unsigned long b);

/* set a platform dependent unsigned long value */
int mp_set_long(mp_int *a, unsigned long b);

/* get a 32-bit value */
unsigned long mp_get_int(mp_int * a);

/* get a platform dependent unsigned long value */
unsigned long mp_get_long(mp_int * a);

/* initialize and set a digit */
int mp_init_set (mp_int * a, mp_digit b);

Expand Down
8 changes: 4 additions & 4 deletions src/6model/reprs/P6bigint.c
Expand Up @@ -89,10 +89,10 @@ static INTVAL hint_for(PARROT_INTERP, STable *st, PMC *class_handle, STRING *nam
static void set_int(PARROT_INTERP, STable *st, void *data, INTVAL value) {
mp_int *i = &((P6bigintBody *)data)->i;
if (value >= 0) {
mp_set_int(i, value);
mp_set_long(i, value);
}
else {
mp_set_int(i, -value);
mp_set_long(i, -value);
mp_neg(i, i);
}
}
Expand All @@ -104,12 +104,12 @@ static INTVAL get_int(PARROT_INTERP, STable *st, void *data) {
mp_int *i = &((P6bigintBody *)data)->i;
if (MP_LT == mp_cmp_d(i, 0)) {
mp_neg(i, i);
ret = mp_get_int(i);
ret = mp_get_long(i);
mp_neg(i, i);
return -ret;
}
else {
return mp_get_int(i);
return mp_get_long(i);
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/ops/nqp_bigint.ops
Expand Up @@ -412,9 +412,7 @@ inline op nqp_bigint_pow(out PMC, in PMC, in PMC, in PMC) :base_core {
inline op nqp_bigint_is_big(out INT, in PMC) :base_core {
mp_int *a = get_bigint(interp, $2);
$1 = a->used > 1;
if ($1 == 0) {
/* mp_{set,get}_int is limited to 32 bit, so be extra careful. */
if (DIGIT(a, 0) & (~0xFFFFFFFFUL))
$1 = 1;
}
/* XXX somebody please check that on a 32 bit platform */
if ( sizeof(INTVAL) * 8 < DIGIT_BIT && $1 == 0 && DIGIT(a, 0) & ~0x7FFFFFFFUL)
$1 = 1;
}
8 changes: 8 additions & 0 deletions tools/build/Makefile.in
Expand Up @@ -234,6 +234,7 @@ LIBTOMMATH_BIN = $(TOM)core$(O) \
$(TOM)_mp_fwrite$(O) \
$(TOM)_mp_gcd$(O) \
$(TOM)_mp_get_int$(O) \
$(TOM)_mp_get_long$(O) \
$(TOM)_mp_grow$(O) \
$(TOM)_mp_init_copy$(O) \
$(TOM)_mp_init_multi$(O) \
Expand Down Expand Up @@ -286,6 +287,7 @@ LIBTOMMATH_BIN = $(TOM)core$(O) \
$(TOM)_mp_reduce_setup$(O) \
$(TOM)_mp_rshd$(O) \
$(TOM)_mp_set_int$(O) \
$(TOM)_mp_set_long$(O) \
$(TOM)_mp_set$(O) \
$(TOM)_mp_shrink$(O) \
$(TOM)_mp_signed_bin_size$(O) \
Expand Down Expand Up @@ -358,6 +360,7 @@ LIBTOMMATH_SOURCE = 3rdparty/libtommath/bncore.c \
3rdparty/libtommath/bn_mp_fwrite.c \
3rdparty/libtommath/bn_mp_gcd.c \
3rdparty/libtommath/bn_mp_get_int.c \
3rdparty/libtommath/bn_mp_get_long.c \
3rdparty/libtommath/bn_mp_grow.c \
3rdparty/libtommath/bn_mp_init.c \
3rdparty/libtommath/bn_mp_init_copy.c \
Expand Down Expand Up @@ -411,6 +414,7 @@ LIBTOMMATH_SOURCE = 3rdparty/libtommath/bncore.c \
3rdparty/libtommath/bn_mp_rshd.c \
3rdparty/libtommath/bn_mp_set.c \
3rdparty/libtommath/bn_mp_set_int.c \
3rdparty/libtommath/bn_mp_set_long.c \
3rdparty/libtommath/bn_mp_shrink.c \
3rdparty/libtommath/bn_mp_signed_bin_size.c \
3rdparty/libtommath/bn_mp_sqr.c \
Expand Down Expand Up @@ -764,6 +768,8 @@ $(OPS_DIR)/$(OPS)$(LOAD_EXT): $(OPS_DIR)/$(OPS_SOURCE) $(DYNPMC)
cd 3rdparty/libtommath && $(CC) -c @cc_o_out@bn_mp_gcd$(O) -I. $(CFLAGS) bn_mp_gcd.c
3rdparty/libtommath/bn_mp_get_int$(O): 3rdparty/libtommath/bn_mp_get_int.c $(LIBTOMMATH_H)
cd 3rdparty/libtommath && $(CC) -c @cc_o_out@bn_mp_get_int$(O) -I. $(CFLAGS) bn_mp_get_int.c
3rdparty/libtommath/bn_mp_get_long$(O): 3rdparty/libtommath/bn_mp_get_long.c $(LIBTOMMATH_H)
cd 3rdparty/libtommath && $(CC) -c @cc_o_out@bn_mp_get_long$(O) -I. $(CFLAGS) bn_mp_get_long.c
3rdparty/libtommath/bn_mp_grow$(O): 3rdparty/libtommath/bn_mp_grow.c $(LIBTOMMATH_H)
cd 3rdparty/libtommath && $(CC) -c @cc_o_out@bn_mp_grow$(O) -I. $(CFLAGS) bn_mp_grow.c
3rdparty/libtommath/bn_mp_init$(O): 3rdparty/libtommath/bn_mp_init.c $(LIBTOMMATH_H)
Expand Down Expand Up @@ -870,6 +876,8 @@ $(OPS_DIR)/$(OPS)$(LOAD_EXT): $(OPS_DIR)/$(OPS_SOURCE) $(DYNPMC)
cd 3rdparty/libtommath && $(CC) -c @cc_o_out@bn_mp_set$(O) -I. $(CFLAGS) bn_mp_set.c
3rdparty/libtommath/bn_mp_set_int$(O): 3rdparty/libtommath/bn_mp_set_int.c $(LIBTOMMATH_H)
cd 3rdparty/libtommath && $(CC) -c @cc_o_out@bn_mp_set_int$(O) -I. $(CFLAGS) bn_mp_set_int.c
3rdparty/libtommath/bn_mp_set_long$(O): 3rdparty/libtommath/bn_mp_set_long.c $(LIBTOMMATH_H)
cd 3rdparty/libtommath && $(CC) -c @cc_o_out@bn_mp_set_long$(O) -I. $(CFLAGS) bn_mp_set_long.c
3rdparty/libtommath/bn_mp_shrink$(O): 3rdparty/libtommath/bn_mp_shrink.c $(LIBTOMMATH_H)
cd 3rdparty/libtommath && $(CC) -c @cc_o_out@bn_mp_shrink$(O) -I. $(CFLAGS) bn_mp_shrink.c
3rdparty/libtommath/bn_mp_signed_bin_size$(O): 3rdparty/libtommath/bn_mp_signed_bin_size.c $(LIBTOMMATH_H)
Expand Down

0 comments on commit bee86c9

Please sign in to comment.