1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
/* for Perl prior to v5.7.1 */
#ifndef SvUOK
# define SvUOK(sv) SvIOK_UV(sv)
#endif
double XS_BASE = 0;
double XS_BASE_LEN = 0;
MODULE = Math::BigInt::FastCalc PACKAGE = Math::BigInt::FastCalc
PROTOTYPES: DISABLE
#############################################################################
# 2002-08-12 0.03 Tels unreleased
# * is_zero/is_one/is_odd/is_even/len work now (pass v1.61 tests)
# 2002-08-13 0.04 Tels unreleased
# * returns no/yes for is_foo() methods to be faster
# 2002-08-18 0.06alpha
# * added _num(), _inc() and _dec()
# 2002-08-25 0.06 Tels
# * added __strip_zeros(), _copy()
# 2004-08-13 0.07 Tels
# * added _is_two(), _is_ten(), _ten()
# 2007-04-02 0.08 Tels
# * plug leaks by creating mortals
# 2007-05-27 0.09 Tels
# * add _new()
#define RETURN_MORTAL_INT(value) \
ST(0) = sv_2mortal(newSViv(value)); \
XSRETURN(1);
#define RETURN_MORTAL_BOOL(temp, comp) \
ST(0) = sv_2mortal(boolSV( SvIV(temp) == comp));
#define CONSTANT_OBJ(int) \
RETVAL = newAV(); \
sv_2mortal((SV*)RETVAL); \
av_push (RETVAL, newSViv( int ));
void
_set_XS_BASE(BASE, BASE_LEN)
SV* BASE
SV* BASE_LEN
CODE:
XS_BASE = SvNV(BASE);
XS_BASE_LEN = SvIV(BASE_LEN);
##############################################################################
# _new
AV *
_new(class, x)
SV* x
INIT:
STRLEN len;
char* cur;
STRLEN part_len;
CODE:
/* create the array */
RETVAL = newAV();
sv_2mortal((SV*)RETVAL);
if (SvUOK(x) && SvUV(x) < XS_BASE)
{
/* shortcut for integer arguments */
av_push (RETVAL, newSVuv( SvUV(x) ));
}
else
{
/* split the input (as string) into XS_BASE_LEN long parts */
/* in perl:
[ reverse(unpack("a" . ($il % $BASE_LEN+1)
. ("a$BASE_LEN" x ($il / $BASE_LEN)), $_[1])) ];
*/
cur = SvPV(x, len); /* convert to string & store length */
cur += len; /* doing "cur = SvEND(x)" does not work! */
# process the string from the back
while (len > 0)
{
/* use either BASE_LEN or the amount of remaining digits */
part_len = (STRLEN) XS_BASE_LEN;
if (part_len > len)
{
part_len = len;
}
/* processed so many digits */
cur -= part_len;
len -= part_len;
/* printf ("part '%s' (part_len: %i, len: %i, BASE_LEN: %i)\n", cur, part_len, len, XS_BASE_LEN); */
if (part_len > 0)
{
av_push (RETVAL, newSVpvn(cur, part_len) );
}
}
}
OUTPUT:
RETVAL
##############################################################################
# _copy
void
_copy(class, x)
SV* x
INIT:
AV* a;
AV* a2;
I32 elems;
CODE:
a = (AV*)SvRV(x); /* ref to aray, don't check ref */
elems = av_len(a); /* number of elems in array */
a2 = (AV*)sv_2mortal((SV*)newAV());
av_extend (a2, elems); /* pre-padd */
while (elems >= 0)
{
/* av_store( a2, elems, newSVsv( (SV*)*av_fetch(a, elems, 0) ) ); */
/* looking and trying to preserve IV is actually slower when copying */
/* temp = (SV*)*av_fetch(a, elems, 0);
if (SvIOK(temp))
{
av_store( a2, elems, newSViv( SvIV( (SV*)*av_fetch(a, elems, 0) )));
}
else
{
av_store( a2, elems, newSVnv( SvNV( (SV*)*av_fetch(a, elems, 0) )));
}
*/
av_store( a2, elems, newSVnv( SvNV( (SV*)*av_fetch(a, elems, 0) )));
elems--;
}
ST(0) = sv_2mortal( newRV_inc((SV*) a2) );
##############################################################################
# __strip_zeros (also check for empty arrays from div)
void
__strip_zeros(x)
SV* x
INIT:
AV* a;
SV* temp;
I32 elems;
I32 index;
CODE:
a = (AV*)SvRV(x); /* ref to aray, don't check ref */
elems = av_len(a); /* number of elems in array */
ST(0) = x; /* we return x */
if (elems == -1)
{
av_push (a, newSViv(0)); /* correct empty arrays */
XSRETURN(1);
}
if (elems == 0)
{
XSRETURN(1); /* nothing to do since only one elem */
}
index = elems;
while (index > 0)
{
temp = *av_fetch(a, index, 0); /* fetch ptr to current element */
if (SvNV(temp) != 0)
{
break;
}
index--;
}
if (index < elems)
{
index = elems - index;
while (index-- > 0)
{
av_pop (a);
}
}
XSRETURN(1);
##############################################################################
# decrement (subtract one)
void
_dec(class,x)
SV* x
INIT:
AV* a;
SV* temp;
I32 elems;
I32 index;
NV MAX;
CODE:
a = (AV*)SvRV(x); /* ref to aray, don't check ref */
elems = av_len(a); /* number of elems in array */
ST(0) = x; /* we return x */
MAX = XS_BASE - 1;
index = 0;
while (index <= elems)
{
temp = *av_fetch(a, index, 0); /* fetch ptr to current element */
sv_setnv (temp, SvNV(temp)-1); /* decrement */
if (SvNV(temp) >= 0)
{
break; /* early out */
}
sv_setnv (temp, MAX); /* overflow, so set this to $MAX */
index++;
}
/* do have more than one element? */
/* (more than one because [0] should be kept as single-element) */
if (elems > 0)
{
temp = *av_fetch(a, elems, 0); /* fetch last element */
if (SvIV(temp) == 0) /* did last elem overflow? */
{
av_pop(a); /* yes, so shrink array */
/* aka remove leading zeros */
}
}
XSRETURN(1); /* return x */
##############################################################################
# increment (add one)
void
_inc(class,x)
SV* x
INIT:
AV* a;
SV* temp;
I32 elems;
I32 index;
NV BASE;
CODE:
a = (AV*)SvRV(x); /* ref to aray, don't check ref */
elems = av_len(a); /* number of elems in array */
ST(0) = x; /* we return x */
BASE = XS_BASE;
index = 0;
while (index <= elems)
{
temp = *av_fetch(a, index, 0); /* fetch ptr to current element */
sv_setnv (temp, SvNV(temp)+1);
if (SvNV(temp) < BASE)
{
XSRETURN(1); /* return (early out) */
}
sv_setiv (temp, 0); /* overflow, so set this elem to 0 */
index++;
}
temp = *av_fetch(a, elems, 0); /* fetch last element */
if (SvIV(temp) == 0) /* did last elem overflow? */
{
av_push(a, newSViv(1)); /* yes, so extend array by 1 */
}
XSRETURN(1); /* return x */
##############################################################################
# Make a number (scalar int/float) from a BigInt object
void
_num(class,x)
SV* x
INIT:
AV* a;
NV fac;
SV* temp;
NV num;
I32 elems;
I32 index;
NV BASE;
CODE:
a = (AV*)SvRV(x); /* ref to aray, don't check ref */
elems = av_len(a); /* number of elems in array */
if (elems == 0) /* only one element? */
{
ST(0) = *av_fetch(a, 0, 0); /* fetch first (only) element */
XSRETURN(1); /* return it */
}
fac = 1.0; /* factor */
index = 0;
num = 0.0;
BASE = XS_BASE;
while (index <= elems)
{
temp = *av_fetch(a, index, 0); /* fetch current element */
num += fac * SvNV(temp);
fac *= BASE;
index++;
}
ST(0) = newSVnv(num);
##############################################################################
AV *
_zero(class)
CODE:
CONSTANT_OBJ(0)
OUTPUT:
RETVAL
##############################################################################
AV *
_one(class)
CODE:
CONSTANT_OBJ(1)
OUTPUT:
RETVAL
##############################################################################
AV *
_two(class)
CODE:
CONSTANT_OBJ(2)
OUTPUT:
RETVAL
##############################################################################
AV *
_ten(class)
CODE:
CONSTANT_OBJ(10)
OUTPUT:
RETVAL
##############################################################################
void
_is_even(class, x)
SV* x
INIT:
AV* a;
SV* temp;
CODE:
a = (AV*)SvRV(x); /* ref to aray, don't check ref */
temp = *av_fetch(a, 0, 0); /* fetch first element */
ST(0) = sv_2mortal(boolSV((SvIV(temp) & 1) == 0));
##############################################################################
void
_is_odd(class, x)
SV* x
INIT:
AV* a;
SV* temp;
CODE:
a = (AV*)SvRV(x); /* ref to aray, don't check ref */
temp = *av_fetch(a, 0, 0); /* fetch first element */
ST(0) = sv_2mortal(boolSV((SvIV(temp) & 1) != 0));
##############################################################################
void
_is_one(class, x)
SV* x
INIT:
AV* a;
SV* temp;
CODE:
a = (AV*)SvRV(x); /* ref to aray, don't check ref */
if ( av_len(a) != 0)
{
ST(0) = &PL_sv_no;
XSRETURN(1); /* len != 1, can't be '1' */
}
temp = *av_fetch(a, 0, 0); /* fetch first element */
RETURN_MORTAL_BOOL(temp, 1);
##############################################################################
void
_is_two(class, x)
SV* x
INIT:
AV* a;
SV* temp;
CODE:
a = (AV*)SvRV(x); /* ref to aray, don't check ref */
if ( av_len(a) != 0)
{
ST(0) = &PL_sv_no;
XSRETURN(1); /* len != 1, can't be '2' */
}
temp = *av_fetch(a, 0, 0); /* fetch first element */
RETURN_MORTAL_BOOL(temp, 2);
##############################################################################
void
_is_ten(class, x)
SV* x
INIT:
AV* a;
SV* temp;
CODE:
a = (AV*)SvRV(x); /* ref to aray, don't check ref */
if ( av_len(a) != 0)
{
ST(0) = &PL_sv_no;
XSRETURN(1); /* len != 1, can't be '10' */
}
temp = *av_fetch(a, 0, 0); /* fetch first element */
RETURN_MORTAL_BOOL(temp, 10);
##############################################################################
void
_is_zero(class, x)
SV* x
INIT:
AV* a;
SV* temp;
CODE:
a = (AV*)SvRV(x); /* ref to aray, don't check ref */
if ( av_len(a) != 0)
{
ST(0) = &PL_sv_no;
XSRETURN(1); /* len != 1, can't be '0' */
}
temp = *av_fetch(a, 0, 0); /* fetch first element */
RETURN_MORTAL_BOOL(temp, 0);
##############################################################################
void
_len(class,x)
SV* x
INIT:
AV* a;
SV* temp;
IV elems;
STRLEN len;
CODE:
a = (AV*)SvRV(x); /* ref to aray, don't check ref */
elems = av_len(a); /* number of elems in array */
temp = *av_fetch(a, elems, 0); /* fetch last element */
SvPV(temp, len); /* convert to string & store length */
len += (IV) XS_BASE_LEN * elems;
ST(0) = sv_2mortal(newSViv(len));
##############################################################################
void
_acmp(class, cx, cy);
SV* cx
SV* cy
INIT:
AV* array_x;
AV* array_y;
I32 elemsx, elemsy, diff;
SV* tempx;
SV* tempy;
STRLEN lenx;
STRLEN leny;
NV diff_nv;
I32 diff_str;
CODE:
array_x = (AV*)SvRV(cx); /* ref to aray, don't check ref */
array_y = (AV*)SvRV(cy); /* ref to aray, don't check ref */
elemsx = av_len(array_x);
elemsy = av_len(array_y);
diff = elemsx - elemsy; /* difference */
if (diff > 0)
{
RETURN_MORTAL_INT(1); /* len differs: X > Y */
}
else if (diff < 0)
{
RETURN_MORTAL_INT(-1); /* len differs: X < Y */
}
/* both have same number of elements, so check length of last element
and see if it differes */
tempx = *av_fetch(array_x, elemsx, 0); /* fetch last element */
tempy = *av_fetch(array_y, elemsx, 0); /* fetch last element */
SvPV(tempx, lenx); /* convert to string & store length */
SvPV(tempy, leny); /* convert to string & store length */
diff_str = (I32)lenx - (I32)leny;
if (diff_str > 0)
{
RETURN_MORTAL_INT(1); /* same len, but first elems differs in len */
}
if (diff_str < 0)
{
RETURN_MORTAL_INT(-1); /* same len, but first elems differs in len */
}
/* same number of digits, so need to make a full compare */
diff_nv = 0;
while (elemsx >= 0)
{
tempx = *av_fetch(array_x, elemsx, 0); /* fetch curr x element */
tempy = *av_fetch(array_y, elemsx, 0); /* fetch curr y element */
diff_nv = SvNV(tempx) - SvNV(tempy);
if (diff_nv != 0)
{
break;
}
elemsx--;
}
if (diff_nv > 0)
{
RETURN_MORTAL_INT(1);
}
if (diff_nv < 0)
{
RETURN_MORTAL_INT(-1);
}
ST(0) = sv_2mortal(newSViv(0)); /* X and Y are equal */
|