Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to calculate number of padding bytes

What's the most efficient way to calculate the amount of padding for 8-bit data that needs to be a multiple of 32-bit in C?

At the moment I do it like this:

pad = (4-size%4)%4;
like image 513
Tom Avatar asked Dec 30 '13 18:12

Tom


3 Answers

As long as the optimizing compiler uses bitmasking for the % 4 instead of division, I think your code is probably pretty good. This might be a slight improvement:

// only the last 2 bits (hence & 3) matter
pad = (4 - (size & 3)) & 3;

But again, the optimizing compiler is probably smart enough to be reducing your code to this anyway. I can't think of anything better.

like image 143
TypeIA Avatar answered Oct 23 '22 21:10

TypeIA


// align n bytes on size boundary
pad n size = (~n + 1) & (size - 1)

this is similar to TypeIA's solution and only machine language ops are used.

(~n + 1) computes the negative value, that would make up 0 when added to n
& (size - 1) filters only the last relevant bits.

examples

pad 13 8 = 3
pad 11 4 = 1
like image 29
citykid Avatar answered Oct 23 '22 21:10

citykid


pad = (-size)&3;

This should be the fastest.

size 0: pad 0
size 1: pad 3
size 2: pad 2
size 3: pad 1
like image 42
Wenjin Zhang Avatar answered Oct 23 '22 22:10

Wenjin Zhang