/***** * mathop.h * Tom Prince 2005/3/18 * * Defines some runtime functions used by the stack machine. * *****/ #ifndef MATHOP_H #define MATHOP_H #include #include "stack.h" #include "mod.h" #include "triple.h" namespace run { template struct less { bool operator() (T x, T y, size_t=0) {return x < y;} }; template struct lessequals { bool operator() (T x, T y, size_t=0) {return x <= y;} }; template struct equals { bool operator() (T x, T y, size_t=0) {return x == y;} }; template struct greaterequals { bool operator() (T x, T y, size_t=0) {return x >= y;} }; template struct greater { bool operator() (T x, T y, size_t=0) {return x > y;} }; template struct notequals { bool operator() (T x, T y, size_t=0) {return x != y;} }; template struct And { bool operator() (T x, T y, size_t=0) {return x && y;} }; template struct Or { bool operator() (T x, T y, size_t=0) {return x || y;} }; template struct Xor { bool operator() (T x, T y, size_t=0) {return x ^ y;} }; template struct plus { T operator() (T x, T y, size_t=0) {return x+y;} }; template struct minus { T operator() (T x, T y, size_t=0) {return x-y;} }; template struct times { T operator() (T x, T y, size_t=0) {return x*y;} }; template <> struct times { camp::triple operator() (double x, camp::triple y, size_t=0) {return x*y;} }; template struct timesR { T operator () (T y, double x, size_t=0) {return x*y;} }; extern void dividebyzero(size_t i=0); extern void integeroverflow(size_t i=0); template struct divide { T operator() (T x, T y, size_t i=0) { if(y == 0) dividebyzero(i); return x/y; } }; template <> struct divide { camp::triple operator() (camp::triple x, double y, size_t=0) {return x/y;} }; inline bool validInt(double x) { return x > (double) Int_MIN-0.5 && x < (double) Int_MAX+0.5; } inline void checkInt(double x, size_t i) { if(validInt(x)) return; integeroverflow(i); } inline Int Intcast(double x) { if(validInt(x)) return (Int) x; integeroverflow(0); return 0; } template<> struct plus { Int operator() (Int x, Int y, size_t i=0) { if((y > 0 && x > Int_MAX-y) || (y < 0 && x < Int_MIN-y)) integeroverflow(i); return x+y; } }; template<> struct minus { Int operator() (Int x, Int y, size_t i=0) { if((y < 0 && x > Int_MAX+y) || (y > 0 && x < Int_MIN+y)) integeroverflow(i); return x-y; } }; template<> struct times { Int operator() (Int x, Int y, size_t i=0) { if(y == 0) return 0; if(y < 0) {y=-y; x=-x;} if((y > int_MAX || x > int_MAX/(int) y || x < int_MIN/(int) y) && (x > Int_MAX/y || x < Int_MIN/y)) integeroverflow(i); return x*y; } }; template<> struct divide { double operator() (Int x, Int y, size_t i=0) { if(y == 0) dividebyzero(i); return ((double) x)/(double) y; } }; template void Negate(vm::stack *s) { T a=vm::pop(s); s->push(-a); } inline Int Negate(Int x, size_t i=0) { if(x < -Int_MAX) integeroverflow(i); return -x; } template<> inline void Negate(vm::stack *s) { s->push(Negate(vm::pop(s))); } inline double pow(double x, double y) { return ::pow(x,y); } template T pow(T x, Int y) { if(y == 0) return 1.0; if(x == 0.0 && y > 0) return 0.0; if(y < 0) {y=-y; x=1/x;} T r=1.0; for(;;) { if(y & 1) r *= x; if((y >>= 1) == 0) return r; x *= x; } } template struct power { T operator() (T x, T y, size_t=0) {return pow(x,y);} }; template <> struct power { Int operator() (Int x, Int p, size_t i=0) { if(p == 0) return 1; Int sign=1; if(x < 0) { if(p % 2) sign=-1; x=-x; } if(p > 0) { if(x == 0) return 0; Int r = 1; for(;;) { if(p & 1) { if(r > Int_MAX/x) integeroverflow(i); r *= x; } if((p >>= 1) == 0) return sign*r; if(x > Int_MAX/x) integeroverflow(i); x *= x; } } else { if(x == 1) return sign; ostringstream buf; if(i > 0) buf << "array element " << i << ": "; buf << "Only 1 and -1 can be raised to negative exponents as integers."; vm::error(buf); return 0; } } }; template struct mod { T operator() (T x, T y, size_t i=0) { if(y == 0) dividebyzero(i); return portableMod(x,y); } }; template struct quotient { Int operator() (Int x, Int y, size_t i=0) { if(y == 0) dividebyzero(i); if(y == -1) return Negate(x); // Implementation-independent definition of integer division: round down Int q=x/y; if(!((x < 0)^(y < 0)) || y*q == x) return q; return q-1; } }; template struct min { T operator() (T x, T y, size_t=0) {return x < y ? x : y;} }; template struct max { T operator() (T x, T y, size_t=0) {return x > y ? x : y;} }; template inline T Min(T a, T b) { return (a < b) ? a : b; } template inline T Max(T a, T b) { return (a > b) ? a : b; } template struct minbound { camp::pair operator() (camp::pair z, camp::pair w) { return camp::pair(Min(z.getx(),w.getx()),Min(z.gety(),w.gety())); } camp::triple operator() (camp::triple u, camp::triple v) { return camp::triple(Min(u.getx(),v.getx()),Min(u.gety(),v.gety()), Min(u.getz(),v.getz())); } }; template struct maxbound { camp::pair operator() (camp::pair z, camp::pair w) { return camp::pair(Max(z.getx(),w.getx()),Max(z.gety(),w.gety())); } camp::triple operator() (camp::triple u, camp::triple v) { return camp::triple(Max(u.getx(),v.getx()),Max(u.gety(),v.gety()), Max(u.getz(),v.getz())); } }; template void realReal(vm::stack *s) { double x=vm::pop(s); s->push(func(x)); } template class op> void binaryOp(vm::stack *s) { T b=vm::pop(s); T a=vm::pop(s); s->push(op()(a,b)); } template void interp(vm::stack *s) { double t=vm::pop(s); T b=vm::pop(s); T a=vm::pop(s); s->push((1-t)*a+t*b); } } // namespace run #endif //MATHOP_H