| 1 |
/*********************************************************************** |
| 2 |
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC * |
| 3 |
* Copyright (C) 2001 by New Riders Publishing * |
| 4 |
* See COPYRIGHT for license information. * |
| 5 |
***********************************************************************/ |
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
#include <stdlib.h> |
| 9 |
|
| 10 |
int main (int argc, char* argv[]) |
| 11 |
{ |
| 12 |
long max = atoi (argv[1]); |
| 13 |
long number; |
| 14 |
unsigned position; |
| 15 |
volatile unsigned result; |
| 16 |
|
| 17 |
/* Repeat the operation for a large number of values. */ |
| 18 |
for (number = 1; number <= max; ++number) { |
| 19 |
/* Compute the position of the most significant set bit using the |
| 20 |
bsrl assembly instruction. */ |
| 21 |
asm ("bsrl %1, %0" : "=r" (position) : "r" (number)); |
| 22 |
result = position; |
| 23 |
} |
| 24 |
|
| 25 |
return 0; |
| 26 |
} |