Consider the following scenario: let "background" and "foreground" be two layers of visual data, composed of 16-bit values. I'd like to merge them as follows - for each offset, if foreground contains zero, it is considered transparent (=use the background value). Otherwise, use the value in foreground.
Something like this:
I'm looking to implement this in 8086 assembly. If I test each word value for zero and use conditional jumps, it's straightforward... but also very, very slow.
Is there a faster, more optimized way to do this? Hopefully without branching, if at all possible? (The choice of zero to represent transparency is arbitrary, BTW).
One way is to "compile" the layers, as in the "compiled sprites" technique sometimes used for fast animation... i.e. instead of plain data, generate the layers as code that performs the merging in-place (with the data as immediate values), and simply does nothing in the 'transparent' case. But that wouldn't work so well, because my use-case requires working with arbitrary pieces of each layer (so the code wouldn't have a fixed entry point, and the expense of compensating for this could nullify a lot of the gain).
Something like this:
Code:
for (i=0; i<size; i++)
{
merged[i] = (foreground[i] == 0) ? background[i] : foreground[i];
}
I'm looking to implement this in 8086 assembly. If I test each word value for zero and use conditional jumps, it's straightforward... but also very, very slow.
Is there a faster, more optimized way to do this? Hopefully without branching, if at all possible? (The choice of zero to represent transparency is arbitrary, BTW).
One way is to "compile" the layers, as in the "compiled sprites" technique sometimes used for fast animation... i.e. instead of plain data, generate the layers as code that performs the merging in-place (with the data as immediate values), and simply does nothing in the 'transparent' case. But that wouldn't work so well, because my use-case requires working with arbitrary pieces of each layer (so the code wouldn't have a fixed entry point, and the expense of compensating for this could nullify a lot of the gain).