Jump to content

  • Log In with Google      Sign In   
  • Create Account

This beauty...

  • You cannot reply to this topic
17 replies to this topic

#1 Migi0027   Crossbones+   -  Reputation: 2117

Like
2Likes
Like

Posted 17 September 2013 - 07:05 AM

Just saw this beauty from some example code:

DXT3AlphaBlock compressDXT3Alpha(vec4 colors[16])
{
	DXT3AlphaBlock dxt3Alpha;

	unsigned int quantizedAlpha0 = quantize4(int(colors[0].w * 255.0 + .5));
	unsigned int quantizedAlpha1 = quantize4(int(colors[1].w * 255.0 + .5));
	unsigned int quantizedAlpha2 = quantize4(int(colors[2].w * 255.0 + .5));
	unsigned int quantizedAlpha3 = quantize4(int(colors[3].w * 255.0 + .5));
	unsigned int quantizedAlpha4 = quantize4(int(colors[4].w * 255.0 + .5));
	unsigned int quantizedAlpha5 = quantize4(int(colors[5].w * 255.0 + .5));
	unsigned int quantizedAlpha6 = quantize4(int(colors[6].w * 255.0 + .5));
	unsigned int quantizedAlpha7 = quantize4(int(colors[7].w * 255.0 + .5));
	dxt3Alpha.alphas[0] =
		quantizedAlpha0 << unsigned int(0) |
		quantizedAlpha1 << unsigned int(4) |
		quantizedAlpha2 << unsigned int(8) |
		quantizedAlpha3 << unsigned int(12) |
		quantizedAlpha4 << unsigned int(16) |
		quantizedAlpha5 << unsigned int(20) |
		quantizedAlpha6 << unsigned int(24) |
		quantizedAlpha7 << unsigned int(28);
	
	unsigned int quantizedAlpha8 = quantize4(int(colors[8].w * 255.0 + .5));
	unsigned int quantizedAlpha9 = quantize4(int(colors[9].w * 255.0 + .5));
	unsigned int quantizedAlpha10 = quantize4(int(colors[10].w * 255.0 + .5));
	unsigned int quantizedAlpha11 = quantize4(int(colors[11].w * 255.0 + .5));
	unsigned int quantizedAlpha12 = quantize4(int(colors[12].w * 255.0 + .5));
	unsigned int quantizedAlpha13 = quantize4(int(colors[13].w * 255.0 + .5));
	unsigned int quantizedAlpha14 = quantize4(int(colors[14].w * 255.0 + .5));
	unsigned int quantizedAlpha15 = quantize4(int(colors[15].w * 255.0 + .5));
	dxt3Alpha.alphas[1] =
		quantizedAlpha8 << unsigned int(0) |
		quantizedAlpha9 << unsigned int(4) |
		quantizedAlpha10 << unsigned int(8) |
		quantizedAlpha11 << unsigned int(12) |
		quantizedAlpha12 << unsigned int(16) |
		quantizedAlpha13 << unsigned int(20) |
		quantizedAlpha14 << unsigned int(24) |
		quantizedAlpha15 << unsigned int(28);
	
	return dxt3Alpha;
}

What do you think?

-MIGI0027


Hi! Cuboid Zone
The Rule: Be polite, be professional, but have a plan to steal all their shaders!

Sponsor:

#2 Wolftein   Members   -  Reputation: 167

Like
1Likes
Like

Posted 17 September 2013 - 01:17 PM

Who ever coded that doesn't know what "funroll-all-loops" is :P and tried to achieve same result by hand.


Feel free to comment and star my project! <https://github.com/Ghrum/Ghrum>


#3 Servant of the Lord   Crossbones+   -  Reputation: 21008

Like
8Likes
Like

Posted 17 September 2013 - 02:12 PM

Almost looks like ASCII art - is that a Death Star turret gun?


It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#4 Migi0027   Crossbones+   -  Reputation: 2117

Like
0Likes
Like

Posted 17 September 2013 - 02:19 PM

Almost looks like ASCII art - is that a Death Star turret gun?

 

Hah! Never saw that coming, but now when I look at it... huh.png


Hi! Cuboid Zone
The Rule: Be polite, be professional, but have a plan to steal all their shaders!

#5 mhagain   Crossbones+   -  Reputation: 8277

Like
0Likes
Like

Posted 17 September 2013 - 05:37 PM

Could be code generated by a tool, although it would be unusual to see that in sample code.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#6 JTippetts   Moderators   -  Reputation: 8650

Like
3Likes
Like

Posted 17 September 2013 - 09:43 PM

Hasn't really been all that many years since hand unrolling was a common practice. Still, it is pretty unusual to see it nowadays.



#7 Hodgman   Moderators   -  Reputation: 31822

Like
3Likes
Like

Posted 17 September 2013 - 10:15 PM

I actually find that the original version is more easily understood than doing it with loops tongue.png

DXT3AlphaBlock compressDXT3Alpha(vec4 colors[16])
{
	DXT3AlphaBlock dxt3Alpha;

	for( int j=0; j!=2; ++j )
	{
		unsigned int result = 0;
		for( int i=7; i>=0; --i )
		{
			unsigned int quantized = quantize4(int(colors[i+j*8].w * 255.0 + .5));
			result = result<<2 | quantized;
		}
		dxt3Alpha.alphas[j] = result;
	}

	return dxt3Alpha;
}


#8 DekuTree64   Members   -  Reputation: 986

Like
1Likes
Like

Posted 18 September 2013 - 08:20 AM

Not that bad. Probably about the same amount of time to write either way. More thought in a loop, more copypasta and speed typing in the unrolled version. Loop is quicker to edit later, but it doesn't look like the kind of thing that would ever need editing. I think it's more readable in the unrolled form.



#9 Krohm   Crossbones+   -  Reputation: 3249

Like
3Likes
Like

Posted 19 September 2013 - 12:44 AM

Amateur. I've once dealt with code written... I think in Korea. The variables were numbered up to... like 74, in a convenient 800 LOC function.



#10 fir   Members   -  Reputation: -456

Like
0Likes
Like

Posted 19 September 2013 - 01:22 AM

Not that bad. Probably about the same amount of time to write either way. More thought in a loop, more copypasta and speed typing in the unrolled version. Loop is quicker to edit later, but it doesn't look like the kind of thing that would ever need editing. I think it's more readable in the unrolled form.

 

I agree, imo loop version is probably somewhat better for me but this is okay too



#11 l0calh05t   Members   -  Reputation: 816

Like
1Likes
Like

Posted 24 September 2013 - 06:04 AM

 

I actually find that the original version is more easily understood than doing it with loops tongue.png

DXT3AlphaBlock compressDXT3Alpha(vec4 colors[16])
{
	DXT3AlphaBlock dxt3Alpha;

	for( int j=0; j!=2; ++j )
	{
		unsigned int result = 0;
		for( int i=7; i>=0; --i )
		{
			unsigned int quantized = quantize4(int(colors[i+j*8].w * 255.0 + .5));
			result = result<<2 | quantized;
		}
		dxt3Alpha.alphas[j] = result;
	}

	return dxt3Alpha;
}

Shouldn't that result be shifted by 4, not 2? Also, why not do a forward loop and shift quantized instead of result?

for(unsigned j = 0; j < 8; ++j)
{
	unsigned int quantized = quantize4(int(colors[i+j*8].w * 255.0 + .5));
	result |= quantized << (4 * j);
}

Most people would consider it more readable and the multiply will be optimized away during unrolling anyways. Personally I'd also write the reverse loop as (should mention though that some compilers don't manage to unroll this, but the specific compiler I'm thinking of is unable to unroll both types of reverse loop, so...)

for(unsigned j = 8; j-- != 0;) // or even for(unsigned j = 8; j--;)

Edited by l0calh05t, 01 October 2013 - 09:10 AM.


#12 Hodgman   Moderators   -  Reputation: 31822

Like
0Likes
Like

Posted 24 September 2013 - 06:21 AM


Shouldn't that result be shifted by 4, not 2? Also, why not do a forward loop and shift quantized instead of result?
Yep, typo. I of course didn't test that code biggrin.png

 

I wrote the shifting logic that way because I've been burnt by a micro-coded shift-by-variable instruction, where shifting by a constant takes 1 cycle, but shifting by a variable breaks down into a little for loop that shifts by 1 n times, over n*k cycles... I can't actually remember whether this is the case on modern PC CPUs or not tongue.png



#13 Sik_the_hedgehog   Crossbones+   -  Reputation: 1833

Like
0Likes
Like

Posted 24 September 2013 - 06:40 PM

I really wonder which CPU did that to you because I can't think of any like that (either you could only shift by 1, or you could shift multiple bits either by a constant or by a register).

 

Unless you mean the microcode in CPUs... They used to have a barrel shifter that did any bit shift in 1 cycle (especially useful for indirect addressing modes since the index could be bit shifted), but I think Intel removed that at some point. No idea what's the current situation right now (but even then it's still the fastest method).


Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

#14 Hodgman   Moderators   -  Reputation: 31822

Like
2Likes
Like

Posted 24 September 2013 - 09:21 PM

I really wonder which CPU did that to you because I can't think of any like that.
Unless you mean the microcode in CPUs...

Yeah. The PPC's used in the PS3/360 have a shift-by-variable instruction, but it's microcoded. When the CPU hits one of them, it flushes the CPU pipeline, fetches the algorithm from ROM and runs it (while locking down shared CPU resources - e.g. dual issuing disabled), pretending that it was just a single instruction, before resuming normal operations.
 
It's nice that the new consoles are moving over to x86-land now and ditching these simple CPUs... though I do have a soft-spot for the SPE's; async memcpy ftw!


Edited by Hodgman, 24 September 2013 - 09:27 PM.


#15 Sik_the_hedgehog   Crossbones+   -  Reputation: 1833

Like
1Likes
Like

Posted 24 September 2013 - 10:34 PM

That's the most broken implementation of microcode I've ever seen o_____O


Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

#16 TheComet   Crossbones+   -  Reputation: 1643

Like
0Likes
Like

Posted 02 October 2013 - 07:33 AM

What do you think?

 

The reason why it's unrolled is because it's speed critical (considering this is code for graphics processing).

 

What the person probably didn't know is that the compiler's optimizer will perform loop unrolls on short for-loops anyway, so Hodgman's solution will produce the same result as the code posted by the OP when looking at the disassembly.

 

In the worst of cases, you could use C++ templates to unroll loops, but I wouldn't encourage it.


YOUR_OPINION >/dev/null

#17 BGB   Crossbones+   -  Reputation: 1554

Like
2Likes
Like

Posted 02 October 2013 - 12:06 PM

 

What do you think?

 

The reason why it's unrolled is because it's speed critical (considering this is code for graphics processing).

 

What the person probably didn't know is that the compiler's optimizer will perform loop unrolls on short for-loops anyway, so Hodgman's solution will produce the same result as the code posted by the OP when looking at the disassembly.

 

In the worst of cases, you could use C++ templates to unroll loops, but I wouldn't encourage it.

 

 

manual unrolling can make a bit more of a difference though if often compiling code in debug mode and still needing good performance, which can still happen sometimes (leading, sometimes, to cases where manual unrolling is justified).

 

but, then again, I am left to suspect in the above that mixing floating point and integer math could potentially be a bigger performance impact than would be whether or not the loop is unrolled.



#18 wodinoneeye   Members   -  Reputation: 877

Like
0Likes
Like

Posted 12 October 2013 - 01:41 AM

"I wrote the shifting logic that way because I've been burnt by a micro-coded shift-by-variable instruction, where shifting by a constant takes 1 cycle, but shifting by a variable breaks down into a little for loop that shifts by 1 n times, over n*k cycles... I can't actually remember whether this is the case on modern PC CPUs or not tongue.png

"

 

But why all the bulking up the cose with  unsigned int(4)   unsigned int(8)   when  4 , 8 ...     is all you need  The shift only takes ints so you could unclutter it a bit at least for that

 

 

Also since you own this function couldnt you shorten the variable names  (its really fairly obvious what it does and is quite repetative and a simple comment would tell any less knowlegable person what it does

 

 

also  what is  the guts of   quantize4()   and could you imbed it even further to chop out extraneous operations ???

A table lookup for a non linear conversion function with a byte domain  (feeding it the float to int calc directly as subscript)  ???

or mutate the inline quantize4()   function  with the    * 255.0 + .5  imbedded inside it return an unsigned int from it and imbed the whole call right into the  shift equation sequence    (probably need to speed testcompare  it to see if any such condensations  make any diference besides looking cleaner  .... getting rid of the intermediary variables....)

 

heh,  you could also reuse  the 0 -7 qA variables for the second set 8 -15

 

 

 

something less bulky like

DXT3AlphaBlock compressDXT3Alpha(vec4 colors[16])
{  
unsigned int    qA0,qA1,qA2,QA3,qA4,qA5,qA6,qA7;
DXT3AlphaBlock  dxt3Alpha;

qA0 = quantize4(int(colors[0].w * 255.0 + .5));
qA1 = quantize4(int(colors[1].w * 255.0 + .5));
qA2 = quantize4(int(colors[2].w * 255.0 + .5));
qA3 = quantize4(int(colors[3].w * 255.0 + .5));
qA4 = quantize4(int(colors[4].w * 255.0 + .5));
qA5 = quantize4(int(colors[5].w * 255.0 + .5));
qA6 = quantize4(int(colors[6].w * 255.0 + .5));
qA7 = quantize4(int(colors[7].w * 255.0 + .5));

dxt3Alpha.alphas[0] =
	qA0 << 0  |
	qA1 << 4  |
	qA2 << 8  |
	qA3 << 12 |
	qA4 << 16 |
	qA5 << 20 |
	qA6 << 24 |
	qA7 << 28;
	
qA0 = quantize4(int(colors[8].w * 255.0 + .5));
qA1 = quantize4(int(colors[9].w * 255.0 + .5));
qA2 = quantize4(int(colors[10].w * 255.0 + .5));
qA3 = quantize4(int(colors[11].w * 255.0 + .5));
qA4 = quantize4(int(colors[12].w * 255.0 + .5));
qA5 = quantize4(int(colors[13].w * 255.0 + .5));
qA6 = quantize4(int(colors[14].w * 255.0 + .5));
qA7 = quantize4(int(colors[15].w * 255.0 + .5));

dxt3Alpha.alphas[1] =
	qA0 << 0  |
	qA1 << 4  |
	qA2 << 8  |
	qA3 << 12 |
	qA4 << 16 |
	qA5 << 20 |
	qA6 << 24 |
	qA7 << 28;
	
return dxt3Alpha;
}

This is a function taht looks like it will be crunching alot of bulk data for texture conversion  so  doing such (and similar)  optimization could add up for the actuual programs

 

 

 

---

DXT3AlphaBlock compressDXT3Alpha(vec4 colors[16])
{  

DXT3AlphaBlock  dxt3Alpha;

dxt3Alpha.alphas[0] =
  quantize4(int(colors[0].w * 255.0 + .5)) << 0  |
  quantize4(int(colors[1].w * 255.0 + .5)) << 4  |
  quantize4(int(colors[2].w * 255.0 + .5)) << 8  |
  quantize4(int(colors[3].w * 255.0 + .5)) << 12 |
  quantize4(int(colors[4].w * 255.0 + .5)) << 16 |
  quantize4(int(colors[5].w * 255.0 + .5)) << 20 |
  quantize4(int(colors[6].w * 255.0 + .5)) << 24 |
  quantize4(int(colors[7].w * 255.0 + .5)) << 28;

dxt3Alpha.alphas[1] =
  quantize4(int(colors[8].w * 255.0 + .5)) << 0  |
  quantize4(int(colors[9].w * 255.0 + .5)) << 4  |
  quantize4(int(colors[10].w * 255.0 + .5)) << 8  |
  quantize4(int(colors[11].w * 255.0 + .5)) << 12 |
  quantize4(int(colors[12].w * 255.0 + .5)) << 16 |
  quantize4(int(colors[13].w * 255.0 + .5)) << 20 |
  quantize4(int(colors[14].w * 255.0 + .5)) << 24 |
  quantize4(int(colors[15].w * 255.0 + .5)) << 28;
	
return dxt3Alpha;
}

wrapper of    ((unsigned int)  quantize4(...))       possibly needed  if the  shift is wonky

 

 

pointer math on the      colors[].w       Float ptr  with      ptr += 4   to eliminate the .array index multiply ???

 

 

etc....


Edited by wodinoneeye, 12 October 2013 - 02:36 AM.

--------------------------------------------Ratings are Opinion, not Fact





PARTNERS