root / synthbench / euroben-ports / base / C-MPI / mod2f / ilog2.c @ 0:839f52ef7657
History | View | Annotate | Download (798 Bytes)
1 |
#include <stdio.h> |
---|---|
2 |
#include <stdlib.h> |
3 |
|
4 |
int ilog2( int n ) |
5 |
// ---------------------------------------------------------------------
|
6 |
// --- Function 'log2' determines whether the integer input argument
|
7 |
// is a power of 2. If so, it returns this power.
|
8 |
// ---------------------------------------------------------------------
|
9 |
{ |
10 |
int m, n1;
|
11 |
// ---------------------------------------------------------------------
|
12 |
if ( n < 1 ) { |
13 |
printf( "*** ERROR: argument %d is not valid\n", n );
|
14 |
exit( -1 );
|
15 |
} |
16 |
if ( n == 1 ) return( 0 ); |
17 |
n1 = n; |
18 |
m = 0;
|
19 |
while( 1 ) { |
20 |
if ( ( n1 != 1 ) && ( n1%2 != 0 ) ) { |
21 |
printf( "*** ERROR: argument %d is not a power of 2\n", n );
|
22 |
exit( -1 );
|
23 |
} |
24 |
if ( n1 == 1 ) return( m ); |
25 |
n1 = n1/2;
|
26 |
m++; |
27 |
} |
28 |
} |