| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef VMBIGNUM_H |
| 21 |
#define VMBIGNUM_H |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include "os.h" |
| 25 |
#include "vmtype.h" |
| 26 |
#include "vmobj.h" |
| 27 |
#include "vmglob.h" |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
struct CVmBigNumCacheReg |
| 40 |
{ |
| 41 |
|
| 42 |
void clear() |
| 43 |
{ |
| 44 |
buf_ = 0; |
| 45 |
siz_ = 0; |
| 46 |
nxt_ = 0; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
int alloc_mem(size_t siz) |
| 55 |
{ |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
if (siz_ >= siz) |
| 61 |
return FALSE; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
siz = (siz + 63) & ~63; |
| 73 |
|
| 74 |
|
| 75 |
free_mem(); |
| 76 |
|
| 77 |
|
| 78 |
siz_ = siz; |
| 79 |
|
| 80 |
|
| 81 |
buf_ = (char *)t3malloc(siz_); |
| 82 |
|
| 83 |
|
| 84 |
return TRUE; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
void free_mem() |
| 89 |
{ |
| 90 |
|
| 91 |
if (buf_ != 0) |
| 92 |
t3free(buf_); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
char *buf_; |
| 97 |
|
| 98 |
|
| 99 |
size_t siz_; |
| 100 |
|
| 101 |
|
| 102 |
CVmBigNumCacheReg *nxt_; |
| 103 |
}; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
class CVmBigNumCache |
| 109 |
{ |
| 110 |
public: |
| 111 |
CVmBigNumCache(size_t max_regs); |
| 112 |
~CVmBigNumCache(); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
char *alloc_reg(size_t siz, uint *hdl); |
| 121 |
|
| 122 |
|
| 123 |
void release_reg(uint hdl); |
| 124 |
|
| 125 |
|
| 126 |
void release_all(); |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
char *get_ln10_reg(size_t siz, int *expanded) |
| 137 |
{ return alloc_reg(&ln10_, siz, expanded); } |
| 138 |
|
| 139 |
|
| 140 |
char *get_ln2_reg(size_t siz, int *expanded) |
| 141 |
{ return alloc_reg(&ln2_, siz, expanded); } |
| 142 |
|
| 143 |
|
| 144 |
char *get_pi_reg(size_t siz, int *expanded) |
| 145 |
{ return alloc_reg(&pi_, siz, expanded); } |
| 146 |
|
| 147 |
|
| 148 |
char *get_e_reg(size_t siz, int *expanded) |
| 149 |
{ return alloc_reg(&e_, siz, expanded); } |
| 150 |
|
| 151 |
|
| 152 |
char *get_dbl_max_reg(size_t siz, int *expanded) |
| 153 |
{ return alloc_reg(&dbl_max_, siz, expanded); } |
| 154 |
|
| 155 |
|
| 156 |
char *get_dbl_min_reg(size_t siz, int *expanded) |
| 157 |
{ return alloc_reg(&dbl_min_, siz, expanded); } |
| 158 |
|
| 159 |
private: |
| 160 |
|
| 161 |
char *alloc_reg(CVmBigNumCacheReg *reg, size_t siz, int *expanded) |
| 162 |
{ |
| 163 |
|
| 164 |
*expanded = reg->alloc_mem(siz); |
| 165 |
|
| 166 |
|
| 167 |
return reg->buf_; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
CVmBigNumCacheReg *reg_; |
| 172 |
|
| 173 |
|
| 174 |
CVmBigNumCacheReg *free_reg_; |
| 175 |
|
| 176 |
|
| 177 |
CVmBigNumCacheReg *unalloc_reg_; |
| 178 |
|
| 179 |
|
| 180 |
CVmBigNumCacheReg ln10_; |
| 181 |
|
| 182 |
|
| 183 |
CVmBigNumCacheReg ln2_; |
| 184 |
|
| 185 |
|
| 186 |
CVmBigNumCacheReg pi_; |
| 187 |
|
| 188 |
|
| 189 |
CVmBigNumCacheReg e_; |
| 190 |
|
| 191 |
|
| 192 |
CVmBigNumCacheReg dbl_max_; |
| 193 |
CVmBigNumCacheReg dbl_min_; |
| 194 |
|
| 195 |
|
| 196 |
size_t max_regs_; |
| 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 |
#define VMBN_PREC 0 |
| 243 |
#define VMBN_EXP 2 |
| 244 |
#define VMBN_FLAGS 4 |
| 245 |
#define VMBN_MANT 5 |
| 246 |
|
| 247 |
|
| 248 |
#define VMBN_F_NEG 0x0001 |
| 249 |
#define VMBN_F_TYPE_MASK 0x0006 |
| 250 |
#define VMBN_F_ZERO 0x0008 |
| 251 |
|
| 252 |
|
| 253 |
#define VMBN_T_NUM 0x0000 |
| 254 |
#define VMBN_T_NAN 0x0002 |
| 255 |
#define VMBN_T_INF 0x0004 |
| 256 |
#define VMBN_T_RSRVD 0x0006 |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
#define VMBN_FORMAT_SIGN 0x0001 |
| 265 |
|
| 266 |
|
| 267 |
#define VMBN_FORMAT_EXP 0x0002 |
| 268 |
|
| 269 |
|
| 270 |
#define VMBN_FORMAT_EXP_SIGN 0x0004 |
| 271 |
|
| 272 |
|
| 273 |
#define VMBN_FORMAT_LEADING_ZERO 0x0008 |
| 274 |
|
| 275 |
|
| 276 |
#define VMBN_FORMAT_POINT 0x0010 |
| 277 |
|
| 278 |
|
| 279 |
#define VMBN_FORMAT_EXP_CAP 0x0020 |
| 280 |
|
| 281 |
|
| 282 |
#define VMBN_FORMAT_COMMAS 0x0020 |
| 283 |
|
| 284 |
|
| 285 |
#define VMBN_FORMAT_POS_SPACE 0x0040 |
| 286 |
|
| 287 |
|
| 288 |
#define VMBN_FORMAT_EUROSTYLE 0x0080 |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
#define VMBN_FORMAT_COMPACT 0x0100 |
| 297 |
|
| 298 |
|
| 299 |
#define VMBN_FORMAT_MAXSIG 0x0200 |
| 300 |
|
| 301 |
|
| 302 |
#define VMBN_FORMAT_TRAILING_ZEROS 0x0400 |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
enum vmobjbn_meta_fnset |
| 310 |
{ |
| 311 |
|
| 312 |
VMOBJBN_UNDEF = 0, |
| 313 |
|
| 314 |
|
| 315 |
VMOBJBN_FORMAT = 1, |
| 316 |
|
| 317 |
|
| 318 |
VMOBJBN_EQUAL_RND = 2, |
| 319 |
|
| 320 |
|
| 321 |
VMOBJBN_GET_PREC = 3, |
| 322 |
|
| 323 |
|
| 324 |
VMOBJBN_SET_PREC = 4, |
| 325 |
|
| 326 |
|
| 327 |
VMOBJBN_FRAC = 5, |
| 328 |
|
| 329 |
|
| 330 |
VMOBJBN_WHOLE = 6, |
| 331 |
|
| 332 |
|
| 333 |
VMOBJBN_ROUND_DEC = 7, |
| 334 |
|
| 335 |
|
| 336 |
VMOBJBN_ABS = 8, |
| 337 |
|
| 338 |
|
| 339 |
VMOBJBN_CEIL = 9, |
| 340 |
|
| 341 |
|
| 342 |
VMOBJBN_FLOOR = 10, |
| 343 |
|
| 344 |
|
| 345 |
VMOBJBN_GETSCALE = 11, |
| 346 |
|
| 347 |
|
| 348 |
VMOBJBN_SCALE = 12, |
| 349 |
|
| 350 |
|
| 351 |
VMOBJBN_NEGATE = 13, |
| 352 |
|
| 353 |
|
| 354 |
VMOBJBN_COPYSIGN = 14, |
| 355 |
|
| 356 |
|
| 357 |
VMOBJBN_ISNEG = 15, |
| 358 |
|
| 359 |
|
| 360 |
VMOBJBN_REMAINDER = 16, |
| 361 |
|
| 362 |
|
| 363 |
VMOBJBN_SIN = 17, |
| 364 |
|
| 365 |
|
| 366 |
VMOBJBN_COS = 18, |
| 367 |
|
| 368 |
|
| 369 |
VMOBJBN_TAN = 19, |
| 370 |
|
| 371 |
|
| 372 |
VMOBJBN_D2R = 20, |
| 373 |
|
| 374 |
|
| 375 |
VMOBJBN_R2D = 21, |
| 376 |
|
| 377 |
|
| 378 |
VMOBJBN_ASIN = 22, |
| 379 |
|
| 380 |
|
| 381 |
VMOBJBN_ACOS = 23, |
| 382 |
|
| 383 |
|
| 384 |
VMOBJBN_ATAN = 24, |
| 385 |
|
| 386 |
|
| 387 |
VMOBJBN_SQRT = 25, |
| 388 |
|
| 389 |
|
| 390 |
VMOBJBN_LN = 26, |
| 391 |
|
| 392 |
|
| 393 |
VMOBJBN_EXP = 27, |
| 394 |
|
| 395 |
|
| 396 |
VMOBJBN_LOG10 = 28, |
| 397 |
|
| 398 |
|
| 399 |
VMOBJBN_POW = 29, |
| 400 |
|
| 401 |
|
| 402 |
VMOBJBN_SINH = 30, |
| 403 |
|
| 404 |
|
| 405 |
VMOBJBN_COSH = 31, |
| 406 |
|
| 407 |
|
| 408 |
VMOBJBN_TANH = 32, |
| 409 |
|
| 410 |
|
| 411 |
VMOBJBN_GET_PI = 33, |
| 412 |
|
| 413 |
|
| 414 |
VMOBJBN_GET_E = 34, |
| 415 |
|
| 416 |
|
| 417 |
VMOBJN_numType = 35 |
| 418 |
}; |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
class IBigNumStringBuf |
| 426 |
{ |
| 427 |
public: |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
virtual char *get_buf(size_t need) = 0; |
| 438 |
}; |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
class CBigNumStringBufFixed: public IBigNumStringBuf |
| 444 |
{ |
| 445 |
public: |
| 446 |
CBigNumStringBufFixed(char *buf, size_t len) : buf(buf), len(len) { } |
| 447 |
char *get_buf(size_t need) { return len >= need ? buf : 0; } |
| 448 |
|
| 449 |
char *buf; |
| 450 |
size_t len; |
| 451 |
}; |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
class CBigNumStringBufAlo: public CBigNumStringBufFixed |
| 461 |
{ |
| 462 |
public: |
| 463 |
|
| 464 |
CBigNumStringBufAlo(VMG_ vm_val_t *strval) |
| 465 |
: CBigNumStringBufFixed(0, 0), strval(strval) |
| 466 |
{ |
| 467 |
vmg = VMGLOB_ADDR; |
| 468 |
if (strval != 0) |
| 469 |
strval->set_nil(); |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
CBigNumStringBufAlo(VMG_ vm_val_t *strval, char *buf, size_t len) |
| 478 |
: CBigNumStringBufFixed(buf, len), strval(strval) |
| 479 |
{ |
| 480 |
vmg = VMGLOB_ADDR; |
| 481 |
if (strval != 0) |
| 482 |
strval->set_nil(); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
char *get_buf(size_t need); |
| 487 |
|
| 488 |
|
| 489 |
vm_val_t *strval; |
| 490 |
|
| 491 |
|
| 492 |
vm_globals *vmg; |
| 493 |
}; |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
template <int prec> class bignum_t; |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
class CVmObjBigNum: public CVmObject |
| 508 |
{ |
| 509 |
friend class CVmMetaclassBigNum; |
| 510 |
template <int> friend class bignum_t; |
| 511 |
friend class vbignum_t; |
| 512 |
|
| 513 |
public: |
| 514 |
|
| 515 |
static class CVmMetaclass *metaclass_reg_; |
| 516 |
class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; } |
| 517 |
|
| 518 |
|
| 519 |
virtual int is_of_metaclass(class CVmMetaclass *meta) const |
| 520 |
{ |
| 521 |
|
| 522 |
return (meta == metaclass_reg_ |
| 523 |
|| CVmObject::is_of_metaclass(meta)); |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
static void init_cache(); |
| 528 |
static void term_cache(); |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
int write_to_data_file(class CVmDataSource *fp); |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
static int read_from_data_file( |
| 542 |
VMG_ vm_val_t *retval, class CVmDataSource *fp); |
| 543 |
|
| 544 |
|
| 545 |
static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, |
| 546 |
uint argc); |
| 547 |
|
| 548 |
|
| 549 |
static int call_stat_prop(VMG_ vm_val_t *result, |
| 550 |
const uchar **pc_ptr, uint *argc, |
| 551 |
vm_prop_id_t prop); |
| 552 |
|
| 553 |
|
| 554 |
virtual void reserve_const_data(VMG_ class CVmConstMapper *mapper, |
| 555 |
vm_obj_id_t self); |
| 556 |
|
| 557 |
|
| 558 |
virtual void convert_to_const_data(VMG_ class CVmConstMapper *mapper, |
| 559 |
vm_obj_id_t self); |
| 560 |
|
| 561 |
|
| 562 |
static vm_obj_id_t create(VMG_ int in_root_set, size_t digits); |
| 563 |
|
| 564 |
|
| 565 |
static vm_obj_id_t create(VMG_ int in_root_set, long val, size_t digits); |
| 566 |
|
| 567 |
|
| 568 |
static vm_obj_id_t createu(VMG_ int in_root_set, ulong val, size_t digits); |
| 569 |
|
| 570 |
|
| 571 |
static vm_obj_id_t create(VMG_ int in_root_set, |
| 572 |
const char *str, size_t len, size_t digits); |
| 573 |
|
| 574 |
|
| 575 |
static vm_obj_id_t create(VMG_ int in_root_set, double val); |
| 576 |
|
| 577 |
|
| 578 |
static vm_obj_id_t create(VMG_ int in_root_set, double val, size_t digits); |
| 579 |
|
| 580 |
|
| 581 |
template <int prec> static vm_obj_id_t create( |
| 582 |
VMG_ int in_root_set, const bignum_t<prec> *b) |
| 583 |
{ |
| 584 |
vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE); |
| 585 |
new (vmg_ id) CVmObjBigNum(vmg_ prec); |
| 586 |
CVmObjBigNum *n = (CVmObjBigNum *)vm_objp(vmg_ id); |
| 587 |
copy_val(n->ext_, b->ext, FALSE); |
| 588 |
return id; |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
template <int prec> static vm_obj_id_t create( |
| 593 |
VMG_ int in_root_set, const bignum_t<prec> &b) |
| 594 |
{ |
| 595 |
vm_obj_id_t id = vm_new_id(vmg_ in_root_set, FALSE, FALSE); |
| 596 |
new (vmg_ id) CVmObjBigNum(vmg_ prec); |
| 597 |
CVmObjBigNum *n = (CVmObjBigNum *)vm_objp(vmg_ id); |
| 598 |
copy_val(n->ext_, b.ext, FALSE); |
| 599 |
return id; |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
static vm_obj_id_t create_rp8(VMG_ int in_root_set, const char *buf); |
| 611 |
static vm_obj_id_t create_rp8s(VMG_ int in_root_set, const char *buf); |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
static vm_obj_id_t create_int64(VMG_ int in_root_set, |
| 626 |
uint32_t hi, uint32_t lo); |
| 627 |
|
| 628 |
|
| 629 |
static vm_obj_id_t create_from_ber(VMG_ int in_root_set, |
| 630 |
const char *buf, size_t len); |
| 631 |
|
| 632 |
|
| 633 |
static vm_obj_id_t create_from_ieee754( |
| 634 |
VMG_ int in_root_set, const char *buf, int bits); |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
static vm_obj_id_t create(VMG_ int in_root_set, |
| 641 |
const char *str, size_t len); |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
static vm_obj_id_t create_radix(VMG_ int in_root_set, |
| 648 |
const char *str, size_t len, int radix); |
| 649 |
|
| 650 |
|
| 651 |
static int is_bignum_obj(VMG_ vm_obj_id_t obj) |
| 652 |
{ return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); } |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
static void cast_to_bignum(VMG_ vm_val_t *bnval, const vm_val_t *srcval); |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
static void parse_str_into(char *ext, const char *str, size_t len); |
| 667 |
static void parse_str_into(char *ext, const char *str, size_t len, |
| 668 |
int radix); |
| 669 |
|
| 670 |
|
| 671 |
static char *parse_str_alo(size_t &buflen, |
| 672 |
const char *str, size_t len, int radix); |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
static int precision_from_string(const char *str, size_t len); |
| 680 |
static int precision_from_string(const char *str, size_t len, int radix); |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
static int radix_from_string(const char *str, size_t len); |
| 689 |
|
| 690 |
|
| 691 |
void notify_delete(VMG_ int in_root_set); |
| 692 |
|
| 693 |
|
| 694 |
void set_prop(VMG_ class CVmUndo *undo, |
| 695 |
vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val); |
| 696 |
|
| 697 |
|
| 698 |
int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val, |
| 699 |
vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc); |
| 700 |
|
| 701 |
|
| 702 |
void notify_new_savept() { } |
| 703 |
void apply_undo(VMG_ struct CVmUndoRecord *) { } |
| 704 |
void mark_undo_ref(VMG_ struct CVmUndoRecord *) { } |
| 705 |
void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { } |
| 706 |
|
| 707 |
|
| 708 |
void mark_refs(VMG_ uint) { } |
| 709 |
|
| 710 |
|
| 711 |
void remove_stale_weak_refs(VMG0_) { } |
| 712 |
|
| 713 |
|
| 714 |
void load_from_image(VMG_ vm_obj_id_t, const char *ptr, size_t) |
| 715 |
{ ext_ = (char *)ptr; } |
| 716 |
|
| 717 |
|
| 718 |
virtual ulong rebuild_image(VMG_ char *buf, ulong buflen); |
| 719 |
|
| 720 |
|
| 721 |
void save_to_file(VMG_ class CVmFile *fp); |
| 722 |
|
| 723 |
|
| 724 |
void restore_from_file(VMG_ vm_obj_id_t self, |
| 725 |
class CVmFile *fp, class CVmObjFixup *fixup); |
| 726 |
|
| 727 |
|
| 728 |
int add_val(VMG_ vm_val_t *result, |
| 729 |
vm_obj_id_t self, const vm_val_t *val); |
| 730 |
|
| 731 |
|
| 732 |
int sub_val(VMG_ vm_val_t *result, |
| 733 |
vm_obj_id_t self, const vm_val_t *val); |
| 734 |
|
| 735 |
|
| 736 |
int mul_val(VMG_ vm_val_t *result, |
| 737 |
vm_obj_id_t self, const vm_val_t *val); |
| 738 |
|
| 739 |
|
| 740 |
int div_val(VMG_ vm_val_t *result, |
| 741 |
vm_obj_id_t self, const vm_val_t *val); |
| 742 |
|
| 743 |
|
| 744 |
int neg_val(VMG_ vm_val_t *result, vm_obj_id_t self); |
| 745 |
|
| 746 |
|
| 747 |
int abs_val(VMG_ vm_val_t *result, vm_obj_id_t self); |
| 748 |
|
| 749 |
|
| 750 |
int sgn_val(VMG_ vm_val_t *result, vm_obj_id_t self); |
| 751 |
|
| 752 |
|
| 753 |
int equals(VMG_ vm_obj_id_t self, const vm_val_t *val, int depth) const; |
| 754 |
|
| 755 |
|
| 756 |
uint calc_hash(VMG_ vm_obj_id_t self, int depth) const; |
| 757 |
|
| 758 |
|
| 759 |
int compare_to(VMG_ vm_obj_id_t , const vm_val_t *) const; |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
virtual const char *cast_to_string(VMG_ vm_obj_id_t self, |
| 770 |
vm_val_t *new_str) const; |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
virtual const char *explicit_to_string( |
| 776 |
VMG_ vm_obj_id_t self, vm_val_t *new_str, int radix, int flags) const; |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
static const char *cvt_to_string( |
| 832 |
VMG_ vm_obj_id_t self, vm_val_t *new_str, const char *ext, |
| 833 |
int max_digits, int whole_places, int frac_digits, int exp_digits, |
| 834 |
ulong flags, vm_val_t *lead_fill); |
| 835 |
|
| 836 |
|
| 837 |
const char *cvt_to_string_buf( |
| 838 |
char *buf, size_t buflen, int max_digits, |
| 839 |
int whole_places, int frac_digits, int exp_digits, ulong flags); |
| 840 |
|
| 841 |
|
| 842 |
static const char *cvt_to_string_buf( |
| 843 |
char *buf, size_t buflen, const char *ext, |
| 844 |
int max_digits, int whole_places, int frac_digits, int exp_digits, |
| 845 |
ulong flags, const char *lead_fill, size_t lead_fill_len); |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
const char *cvt_to_string_buf( |
| 852 |
VMG_ vm_val_t *new_str, char *buf, size_t buflen, int max_digits, |
| 853 |
int whole_places, int frac_digits, int exp_digits, ulong flags); |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
static const char *cvt_int_to_string_buf( |
| 861 |
char *buf, size_t buflen, int32_t intval, |
| 862 |
int max_digits, int whole_places, int frac_digits, int exp_digits, |
| 863 |
ulong flags); |
| 864 |
|
| 865 |
|
| 866 |
const char *cvt_to_string_in_radix( |
| 867 |
VMG_ vm_obj_id_t self, vm_val_t *new_str, int radix) const; |
| 868 |
|
| 869 |
|
| 870 |
static void compute_frac(char *ext); |
| 871 |
static void compute_whole(char *ext); |
| 872 |
|
| 873 |
|
| 874 |
static void compute_sum(VMG_ vm_val_t *result, |
| 875 |
const char *ext1, const char *ext2); |
| 876 |
|
| 877 |
|
| 878 |
static void compute_diff(VMG_ vm_val_t *result, |
| 879 |
const char *ext1, const char *ext2); |
| 880 |
|
| 881 |
|
| 882 |
static void compute_prod(VMG_ vm_val_t *result, |
| 883 |
const char *ext1, const char *ext2); |
| 884 |
|
| 885 |
|
| 886 |
static void compute_quotient(VMG_ vm_val_t *result, |
| 887 |
const char *ext1, const char *ext2); |
| 888 |
|
| 889 |
|
| 890 |
static void compute_rem(VMG_ vm_val_t *result, |
| 891 |
const char *ext1, const char *ext2); |
| 892 |
|
| 893 |
|
| 894 |
static void compute_ln_into(char *dst, const char *src); |
| 895 |
|
| 896 |
|
| 897 |
static void compute_exp_into(char *dst, const char *src); |
| 898 |
|
| 899 |
|
| 900 |
static void compute_sinhcosh_into(char *dst, const char *src, |
| 901 |
int is_cosh, int is_tanh); |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
static int compute_eq_exact(const char *ext1, const char *ext2); |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
static int compute_eq_round(VMG_ const char *ext1, const char *ext2); |
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
static const char *round_val(VMG_ vm_val_t *new_val, const char *ext, |
| 926 |
size_t digits, int always_create); |
| 927 |
|
| 928 |
|
| 929 |
static void set_int_val(char *ext, long val); |
| 930 |
static void set_uint_val(char *ext, ulong val); |
| 931 |
|
| 932 |
|
| 933 |
void set_rp8(const uchar *p); |
| 934 |
|
| 935 |
|
| 936 |
static void set_double_val(char *ext, double val); |
| 937 |
|
| 938 |
|
| 939 |
void set_str_val(const char *str, size_t len); |
| 940 |
void set_str_val(const char *str, size_t len, int radix); |
| 941 |
|
| 942 |
|
| 943 |
char *get_ext() const { return ext_; } |
| 944 |
|
| 945 |
|
| 946 |
virtual int is_numeric() const { return TRUE; } |
| 947 |
|
| 948 |
|
| 949 |
virtual long cast_to_int(VMG0_) const |
| 950 |
{ |
| 951 |
|
| 952 |
return convert_to_int(); |
| 953 |
} |
| 954 |
|
| 955 |
|
| 956 |
virtual int get_as_int(long *val) const |
| 957 |
{ |
| 958 |
|
| 959 |
*val = convert_to_int(); |
| 960 |
return TRUE; |
| 961 |
} |
| 962 |
|
| 963 |
|
| 964 |
virtual int get_as_double(VMG_ double *val) const |
| 965 |
{ |
| 966 |
*val = convert_to_double(); |
| 967 |
return TRUE; |
| 968 |
} |
| 969 |
|
| 970 |
|
| 971 |
virtual void promote_int(VMG_ vm_val_t *val) const; |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
int32_t convert_to_int(int &ov) const { return ext_to_int(ext_, ov); } |
| 978 |
uint32_t convert_to_uint(int &ov) const; |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
static int32_t ext_to_int(const char *ext, int &ov); |
| 985 |
|
| 986 |
|
| 987 |
int32_t convert_to_int() const |
| 988 |
{ |
| 989 |
int ov; |
| 990 |
int32_t l = convert_to_int(ov); |
| 991 |
if (ov) |
| 992 |
err_throw(VMERR_NUM_OVERFLOW); |
| 993 |
return l; |
| 994 |
} |
| 995 |
uint32_t convert_to_uint() const |
| 996 |
{ |
| 997 |
int ov; |
| 998 |
uint32_t l = convert_to_uint(ov); |
| 999 |
if (ov) |
| 1000 |
err_throw(VMERR_NUM_OVERFLOW); |
| 1001 |
return l; |
| 1002 |
} |
| 1003 |
|
| 1004 |
|
| 1005 |
virtual void cast_to_num(VMG_ vm_val_t *val, vm_obj_id_t self) const |
| 1006 |
{ |
| 1007 |
|
| 1008 |
val->set_obj(self); |
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
double convert_to_double() const { return ext_to_double(ext_); } |
| 1013 |
static double ext_to_double(const char *ext); |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
void convert_to_ieee754(VMG_ char *buf, int bits, int &ov); |
| 1029 |
|
| 1030 |
|
| 1031 |
void set_ieee754_value(VMG_ const char *buf, int bits); |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
void wp8(char *buf, int &ov) const; |
| 1053 |
void wp8s(char *buf, int &ov) const; |
| 1054 |
|
| 1055 |
|
| 1056 |
void encode_ber(char *buf, size_t buflen, size_t &result_len, int &ov); |
| 1057 |
|
| 1058 |
protected: |
| 1059 |
|
| 1060 |
CVmObjBigNum(); |
| 1061 |
|
| 1062 |
|
| 1063 |
CVmObjBigNum(VMG_ size_t digits); |
| 1064 |
|
| 1065 |
|
| 1066 |
CVmObjBigNum(VMG_ long val, size_t digits); |
| 1067 |
|
| 1068 |
|
| 1069 |
CVmObjBigNum(VMG_ const char *str, size_t len, size_t digits); |
| 1070 |
|
| 1071 |
|
| 1072 |
CVmObjBigNum(VMG_ double val, size_t digits); |
| 1073 |
|
| 1074 |
|
| 1075 |
int cvt_to_bignum(VMG_ vm_obj_id_t self, vm_val_t *val) const; |
| 1076 |
|
| 1077 |
|
| 1078 |
static uint32_t convert_to_int_base(const char *ext, int &ov); |
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
static const char *cvt_to_string_gen( |
| 1084 |
IBigNumStringBuf *buf, const char *ext, |
| 1085 |
int max_digits, int whole_places, int frac_digits, int exp_digits, |
| 1086 |
ulong flags, const char *lead_fill, size_t lead_fill_len); |
| 1087 |
|
| 1088 |
|
| 1089 |
int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; } |
| 1090 |
|
| 1091 |
|
| 1092 |
int getp_format(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1093 |
|
| 1094 |
|
| 1095 |
int getp_equal_rnd(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1096 |
|
| 1097 |
|
| 1098 |
int getp_get_prec(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1099 |
|
| 1100 |
|
| 1101 |
int getp_set_prec(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1102 |
|
| 1103 |
|
| 1104 |
int getp_frac(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1105 |
|
| 1106 |
|
| 1107 |
int getp_whole(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1108 |
|
| 1109 |
|
| 1110 |
int getp_round_dec(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1111 |
|
| 1112 |
|
| 1113 |
int getp_abs(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1114 |
|
| 1115 |
|
| 1116 |
int getp_ceil(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1117 |
|
| 1118 |
|
| 1119 |
int getp_floor(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1120 |
|
| 1121 |
|
| 1122 |
int getp_get_scale(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1123 |
|
| 1124 |
|
| 1125 |
int getp_scale(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1126 |
|
| 1127 |
|
| 1128 |
int getp_negate(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1129 |
|
| 1130 |
|
| 1131 |
int getp_copy_sign(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1132 |
|
| 1133 |
|
| 1134 |
int getp_is_neg(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1135 |
|
| 1136 |
|
| 1137 |
int getp_remainder(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1138 |
|
| 1139 |
|
| 1140 |
int getp_sin(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1141 |
|
| 1142 |
|
| 1143 |
int getp_cos(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1144 |
|
| 1145 |
|
| 1146 |
int getp_tan(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1147 |
|
| 1148 |
|
| 1149 |
int getp_rad2deg(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1150 |
|
| 1151 |
|
| 1152 |
int getp_deg2rad(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1153 |
|
| 1154 |
|
| 1155 |
int getp_asin(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1156 |
|
| 1157 |
|
| 1158 |
int getp_acos(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1159 |
|
| 1160 |
|
| 1161 |
int getp_atan(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1162 |
|
| 1163 |
|
| 1164 |
int getp_sqrt(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1165 |
|
| 1166 |
|
| 1167 |
int getp_ln(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1168 |
|
| 1169 |
|
| 1170 |
int getp_exp(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1171 |
|
| 1172 |
|
| 1173 |
int getp_log10(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1174 |
|
| 1175 |
|
| 1176 |
int getp_pow(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1177 |
|
| 1178 |
|
| 1179 |
int getp_sinh(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1180 |
|
| 1181 |
|
| 1182 |
int getp_cosh(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1183 |
|
| 1184 |
|
| 1185 |
int getp_tanh(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 1186 |
|
| 1187 |
|
| 1188 |
int getp_pi(VMG_ vm_obj_id_t, vm_val_t *val, uint *argc) |
| 1189 |
{ return s_getp_pi(vmg_ val, argc); } |
| 1190 |
|
| 1191 |
|
| 1192 |
int getp_e(VMG_ vm_obj_id_t, vm_val_t *val, uint *argc) |
| 1193 |
{ return s_getp_e(vmg_ val, argc); } |
| 1194 |
|
| 1195 |
|
| 1196 |
int getp_numType(VMG_ vm_obj_id_t, vm_val_t *val, uint *argc); |
| 1197 |
|
| 1198 |
|
| 1199 |
static int s_getp_pi(VMG_ vm_val_t *val, uint *argc); |
| 1200 |
|
| 1201 |
|
| 1202 |
static int s_getp_e(VMG_ vm_val_t *val, uint *argc); |
| 1203 |
|
| 1204 |
|
| 1205 |
int setup_getp_0(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 1206 |
uint *argc, char **new_ext); |
| 1207 |
|
| 1208 |
|
| 1209 |
int setup_getp_1(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 1210 |
uint *argc, char **new_ext, |
| 1211 |
vm_val_t *val2, const char **val2_ext, |
| 1212 |
int use_self_prec); |
| 1213 |
|
| 1214 |
|
| 1215 |
int setup_getp_retval(VMG_ vm_obj_id_t self, vm_val_t *retval, |
| 1216 |
char **new_ext, size_t prec); |
| 1217 |
|
| 1218 |
|
| 1219 |
int calc_asincos(VMG_ vm_obj_id_t self, |
| 1220 |
vm_val_t *retval, uint *argc, int is_acos); |
| 1221 |
|
| 1222 |
|
| 1223 |
int calc_sinhcosh(VMG_ vm_obj_id_t self, |
| 1224 |
vm_val_t *retval, uint *argc, |
| 1225 |
int is_cosh, int is_tanh); |
| 1226 |
|
| 1227 |
|
| 1228 |
static void calc_asincos_into(char *new_ext, const char *ext, |
| 1229 |
int is_acos); |
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
static char *calc_asin_series(char *ext1, char *ext2, |
| 1237 |
char *ext3, char *ext4, char *ext5); |
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
static char *compute_ln_series_into(char *ext1, char *ext2, |
| 1245 |
char *ext3, char *ext4, char *ext5); |
| 1246 |
|
| 1247 |
|
| 1248 |
void alloc_bignum(VMG_ size_t prec); |
| 1249 |
|
| 1250 |
|
| 1251 |
static size_t calc_alloc(size_t prec) |
| 1252 |
{ return (2 + 2 + 1) + ((prec + 1)/2); } |
| 1253 |
|
| 1254 |
|
| 1255 |
static char *compute_init_2op(VMG_ vm_val_t *result, |
| 1256 |
const char *ext1, const char *ext2); |
| 1257 |
|
| 1258 |
|
| 1259 |
static void compute_sqrt_into(char *new_ext, const char *ext); |
| 1260 |
|
| 1261 |
|
| 1262 |
static void compute_sum_into(char *new_ext, |
| 1263 |
const char *ext1, const char *ext2); |
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
static void compute_abs_sum_into(char *new_ext, |
| 1272 |
const char *ext1, const char *ext2); |
| 1273 |
|
| 1274 |
|
| 1275 |
static void compute_diff_into(char *new_ext, |
| 1276 |
const char *ext1, const char *ext2); |
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
static void compute_abs_diff_into(char *new_ext, |
| 1286 |
const char *ext1, const char *ext2); |
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
static void compute_prod_into(char *new_ext, |
| 1294 |
const char *ext1, const char *ext2); |
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
static void compute_quotient_into(char *new_ext, |
| 1301 |
char *new_rem_ext, |
| 1302 |
const char *ext1, const char *ext2); |
| 1303 |
|
| 1304 |
|
| 1305 |
static int compare_ext(const char *a, const char *b); |
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
static int compare_abs(const char *ext1, const char *ext2); |
| 1316 |
|
| 1317 |
|
| 1318 |
static size_t get_prec(const char *ext) |
| 1319 |
{ return osrp2(ext + VMBN_PREC); } |
| 1320 |
static void set_prec(char *ext, size_t prec) |
| 1321 |
{ oswp2(ext + VMBN_PREC, prec); } |
| 1322 |
|
| 1323 |
|
| 1324 |
static int get_exp(const char *ext) |
| 1325 |
{ return osrp2s(ext + VMBN_EXP); } |
| 1326 |
static void set_exp(char *ext, int exp) |
| 1327 |
{ oswp2s(ext + VMBN_EXP, exp); } |
| 1328 |
|
| 1329 |
|
| 1330 |
static int get_neg(const char *ext) |
| 1331 |
{ return (ext[VMBN_FLAGS] & VMBN_F_NEG) != 0; } |
| 1332 |
|
| 1333 |
|
| 1334 |
static void set_neg(char *ext, int neg) |
| 1335 |
{ |
| 1336 |
if (neg) |
| 1337 |
ext[VMBN_FLAGS] |= VMBN_F_NEG; |
| 1338 |
else |
| 1339 |
ext[VMBN_FLAGS] &= ~VMBN_F_NEG; |
| 1340 |
} |
| 1341 |
|
| 1342 |
|
| 1343 |
static int get_type(const char *ext) |
| 1344 |
{ return ext[VMBN_FLAGS] & VMBN_F_TYPE_MASK; } |
| 1345 |
|
| 1346 |
|
| 1347 |
static void set_type(char *ext, int typ) |
| 1348 |
{ |
| 1349 |
|
| 1350 |
ext[VMBN_FLAGS] &= ~VMBN_F_TYPE_MASK; |
| 1351 |
|
| 1352 |
|
| 1353 |
ext[VMBN_FLAGS] |= typ; |
| 1354 |
} |
| 1355 |
|
| 1356 |
|
| 1357 |
static unsigned int get_dig(const char *ext, size_t i) |
| 1358 |
{ |
| 1359 |
|
| 1360 |
unsigned int pair = ext[VMBN_MANT + i/2]; |
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
pair >>= ((1 - (i & 1)) << 2); |
| 1375 |
|
| 1376 |
|
| 1377 |
return (pair & 0x0f); |
| 1378 |
} |
| 1379 |
|
| 1380 |
|
| 1381 |
static void set_dig(char *ext, size_t i, unsigned int dig) |
| 1382 |
{ |
| 1383 |
unsigned char mask; |
| 1384 |
|
| 1385 |
|
| 1386 |
dig &= 0x0F; |
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
dig <<= ((1 - (i & 1)) << 2); |
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
mask = (0x0F << ((i & 1) << 2)); |
| 1406 |
|
| 1407 |
|
| 1408 |
ext[VMBN_MANT + i/2] &= mask; |
| 1409 |
|
| 1410 |
|
| 1411 |
ext[VMBN_MANT + i/2] |= (unsigned char)dig; |
| 1412 |
} |
| 1413 |
|
| 1414 |
|
| 1415 |
static void shift_left(char *ext, unsigned int shift); |
| 1416 |
static void shift_right(char *ext, unsigned int shift); |
| 1417 |
|
| 1418 |
|
| 1419 |
static void mul_by_long(char *ext, unsigned long val); |
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
static void div_by_long(char *ext, unsigned long val, |
| 1430 |
unsigned long *remp = 0); |
| 1431 |
|
| 1432 |
|
| 1433 |
static void div_by_2e32(char *ext, uint32_t *remainder); |
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
void wp8abs(char *buf, int &ov) const; |
| 1440 |
|
| 1441 |
|
| 1442 |
static void twos_complement_p8(unsigned char *buf); |
| 1443 |
|
| 1444 |
|
| 1445 |
static void increment_abs(char *ext); |
| 1446 |
|
| 1447 |
|
| 1448 |
static void round_for_dropped_digits( |
| 1449 |
char *ext, int trail_dig, int trail_val); |
| 1450 |
|
| 1451 |
|
| 1452 |
static int get_ORdigs(const char *ext, int d); |
| 1453 |
|
| 1454 |
|
| 1455 |
static void round_to_int(char *ext); |
| 1456 |
|
| 1457 |
|
| 1458 |
static void round_to(char *ext, int digits); |
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
static int get_round_dir(const char *ext, int digits); |
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
static void round_up_abs(char *ext); |
| 1472 |
static void round_up_abs(char *ext, int keep_digits); |
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
static void copy_val(char *dst, const char *src, int round); |
| 1483 |
|
| 1484 |
|
| 1485 |
static void normalize(char *ext); |
| 1486 |
|
| 1487 |
|
| 1488 |
static void set_zero(char *ext) |
| 1489 |
{ |
| 1490 |
|
| 1491 |
set_exp(ext, 1); |
| 1492 |
|
| 1493 |
|
| 1494 |
ext[VMBN_FLAGS] |= VMBN_F_ZERO; |
| 1495 |
|
| 1496 |
|
| 1497 |
set_neg(ext, FALSE); |
| 1498 |
|
| 1499 |
|
| 1500 |
set_type(ext, VMBN_T_NUM); |
| 1501 |
|
| 1502 |
|
| 1503 |
memset(ext + VMBN_MANT, 0, (get_prec(ext) + 1)/2); |
| 1504 |
} |
| 1505 |
|
| 1506 |
|
| 1507 |
static int is_zero(const char *ext) |
| 1508 |
{ return (ext[VMBN_FLAGS] & VMBN_F_ZERO) != 0; } |
| 1509 |
|
| 1510 |
|
| 1511 |
static void negate(char *ext) |
| 1512 |
{ |
| 1513 |
|
| 1514 |
if (!is_zero(ext)) |
| 1515 |
{ |
| 1516 |
|
| 1517 |
set_neg(ext, !get_neg(ext)); |
| 1518 |
} |
| 1519 |
} |
| 1520 |
|
| 1521 |
|
| 1522 |
static void make_negative(char *ext) |
| 1523 |
{ |
| 1524 |
|
| 1525 |
if (!is_zero(ext)) |
| 1526 |
set_neg(ext, TRUE); |
| 1527 |
} |
| 1528 |
|
| 1529 |
|
| 1530 |
static int is_frac_zero(const char *ext); |
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
static int is_nan(const char *ext) |
| 1537 |
{ |
| 1538 |
|
| 1539 |
return (get_type(ext) != VMBN_T_NUM); |
| 1540 |
} |
| 1541 |
|
| 1542 |
|
| 1543 |
static int is_infinity(const char *ext) |
| 1544 |
{ return get_type(ext) == VMBN_T_INF; } |
| 1545 |
|
| 1546 |
|
| 1547 |
void calc_sin_series(VMG_ char *new_ext, char *ext1, char *ext2, |
| 1548 |
char *ext3, char *ext4, char *ext5, |
| 1549 |
char *ext6, char *ext7); |
| 1550 |
|
| 1551 |
|
| 1552 |
void calc_cos_series(VMG_ char *new_ext, char *ext1, char *ext2, |
| 1553 |
char *ext3, char *ext4, char *ext5, |
| 1554 |
char *ext6, char *ext7); |
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
static char *get_objid_ext(VMG_ vm_obj_id_t obj_id) |
| 1561 |
{ |
| 1562 |
|
| 1563 |
return get_objid_obj(vmg_ obj_id)->get_ext(); |
| 1564 |
} |
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
static CVmObjBigNum *get_objid_obj(VMG_ vm_obj_id_t obj_id) |
| 1571 |
{ |
| 1572 |
|
| 1573 |
return (CVmObjBigNum *)vm_objp(vmg_ obj_id); |
| 1574 |
} |
| 1575 |
|
| 1576 |
|
| 1577 |
static char *alloc_temp_reg(size_t prec, uint *hdl); |
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
static void alloc_temp_regs(size_t prec, size_t cnt, ...); |
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
static void release_temp_regs(size_t cnt, ...); |
| 1592 |
|
| 1593 |
|
| 1594 |
static void release_temp_reg(uint hdl); |
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
static const char *cache_ln10(size_t prec); |
| 1602 |
|
| 1603 |
|
| 1604 |
static const char *cache_ln2(size_t prec); |
| 1605 |
|
| 1606 |
|
| 1607 |
static const char *cache_pi(size_t prec); |
| 1608 |
|
| 1609 |
|
| 1610 |
static const char *cache_e(size_t prec); |
| 1611 |
|
| 1612 |
|
| 1613 |
static const char *get_one() { return (const char *)one_; } |
| 1614 |
|
| 1615 |
|
| 1616 |
static const char *cache_dbl_max(); |
| 1617 |
static const char *cache_dbl_min(); |
| 1618 |
|
| 1619 |
|
| 1620 |
static const unsigned char one_[]; |
| 1621 |
|
| 1622 |
|
| 1623 |
static int (CVmObjBigNum::*func_table_[])(VMG_ vm_obj_id_t self, |
| 1624 |
vm_val_t *retval, uint *argc); |
| 1625 |
}; |
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
class CVmMetaclassBigNum: public CVmMetaclass |
| 1632 |
{ |
| 1633 |
public: |
| 1634 |
|
| 1635 |
const char *get_meta_name() const { return "bignumber/030001"; } |
| 1636 |
|
| 1637 |
|
| 1638 |
void create_for_image_load(VMG_ vm_obj_id_t id) |
| 1639 |
{ |
| 1640 |
new (vmg_ id) CVmObjBigNum(); |
| 1641 |
G_obj_table->set_obj_gc_characteristics(id, FALSE, FALSE); |
| 1642 |
} |
| 1643 |
|
| 1644 |
|
| 1645 |
void create_for_restore(VMG_ vm_obj_id_t id) |
| 1646 |
{ |
| 1647 |
new (vmg_ id) CVmObjBigNum(); |
| 1648 |
G_obj_table->set_obj_gc_characteristics(id, FALSE, FALSE); |
| 1649 |
} |
| 1650 |
|
| 1651 |
|
| 1652 |
vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc) |
| 1653 |
{ return CVmObjBigNum::create_from_stack(vmg_ pc_ptr, argc); } |
| 1654 |
|
| 1655 |
|
| 1656 |
int call_stat_prop(VMG_ vm_val_t *result, |
| 1657 |
const uchar **pc_ptr, uint *argc, |
| 1658 |
vm_prop_id_t prop) |
| 1659 |
{ |
| 1660 |
return CVmObjBigNum::call_stat_prop(vmg_ result, pc_ptr, argc, prop); |
| 1661 |
} |
| 1662 |
}; |
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
template <int prec> class bignum_t |
| 1674 |
{ |
| 1675 |
friend class CVmObjBigNum; |
| 1676 |
|
| 1677 |
public: |
| 1678 |
bignum_t() { init(); } |
| 1679 |
bignum_t(long i) { init(); set(i); } |
| 1680 |
bignum_t(double d) { init(); set(d); } |
| 1681 |
bignum_t(VMG_ const vm_val_t *val) { init(); set(vmg_ val); } |
| 1682 |
template <int precb> bignum_t(const bignum_t<precb> b) |
| 1683 |
{ init(); set(b); } |
| 1684 |
template <int precb> bignum_t(const bignum_t<precb> *b) |
| 1685 |
{ init(); set(*b); } |
| 1686 |
|
| 1687 |
|
| 1688 |
void set(long i) { CVmObjBigNum::set_int_val(ext, i); } |
| 1689 |
void set(double d) { CVmObjBigNum::set_double_val(ext, d); } |
| 1690 |
template <int bprec> void set(const bignum_t<bprec> &b) |
| 1691 |
{ CVmObjBigNum::copy_val(ext, b.ext, TRUE); } |
| 1692 |
|
| 1693 |
void set(VMG_ const vm_val_t *val) |
| 1694 |
{ |
| 1695 |
CVmObjBigNum *b; |
| 1696 |
if (val->typ == VM_INT) |
| 1697 |
CVmObjBigNum::set_int_val(ext, val->val.intval); |
| 1698 |
else if ((b = vm_val_cast(CVmObjBigNum, val)) != 0) |
| 1699 |
CVmObjBigNum::copy_val(ext, b->get_ext(), TRUE); |
| 1700 |
else if (val->is_numeric(vmg0_)) |
| 1701 |
CVmObjBigNum::set_double_val(ext, val->num_to_double(vmg0_)); |
| 1702 |
else |
| 1703 |
err_throw(VMERR_NUM_VAL_REQD); |
| 1704 |
} |
| 1705 |
|
| 1706 |
|
| 1707 |
operator int32_t() |
| 1708 |
{ |
| 1709 |
int ov; |
| 1710 |
int32_t ret = CVmObjBigNum::ext_to_int(ext, ov); |
| 1711 |
if (ov) |
| 1712 |
err_throw(VMERR_NUM_OVERFLOW); |
| 1713 |
return ret; |
| 1714 |
} |
| 1715 |
operator double() |
| 1716 |
{ |
| 1717 |
return CVmObjBigNum::ext_to_double(ext); |
| 1718 |
} |
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
bignum_t operator +(long l) const |
| 1724 |
{ |
| 1725 |
bignum_t<prec> bl(l); |
| 1726 |
return *this + bl; |
| 1727 |
} |
| 1728 |
bignum_t operator +(double d) const |
| 1729 |
{ |
| 1730 |
bignum_t<prec> bd(d); |
| 1731 |
return *this + bd; |
| 1732 |
} |
| 1733 |
template <int precb> bignum_t operator +(const bignum_t<precb> &b) const |
| 1734 |
{ |
| 1735 |
bignum_t<(precb > prec ? precb : prec)> result; |
| 1736 |
CVmObjBigNum::compute_sum_into(result.ext, ext, b.ext); |
| 1737 |
return result; |
| 1738 |
} |
| 1739 |
|
| 1740 |
bignum_t &operator +=(long l) { set(*this + l); return *this; } |
| 1741 |
bignum_t &operator +=(double d) { set(*this + d); return *this; } |
| 1742 |
template <int precb> bignum_t &operator +=(bignum_t<precb> &b) |
| 1743 |
{ set(*this + b); return *this; } |
| 1744 |
template <int precb> bignum_t &operator +=(bignum_t<precb> b) |
| 1745 |
{ set(*this + b); return *this; } |
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
bignum_t operator -(long l) const |
| 1751 |
{ |
| 1752 |
bignum_t<prec> bl(l); |
| 1753 |
return *this - bl; |
| 1754 |
} |
| 1755 |
bignum_t operator -(double d) const |
| 1756 |
{ |
| 1757 |
bignum_t<prec> bd(d); |
| 1758 |
return *this - bd; |
| 1759 |
} |
| 1760 |
template <int precb> bignum_t operator -(bignum_t<precb> &b) const |
| 1761 |
{ |
| 1762 |
bignum_t<(precb > prec ? precb : prec)> result; |
| 1763 |
CVmObjBigNum::compute_diff_into(result.ext, ext, b.ext); |
| 1764 |
return result; |
| 1765 |
} |
| 1766 |
|
| 1767 |
bignum_t &operator -=(long l) { set(*this - l); return *this; } |
| 1768 |
bignum_t &operator -=(double d) { set(*this - d); return *this; } |
| 1769 |
template <int precb> bignum_t &operator -=(bignum_t<precb> &b) |
| 1770 |
{ set(*this - b); return *this; } |
| 1771 |
template <int precb> bignum_t &operator -=(bignum_t<precb> b) |
| 1772 |
{ set(*this - b); return *this; } |
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
bignum_t<prec> operator -() |
| 1778 |
{ |
| 1779 |
bignum_t<prec> result(this); |
| 1780 |
CVmObjBigNum::negate(result.ext); |
| 1781 |
return result; |
| 1782 |
} |
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
bignum_t operator *(long l) const |
| 1788 |
{ |
| 1789 |
bignum_t<prec> bl(l); |
| 1790 |
return *this * bl; |
| 1791 |
} |
| 1792 |
bignum_t operator *(double d) const |
| 1793 |
{ |
| 1794 |
bignum_t<prec> bd(d); |
| 1795 |
return *this * bd; |
| 1796 |
} |
| 1797 |
template <int precb> bignum_t operator *(bignum_t<precb> &b) const |
| 1798 |
{ |
| 1799 |
bignum_t<(precb > prec ? precb : prec)> result; |
| 1800 |
CVmObjBigNum::compute_prod_into(result.ext, ext, b.ext); |
| 1801 |
return result; |
| 1802 |
} |
| 1803 |
|
| 1804 |
bignum_t &operator *=(long l) { set(*this * l); return *this; } |
| 1805 |
bignum_t &operator *=(double d) { set(*this * d); return *this; } |
| 1806 |
template <int precb> bignum_t &operator *=(bignum_t<precb> &b) |
| 1807 |
{ set(*this * b); return *this; } |
| 1808 |
template <int precb> bignum_t &operator *=(bignum_t<precb> b) |
| 1809 |
{ set(*this * b); return *this; } |
| 1810 |
|
| 1811 |
|
| 1812 |
|
| 1813 |
|
| 1814 |
bignum_t operator /(long l) const |
| 1815 |
{ |
| 1816 |
bignum_t<prec> bl(l); |
| 1817 |
return *this / bl; |
| 1818 |
} |
| 1819 |
bignum_t operator /(double d) const |
| 1820 |
{ |
| 1821 |
bignum_t<prec> bd(d); |
| 1822 |
return *this / bd; |
| 1823 |
} |
| 1824 |
template <int precb> bignum_t operator /(bignum_t<precb> &b) const |
| 1825 |
{ |
| 1826 |
bignum_t<(precb > prec ? precb : prec)> result; |
| 1827 |
CVmObjBigNum::compute_quotient_into(result.ext, 0, ext, b.ext); |
| 1828 |
return result; |
| 1829 |
} |
| 1830 |
|
| 1831 |
bignum_t &operator /=(long l) { set(*this / l); return *this; } |
| 1832 |
bignum_t &operator /=(double d) { set(*this / d); return *this; } |
| 1833 |
template <int precb> bignum_t &operator /=(bignum_t<precb> &b) |
| 1834 |
{ set(*this / b); return *this; } |
| 1835 |
template <int precb> bignum_t &operator /=(bignum_t<precb> b) |
| 1836 |
{ set(*this / b); return *this; } |
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
bignum_t operator %(long l) const |
| 1842 |
{ |
| 1843 |
bignum_t<prec> bl(l); |
| 1844 |
return *this % bl; |
| 1845 |
} |
| 1846 |
bignum_t operator %(double d) const |
| 1847 |
{ |
| 1848 |
bignum_t<prec> bd(d); |
| 1849 |
return *this % bd; |
| 1850 |
} |
| 1851 |
template <int precb> bignum_t operator %(bignum_t<precb> &b) const |
| 1852 |
{ |
| 1853 |
bignum_t<(precb > prec ? precb : prec)> quo, rem; |
| 1854 |
CVmObjBigNum::compute_quotient_into(quo.ext, rem.ext, ext, b.ext); |
| 1855 |
return rem; |
| 1856 |
} |
| 1857 |
template <int precb> bignum_t &operator %=(bignum_t<precb> &b) |
| 1858 |
{ set(*this % b); return *this; } |
| 1859 |
template <int precb> bignum_t &operator %=(bignum_t<precb> b) |
| 1860 |
{ set(*this % b); return *this; } |
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
void format(char *buf, size_t buflen) |
| 1866 |
{ |
| 1867 |
CVmObjBigNum::cvt_to_string_buf( |
| 1868 |
buf, buflen, ext, -1, -1, -1, -1, VMBN_FORMAT_POINT, 0, 0); |
| 1869 |
} |
| 1870 |
void format(char *buf, size_t buflen, int maxdigits, int fracdigits) |
| 1871 |
{ |
| 1872 |
CVmObjBigNum::cvt_to_string_buf( |
| 1873 |
buf, buflen, ext, maxdigits, -1, fracdigits, -1, |
| 1874 |
VMBN_FORMAT_POINT, 0, 0); |
| 1875 |
} |
| 1876 |
|
| 1877 |
protected: |
| 1878 |
void init() |
| 1879 |
{ |
| 1880 |
oswp2(ext, prec); |
| 1881 |
ext[VMBN_FLAGS] = 0; |
| 1882 |
} |
| 1883 |
char ext[VMBN_MANT + (prec+1)/2]; |
| 1884 |
}; |
| 1885 |
|
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
class vbignum_t |
| 1894 |
{ |
| 1895 |
public: |
| 1896 |
|
| 1897 |
vbignum_t(long l) { init(10); set(l); } |
| 1898 |
vbignum_t(ulong l) { init(10); set(l); } |
| 1899 |
vbignum_t(double d) { init(18); set(d); } |
| 1900 |
vbignum_t(const char *str, size_t len) |
| 1901 |
{ |
| 1902 |
init(CVmObjBigNum::precision_from_string(str, len)); |
| 1903 |
CVmObjBigNum::parse_str_into(ext_, str, len); |
| 1904 |
} |
| 1905 |
vbignum_t(const char *str, size_t len, int radix) |
| 1906 |
{ |
| 1907 |
init(CVmObjBigNum::precision_from_string(str, len, radix)); |
| 1908 |
CVmObjBigNum::parse_str_into(ext_, str, len, radix); |
| 1909 |
} |
| 1910 |
|
| 1911 |
|
| 1912 |
~vbignum_t() { delete [] ext_; } |
| 1913 |
|
| 1914 |
|
| 1915 |
void set(long l) { CVmObjBigNum::set_int_val(ext_, l); } |
| 1916 |
void set(ulong l) { CVmObjBigNum::set_uint_val(ext_, l); } |
| 1917 |
void set(double d) { CVmObjBigNum::set_double_val(ext_, d); } |
| 1918 |
|
| 1919 |
|
| 1920 |
int prec() const { return CVmObjBigNum::get_prec(ext_); } |
| 1921 |
|
| 1922 |
|
| 1923 |
int is_zero() const { return CVmObjBigNum::is_zero(ext_); } |
| 1924 |
|
| 1925 |
|
| 1926 |
int32_t to_int(int &ov) { return CVmObjBigNum::ext_to_int(ext_, ov); } |
| 1927 |
|
| 1928 |
|
| 1929 |
const char *format(IBigNumStringBuf *buf) const |
| 1930 |
{ |
| 1931 |
return CVmObjBigNum::cvt_to_string_gen( |
| 1932 |
buf, ext_, -1, -1, -1, -1, VMBN_FORMAT_POINT, 0, 0); |
| 1933 |
} |
| 1934 |
const char *format(IBigNumStringBuf *buf, |
| 1935 |
int maxdigits, int fracdigits) const |
| 1936 |
{ |
| 1937 |
return CVmObjBigNum::cvt_to_string_gen( |
| 1938 |
buf, ext_, maxdigits, -1, fracdigits, -1, |
| 1939 |
VMBN_FORMAT_POINT, 0, 0); |
| 1940 |
} |
| 1941 |
const char *format(char *buf, size_t buflen) const |
| 1942 |
{ |
| 1943 |
return CVmObjBigNum::cvt_to_string_buf( |
| 1944 |
buf, buflen, ext_, -1, -1, -1, -1, VMBN_FORMAT_POINT, 0, 0); |
| 1945 |
} |
| 1946 |
const char *format(char *buf, size_t buflen, |
| 1947 |
int maxdigits, int fracdigits) const |
| 1948 |
{ |
| 1949 |
return CVmObjBigNum::cvt_to_string_buf( |
| 1950 |
buf, buflen, ext_, maxdigits, -1, fracdigits, -1, |
| 1951 |
VMBN_FORMAT_POINT, 0, 0); |
| 1952 |
} |
| 1953 |
|
| 1954 |
|
| 1955 |
operator int32_t() |
| 1956 |
{ |
| 1957 |
int ov; |
| 1958 |
int32_t ret = CVmObjBigNum::ext_to_int(ext_, ov); |
| 1959 |
if (ov) |
| 1960 |
err_throw(VMERR_NUM_OVERFLOW); |
| 1961 |
return ret; |
| 1962 |
} |
| 1963 |
operator double() |
| 1964 |
{ |
| 1965 |
return CVmObjBigNum::ext_to_double(ext_); |
| 1966 |
} |
| 1967 |
|
| 1968 |
|
| 1969 |
int compare(const vbignum_t &b) const |
| 1970 |
{ return CVmObjBigNum::compare_ext(ext_, b.ext_); } |
| 1971 |
|
| 1972 |
|
| 1973 |
vbignum_t *operator +(long l) const |
| 1974 |
{ |
| 1975 |
vbignum_t bl(l); |
| 1976 |
return *this + bl; |
| 1977 |
} |
| 1978 |
vbignum_t *operator +(const vbignum_t &b) const |
| 1979 |
{ |
| 1980 |
vbignum_t *sum = new vbignum_t(); |
| 1981 |
sum->init(maxprec(this, &b)); |
| 1982 |
CVmObjBigNum::compute_sum_into(sum->ext_, ext_, b.ext_); |
| 1983 |
return sum; |
| 1984 |
} |
| 1985 |
|
| 1986 |
|
| 1987 |
vbignum_t *operator -(long l) const |
| 1988 |
{ |
| 1989 |
vbignum_t bl(l); |
| 1990 |
return *this - bl; |
| 1991 |
} |
| 1992 |
vbignum_t *operator -(const vbignum_t &b) const |
| 1993 |
{ |
| 1994 |
vbignum_t *res = new vbignum_t(); |
| 1995 |
res->init(maxprec(this, &b)); |
| 1996 |
CVmObjBigNum::compute_diff_into(res->ext_, ext_, b.ext_); |
| 1997 |
return res; |
| 1998 |
} |
| 1999 |
|
| 2000 |
|
| 2001 |
vbignum_t *operator *(long l) const |
| 2002 |
{ |
| 2003 |
vbignum_t bl(l); |
| 2004 |
return *this * bl; |
| 2005 |
} |
| 2006 |
vbignum_t *operator *(const vbignum_t &b) const |
| 2007 |
{ |
| 2008 |
vbignum_t *res = new vbignum_t(); |
| 2009 |
res->init(maxprec(this, &b)); |
| 2010 |
CVmObjBigNum::compute_prod_into(res->ext_, ext_, b.ext_); |
| 2011 |
return res; |
| 2012 |
} |
| 2013 |
|
| 2014 |
|
| 2015 |
vbignum_t *operator /(long l) const |
| 2016 |
{ |
| 2017 |
vbignum_t bl(l); |
| 2018 |
return *this / bl; |
| 2019 |
} |
| 2020 |
vbignum_t *operator /(const vbignum_t &b) const |
| 2021 |
{ |
| 2022 |
vbignum_t *res = new vbignum_t(), rem; |
| 2023 |
res->init(maxprec(this, &b)); |
| 2024 |
rem.init(2); |
| 2025 |
CVmObjBigNum::compute_quotient_into(res->ext_, rem.ext_, ext_, b.ext_); |
| 2026 |
return res; |
| 2027 |
} |
| 2028 |
|
| 2029 |
|
| 2030 |
vbignum_t *operator %(long l) const |
| 2031 |
{ |
| 2032 |
vbignum_t bl(l); |
| 2033 |
return *this % bl; |
| 2034 |
} |
| 2035 |
vbignum_t *operator %(const vbignum_t &b) const |
| 2036 |
{ |
| 2037 |
vbignum_t *res = new vbignum_t(), quo; |
| 2038 |
res->init(maxprec(this, &b)); |
| 2039 |
quo.init(2); |
| 2040 |
CVmObjBigNum::compute_quotient_into(quo.ext_, res->ext_, ext_, b.ext_); |
| 2041 |
return res; |
| 2042 |
} |
| 2043 |
|
| 2044 |
|
| 2045 |
static int maxprec(const vbignum_t *a, const vbignum_t *b) |
| 2046 |
{ return a->prec() > b->prec() ? a->prec() : b->prec(); } |
| 2047 |
|
| 2048 |
protected: |
| 2049 |
|
| 2050 |
vbignum_t() { ext_ = 0; } |
| 2051 |
|
| 2052 |
|
| 2053 |
void init(int prec) |
| 2054 |
{ |
| 2055 |
ext_ = new char[CVmObjBigNum::calc_alloc(prec)]; |
| 2056 |
oswp2(ext_, prec); |
| 2057 |
ext_[VMBN_FLAGS] = 0; |
| 2058 |
} |
| 2059 |
|
| 2060 |
|
| 2061 |
char *ext_; |
| 2062 |
}; |
| 2063 |
|
| 2064 |
#endif |
| 2065 |
|
| 2066 |
|
| 2067 |
|
| 2068 |
|
| 2069 |
VM_REGISTER_METACLASS(CVmObjBigNum) |