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
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2006-2011. All Rights Reserved.
*
* The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved online at http://www.erlang.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* %CopyrightEnd%
*/
/*
* Numeric guard BIFs.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "sys.h"
#include "erl_vm.h"
#include "global.h"
#include "erl_process.h"
#include "error.h"
#include "bif.h"
#include "big.h"
#include "erl_binary.h"
#include "erl_map.h"
static Eterm gc_double_to_integer(Process* p, double x, Eterm* reg, Uint live);
static Eterm double_to_integer(Process* p, double x);
/*
* Guard BIFs called using apply/3 and guard BIFs that never build
* anything on the heap.
*/
BIF_RETTYPE abs_1(BIF_ALIST_1)
{
Eterm res;
Sint i0, i;
Eterm* hp;
/* integer arguments */
if (is_small(BIF_ARG_1)) {
i0 = signed_val(BIF_ARG_1);
i = ERTS_SMALL_ABS(i0);
if (i0 == MIN_SMALL) {
hp = HAlloc(BIF_P, BIG_UINT_HEAP_SIZE);
BIF_RET(uint_to_big(i, hp));
} else {
BIF_RET(make_small(i));
}
} else if (is_big(BIF_ARG_1)) {
if (!big_sign(BIF_ARG_1)) {
BIF_RET(BIF_ARG_1);
} else {
int sz = big_arity(BIF_ARG_1) + 1;
Uint* x;
hp = HAlloc(BIF_P, sz); /* See note at beginning of file */
sz--;
res = make_big(hp);
x = big_val(BIF_ARG_1);
*hp++ = make_pos_bignum_header(sz);
x++; /* skip thing */
while(sz--)
*hp++ = *x++;
BIF_RET(res);
}
} else if (is_float(BIF_ARG_1)) {
FloatDef f;
GET_DOUBLE(BIF_ARG_1, f);
if (f.fd < 0.0) {
hp = HAlloc(BIF_P, FLOAT_SIZE_OBJECT);
f.fd = fabs(f.fd);
res = make_float(hp);
PUT_DOUBLE(f, hp);
BIF_RET(res);
}
else
BIF_RET(BIF_ARG_1);
}
BIF_ERROR(BIF_P, BADARG);
}
BIF_RETTYPE float_1(BIF_ALIST_1)
{
Eterm res;
Eterm* hp;
FloatDef f;
/* check args */
if (is_not_integer(BIF_ARG_1)) {
if (is_float(BIF_ARG_1)) {
BIF_RET(BIF_ARG_1);
} else {
badarg:
BIF_ERROR(BIF_P, BADARG);
}
}
if (is_small(BIF_ARG_1)) {
Sint i = signed_val(BIF_ARG_1);
f.fd = i; /* use "C"'s auto casting */
} else if (big_to_double(BIF_ARG_1, &f.fd) < 0) {
goto badarg;
}
hp = HAlloc(BIF_P, FLOAT_SIZE_OBJECT);
res = make_float(hp);
PUT_DOUBLE(f, hp);
BIF_RET(res);
}
BIF_RETTYPE trunc_1(BIF_ALIST_1)
{
Eterm res;
FloatDef f;
/* check arg */
if (is_not_float(BIF_ARG_1)) {
if (is_integer(BIF_ARG_1))
BIF_RET(BIF_ARG_1);
BIF_ERROR(BIF_P, BADARG);
}
/* get the float */
GET_DOUBLE(BIF_ARG_1, f);
/* truncate it and return the resultant integer */
res = double_to_integer(BIF_P, (f.fd >= 0.0) ? floor(f.fd) : ceil(f.fd));
BIF_RET(res);
}
BIF_RETTYPE round_1(BIF_ALIST_1)
{
Eterm res;
FloatDef f;
/* check arg */
if (is_not_float(BIF_ARG_1)) {
if (is_integer(BIF_ARG_1))
BIF_RET(BIF_ARG_1);
BIF_ERROR(BIF_P, BADARG);
}
/* get the float */
GET_DOUBLE(BIF_ARG_1, f);
/* round it and return the resultant integer */
res = double_to_integer(BIF_P, (f.fd > 0.0) ? f.fd + 0.5 : f.fd - 0.5);
BIF_RET(res);
}
BIF_RETTYPE length_1(BIF_ALIST_1)
{
Eterm list;
Uint i;
if (is_nil(BIF_ARG_1))
BIF_RET(SMALL_ZERO);
if (is_not_list(BIF_ARG_1)) {
BIF_ERROR(BIF_P, BADARG);
}
list = BIF_ARG_1;
i = 0;
while (is_list(list)) {
i++;
list = CDR(list_val(list));
}
if (is_not_nil(list)) {
BIF_ERROR(BIF_P, BADARG);
}
BIF_RET(make_small(i));
}
/* returns the size of a tuple or a binary */
BIF_RETTYPE size_1(BIF_ALIST_1)
{
if (is_tuple(BIF_ARG_1)) {
Eterm* tupleptr = tuple_val(BIF_ARG_1);
BIF_RET(make_small(arityval(*tupleptr)));
} else if (is_binary(BIF_ARG_1)) {
Uint sz = binary_size(BIF_ARG_1);
if (IS_USMALL(0, sz)) {
return make_small(sz);
} else {
Eterm* hp = HAlloc(BIF_P, BIG_UINT_HEAP_SIZE);
BIF_RET(uint_to_big(sz, hp));
}
}
BIF_ERROR(BIF_P, BADARG);
}
/**********************************************************************/
/* returns the bitsize of a bitstring */
BIF_RETTYPE bit_size_1(BIF_ALIST_1)
{
Uint low_bits;
Uint bytesize;
Uint high_bits;
if (is_binary(BIF_ARG_1)) {
bytesize = binary_size(BIF_ARG_1);
high_bits = bytesize >> ((sizeof(Uint) * 8)-3);
low_bits = (bytesize << 3) + binary_bitsize(BIF_ARG_1);
if (high_bits == 0) {
if (IS_USMALL(0,low_bits)) {
BIF_RET(make_small(low_bits));
} else {
Eterm* hp = HAlloc(BIF_P, BIG_UINT_HEAP_SIZE);
BIF_RET(uint_to_big(low_bits, hp));
}
} else {
Uint sz = BIG_UINT_HEAP_SIZE+1;
Eterm* hp = HAlloc(BIF_P, sz);
hp[0] = make_pos_bignum_header(sz-1);
BIG_DIGIT(hp,0) = low_bits;
BIG_DIGIT(hp,1) = high_bits;
BIF_RET(make_big(hp));
}
} else {
BIF_ERROR(BIF_P, BADARG);
}
}
/**********************************************************************/
/* returns the number of bytes need to store a bitstring */
BIF_RETTYPE byte_size_1(BIF_ALIST_1)
{
if (is_binary(BIF_ARG_1)) {
Uint bytesize = binary_size(BIF_ARG_1);
if (binary_bitsize(BIF_ARG_1) > 0) {
bytesize++;
}
if (IS_USMALL(0, bytesize)) {
BIF_RET(make_small(bytesize));
} else {
Eterm* hp = HAlloc(BIF_P, BIG_UINT_HEAP_SIZE);
BIF_RET(uint_to_big(bytesize, hp));
}
} else {
BIF_ERROR(BIF_P, BADARG);
}
}
/*
* Generate the integer part from a double.
*/
static Eterm
double_to_integer(Process* p, double x)
{
int is_negative;
int ds;
ErtsDigit* xp;
int i;
Eterm res;
size_t sz;
Eterm* hp;
double dbase;
if ((x < (double) (MAX_SMALL+1)) && (x > (double) (MIN_SMALL-1))) {
Sint xi = x;
return make_small(xi);
}
if (x >= 0) {
is_negative = 0;
} else {
is_negative = 1;
x = -x;
}
/* Unscale & (calculate exponent) */
ds = 0;
dbase = ((double)(D_MASK)+1);
while(x >= 1.0) {
x /= dbase; /* "shift" right */
ds++;
}
sz = BIG_NEED_SIZE(ds); /* number of words including arity */
hp = HAlloc(p, sz);
res = make_big(hp);
xp = (ErtsDigit*) (hp + 1);
for (i = ds-1; i >= 0; i--) {
ErtsDigit d;
x *= dbase; /* "shift" left */
d = x; /* trunc */
xp[i] = d; /* store digit */
x -= d; /* remove integer part */
}
while ((ds & (BIG_DIGITS_PER_WORD-1)) != 0) {
xp[ds++] = 0;
}
if (is_negative) {
*hp = make_neg_bignum_header(sz-1);
} else {
*hp = make_pos_bignum_header(sz-1);
}
return res;
}
/********************************************************************************
* binary_part guards. The actual implementation is in erl_bif_binary.c
********************************************************************************/
BIF_RETTYPE binary_part_3(BIF_ALIST_3)
{
return erts_binary_part(BIF_P,BIF_ARG_1,BIF_ARG_2, BIF_ARG_3);
}
BIF_RETTYPE binary_part_2(BIF_ALIST_2)
{
Eterm *tp;
if (is_not_tuple(BIF_ARG_2)) {
goto badarg;
}
tp = tuple_val(BIF_ARG_2);
if (arityval(*tp) != 2) {
goto badarg;
}
return erts_binary_part(BIF_P,BIF_ARG_1,tp[1], tp[2]);
badarg:
BIF_ERROR(BIF_P,BADARG);
}
/*
* The following code is used when a guard that may build on the
* heap is called directly. They must not use HAlloc(), but must
* do a garbage collection if there is insufficient heap space.
*
* Important note: All error checking MUST be done before doing
* a garbage collection. The compiler assumes that all registers
* are still valid if a guard BIF generates an exception.
*/
#define ERTS_NEED_GC(p, need) ((HEAP_LIMIT((p)) - HEAP_TOP((p))) <= (need))
Eterm erts_gc_length_1(Process* p, Eterm* reg, Uint live)
{
Eterm list = reg[live];
int i;
if (is_nil(list))
return SMALL_ZERO;
i = 0;
while (is_list(list)) {
i++;
list = CDR(list_val(list));
}
if (is_not_nil(list)) {
BIF_ERROR(p, BADARG);
}
return make_small(i);
}
Eterm erts_gc_size_1(Process* p, Eterm* reg, Uint live)
{
Eterm arg = reg[live];
if (is_tuple(arg)) {
Eterm* tupleptr = tuple_val(arg);
return make_small(arityval(*tupleptr));
} else if (is_binary(arg)) {
Uint sz = binary_size(arg);
if (IS_USMALL(0, sz)) {
return make_small(sz);
} else {
Eterm* hp;
if (ERTS_NEED_GC(p, BIG_UINT_HEAP_SIZE)) {
erts_garbage_collect(p, BIG_UINT_HEAP_SIZE, reg, live);
}
hp = p->htop;
p->htop += BIG_UINT_HEAP_SIZE;
return uint_to_big(sz, hp);
}
}
BIF_ERROR(p, BADARG);
}
Eterm erts_gc_bit_size_1(Process* p, Eterm* reg, Uint live)
{
Eterm arg = reg[live];
if (is_binary(arg)) {
Uint low_bits;
Uint bytesize;
Uint high_bits;
bytesize = binary_size(arg);
high_bits = bytesize >> ((sizeof(Uint) * 8)-3);
low_bits = (bytesize << 3) + binary_bitsize(arg);
if (high_bits == 0) {
if (IS_USMALL(0,low_bits)) {
return make_small(low_bits);
} else {
Eterm* hp;
if (ERTS_NEED_GC(p, BIG_UINT_HEAP_SIZE)) {
erts_garbage_collect(p, BIG_UINT_HEAP_SIZE, reg, live);
}
hp = p->htop;
p->htop += BIG_UINT_HEAP_SIZE;
return uint_to_big(low_bits, hp);
}
} else {
Uint sz = BIG_UINT_HEAP_SIZE+1;
Eterm* hp;
if (ERTS_NEED_GC(p, sz)) {
erts_garbage_collect(p, sz, reg, live);
}
hp = p->htop;
p->htop += sz;
hp[0] = make_pos_bignum_header(sz-1);
BIG_DIGIT(hp,0) = low_bits;
BIG_DIGIT(hp,1) = high_bits;
return make_big(hp);
}
} else {
BIF_ERROR(p, BADARG);
}
}
Eterm erts_gc_byte_size_1(Process* p, Eterm* reg, Uint live)
{
Eterm arg = reg[live];
if (is_binary(arg)) {
Uint bytesize = binary_size(arg);
if (binary_bitsize(arg) > 0) {
bytesize++;
}
if (IS_USMALL(0, bytesize)) {
return make_small(bytesize);
} else {
Eterm* hp;
if (ERTS_NEED_GC(p, BIG_UINT_HEAP_SIZE)) {
erts_garbage_collect(p, BIG_UINT_HEAP_SIZE, reg, live);
}
hp = p->htop;
p->htop += BIG_UINT_HEAP_SIZE;
return uint_to_big(bytesize, hp);
}
} else {
BIF_ERROR(p, BADARG);
}
}
Eterm erts_gc_map_size_1(Process* p, Eterm* reg, Uint live)
{
Eterm arg = reg[live];
if (is_map(arg)) {
map_t *mp = (map_t*)map_val(arg);
Uint size = map_get_size(mp);
if (IS_USMALL(0, size)) {
return make_small(size);
} else {
Eterm* hp;
if (ERTS_NEED_GC(p, BIG_UINT_HEAP_SIZE)) {
erts_garbage_collect(p, BIG_UINT_HEAP_SIZE, reg, live);
}
hp = p->htop;
p->htop += BIG_UINT_HEAP_SIZE;
return uint_to_big(size, hp);
}
} else {
BIF_ERROR(p, BADARG);
}
}
Eterm erts_gc_abs_1(Process* p, Eterm* reg, Uint live)
{
Eterm arg;
Eterm res;
Sint i0, i;
Eterm* hp;
arg = reg[live];
/* integer arguments */
if (is_small(arg)) {
i0 = signed_val(arg);
i = ERTS_SMALL_ABS(i0);
if (i0 == MIN_SMALL) {
if (ERTS_NEED_GC(p, BIG_UINT_HEAP_SIZE)) {
erts_garbage_collect(p, BIG_UINT_HEAP_SIZE, reg, live+1);
arg = reg[live];
}
hp = p->htop;
p->htop += BIG_UINT_HEAP_SIZE;
return uint_to_big(i, hp);
} else {
return make_small(i);
}
} else if (is_big(arg)) {
if (!big_sign(arg)) {
return arg;
} else {
int sz = big_arity(arg) + 1;
Uint* x;
if (ERTS_NEED_GC(p, sz)) {
erts_garbage_collect(p, sz, reg, live+1);
arg = reg[live];
}
hp = p->htop;
p->htop += sz;
sz--;
res = make_big(hp);
x = big_val(arg);
*hp++ = make_pos_bignum_header(sz);
x++; /* skip thing */
while(sz--)
*hp++ = *x++;
return res;
}
} else if (is_float(arg)) {
FloatDef f;
GET_DOUBLE(arg, f);
if (f.fd < 0.0) {
if (ERTS_NEED_GC(p, FLOAT_SIZE_OBJECT)) {
erts_garbage_collect(p, FLOAT_SIZE_OBJECT, reg, live+1);
arg = reg[live];
}
hp = p->htop;
p->htop += FLOAT_SIZE_OBJECT;
f.fd = fabs(f.fd);
res = make_float(hp);
PUT_DOUBLE(f, hp);
return res;
}
else
return arg;
}
BIF_ERROR(p, BADARG);
}
Eterm erts_gc_float_1(Process* p, Eterm* reg, Uint live)
{
Eterm arg;
Eterm res;
Eterm* hp;
FloatDef f;
/* check args */
arg = reg[live];
if (is_not_integer(arg)) {
if (is_float(arg)) {
return arg;
} else {
badarg:
BIF_ERROR(p, BADARG);
}
}
if (is_small(arg)) {
Sint i = signed_val(arg);
f.fd = i; /* use "C"'s auto casting */
} else if (big_to_double(arg, &f.fd) < 0) {
goto badarg;
}
if (ERTS_NEED_GC(p, FLOAT_SIZE_OBJECT)) {
erts_garbage_collect(p, FLOAT_SIZE_OBJECT, reg, live+1);
arg = reg[live];
}
hp = p->htop;
p->htop += FLOAT_SIZE_OBJECT;
res = make_float(hp);
PUT_DOUBLE(f, hp);
return res;
}
Eterm erts_gc_round_1(Process* p, Eterm* reg, Uint live)
{
Eterm arg;
FloatDef f;
arg = reg[live];
if (is_not_float(arg)) {
if (is_integer(arg)) {
return arg;
}
BIF_ERROR(p, BADARG);
}
GET_DOUBLE(arg, f);
return gc_double_to_integer(p, (f.fd > 0.0) ? f.fd + 0.5 : f.fd - 0.5,
reg, live);
}
Eterm erts_gc_trunc_1(Process* p, Eterm* reg, Uint live)
{
Eterm arg;
FloatDef f;
arg = reg[live];
if (is_not_float(arg)) {
if (is_integer(arg)) {
return arg;
}
BIF_ERROR(p, BADARG);
}
/* get the float */
GET_DOUBLE(arg, f);
/* truncate it and return the resultant integer */
return gc_double_to_integer(p, (f.fd >= 0.0) ? floor(f.fd) : ceil(f.fd),
reg, live);
}
static Eterm
gc_double_to_integer(Process* p, double x, Eterm* reg, Uint live)
{
int is_negative;
int ds;
ErtsDigit* xp;
int i;
Eterm res;
size_t sz;
Eterm* hp;
double dbase;
if ((x < (double) (MAX_SMALL+1)) && (x > (double) (MIN_SMALL-1))) {
Sint xi = x;
return make_small(xi);
}
if (x >= 0) {
is_negative = 0;
} else {
is_negative = 1;
x = -x;
}
/* Unscale & (calculate exponent) */
ds = 0;
dbase = ((double)(D_MASK)+1);
while(x >= 1.0) {
x /= dbase; /* "shift" right */
ds++;
}
sz = BIG_NEED_SIZE(ds); /* number of words including arity */
if (ERTS_NEED_GC(p, sz)) {
erts_garbage_collect(p, sz, reg, live);
}
hp = p->htop;
p->htop += sz;
res = make_big(hp);
xp = (ErtsDigit*) (hp + 1);
for (i = ds-1; i >= 0; i--) {
ErtsDigit d;
x *= dbase; /* "shift" left */
d = x; /* trunc */
xp[i] = d; /* store digit */
x -= d; /* remove integer part */
}
while ((ds & (BIG_DIGITS_PER_WORD-1)) != 0) {
xp[ds++] = 0;
}
if (is_negative) {
*hp = make_neg_bignum_header(sz-1);
} else {
*hp = make_pos_bignum_header(sz-1);
}
return res;
}
/********************************************************************************
* binary_part guards. The actual implementation is in erl_bif_binary.c
********************************************************************************/
Eterm erts_gc_binary_part_3(Process* p, Eterm* reg, Uint live)
{
return erts_gc_binary_part(p,reg,live,0);
}
Eterm erts_gc_binary_part_2(Process* p, Eterm* reg, Uint live)
{
return erts_gc_binary_part(p,reg,live,1);
}
|