//
// Mathematical functions useful in computer calculations.
//


#import "compmath.h"


//
// log2()
//
// Return base-2 log of x.
//

double log2(double x)
{
    return (log(x) / log(2));
}


//
// pow2()
//
// Return 2^n.
//

int pow2(int n)
{
    switch(n) {
        case  0: return 0x00000001; break;
	case  1: return 0x00000002; break;
	case  2: return 0x00000004; break;
	case  3: return 0x00000008; break;
	case  4: return 0x00000010; break;
	case  5: return 0x00000020; break;
	case  6: return 0x00000040; break;
	case  7: return 0x00000080; break;
        case  8: return 0x00000100; break;
	case  9: return 0x00000200; break;
	case 10: return 0x00000400; break;
	case 11: return 0x00000800; break;
	case 12: return 0x00001000; break;
	case 13: return 0x00002000; break;
	case 14: return 0x00004000; break;
	case 15: return 0x00008000; break;
        case 16: return 0x00010000; break;
	case 17: return 0x00020000; break;
	case 18: return 0x00040000; break;
	case 19: return 0x00080000; break;
	case 20: return 0x00100000; break;
	case 21: return 0x00200000; break;
	case 22: return 0x00400000; break;
	case 23: return 0x00800000; break;
        case 24: return 0x01000000; break;
	case 25: return 0x02000000; break;
	case 26: return 0x04000000; break;
	case 27: return 0x08000000; break;
	case 28: return 0x10000000; break;
	case 29: return 0x20000000; break;
	case 30: return 0x40000000; break;
	case 31: return 0x80000000; break;
    }

    return 0x000000001;
}


//
// ispowerof2()
//
// Return 0 if n is not a power of 2, otherwise return 1.
//

int ispowerof2(unsigned int n)
{
    switch(n) {
	case 0x00000001: case 0x00000002: case 0x00000004: case 0x00000008:
	case 0x00000010: case 0x00000020: case 0x00000040: case 0x00000080:
	case 0x00000100: case 0x00000200: case 0x00000400: case 0x00000800:
	case 0x00001000: case 0x00002000: case 0x00004000: case 0x00008000:
	case 0x00010000: case 0x00020000: case 0x00040000: case 0x00080000:
	case 0x00100000: case 0x00200000: case 0x00400000: case 0x00800000:
	case 0x01000000: case 0x02000000: case 0x04000000: case 0x08000000:
	case 0x10000000: case 0x20000000: case 0x40000000: case 0x80000000:
	    return 1;
	    break;
	default:
	    return 0;
	    break;
    }
    
    return 0;
}


//
// bits2long()
//
// Converts an array of bits to a long.
//

long bits2long(bit *bits, int numbits)
{
    long	temp = 0L;
    int		i;
    
    if(numbits < 1)
        return temp;
    
    for(i = (numbits - 1); i >= 0; i--) {	// Grab bits, highest-order first.
        temp << 1;				// Shift temp left 1 bit.
 	temp = temp + bits[i];			// Add the current bit.
    }
    
    return temp;
}


//
// bitsequal()
//
// Returns 1 if all the bits in the arrays are equal,
// otherwise returns 0.
//
int bitsequal(bit *a, bit *b, int numbits)
{
    int i;
    
    for(i = 0; i < numbits; i++) {
        if(a[i] != b[i])
	    return 0;
    }
    
    return 1;
}


//
// End of file.
//