On 11/21/06, Bill Long <[log in to unmask]> wrote:
If it is written in assembler, I'm not sure why you would call it a "C function".

One can more easily mix assembler in C and let the C compiler take care of the calling sequence etc. I finally found an example of how to do it (with gcc at least):

// This only works on x86(_64) machines with gcc and
// only works for 32-bit integers!
unsigned leadz_c(int number) {
   unsigned position;
   if(number)
      asm("bsrl %1, %0" : "=r" (position) : "r" (number));
   else
      position=-1;       
   return(31-position);
}

which can be called from Fortran using Interop:

   interface
      function leadz_c(i) bind(c)
         integee(c_int), value :: i
         integer(c_int) :: leadz_c
      end function
   end interface

LEADZ was part of the HPF spec.  If your compiler supports (or used to support) HPF, it may already have a routine in the supplied intrinsics library.  You could probably call such a routine directly with the right interface.
Yes, but at least one compiler I tried did not use the pentium instruction but rather generated inefficient inlined code that uses shifting to find the first nonzero bit. Probably no one complained yet since no one has used LEADZ in a long time :-\

Aleks