Skip to content

Commit bee86c9

Browse files
committed
add platform dependent implementation of set_int and get_int; adjust nqp::isbig_I accordingly
1 parent 506177f commit bee86c9

File tree

6 files changed

+108
-9
lines changed

6 files changed

+108
-9
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <tommath.h>
2+
#ifdef BN_MP_GET_INT_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis
4+
*
5+
* LibTomMath is a library that provides multiple-precision
6+
* integer arithmetic as well as number theoretic functionality.
7+
*
8+
* The library was designed directly after the MPI library by
9+
* Michael Fromberger but has been written from scratch with
10+
* additional optimizations in place.
11+
*
12+
* The library is free for all purposes without any express
13+
* guarantee it works.
14+
*
15+
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
16+
*/
17+
18+
/* get the lower unsigned long of an mp_int, platform dependent */
19+
unsigned long mp_get_long(mp_int * a)
20+
{
21+
int i;
22+
unsigned long res;
23+
24+
if (a->used == 0) {
25+
return 0;
26+
}
27+
28+
/* get number of digits of the lsb we have to read */
29+
i = MIN(a->used,(int)((sizeof(unsigned long)*CHAR_BIT+DIGIT_BIT-1)/DIGIT_BIT))-1;
30+
31+
/* get most significant digit of result */
32+
res = DIGIT(a,i);
33+
34+
while (--i >= 0) {
35+
res = (res << DIGIT_BIT) | DIGIT(a,i);
36+
}
37+
return res;
38+
}
39+
#endif
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <tommath.h>
2+
#ifdef BN_MP_SET_INT_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis
4+
*
5+
* LibTomMath is a library that provides multiple-precision
6+
* integer arithmetic as well as number theoretic functionality.
7+
*
8+
* The library was designed directly after the MPI library by
9+
* Michael Fromberger but has been written from scratch with
10+
* additional optimizations in place.
11+
*
12+
* The library is free for all purposes without any express
13+
* guarantee it works.
14+
*
15+
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
16+
*/
17+
18+
/* set a platform dependent unsigned long int */
19+
int mp_set_long (mp_int * a, unsigned long b)
20+
{
21+
int x, res;
22+
23+
mp_zero (a);
24+
25+
/* set four bits at a time */
26+
for (x = 0; x < sizeof(unsigned long) * 2; x++) {
27+
/* shift the number up four bits */
28+
if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) {
29+
return res;
30+
}
31+
32+
/* OR in the top four bits of the source */
33+
a->dp[0] |= (b >> ((sizeof(unsigned long)) * 8 - 4)) & 15;
34+
35+
/* shift the source up to the next four bits */
36+
b <<= 4;
37+
38+
/* ensure that digits are not clamped off */
39+
a->used += 1;
40+
}
41+
mp_clamp (a);
42+
return MP_OKAY;
43+
}
44+
#endif
45+
46+
/* $Source$ */
47+
/* $Revision$ */
48+
/* $Date$ */

3rdparty/libtommath/tommath.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,15 @@ void mp_set(mp_int *a, mp_digit b);
231231
/* set a 32-bit const */
232232
int mp_set_int(mp_int *a, unsigned long b);
233233

234+
/* set a platform dependent unsigned long value */
235+
int mp_set_long(mp_int *a, unsigned long b);
236+
234237
/* get a 32-bit value */
235238
unsigned long mp_get_int(mp_int * a);
236239

240+
/* get a platform dependent unsigned long value */
241+
unsigned long mp_get_long(mp_int * a);
242+
237243
/* initialize and set a digit */
238244
int mp_init_set (mp_int * a, mp_digit b);
239245

src/6model/reprs/P6bigint.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ static INTVAL hint_for(PARROT_INTERP, STable *st, PMC *class_handle, STRING *nam
8989
static void set_int(PARROT_INTERP, STable *st, void *data, INTVAL value) {
9090
mp_int *i = &((P6bigintBody *)data)->i;
9191
if (value >= 0) {
92-
mp_set_int(i, value);
92+
mp_set_long(i, value);
9393
}
9494
else {
95-
mp_set_int(i, -value);
95+
mp_set_long(i, -value);
9696
mp_neg(i, i);
9797
}
9898
}
@@ -104,12 +104,12 @@ static INTVAL get_int(PARROT_INTERP, STable *st, void *data) {
104104
mp_int *i = &((P6bigintBody *)data)->i;
105105
if (MP_LT == mp_cmp_d(i, 0)) {
106106
mp_neg(i, i);
107-
ret = mp_get_int(i);
107+
ret = mp_get_long(i);
108108
mp_neg(i, i);
109109
return -ret;
110110
}
111111
else {
112-
return mp_get_int(i);
112+
return mp_get_long(i);
113113
}
114114
}
115115

src/ops/nqp_bigint.ops

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,7 @@ inline op nqp_bigint_pow(out PMC, in PMC, in PMC, in PMC) :base_core {
412412
inline op nqp_bigint_is_big(out INT, in PMC) :base_core {
413413
mp_int *a = get_bigint(interp, $2);
414414
$1 = a->used > 1;
415-
if ($1 == 0) {
416-
/* mp_{set,get}_int is limited to 32 bit, so be extra careful. */
417-
if (DIGIT(a, 0) & (~0xFFFFFFFFUL))
418-
$1 = 1;
419-
}
415+
/* XXX somebody please check that on a 32 bit platform */
416+
if ( sizeof(INTVAL) * 8 < DIGIT_BIT && $1 == 0 && DIGIT(a, 0) & ~0x7FFFFFFFUL)
417+
$1 = 1;
420418
}

tools/build/Makefile.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ LIBTOMMATH_BIN = $(TOM)core$(O) \
234234
$(TOM)_mp_fwrite$(O) \
235235
$(TOM)_mp_gcd$(O) \
236236
$(TOM)_mp_get_int$(O) \
237+
$(TOM)_mp_get_long$(O) \
237238
$(TOM)_mp_grow$(O) \
238239
$(TOM)_mp_init_copy$(O) \
239240
$(TOM)_mp_init_multi$(O) \
@@ -286,6 +287,7 @@ LIBTOMMATH_BIN = $(TOM)core$(O) \
286287
$(TOM)_mp_reduce_setup$(O) \
287288
$(TOM)_mp_rshd$(O) \
288289
$(TOM)_mp_set_int$(O) \
290+
$(TOM)_mp_set_long$(O) \
289291
$(TOM)_mp_set$(O) \
290292
$(TOM)_mp_shrink$(O) \
291293
$(TOM)_mp_signed_bin_size$(O) \
@@ -358,6 +360,7 @@ LIBTOMMATH_SOURCE = 3rdparty/libtommath/bncore.c \
358360
3rdparty/libtommath/bn_mp_fwrite.c \
359361
3rdparty/libtommath/bn_mp_gcd.c \
360362
3rdparty/libtommath/bn_mp_get_int.c \
363+
3rdparty/libtommath/bn_mp_get_long.c \
361364
3rdparty/libtommath/bn_mp_grow.c \
362365
3rdparty/libtommath/bn_mp_init.c \
363366
3rdparty/libtommath/bn_mp_init_copy.c \
@@ -411,6 +414,7 @@ LIBTOMMATH_SOURCE = 3rdparty/libtommath/bncore.c \
411414
3rdparty/libtommath/bn_mp_rshd.c \
412415
3rdparty/libtommath/bn_mp_set.c \
413416
3rdparty/libtommath/bn_mp_set_int.c \
417+
3rdparty/libtommath/bn_mp_set_long.c \
414418
3rdparty/libtommath/bn_mp_shrink.c \
415419
3rdparty/libtommath/bn_mp_signed_bin_size.c \
416420
3rdparty/libtommath/bn_mp_sqr.c \
@@ -764,6 +768,8 @@ $(OPS_DIR)/$(OPS)$(LOAD_EXT): $(OPS_DIR)/$(OPS_SOURCE) $(DYNPMC)
764768
cd 3rdparty/libtommath && $(CC) -c @cc_o_out@bn_mp_gcd$(O) -I. $(CFLAGS) bn_mp_gcd.c
765769
3rdparty/libtommath/bn_mp_get_int$(O): 3rdparty/libtommath/bn_mp_get_int.c $(LIBTOMMATH_H)
766770
cd 3rdparty/libtommath && $(CC) -c @cc_o_out@bn_mp_get_int$(O) -I. $(CFLAGS) bn_mp_get_int.c
771+
3rdparty/libtommath/bn_mp_get_long$(O): 3rdparty/libtommath/bn_mp_get_long.c $(LIBTOMMATH_H)
772+
cd 3rdparty/libtommath && $(CC) -c @cc_o_out@bn_mp_get_long$(O) -I. $(CFLAGS) bn_mp_get_long.c
767773
3rdparty/libtommath/bn_mp_grow$(O): 3rdparty/libtommath/bn_mp_grow.c $(LIBTOMMATH_H)
768774
cd 3rdparty/libtommath && $(CC) -c @cc_o_out@bn_mp_grow$(O) -I. $(CFLAGS) bn_mp_grow.c
769775
3rdparty/libtommath/bn_mp_init$(O): 3rdparty/libtommath/bn_mp_init.c $(LIBTOMMATH_H)
@@ -870,6 +876,8 @@ $(OPS_DIR)/$(OPS)$(LOAD_EXT): $(OPS_DIR)/$(OPS_SOURCE) $(DYNPMC)
870876
cd 3rdparty/libtommath && $(CC) -c @cc_o_out@bn_mp_set$(O) -I. $(CFLAGS) bn_mp_set.c
871877
3rdparty/libtommath/bn_mp_set_int$(O): 3rdparty/libtommath/bn_mp_set_int.c $(LIBTOMMATH_H)
872878
cd 3rdparty/libtommath && $(CC) -c @cc_o_out@bn_mp_set_int$(O) -I. $(CFLAGS) bn_mp_set_int.c
879+
3rdparty/libtommath/bn_mp_set_long$(O): 3rdparty/libtommath/bn_mp_set_long.c $(LIBTOMMATH_H)
880+
cd 3rdparty/libtommath && $(CC) -c @cc_o_out@bn_mp_set_long$(O) -I. $(CFLAGS) bn_mp_set_long.c
873881
3rdparty/libtommath/bn_mp_shrink$(O): 3rdparty/libtommath/bn_mp_shrink.c $(LIBTOMMATH_H)
874882
cd 3rdparty/libtommath && $(CC) -c @cc_o_out@bn_mp_shrink$(O) -I. $(CFLAGS) bn_mp_shrink.c
875883
3rdparty/libtommath/bn_mp_signed_bin_size$(O): 3rdparty/libtommath/bn_mp_signed_bin_size.c $(LIBTOMMATH_H)

0 commit comments

Comments
 (0)