Not sure if it’s been posted before, but I’ve just stumbled upon this interesting video on YT:
There are several funny parts i.e. the author spends most of the time explaining Fortran talking about implicit none lol. The most interesting part to me is that he came up with some Fortran code to verify a list of prime numbers that’s ~2x slower than his C++ implementation (not shown in the video).
It seems like he’s doing that with bit operations on an array of integer(int8) integers, like
logical function getbit(n)
integer(int64), intent(in) :: n
integer, parameter :: bitfield_unit = 8
getbit = btest(array_of_bits(n/bitfield_unit),mod(n,bitfield_unit)) ! array_of_bits was preallocated
end function getbit
You are quite right. The code does not exploit the fact that bitfield_unit is a power of 2, as a consequence of which n/bitfield_unit could be replaced by shiftr(i,3). Likewise, mod(n,bitfield_unit)) could be replaced by iand(n,7). Depending on what the rest of the code does with the result getbit, the new popcnt and, if available, lzcnt intrinsics may improve the efficiency even more.
Right, since the array of bits is defined integer(int8) so he’s just locating the n-th bit into that large array, as of course that’s the smallest integer type he can use for that. I wonder how faster that would have become (at the price of more memory usage), had he used logical(int8)s instead of bits…