Re: [CHALLENGE] finding rightmost zero bit



"Bart Vandewoestyne" <MyFirstName.MyLastName@xxxxxxxxxx> wrote in message
news:1125320645.463751@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

A solution and some comments:

# File: funcs.s
# Public domain 2005 James Van Buskirk

..globl _timer_
..globl _findpos_

..data

..text
..align 4

_timer_:
rdtsc
ret

_findpos_:
movl 4(%esp), %eax
movl 0(%eax), %eax
not %eax
bsf %eax, %eax
inc %eax
ret

# End of file: funcs.s

! File: time_findpos.f90
! Public domain 2005 James Van Buskirk

module mykinds
implicit none
integer, parameter :: ik8 = selected_int_kind(18)
integer, parameter :: dp = selected_real_kind(15,300)
end module mykinds

program time_findpos
use mykinds
implicit none
interface
function timer()
use mykinds
implicit none
integer(ik8) timer
end function timer
end interface
interface
function findpos(x)
implicit none
integer, intent(in) :: x
integer findpos
end function findpos
end interface
integer start, finish
integer(ik8) t0, t1
real(dp), parameter :: freq = 1.4e9_dp
integer i
integer(ik8) total


write(*,'(a)',advance='no') ' Enter first number:> '
read(*,*) start
write(*,'(a)',advance='no') ' Enter lasst number:> '
read(*,*) finish
total = 0
t0 = timer()
do i = start, finish
total = total+findpos(i)
end do
t1 = timer()
write(*,'(a,i0)') ' Total bits counted = ', total
write(*,'(a,f0.3)') ' Total time = ', (t1-t0)/freq
end program time_findpos

! End of file: time_findpos

C:\g95_MINGW\clf\findpos>as funcs.s -ofuncs.o

C:\g95_MINGW\clf\findpos>g95 -std=f95 time_findpos.f90
funcs.o -otime_findpos

C:\g95_MINGW\clf\findpos>time_findpos
Enter first number:> 1
Enter lasst number:> 147483647
Total bits counted = 294967286
Total time = 3.046

Your test program doesn't provide for an output so that
correctness can be verified.

It's unusual for a Fortran program to count the least significant
bit as bit 1. The model for integer data considers it to be
bit 0.

Your test data might prove to be a bad representative compared
to real-world data, and is an illustration of why fools often
consider branchful code to be superior to branchless code.
If the loop is unrolled a bit, branch prediction will work
well for your data, because you are testing numbers in
arithmetic progression. Try a data set where the outputs
will look more random, and also perhaps a data set where high-
valued outputs are more common. The g95+as answer given above
might look better given such a context.

--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end


.