Click here to Skip to main content
15,896,201 members
Articles / Programming Languages / C#

vALU: Virtual Arithmetic Logic Unit

Rate me:
Please Sign up or sign in to vote.
4.96/5 (12 votes)
2 Jan 2011CPOL3 min read 19.1K   2   20  
Algorithms of Hardware Mathematics
using System;

namespace ALU
{
	class Program
	{
		public static void Main(string[] args)
		{			
			Console.WriteLine("Adder:  4 + 2 = " + Adder(4, 2).ToString());
			Console.WriteLine("Subtractor: 4 - 2 = " + Subtractor(4, 2).ToString());
			Console.WriteLine("Multiplier:  4 * 2 = " + Multiplier(4, 2).ToString());
			Console.WriteLine("Divider:  4 / 2 = " + Divider(4, 2).ToString());
			Console.WriteLine("Square Root:  sqrt(4) = " + SquareRoot(4).ToString());
			Console.WriteLine("BinaryLogaritm:  BiLog(4) = " + BinaryLogarithm(4).ToString());
			Console.WriteLine("Logarithm: Log(4) Base 2 = " + Logarithm(4, 2).ToString());
			
			Console.ReadKey(true);
		}
		
		public static uint Adder(uint Addend_One, uint Addend_Two)
		{
			uint Carry;
			
		    while(Addend_Two != 0)
		    {
				Carry = Addend_One & Addend_Two;
				Addend_One ^= Addend_Two;
				Addend_Two = Carry << 1;
			}	
			return Addend_One;
		}
		
		public static uint Subtractor(uint Subtrahend, uint Minuend)
		{
			Minuend = Adder(~Minuend, 1);
			return Adder(Subtrahend, Minuend);
		}
		
		public static uint Multiplier(uint Factor_One, uint Factor_Two)
		{
			uint Ans;
			Ans = 0;
			while(Factor_Two != 0)
			{
				if((Factor_Two & 1) == 1)
				{
					Ans = Adder(Ans, Factor_One);
				}
				Factor_Two >>= 1;
				Factor_One <<= 1;
			}
			return Ans;
		}
		
		public static uint Divider(uint Dividend, uint Divisor)
		{
			uint Quotient, Hold, Temp;
			
			Hold = Divisor;
			Quotient = 0;
			while(Hold < (1 << 30) && Hold < Dividend)  // Should always be 1 << (Bitsize - 2);
			{
				Hold <<= 1;
			}
			while(Hold >= Divisor)
			{
				Quotient <<= 1;
				if(Dividend >= Hold)
				{
					Temp = Adder(~Hold, 1);
					Dividend = Adder(Dividend, Temp);
					Quotient |= 1;					
				}
				Hold >>= 1;
			}
			return Quotient;			
		}
		
		public static uint Modulus(uint Dividend, uint Divisor)
		{
			uint Hold, Temp;
			
			Hold = Divisor;
			while(Hold < (1 << 30) && Hold < Dividend)   // Should always be 1 << (Bitsize - 2);	
			{
				Hold <<= 1;
			}
			while(Hold >= Divisor)
			{
				if(Dividend >= Hold)
				{
					Temp = Adder(~Hold, 1);
					Dividend = Adder(Dividend, Temp);			
				}
				Hold >>= 1;
			}
			return Dividend;			
		}
		
		public static uint SquareRoot(uint Square)
		{
			uint Root, Temp, One;
			Root = 0;
			One = 1 << 30;  // 1 << (BitSize - 2);
			while(One > Square)
			{
				One >>= 2;
			}
			while(One != 0)
			{
				Temp = Adder(Root, One);
				Root >>= 1;
				if(Square >= Temp)
				{
					Square = Subtractor(Square, Temp);
					Root = Adder(Root, One);
				}
				One >>= 2;
			}
			return Root;
		}
					
		public static uint Logarithm(uint Num, uint Base)
		{
			uint Exp;
			
			Exp = 0;
			while(Num >= Base)
			{
				Num = Divider(Num, Base);
				Exp = Adder(Exp, 1);
			}
			return Exp;
		}
		
		public static uint BinaryLogarithm(uint Num)
		{
			uint Exp;
			
			Exp = 0;
			while(Num >= 2)
			{
				Num >>= 1;
				Exp = Adder(Exp, 1);
			}
			return Exp;
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions