Hash :
397bbe0f
Author :
Thomas de Grivel
Date :
2024-04-04T17:02:17
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 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
/* c3
* Copyright 2022-2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include "alloc.h"
#include "assert.h"
#include "buf.h"
#include "buf_parse.h"
#include "compare.h"
#include "integer.h"
#include "tag.h"
#include "tag_type.h"
#include "ratio.h"
s_integer * integer_abs (const s_integer *a, s_integer *dest)
{
sw r;
assert(a);
assert(dest);
integer_init(dest);
if ((r = mp_abs(&a->mp_int, &dest->mp_int)) != MP_OKAY) {
err_write_1("integer_abs: ");
err_puts(mp_error_to_string(r));
assert(! "integer_abs: mp_abs");
return NULL;
}
return dest;
}
s_integer * integer_add (const s_integer *a, const s_integer *b,
s_integer *dest)
{
sw r;
assert(dest);
assert(a);
assert(b);
integer_init(dest);
if ((r = mp_add(&a->mp_int, &b->mp_int, &dest->mp_int)) != MP_OKAY) {
err_write_1("integer_add: ");
err_puts(mp_error_to_string(r));
assert(! "integer_add: mp_add");
return NULL;
}
return dest;
}
s_integer * integer_band (const s_integer *a, const s_integer *b,
s_integer *dest)
{
sw r;
assert(a);
assert(b);
assert(dest);
integer_init(dest);
if ((r = mp_and(&a->mp_int, &b->mp_int, &dest->mp_int)) != MP_OKAY) {
err_write_1("integer_band: ");
err_puts(mp_error_to_string(r));
assert(! "integer_band: mp_and");
return NULL;
}
return dest;
}
uw integer_bits (const s_integer *i)
{
uw bits;
if (integer_is_zero(i))
return 1;
bits = mp_count_bits(&i->mp_int);
if (integer_is_negative(i))
bits++;
return bits;
}
s_integer * integer_bnot (const s_integer *a, s_integer *dest)
{
sw r;
assert(a);
assert(dest);
integer_init(dest);
if ((r = mp_complement(&a->mp_int, &dest->mp_int)) != MP_OKAY) {
err_write_1("integer_bnot: ");
err_puts(mp_error_to_string(r));
assert(! "integer_bnot: mp_complement");
return NULL;
}
return dest;
}
s_integer * integer_bor (const s_integer *a, const s_integer *b,
s_integer *dest)
{
sw r;
assert(a);
assert(b);
assert(dest);
integer_init(dest);
if ((r = mp_or(&a->mp_int, &b->mp_int, &dest->mp_int)) != MP_OKAY) {
err_write_1("integer_bor: ");
err_puts(mp_error_to_string(r));
assert(! "integer_bor: mp_or");
return NULL;
}
return dest;
}
s_integer * integer_bxor (const s_integer *a, const s_integer *b,
s_integer *dest)
{
sw r;
assert(a);
assert(b);
assert(dest);
integer_init(dest);
if ((r = mp_xor(&a->mp_int, &b->mp_int, &dest->mp_int)) != MP_OKAY) {
err_write_1("integer_bxor: ");
err_puts(mp_error_to_string(r));
assert(! "integer_bxor: mp_xor");
return NULL;
}
return dest;
}
uw integer_bytes (const s_integer *i)
{
return (integer_bits(i) + 7) / 8;
}
s_integer * integer_init_cast (s_integer *a, const s_sym *type,
const s_tag *tag)
{
(void) type;
switch (tag->type) {
case TAG_BOOL:
return integer_init_u8(a, tag->data.bool ? 1 : 0);
case TAG_CHARACTER:
return integer_init_u32(a, tag->data.character);
case TAG_F32:
return integer_init_f32(a, tag->data.f32);
case TAG_F64:
return integer_init_f64(a, tag->data.f64);
case TAG_F128:
return integer_init_f128(a, tag->data.f128);
case TAG_INTEGER:
return integer_init_copy(a, &tag->data.integer);
case TAG_RATIO:
return integer_init_ratio(a, &tag->data.ratio);
case TAG_SW:
return integer_init_sw(a, tag->data.sw);
case TAG_S64:
return integer_init_s64(a, tag->data.s64);
case TAG_S32:
return integer_init_s32(a, tag->data.s32);
case TAG_S16:
return integer_init_s16(a, tag->data.s16);
case TAG_S8:
return integer_init_s8(a, tag->data.s8);
case TAG_U8:
return integer_init_u8(a, tag->data.u8);
case TAG_U16:
return integer_init_u16(a, tag->data.u16);
case TAG_U32:
return integer_init_u32(a, tag->data.u32);
case TAG_U64:
return integer_init_u64(a, tag->data.u64);
case TAG_UW:
return integer_init_uw(a, tag->data.uw);
default:
break;
}
err_write_1("integer_cast: cannot cast ");
err_write_1(tag_type_to_string(tag->type));
err_puts(" to integer");
assert(! "integer_cast: cannot cast");
return NULL;
}
void integer_clean (s_integer *a)
{
assert(a);
mp_clear(&a->mp_int);
}
s_integer * integer_div (const s_integer *a, const s_integer *b,
s_integer *dest)
{
sw r;
assert(a);
assert(b);
assert(dest);
integer_init(dest);
r = mp_div(&a->mp_int, &b->mp_int, &dest->mp_int, NULL);
if (r != MP_OKAY) {
err_write_1("integer_div: ");
err_puts(mp_error_to_string(r));
assert(! "integer_div: mp_div");
return NULL;
}
return dest;
}
s_integer * integer_gcd (const s_integer *a, const s_integer *b,
s_integer *dest)
{
sw r;
assert(a);
assert(b);
assert(dest);
integer_init(dest);
if ((r = mp_gcd(&a->mp_int, &b->mp_int, &dest->mp_int)) != MP_OKAY) {
err_write_1("integer_gcd: ");
err_puts(mp_error_to_string(r));
assert(! "integer_gcd: mp_gcd");
return NULL;
}
return dest;
}
s_integer * integer_init (s_integer *dest)
{
sw r;
assert(dest);
if ((r = mp_init(&dest->mp_int)) != MP_OKAY) {
err_write_1("integer_init: ");
err_puts(mp_error_to_string(r));
assert(! "integer_init: mp_init");
return NULL;
}
return dest;
}
s_integer * integer_init_1 (s_integer *i, const char *p)
{
s_buf buf;
assert(i);
assert(p);
// FIXME
buf_init_1(&buf, false, (char *) p);
buf_parse_integer(&buf, i);
return i;
}
s_integer * integer_init_copy (s_integer *a, const s_integer *src)
{
sw r;
assert(src);
assert(a);
if ((r = mp_init_copy(&a->mp_int, &src->mp_int)) != MP_OKAY) {
err_write_1("integer_init_copy: ");
err_puts(mp_error_to_string(r));
assert(! "integer_init_copy: mp_init_copy");
return NULL;
}
return a;
}
s_integer * integer_init_f32 (s_integer *a, f32 x)
{
assert(a);
integer_init(a);
return integer_set_f32(a, x);
}
s_integer * integer_init_f64 (s_integer *a, f64 x)
{
assert(a);
integer_init(a);
return integer_set_f64(a, x);
}
s_integer * integer_init_f128 (s_integer *a, f128 x)
{
assert(a);
integer_init(a);
return integer_set_f128(a, x);
}
s_integer * integer_init_s8 (s_integer *a, s8 x)
{
assert(a);
integer_init(a);
return integer_set_s32(a, x);
}
s_integer * integer_init_s16 (s_integer *a, s16 x)
{
assert(a);
integer_init(a);
return integer_set_s32(a, x);
}
s_integer * integer_init_s32 (s_integer *a, s32 x)
{
assert(a);
integer_init(a);
return integer_set_s32(a, x);
}
s_integer * integer_init_s64 (s_integer *a, s64 x)
{
assert(a);
integer_init(a);
return integer_set_s64(a, x);
}
s_integer * integer_init_ratio (s_integer *a, const s_ratio *r)
{
assert(a);
integer_init(a);
return integer_set_ratio(a, r);
}
s_integer * integer_init_sw (s_integer *a, sw x)
{
assert(a);
integer_init(a);
return integer_set_sw(a, x);
}
s_integer * integer_init_u8 (s_integer *a, u8 x)
{
assert(a);
integer_init(a);
return integer_set_u32(a, x);
}
s_integer * integer_init_u16 (s_integer *a, u16 x)
{
assert(a);
integer_init(a);
return integer_set_u32(a, x);
}
s_integer * integer_init_u32 (s_integer *a, u32 x)
{
assert(a);
integer_init(a);
return integer_set_u32(a, x);
}
s_integer * integer_init_u64 (s_integer *a, u64 x)
{
assert(a);
integer_init(a);
return integer_set_u64(a, x);
}
s_integer * integer_init_uw (s_integer *a, uw x)
{
assert(a);
integer_init(a);
return integer_set_uw(a, x);
}
s_integer * integer_init_zero (s_integer *dest)
{
assert(dest);
integer_init(dest);
mp_zero(&dest->mp_int);
return dest;
}
bool integer_is_negative (const s_integer *i)
{
assert(i);
return i->mp_int.sign == MP_NEG;
}
bool integer_is_positive (const s_integer *i)
{
assert(i);
return i->mp_int.used && i->mp_int.sign != MP_NEG;
}
bool integer_is_zero (const s_integer *i)
{
assert(i);
return ! i->mp_int.used;
}
s_integer * integer_lcm (const s_integer *a, const s_integer *b,
s_integer *dest)
{
sw r;
assert(a);
assert(b);
assert(dest);
integer_init(dest);
if ((r = mp_lcm(&a->mp_int, &b->mp_int, &dest->mp_int)) != MP_OKAY) {
err_write_1("integer_lcm: ");
err_puts(mp_error_to_string(r));
assert(! "integer_lcm: mp_lcm");
return NULL;
}
return dest;
}
s_integer * integer_lshift (const s_integer *a, sw b, s_integer *dest)
{
sw r;
assert(a);
assert(dest);
integer_init(dest);
if ((r = mp_mul_2d(&a->mp_int, b, &dest->mp_int)) != MP_OKAY) {
err_write_1("integer_lshift: ");
err_puts(mp_error_to_string(r));
assert(! "integer_lshift: mp_mul_2d");
return NULL;
}
return dest;
}
s_integer * integer_mod (const s_integer *a, const s_integer *b,
s_integer *dest)
{
sw r;
assert(a);
assert(b);
assert(dest);
integer_init(dest);
if ((r = mp_mod(&a->mp_int, &b->mp_int, &dest->mp_int)) != MP_OKAY) {
err_write_1("integer_mod: ");
err_puts(mp_error_to_string(r));
assert(! "integer_mod: mp_mod");
return NULL;
}
return dest;
}
s_integer * integer_mul (const s_integer *a, const s_integer *b,
s_integer *dest)
{
sw r;
assert(a);
assert(b);
assert(dest);
integer_init(dest);
if ((r = mp_mul(&a->mp_int, &b->mp_int, &dest->mp_int)) != MP_OKAY) {
err_write_1("integer_mul: ");
err_puts(mp_error_to_string(r));
assert(! "integer_mul: mp_mul");
return NULL;
}
return dest;
}
s_integer * integer_neg (const s_integer *a, s_integer *dest)
{
sw r;
assert(a);
assert(dest);
integer_init(dest);
if ((r = mp_neg(&a->mp_int, &dest->mp_int)) != MP_OKAY) {
err_write_1("integer_neg: ");
err_puts(mp_error_to_string(r));
assert(! "integer_neg: mp_neg");
return NULL;
}
return dest;
}
s_integer * integer_new (void)
{
s_integer *a = NULL;
a = alloc(sizeof(s_integer));
if (! a)
return NULL;
if (! integer_init(a)) {
free(a);
return NULL;
}
return a;
}
s_integer * integer_new_copy (const s_integer *src)
{
s_integer *a;
assert(src);
a = alloc(sizeof(s_integer));
if (! a)
return NULL;
if (! integer_init_copy(a, src)) {
free(a);
return NULL;
}
return a;
}
s_integer * integer_pow (const s_integer *a, const s_integer *b,
s_integer *dest)
{
sw r;
assert(a);
assert(b);
assert(dest);
integer_init(dest);
// FIXME
if ((r = mp_exptmod(&a->mp_int, &b->mp_int, &dest->mp_int, &dest->mp_int))
!= MP_OKAY) {
err_write_1("integer_pow: ");
err_puts(mp_error_to_string(r));
assert(! "integer_pow: mp_exptmod");
return NULL;
}
return dest;
}
s_integer * integer_set_f32 (s_integer *a, f32 x)
{
sw r;
assert(a);
// FIXME
if ((r = mp_set_double(&a->mp_int, x)) != MP_OKAY) {
err_write_1("integer_set_f32: ");
err_puts(mp_error_to_string(r));
assert(! "integer_set_f32: mp_set_double");
return NULL;
}
return a;
}
s_integer * integer_set_f64 (s_integer *a, f64 x)
{
sw r;
assert(a);
if ((r = mp_set_double(&a->mp_int, x)) != MP_OKAY) {
err_write_1("integer_set_f64: ");
err_puts(mp_error_to_string(r));
assert(! "integer_set_f64: mp_set_double");
return NULL;
}
return a;
}
s_integer * integer_set_f128 (s_integer *a, f128 x)
{
sw r;
assert(a);
// FIXME
if ((r = mp_set_double(&a->mp_int, (f64) x)) != MP_OKAY) {
err_write_1("integer_set_f128: ");
err_puts(mp_error_to_string(r));
assert(! "integer_set_f128: mp_set_double");
return NULL;
}
return a;
}
s_integer * integer_set_s32 (s_integer *a, s32 x)
{
assert(a);
mp_set_i32(&a->mp_int, x);
return a;
}
s_integer * integer_set_s64 (s_integer *a, s64 x)
{
assert(a);
mp_set_i64(&a->mp_int, x);
return a;
}
s_integer * integer_set_ratio (s_integer *a, const s_ratio *r)
{
assert(a);
assert(r);
return integer_div(&r->numerator, &r->denominator, a);
}
s_integer * integer_set_sw (s_integer *a, sw x)
{
assert(a);
mp_set_l(&a->mp_int, x);
return a;
}
s_integer * integer_set_u32 (s_integer *a, u32 x)
{
assert(a);
mp_set_u32(&a->mp_int, x);
return a;
}
s_integer * integer_set_u64 (s_integer *a, u64 x)
{
assert(a);
mp_set_u64(&a->mp_int, x);
return a;
}
s_integer * integer_set_uw (s_integer *a, uw x)
{
assert(a);
mp_set_ul(&a->mp_int, x);
return a;
}
s_tag * integer_sqrt (const s_integer *a, s_tag *dest)
{
sw r;
assert(a);
assert(dest);
if (integer_is_negative(a)) {
// FIXME
err_puts("integer_sqrt: not implemented");
assert(! "integer_sqrt: not implemented");
return NULL;
}
dest->type = TAG_INTEGER;
integer_init(&dest->data.integer);
if ((r = mp_sqrt(&a->mp_int, &dest->data.integer.mp_int)) != MP_OKAY) {
err_write_1("integer_sqrt: ");
err_puts(mp_error_to_string(r));
assert(! "integer_sqrt: mp_sqrt");
return NULL;
}
return dest;
}
s_integer * integer_sqrt_positive (const s_integer *a, s_integer *dest)
{
sw r;
assert(a);
assert(dest);
if (integer_is_negative(a)) {
err_puts("integer_sqrt_positive: negative argument");
assert(! "integer_sqrt_positive: negative argument");
return NULL;
}
integer_init(dest);
if ((r = mp_sqrt(&a->mp_int, &dest->mp_int)) != MP_OKAY) {
err_write_1("integer_sqrt_positive: ");
err_puts(mp_error_to_string(r));
assert(! "integer_sqrt_positive: mp_sqrt");
return NULL;
}
return dest;
}
s_integer * integer_sub (const s_integer *a, const s_integer *b,
s_integer *dest)
{
sw r;
assert(dest);
assert(a);
assert(b);
integer_init(dest);
if ((r = mp_sub(&a->mp_int, &b->mp_int, &dest->mp_int)) != MP_OKAY) {
err_write_1("integer_sub: ");
err_puts(mp_error_to_string(r));
assert(! "integer_sub: mp_sub");
return NULL;
}
return dest;
}
f32 integer_to_f32 (const s_integer *i)
{
assert(i);
return (f32) mp_get_double(&i->mp_int);
}
f64 integer_to_f64 (const s_integer *i)
{
assert(i);
return mp_get_double(&i->mp_int);
}
f128 integer_to_f128 (const s_integer *i)
{
assert(i);
// FIXME
return mp_get_double(&i->mp_int);
}
s8 integer_to_s8 (const s_integer *i)
{
assert(i);
return (s8) mp_get_i32(&i->mp_int);
}
s16 integer_to_s16 (const s_integer *i)
{
assert(i);
return (s16) mp_get_i32(&i->mp_int);
}
s32 integer_to_s32 (const s_integer *i)
{
assert(i);
return mp_get_i32(&i->mp_int);
}
s64 integer_to_s64 (const s_integer *i)
{
assert(i);
return mp_get_i64(&i->mp_int);
}
sw integer_to_sw (const s_integer *i)
{
assert(i);
return (sw) mp_get_i64(&i->mp_int);
}
u8 integer_to_u8 (const s_integer *i)
{
assert(i);
return (u8) mp_get_u32(&i->mp_int);
}
u16 integer_to_u16 (const s_integer *i)
{
assert(i);
return (u16) mp_get_u32(&i->mp_int);
}
u32 integer_to_u32 (const s_integer *i)
{
assert(i);
return mp_get_u32(&i->mp_int);
}
u64 integer_to_u64 (const s_integer *i)
{
assert(i);
return mp_get_u64(&i->mp_int);
}
uw integer_to_uw (const s_integer *i)
{
assert(i);
return (uw) mp_get_u64(&i->mp_int);
}