mirror of
				https://github.com/TES3MP/openmw-tes3mp.git
				synced 2025-11-04 06:26:42 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			112 lines
		
	
	
	
		
			2.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			112 lines
		
	
	
	
		
			2.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
#ifndef INTERPRETER_MATHOPCODES_H_INCLUDED
 | 
						|
#define INTERPRETER_MATHOPCODES_H_INCLUDED
 | 
						|
 | 
						|
#include <stdexcept>
 | 
						|
#include <cmath>
 | 
						|
 | 
						|
#include "opcodes.hpp"
 | 
						|
#include "runtime.hpp"
 | 
						|
 | 
						|
namespace Interpreter
 | 
						|
{
 | 
						|
    template<typename T>
 | 
						|
    class OpAddInt : public Opcode0
 | 
						|
    {
 | 
						|
        public:
 | 
						|
        
 | 
						|
            virtual void execute (Runtime& runtime)
 | 
						|
            {
 | 
						|
                T result = getData<T> (runtime[1]) + getData<T> (runtime[0]);
 | 
						|
                
 | 
						|
                runtime.pop();
 | 
						|
                
 | 
						|
                getData<T> (runtime[0]) = result;
 | 
						|
            }           
 | 
						|
    };
 | 
						|
 | 
						|
    template<typename T>
 | 
						|
    class OpSubInt : public Opcode0
 | 
						|
    {
 | 
						|
        public:
 | 
						|
        
 | 
						|
            virtual void execute (Runtime& runtime)
 | 
						|
            {
 | 
						|
                T result = getData<T> (runtime[1]) - getData<T> (runtime[0]);
 | 
						|
                
 | 
						|
                runtime.pop();
 | 
						|
 | 
						|
                getData<T> (runtime[0]) = result;
 | 
						|
            }           
 | 
						|
    };
 | 
						|
 | 
						|
    template<typename T>
 | 
						|
    class OpMulInt : public Opcode0
 | 
						|
    {
 | 
						|
        public:
 | 
						|
        
 | 
						|
            virtual void execute (Runtime& runtime)
 | 
						|
            {
 | 
						|
                T result = getData<T> (runtime[1]) * getData<T> (runtime[0]);
 | 
						|
                
 | 
						|
                runtime.pop();
 | 
						|
 | 
						|
                getData<T> (runtime[0]) = result;
 | 
						|
            }           
 | 
						|
    };
 | 
						|
 | 
						|
    template<typename T>
 | 
						|
    class OpDivInt : public Opcode0
 | 
						|
    {
 | 
						|
        public:
 | 
						|
        
 | 
						|
            virtual void execute (Runtime& runtime)
 | 
						|
            {
 | 
						|
                T left = getData<T> (runtime[0]);
 | 
						|
            
 | 
						|
                if (left==0)
 | 
						|
                    throw std::runtime_error ("division by zero");
 | 
						|
            
 | 
						|
                T result = getData<T> (runtime[1]) / left;
 | 
						|
                
 | 
						|
                runtime.pop();
 | 
						|
 | 
						|
                getData<T> (runtime[0]) = result;
 | 
						|
            }           
 | 
						|
    };
 | 
						|
    
 | 
						|
    class OpSquareRoot : public Opcode0
 | 
						|
    {
 | 
						|
        public:
 | 
						|
        
 | 
						|
            virtual void execute (Runtime& runtime)
 | 
						|
            {
 | 
						|
                Type_Float value = runtime[0].mFloat;
 | 
						|
                
 | 
						|
                if (value<0)
 | 
						|
                    throw std::runtime_error (
 | 
						|
                        "square root of negative number (we aren't that imaginary)");
 | 
						|
                
 | 
						|
                value = std::sqrt (value);
 | 
						|
                
 | 
						|
                runtime[0].mFloat = value;
 | 
						|
            }           
 | 
						|
    };    
 | 
						|
    
 | 
						|
    template<typename T, typename C>
 | 
						|
    class OpCompare : public Opcode0
 | 
						|
    {
 | 
						|
        public:
 | 
						|
        
 | 
						|
            virtual void execute (Runtime& runtime)
 | 
						|
            {
 | 
						|
                int result = C() (getData<T> (runtime[1]), getData<T> (runtime[0]));
 | 
						|
                
 | 
						|
                runtime.pop();
 | 
						|
                
 | 
						|
                runtime[0].mInteger = result;
 | 
						|
            }           
 | 
						|
    };    
 | 
						|
}
 | 
						|
 | 
						|
#endif
 | 
						|
 |