• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# This beauty...

17 replies to this topic

### #1Migi0027 (肉コーダ)  Members

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

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

### #2Wolftein  Members

Posted 17 September 2013 - 01:17 PM

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

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

### #3Servant of the Lord  Members

Posted 17 September 2013 - 02:12 PM

POPULAR

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

It's perfectly fine to abbreviate my username to 'Servant' or 'SotL' 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 -

### #4Migi0027 (肉コーダ)  Members

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...

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

### #5mhagain  Members

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.

### #6JTippetts  Moderators

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.

### #7Hodgman  Moderators

Posted 17 September 2013 - 10:15 PM

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

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;
}

### #8DekuTree64  Members

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.

### #9MaxDZ8  Members

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.

Previously "Krohm"

### #10/ fir   Members

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

### #11l0calh05t  Members

Posted 24 September 2013 - 06:04 AM

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

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.

### #12Hodgman  Moderators

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

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

### #13Sik_the_hedgehog  Members

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.

### #14Hodgman  Moderators

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.

### #15Sik_the_hedgehog  Members

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.

### #16TheComet  Members

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.

"I would try to find halo source code by bungie best fps engine ever created, u see why call of duty loses speed due to its detail." -- GettingNifty

### #17BGB  Members

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.

### #18wodinoneeye  Members

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

"

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