Fastest RGB 24bit color blend (and others...)

yet an other fastest…

this is inlined in 3DzzD so I hope to hadn’t made any typos’ while typing this function

NB: I may have found a faster method while reviewing this one… so I will keep this thread up shortly

EDIT : error on factor range (256 max rather thn 255)

Others :

50% blend :

As you are only using f1 twice, and it’s such a simple expression I think you’ll find it’s faster with the ‘(256-factor)’ inlined.

Neat way of doing it; probably an age old optimisation that has been forgotten (or never learnt) by most thanks to ghz processors & hardware pipelines.

[quote]As you are only using f1 twice, and it’s such a simple expression I think you’ll find it’s faster with the ‘(256-factor)’ inlined.
[/quote]
ha yes probably right, I never thought to try that

also a faster way I use sometime is 255^factor but it introduce 1 bit error

[quote]Neat way of doing it; probably an age old optimisation that has been forgotten (or never learnt) by most thanks to ghz processors & hardware pipelines.
[/quote]
probably as in the ASM period programs was always full of such optimisation, but I am just big fan of playing with bits

Nice. =D

For 50% blending, I kinda like this:
res = ((c1&0xfefeff)+(c2&0xfefeff))>>1;

There’s a slight error in the R and G channels, but it’s very fast.

error is not really noticable,

look a little like this one http://www.java-gaming.org/index.php/topic,18379.0.html ( damn… I never thought of keeping the LSB of RED :slight_smile: )

[quote=“DzzD,post:5,topic:34444”]
I’m sure you meant to say the lsb (least significant bit) of blue, rather than the LSB (Least Significant Byte) of RED =)

arg ! :slight_smile:

just though that if anybody have some other nice bits tips as Markus post above, it would be nice to post it here, so we can have a referent thread of thoses, I have a cupple more that I will try to post here too rather than opening new thread each time

Open source library of one-liner methods :smiley:

yup usefull every time and in any language (maybe can also be usefull for 4k competitor)

They arn’t graphics related, and i’m sure they’ve been posted elsewhere, but here are a few I have lying around:



	public static final int abs(int a) {
		return (a ^ (a >> 31)) - (a >> 31);
	}

	public static final int min(int a, int b) {
		final int signBits = (a - b) >> 31;

		// if b > a, signBits==0xFFFFFFFF
		// else, signBits==0x00000000

		return (b & ~signBits) | (a & signBits);
	}

	public static final int max(int a, int b) {
		final int signBits = (a - b) >> 31;

		return (b & signBits) | (a & ~signBits);
	}

	/**
	 * 
	 * Optimised method to cap a value in the range of 0 to max, Where max is a
	 * non-negative integer (aka >=0, or Z-*)
	 * 
	 * @param value
	 *            The value to be capped
	 * @param max
	 *            the maximum value
	 * @return
	 */
	public static final int cap(int value, int max) {
		return value & ~(value >> 31) & ~((max - value) >> 31) | (((max - value) >> 31) & max);
	}

Note.

They’re unlikely to be the optimal solution (improvements welcome ofcourse!)
They also aren’t guaranteed to be any faster than their respective conventional approaches. =)

If you don’t know it, the book “Hacker’s Delight” (by Henry S Warren) has lots of sneaky bit-twiddling tricks in it.
Simon

Yes, I used this method for blending in my 4k entry last year.

hehe nice one ! yes, maybe some are not the fatest but they are interresting anyway ! it seems that “boolean / two possible value” ( min/max/abs ) are in fact not the best candidate for such optimisation ( Riven posted about the last one, one or two weeks ago )

I will make a summary of them in the first post later when we will get some more

Stuck some branch-removal versions here: http://www.java-gaming.org/index.php/topic,21443.0.html