1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-21 08:53:52 +00:00
openmw/components/interpreter/mathopcodes.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

89 lines
1.8 KiB
C++
Raw Normal View History

2010-06-29 14:11:19 +00:00
#ifndef INTERPRETER_MATHOPCODES_H_INCLUDED
#define INTERPRETER_MATHOPCODES_H_INCLUDED
#include <cmath>
2010-06-29 14:11:19 +00:00
#include <stdexcept>
#include "opcodes.hpp"
#include "runtime.hpp"
namespace Interpreter
{
template <typename T>
class OpAddInt : public Opcode0
{
public:
void execute(Runtime& runtime) override
2010-06-29 14:11:19 +00:00
{
T result = getData<T>(runtime[1]) + getData<T>(runtime[0]);
2022-09-22 18:26:05 +00:00
2010-06-29 14:11:19 +00:00
runtime.pop();
2022-09-22 18:26:05 +00:00
getData<T>(runtime[0]) = result;
2010-06-29 14:11:19 +00:00
}
};
template <typename T>
class OpSubInt : public Opcode0
{
public:
void execute(Runtime& runtime) override
2010-06-29 14:11:19 +00:00
{
T result = getData<T>(runtime[1]) - getData<T>(runtime[0]);
2022-09-22 18:26:05 +00:00
2010-06-29 14:11:19 +00:00
runtime.pop();
2022-09-22 18:26:05 +00:00
getData<T>(runtime[0]) = result;
2010-06-29 14:11:19 +00:00
}
};
template <typename T>
class OpMulInt : public Opcode0
{
public:
void execute(Runtime& runtime) override
2010-06-29 14:11:19 +00:00
{
T result = getData<T>(runtime[1]) * getData<T>(runtime[0]);
2022-09-22 18:26:05 +00:00
2010-06-29 14:11:19 +00:00
runtime.pop();
2022-09-22 18:26:05 +00:00
getData<T>(runtime[0]) = result;
2010-06-29 14:11:19 +00:00
}
};
template <typename T>
class OpDivInt : public Opcode0
{
public:
void execute(Runtime& runtime) override
2010-06-29 14:11:19 +00:00
{
T left = getData<T>(runtime[0]);
2022-09-22 18:26:05 +00:00
2010-06-29 14:11:19 +00:00
if (left == 0)
throw std::runtime_error("division by zero");
2022-09-22 18:26:05 +00:00
T result = getData<T>(runtime[1]) / left;
2022-09-22 18:26:05 +00:00
2010-06-29 14:11:19 +00:00
runtime.pop();
2022-09-22 18:26:05 +00:00
getData<T>(runtime[0]) = result;
2010-06-29 14:11:19 +00:00
}
};
2022-09-22 18:26:05 +00:00
2010-07-01 10:19:52 +00:00
template <typename T, typename C>
class OpCompare : public Opcode0
{
public:
void execute(Runtime& runtime) override
2010-07-01 10:19:52 +00:00
{
int result = C()(getData<T>(runtime[1]), getData<T>(runtime[0]));
2022-09-22 18:26:05 +00:00
2010-07-01 10:19:52 +00:00
runtime.pop();
2022-09-22 18:26:05 +00:00
runtime[0].mInteger = result;
2010-07-01 10:19:52 +00:00
}
};
2010-06-29 14:11:19 +00:00
}
#endif