Add some documentation.

This commit is contained in:
King_DuckZ 2018-09-05 02:01:06 +01:00
commit f0e037fb36
3 changed files with 147 additions and 0 deletions

16
docs/padding.txt Normal file
View file

@ -0,0 +1,16 @@
https://stackoverflow.com/questions/11642210/computing-padding-required-for-n-byte-alignment
There is a faster way to compute the padding, if the alignment is a power of two
(2,4,8,...). The following runs because binary & is similar to % for powers of
two: %(2^x) and &(2^x-1) do the same for positive numbers. Attention: & will
delete the sign bit and therefore always returns the positive modulo result.
So (4 - (string_length & 3)) & 3 will do the same as
(4 - (string_length % 4)) % 4. Using the positive modulo property this can be
simplified to (-string_length) & 3!
If you wanna add that result to the size you can even do more optimizations:
padded_length = (string_length + 3) & ~3 Semantically this 'rounds up' the
number to the padding size of 4.