ivanceras
ivanceras

Reputation: 1455

Is the cost of bitwise operations on 64-bit integers the same as 8-bit integers?

My code involves doing bitwise operations on a huge array of integers. If understand correctly, 64-bit computers do calculations on 64-bit integers in one clock cycle. If I am doing an 8-bit integer bitwise operation, it still consumes 1 clock cycle. If I do eight 8-bit integer operations, it will consume 8 clock cycles. Knowing that I can fit eight 8-bit integers into a 64-bit integer, and do the bitwise operation on the 64-bit integer, will I consume 1 clock cycle instead of 8 clock cycles?

Upvotes: 2

Views: 2118

Answers (2)

user1196549
user1196549

Reputation:

Be prepared for blazing speed with vectorized operations: using the SSE2 or AVX2 intrinsics, you can process 128 or 256 bits in a single go (_m128i _mm_and_si128, _mm256_and_si256 and similar). And the forthcoming AVX512 extensions will allow 512 bits at a time!

Upvotes: 1

Arkku
Arkku

Reputation: 42159

The number of clock cycles taken on a 64-bit operation is not guaranteed to be 1 even on a 64-bit machine, but obviously the processor doesn't know whether the 64-bit value stands for one 64-bit or eight 8-bit integers, so the bitwise operation itself will be as fast for both cases. This part of the code will also almost certainly perform much better for the single 64-bit value, as the 64-bit processor probably works on 64- (or at least 32-bit) quantities even when you do the operations on smaller variables.

For the overall performance of your program much depends on how often you'd then need to convert between the 8- and 64-bit data; the typical indexing of a single 8-bit integer stored within an array of 64-bit integers would be something like (a[i / 8] >> ((i % 8) * 8)) & 0xFF - so at least on the C side† it would add complexity if done often, but if most of your operations are repeated for all elements of the array then the 64-bit solution is likely to win regardless (bearing in mind that the compiler may have to generate similar masking when working on 8-bit variables anyhow).

† You may wish to look at the generated assembler to verify actual complexity, it may look quite different depending on the instruction set…

Upvotes: 3

Related Questions