All pastes #386510 Raw Edit

Noia

public text v1 · immutable
·published 2007-03-08 17:54 UTC
rendered paste body
public static DevInt[] operator /(DevInt dividen, DevInt divisor) {
        // Quotient provided as two-part array containing quotient and remainder respectivly.
        DevInt quotient = new DevInt();
        DevInt remainder = new DevInt(dividen);
        Console.WriteLine(
            ((dividen.Length - divisor.Length) * 8) + "+" + (dividen.MSB - divisor.MSB) + "\nDivisor " + divisor + "\nDividen " + dividen

            );
        Console.WriteLine("Compare "+compare(divisor,dividen));
        if (divisor == 0) {
            throw new ArgumentException("Divide by Zero exception");
        }
        if (divisor > dividen) {
            Console.WriteLine("Divisor > dividen");
            return new DevInt [] { quotient, dividen }; // although implied below, its best to test now that to have to go through these steps again.
        }
        if ((divisor.Length == 1) && ((divisor[0] ^ 0xFE) == 0xFF)) { // divide by 1 optimization.
            return new DevInt[] { dividen, new DevInt() };
        }
        if ( ((divisor[0] | 0xFE) == 0xFF) && ((dividen[0] | 0xFE) == 0xFF) ) {
            // it will be a round number and no remainder, should potentialy be solved with a shifting algorithm
        }
        int x = (8*divisor.length) - (8 - divisor.MSB);
        DevInt tmpdivisor = divisor << ((dividen.Length - divisor.Length) * 8) + (dividen.MSB - divisor.MSB);
        
           // ((dividen.MSB + 8 * dividen.Length) - (divisor.MSB + 8 * (divisor.Length - 1))); // shift one more than what is needed.
        
        int i = 0;
        Console.WriteLine("BEFORE! remainder:" + remainder + "\ntmpdivisor" + tmpdivisor + "\nquotient" + quotient + "\n----");
        do {
            i++;
            Console.WriteLine("\tDURING loop "+i+" remainder:" + remainder + "\ntmpdivisor" + tmpdivisor + "\nquotient" + quotient + "\n----");

            if (remainder >= tmpdivisor) {
                Console.WriteLine("\tremainder = remainder - tmpdivisor;\nremainder:" + remainder + "\ntmpdivisor" + tmpdivisor);
                remainder = remainder - tmpdivisor;
                quotient = quotient << 1;   // Shift must happen before the value is writen, otherwise it will overwrite the value of the previous operation
                quotient[0] = (byte)(quotient[0] | 0x01);
                
             
            }
            else {
                Console.WriteLine("\tremainder < tmpdivisor; shift more");
                quotient = quotient << 1;
                //quotient[0] = (byte)(quotient[0] | 0x00);
            }
            tmpdivisor = tmpdivisor >> 1;

        } while ((i != x) && (remainder > tmpdivisor));
        if (remainder == divisor) { // it can be done once more 
            Console.WriteLine("---------------\nAFTER!\n remainder == divisor!!\n        remainder:" + remainder + "\ntmpdivisor" + tmpdivisor + "\nquotient" + quotient + "\nx= "+x+" i= "+i+"\n--:::--");
            Console.WriteLine("tmpdivisor.MSB = " + tmpdivisor.MSB);
            return new DevInt[] { (quotient + 1), new DevInt() };
        }
        
        Console.WriteLine("---------------\nAFTER! remainder:" + remainder + "\ntmpdivisor" + tmpdivisor + "\nquotient" + quotient + "\n----");
        Console.WriteLine("tmpdivisor.MSB = " + tmpdivisor.MSB);
        return new DevInt[] { quotient, remainder };
        
    }