Last active
September 6, 2018 06:51
-
-
Save doug65536/8beb52a93f1e7384486ab87b4a1fd289 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Writing 16 bit value 0xbbaa (ByteCount = 2) at various offsets | |
+-----------------------------------+---------------------------------------------------------+ | |
| DW | | | |
+--------+--------+--------+--------+-------------+-------------------------------------------+ | |
+ byte 3 | byte 2 | byte 1 | byte 0 | Address & 3 | ((LowerAddress & 3) + ByteCount + 3) >> 2 | | |
+--------+--------+--------+--------+-------------+-------------------------------------------+ | |
| | | bb | aa | 0 | ((0 & 3) + (2 + 3)) >> 2 = 5 / 4 = 1 | | |
| | | | | | | | |
| | bb | aa | | 1 | ((1 & 3) + (2 + 3)) >> 2 = 6 / 4 = 1 | | |
| | | | | | | | |
| bb | aa | | | 2 | ((2 & 3) + (2 + 3)) >> 2 = 7 / 4 = 1 | | |
| | | | | | | | |
| aa | | | | 3 | ((3 & 3) + (2 + 3)) >> 2 = 8 / 4 = 2 | | |
+--------+--------+--------+--------+-------------+-------------------------------------------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[12:58] promach_, regarding last gist comment. pow2 must be a power of two, otherwise it won't work
[12:58] <promach_> yeah, PCIe is a computer protocol, and it should follow the binary rule of computing
[13:00] 2^n is guaranteed to have n zeros in the least significant bits, and bit n is guaranteed to be 1, and all bits to the left of bit n are guaranteed to be zero
[13:00] == M_D_K [~[email protected]] has quit [Quit: ZNC - http://znc.in]
[13:01] (2^n)-1 is guaranteed to have (n-1) 1's in the least significant bits, and all bits >= bit n are guaranteed to be zero
[13:01] ^ that is why it works
[13:03] promach_, pow2-1 is n 1s, ~(pow2 - 1) has all 1s from position n+1 onwards, (x + (pow2-1)) has a 1 in position n+1 and 0s onwards
why does ROUNDUP(x, pow2) = ((x + (pow2 - 1)) & ~(pow2 - 1)) round up to the next integer in pow (where pow2 must be a power of two ) ?
[21:33] promach_: there are two cases to consider - first when x is already multiple of pow2, then the operation is no-op, check yourself
[21:34] promach_: otherwise, x + pow2 - 1 will be >= next multiple of pow2
[21:34] and & ~(pow2-1) just clears low bits
https://www.reddit.com/r/math/comments/7hxr3z/poweroftwo_integer_roundup_maths_algorithms/