18static char SccsId[] =
"%W% %G%";
236float_to_int(Float v USES_REGS)
240 if (isoLanguageFlag())
241 Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, MkFloatTerm(v),NULL);
243 return MkFloatTerm(v);
248 if (isoLanguageFlag())
249 Yap_ArithError(EVALUATION_ERROR_INT_OVERFLOW, MkFloatTerm(v),
"integer (%f)",v);
251 return MkFloatTerm(v);
258 return MkIntegerTerm(i);
260 return Yap_gmp_float_to_big(v);
264#define RBIG_FL(v) return(float_to_int(v PASS_REGS))
281#define asinh(F) (log((F)+sqrt((F)*(F)+1)))
284#define acosh(F) (log((F)+sqrt((F)*(F)-1)))
287#define atanh(F) (log((1+(F))/(1-(F)))/2)
293 if (IsFloatTerm(t)) {
294 return FloatOfTerm(t);
299 if (IsLongIntTerm(t)) {
300 return LongIntOfTerm(t);
302 if (IsBigIntTerm(t)) {
303 return Yap_gmp_to_float(t);
310#define my_rint(X) rint(X)
313double my_rint(
double x)
336msb(Int inp USES_REGS)
342 Yap_ArithError(DOMAIN_ERROR_NOT_LESS_THAN_ZERO, MkIntegerTerm(inp),
343 "msb/1 received %d", inp);
346#if HAVE__BUILTIN_FFSLL
347 out = __builtin_ffsll(inp);
354 if (inp & ((CELL)0xffffffffLL << 32)) {inp >>= 32; out += 32;}
356 if (inp & ((CELL)0xffffL << 16)) {inp >>= 16; out += 16;}
357 if (inp & ((CELL)0xffL << 8)) {inp >>= 8; out += 8;}
358 if (inp & ((CELL)0xfL << 4)) {inp >>= 4; out += 4;}
359 if (inp & ((CELL)0x3L << 2)) {inp >>= 2; out += 2;}
360 if (inp & ((CELL)0x1 << 1)) out++;
366Yap_msb(Int inp USES_REGS)
368 return msb(inp PASS_REGS);
373lsb(Int inp USES_REGS)
379 Yap_ArithError(DOMAIN_ERROR_NOT_LESS_THAN_ZERO, MkIntegerTerm(inp),
380 "msb/1 received %d", inp);
385 if (!(inp & (CELL)0xffffffffLL)) {inp >>= 32; out += 32;}
387 if (!(inp & (CELL)0xffffL)) {inp >>= 16; out += 16;}
388 if (!(inp & (CELL)0xffL)) {inp >>= 8; out += 8;}
389 if (!(inp & (CELL)0xfL)) {inp >>= 4; out += 4;}
390 if (!(inp & (CELL)0x3L)) {inp >>= 2; out += 2;}
391 if (!(inp & ((CELL)0x1))) out++;
397popcount(Int inp USES_REGS)
400 Int c = 0, j = 0,
m = ((CELL)1);
403 Yap_ArithError(DOMAIN_ERROR_NOT_LESS_THAN_ZERO, MkIntegerTerm(inp),
404 "popcount/1 received %d", inp);
408 for(j=0,c=0; j<
sizeof(inp)*8; j++,
m<<=1)
417eval1(Int fi, Term t USES_REGS) {
423 switch (ETypeOfTerm(t)) {
426 Int i = IntegerOfTerm(t);
429 return Yap_gmp_neg_int(i);
432 RINT(-IntegerOfTerm(t));
435 RFLOAT(-FloatOfTerm(t));
437 return Yap_gmp_neg_big(t);
442 switch (ETypeOfTerm(t)) {
444 RINT(~IntegerOfTerm(t));
446 Yap_ArithError(TYPE_ERROR_INTEGER, t,
"\\(%f)", FloatOfTerm(t));
448 return Yap_gmp_unot_big(t);
453 RFLOAT(exp(get_float(t)));
456 Float dbl = get_float(t);
460 Yap_ArithError(EVALUATION_ERROR_UNDEFINED, t,
"log(%f)", dbl);
465 Float dbl = get_float(t);
469 Yap_ArithError(EVALUATION_ERROR_UNDEFINED, t,
"log10(%f)", dbl);
474 Float dbl = get_float(t), out;
478 Yap_ArithError(EVALUATION_ERROR_UNDEFINED, t,
"sqrt(%f)", dbl);
485 Float dbl = get_float(t), out;
491 Float dbl = get_float(t), out;
497 Float dbl = get_float(t), out;
503 Float dbl = get_float(t), out;
509 Float dbl = get_float(t), out;
515 Float dbl = get_float(t), out;
527 Yap_ArithError(EVALUATION_ERROR_UNDEFINED, t,
"asin(%f)", dbl);
540 Yap_ArithError(EVALUATION_ERROR_UNDEFINED, t,
"acos(%f)", dbl);
553 Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t,
"atan(%f)", dbl);
566 Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t,
"asinh(%f)", dbl);
579 Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t,
"acosh(%f)", dbl);
592 Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t,
"atanh(%f)", dbl);
611 Float dbl = get_float(t), out;
621 Float dbl = get_float(t), out;
640 switch (ETypeOfTerm(t)) {
644 dbl = FloatOfTerm(t);
647 return Yap_gmp_floor(t);
653 Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t,
"integer(%f)", dbl);
658 Yap_ArithError(EVALUATION_ERROR_INT_OVERFLOW, MkFloatTerm(dbl),
"integer(%f)",dbl);
666 switch (ETypeOfTerm(t)) {
670 dbl = FloatOfTerm(t);
673 return Yap_gmp_ceiling(t);
679 Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t,
"integer(%f)", dbl);
684 Yap_ArithError(EVALUATION_ERROR_INT_OVERFLOW, MkFloatTerm(dbl),
"integer\
694 switch (ETypeOfTerm(t)) {
698 dbl = FloatOfTerm(t);
701 return Yap_gmp_round(t);
707 Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t,
"integer(%f)", dbl);
712 Yap_ArithError(EVALUATION_ERROR_INT_OVERFLOW, MkFloatTerm(dbl),
"integer\
716 RBIG_FL(my_rint(dbl));
722 switch (ETypeOfTerm(t)) {
726 dbl = FloatOfTerm(t);
729 if (isoLanguageFlag())
730 Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t,
"integer(%f)", dbl);
737 if (isoLanguageFlag())
738 Yap_ArithError(EVALUATION_ERROR_INT_OVERFLOW, MkFloatTerm(dbl),
"integer (%f)",dbl);
745 return Yap_gmp_trunc(t);
751 if (isoLanguageFlag())
752 Yap_ArithError(DOMAIN_ERROR_OUT_OF_RANGE, t,
"integer(%f)", dbl);
759 if (isoLanguageFlag())
760 Yap_ArithError(EVALUATION_ERROR_INT_OVERFLOW, MkFloatTerm(dbl),
"integer (%f)",dbl);
771 switch (ETypeOfTerm(t)) {
773 RFLOAT(IntegerOfTerm(t));
777 RFLOAT(Yap_gmp_to_float(t));
782 switch (ETypeOfTerm(t)) {
786 return Yap_gmp_float_to_rational(FloatOfTerm(t));
793 switch (ETypeOfTerm(t)) {
797 return Yap_gmp_float_rationalize(FloatOfTerm(t));
804 switch (ETypeOfTerm(t)) {
806 RINT(labs(IntegerOfTerm(t)));
808 RFLOAT(fabs(FloatOfTerm(t)));
810 return Yap_gmp_abs_big(t);
815 switch (ETypeOfTerm(t)) {
817 RINT(msb(IntegerOfTerm(t) PASS_REGS));
819 Yap_ArithError(TYPE_ERROR_INTEGER, t,
"msb(%f)", FloatOfTerm(t));
821 return Yap_gmp_msb(t);
826 switch (ETypeOfTerm(t)) {
828 RINT(lsb(IntegerOfTerm(t) PASS_REGS));
830 Yap_ArithError(TYPE_ERROR_INTEGER, t,
"lsb(%f)", FloatOfTerm(t));
832 return Yap_gmp_lsb(t);
837 switch (ETypeOfTerm(t)) {
839 RINT(popcount(IntegerOfTerm(t) PASS_REGS));
841 Yap_ArithError(TYPE_ERROR_INTEGER, t,
"popcount(%f)", FloatOfTerm(t));
844 return Yap_gmp_popcount(t);
849 switch (ETypeOfTerm(t)) {
851 if (isoLanguageFlag()) {
852 Yap_ArithError(TYPE_ERROR_FLOAT, t,
"X is float_fractional_part(%f)", IntegerOfTerm(t));
859 dbl = FloatOfTerm(t);
860 RFLOAT(dbl-ceil(dbl));
864 return Yap_gmp_float_fractional_part(t);
869 switch (ETypeOfTerm(t)) {
871 Yap_ArithError(TYPE_ERROR_FLOAT, t,
"X is float_integer_part(%f)", IntegerOfTerm(t));
873 RFLOAT(rint(FloatOfTerm(t)));
876 return Yap_gmp_float_integer_part(t);
881 switch (ETypeOfTerm(t)) {
884 Int x = IntegerOfTerm(t);
886 RINT((x > 0 ? 1 : (x < 0 ? -1 : 0)));
891 Float dbl = FloatOfTerm(t);
893 RINT((dbl > 0.0 ? 1 : (dbl < 0.0 ? -1 : 0)));
896 return Yap_gmp_sign(t);
901 switch (ETypeOfTerm(t)) {
903 RINT(Yap_random()*IntegerOfTerm(t));
905 Yap_ArithError(TYPE_ERROR_INTEGER, t,
"random(%f)", FloatOfTerm(t));
907 return Yap_gmp_mul_float_big(Yap_random(), t);
916Term Yap_eval_unary(Int f, Term t)
919 return eval1(f,t PASS_REGS);
943 {
"ceiling", op_ceiling},
945 {
"truncate", op_truncate},
946 {
"integer", op_integer},
951 {
"popcount", op_popcount},
952 {
"float_fractional_part", op_ffracp},
953 {
"float_integer_part", op_fintp},
955 {
"lgamma", op_lgamma},
958 {
"rational",op_rational},
959 {
"rationalize",op_rationalize},
960 {
"random", op_random1}
964Yap_NameOfUnaryOp(
int i)
966 return Yap_LookupAtom(InitUnTab[i].OpName);
970p_unary_is( USES_REGS1 )
972 Term t = Deref(ARG2);
974 yap_error_number err;
977 Yap_EvalError(INSTANTIATION_ERROR, t,
"unbound unary operator");
981 top = Yap_Eval(Deref(ARG3));
982 if ((err=Yap_FoundArithError())) {
983 Yap_EvalError(err,ARG3,
"X is op(Y): error in Y ");
990 i = IntegerOfTerm(t);
991 tout = eval1(i, top PASS_REGS);
992 if ((err=Yap_FoundArithError())) {
993 Functor f = Yap_MkFunctor( Yap_NameOfUnaryOp(i), 1 );
994 Term t = Yap_MkApplTerm( f, 1, &top );
995 Yap_EvalError(err, t ,
"error in %s/1 ", RepAtom(NameOfFunctor(f))->StrOfAE);
998 return Yap_unify_constant(ARG1,tout);
999 }
else if (IsAtomTerm(t)) {
1000 Atom name = AtomOfTerm(t);
1004 if (EndOfPAEntr(p = RepExpProp(Yap_GetExpProp(name, 1)))) {
1005 Yap_EvalError(TYPE_ERROR_EVALUABLE, takeIndicator(t),
1006 "functor %s/1 for arithmetic expression",
1007 RepAtom(name)->StrOfAE);
1010 out= eval1(p->FOfEE, top PASS_REGS);
1011 if ((err=Yap_FoundArithError())) {
1014 return Yap_unify_constant(ARG1,out);
1020p_unary_op_as_integer( USES_REGS1 )
1022 Term t = Deref(ARG1);
1025 Yap_EvalError(INSTANTIATION_ERROR,t,
"X is _Y");
1029 return Yap_unify_constant(ARG2,t);
1031 if (IsAtomTerm(t)) {
1032 Atom name = AtomOfTerm(t);
1035 if (EndOfPAEntr(p = RepExpProp(Yap_GetExpProp(name, 1)))) {
1036 return Yap_unify(ARG1,ARG2);
1038 return Yap_unify_constant(ARG2,MkIntTerm(p->FOfEE));
1044Yap_InitUnaryExps(
void)
1049 for (i = 0; i <
sizeof(InitUnTab)/
sizeof(
InitUnEntry); ++i) {
1050 AtomEntry *ae = RepAtom(Yap_LookupAtom(InitUnTab[i].OpName));
1052 Yap_EvalError(RESOURCE_ERROR_HEAP,TermNil,
"at InitUnaryExps");
1055 WRITE_LOCK(ae->ARWLock);
1056 if (Yap_GetExpPropHavingLock(ae, 1)) {
1057 WRITE_UNLOCK(ae->ARWLock);
1061 p->KindOfPE = ExpProperty;
1064 p->FOfEE = InitUnTab[i].f;
1066 WRITE_UNLOCK(ae->ARWLock);
1068 Yap_InitCPred(
"is", 3, p_unary_is, TestPredFlag | SafePredFlag);
1069 Yap_InitCPred(
"$unary_op_as_integer", 2, p_unary_op_as_integer, TestPredFlag|SafePredFlag);}
1073Yap_ReInitUnaryExps(
void)
@ op_log
log( X ), natural logarithm of X
@ op_log10
log10( X ) [ISO]
@ op_exp
exp( X ), natural exponentiation of X
@ op_unot
\ X, The bitwise negation of X
@ op_uminus
- X: the complement of X
@ op_uplus
+ X: the value of X