Skip to content

Commit

Permalink
Merge pull request #1011 from marshallward/popcnt
Browse files Browse the repository at this point in the history
Use popcnt intrinsic for bitcount
  • Loading branch information
adcroft authored Oct 1, 2019
2 parents 3418c1d + e62524b commit 9ca5fb9
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions src/framework/MOM_checksums.F90
Original file line number Diff line number Diff line change
Expand Up @@ -1852,20 +1852,12 @@ end subroutine chksum_error
!> Does a bitcount of a number by first casting to an integer and then using BTEST
!! to check bit by bit
integer function bitcount(x)
real :: x !< Number to be bitcount
real, intent(in) :: x !< Number to be bitcount

! Local variables
integer(kind(x)) :: y !< Store the integer representation of the memory used by x
integer :: bit

bitcount = 0
y = transfer(x,y)

! Fortran standard says that bit indexing start at 0
do bit = 0, bit_size(y)-1
if (BTEST(y,bit)) bitcount = bitcount+1
enddo
integer, parameter :: xk = kind(x) !< Kind type of x

! NOTE: Assumes that reals and integers of kind=xk are the same size
bitcount = popcnt(transfer(x, 1_xk))
end function bitcount

end module MOM_checksums

0 comments on commit 9ca5fb9

Please sign in to comment.