OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
mathopcodes.hpp
Go to the documentation of this file.
1 #ifndef INTERPRETER_MATHOPCODES_H_INCLUDED
2 #define INTERPRETER_MATHOPCODES_H_INCLUDED
3 
4 #include <stdexcept>
5 #include <cmath>
6 
7 #include "opcodes.hpp"
8 #include "runtime.hpp"
9 
10 namespace Interpreter
11 {
12  template<typename T>
13  class OpAddInt : public Opcode0
14  {
15  public:
16 
17  virtual void execute (Runtime& runtime)
18  {
19  T result = getData<T> (runtime[1]) + getData<T> (runtime[0]);
20 
21  runtime.pop();
22 
23  getData<T> (runtime[0]) = result;
24  }
25  };
26 
27  template<typename T>
28  class OpSubInt : public Opcode0
29  {
30  public:
31 
32  virtual void execute (Runtime& runtime)
33  {
34  T result = getData<T> (runtime[1]) - getData<T> (runtime[0]);
35 
36  runtime.pop();
37 
38  getData<T> (runtime[0]) = result;
39  }
40  };
41 
42  template<typename T>
43  class OpMulInt : public Opcode0
44  {
45  public:
46 
47  virtual void execute (Runtime& runtime)
48  {
49  T result = getData<T> (runtime[1]) * getData<T> (runtime[0]);
50 
51  runtime.pop();
52 
53  getData<T> (runtime[0]) = result;
54  }
55  };
56 
57  template<typename T>
58  class OpDivInt : public Opcode0
59  {
60  public:
61 
62  virtual void execute (Runtime& runtime)
63  {
64  T left = getData<T> (runtime[0]);
65 
66  if (left==0)
67  throw std::runtime_error ("division by zero");
68 
69  T result = getData<T> (runtime[1]) / left;
70 
71  runtime.pop();
72 
73  getData<T> (runtime[0]) = result;
74  }
75  };
76 
77  class OpSquareRoot : public Opcode0
78  {
79  public:
80 
81  virtual void execute (Runtime& runtime)
82  {
83  Type_Float value = runtime[0].mFloat;
84 
85  if (value<0)
86  throw std::runtime_error (
87  "square root of negative number (we aren't that imaginary)");
88 
89  value = std::sqrt (value);
90 
91  runtime[0].mFloat = value;
92  }
93  };
94 
95  template<typename T, typename C>
96  class OpCompare : public Opcode0
97  {
98  public:
99 
100  virtual void execute (Runtime& runtime)
101  {
102  int result = C() (getData<T> (runtime[1]), getData<T> (runtime[0]));
103 
104  runtime.pop();
105 
106  runtime[0].mInteger = result;
107  }
108  };
109 }
110 
111 #endif
112 
void pop()
pop stack
Definition: runtime.cpp:94
Runtime data and engine interface.
Definition: runtime.hpp:15
Definition: mathopcodes.hpp:13
opcode for 0 arguments
Definition: opcodes.hpp:9
virtual void execute(Runtime &runtime)
Definition: mathopcodes.hpp:62
Definition: mathopcodes.hpp:43
Definition: mathopcodes.hpp:77
virtual void execute(Runtime &runtime)
Definition: mathopcodes.hpp:32
virtual void execute(Runtime &runtime)
Definition: mathopcodes.hpp:100
virtual void execute(Runtime &runtime)
Definition: mathopcodes.hpp:17
virtual void execute(Runtime &runtime)
Definition: mathopcodes.hpp:81
virtual void execute(Runtime &runtime)
Definition: mathopcodes.hpp:47
float Type_Float
Definition: types.hpp:16
Definition: mathopcodes.hpp:28
Definition: mathopcodes.hpp:58
Definition: mathopcodes.hpp:96