OpenMW
|
00001 #ifndef INTERPRETER_MATHOPCODES_H_INCLUDED 00002 #define INTERPRETER_MATHOPCODES_H_INCLUDED 00003 00004 #include <stdexcept> 00005 #include <cmath> 00006 00007 #include "opcodes.hpp" 00008 #include "runtime.hpp" 00009 00010 namespace Interpreter 00011 { 00012 template<typename T> 00013 class OpAddInt : public Opcode0 00014 { 00015 public: 00016 00017 virtual void execute (Runtime& runtime) 00018 { 00019 T result = getData<T> (runtime[1]) + getData<T> (runtime[0]); 00020 00021 runtime.pop(); 00022 00023 getData<T> (runtime[0]) = result; 00024 } 00025 }; 00026 00027 template<typename T> 00028 class OpSubInt : public Opcode0 00029 { 00030 public: 00031 00032 virtual void execute (Runtime& runtime) 00033 { 00034 T result = getData<T> (runtime[1]) - getData<T> (runtime[0]); 00035 00036 runtime.pop(); 00037 00038 getData<T> (runtime[0]) = result; 00039 } 00040 }; 00041 00042 template<typename T> 00043 class OpMulInt : public Opcode0 00044 { 00045 public: 00046 00047 virtual void execute (Runtime& runtime) 00048 { 00049 T result = getData<T> (runtime[1]) * getData<T> (runtime[0]); 00050 00051 runtime.pop(); 00052 00053 getData<T> (runtime[0]) = result; 00054 } 00055 }; 00056 00057 template<typename T> 00058 class OpDivInt : public Opcode0 00059 { 00060 public: 00061 00062 virtual void execute (Runtime& runtime) 00063 { 00064 T left = getData<T> (runtime[0]); 00065 00066 if (left==0) 00067 throw std::runtime_error ("division by zero"); 00068 00069 T result = getData<T> (runtime[1]) / left; 00070 00071 runtime.pop(); 00072 00073 getData<T> (runtime[0]) = result; 00074 } 00075 }; 00076 00077 class OpSquareRoot : public Opcode0 00078 { 00079 public: 00080 00081 virtual void execute (Runtime& runtime) 00082 { 00083 Type_Float value = runtime[0].mFloat; 00084 00085 if (value<0) 00086 throw std::runtime_error ( 00087 "square root of negative number (we aren't that imaginary)"); 00088 00089 value = std::sqrt (value); 00090 00091 runtime[0].mFloat = value; 00092 } 00093 }; 00094 00095 template<typename T, typename C> 00096 class OpCompare : public Opcode0 00097 { 00098 public: 00099 00100 virtual void execute (Runtime& runtime) 00101 { 00102 int result = C() (getData<T> (runtime[1]), getData<T> (runtime[0])); 00103 00104 runtime.pop(); 00105 00106 runtime[0].mInteger = result; 00107 } 00108 }; 00109 } 00110 00111 #endif 00112