nextpwr2 Function

public function nextpwr2(v) result(res)

Get the next power of 2. i.e given 5 will return 8 (4^2) only works on 32bit ints ref

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: v

Return Value integer


Source Code

    integer function nextpwr2(v) result(res)
    !! Get the next power of 2. i.e given 5 will return 8 (4^2)
    !! only works on 32bit ints
    !! [ref](https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2)
        integer, intent(in) :: v

        res = v - 1
        res = ior(res, rshift(res, 1))
        res = ior(res, rshift(res, 2))
        res = ior(res, rshift(res, 4))
        res = ior(res, rshift(res, 8))
        res = ior(res, rshift(res, 16))
        res = res + 1

    end function nextpwr2