OSDN Git Service

get rid of some divisions we really don't need
authorRudolf Polzer <divverent@xonotic.org>
Fri, 15 Jul 2011 21:45:43 +0000 (23:45 +0200)
committerRudolf Polzer <divverent@xonotic.org>
Fri, 15 Jul 2011 21:45:43 +0000 (23:45 +0200)
s2tc_common.h
s2tc_compressor.cpp
s2tc_libtxc_dxtn.cpp

index 8143de2..69e4a90 100644 (file)
@@ -15,4 +15,29 @@ template <class T> inline T max(const T &a, const T &b)
        return a;
 }
 
+inline int byteidx(int bit)
+{
+       return bit >> 3;
+}
+
+inline int bitidx(int bit)
+{
+       return bit & 7;
+}
+
+inline void setbit(unsigned char *arr, int bit, int v = 1)
+{
+       arr[byteidx(bit)] |= (v << bitidx(bit));
+}
+
+inline void xorbit(unsigned char *arr, int bit, int v = 1)
+{
+       arr[byteidx(bit)] ^= (v << bitidx(bit));
+}
+
+inline int testbit(const unsigned char *arr, int bit, int v = 1)
+{
+       return (arr[byteidx(bit)] & (v << bitidx(bit)));
+}
+
 #endif
index 82ac632..72f46d6 100644 (file)
@@ -561,20 +561,20 @@ namespace
                                                                {
                                                                        // 6
                                                                        ++bitindex;
-                                                                       out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
+                                                                       setbit(&out[2], bitindex);
                                                                        ++bitindex;
-                                                                       out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
+                                                                       setbit(&out[2], bitindex);
                                                                        if(alpha_0_is_unimportant<ColorDist>())
                                                                                visible = false;
                                                                }
                                                                else if(da[3] <= da[0] && da[3] <= da[1])
                                                                {
                                                                        // 7
-                                                                       out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
+                                                                       setbit(&out[2], bitindex);
                                                                        ++bitindex;
-                                                                       out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
+                                                                       setbit(&out[2], bitindex);
                                                                        ++bitindex;
-                                                                       out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
+                                                                       setbit(&out[2], bitindex);
                                                                }
                                                                else if(da[0] <= da[1])
                                                                {
@@ -588,7 +588,7 @@ namespace
                                                                else
                                                                {
                                                                        // 1
-                                                                       out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
+                                                                       setbit(&out[2], bitindex);
                                                                        if(refine != REFINE_NEVER)
                                                                        {
                                                                                ++na1;
@@ -598,7 +598,7 @@ namespace
                                                                if(ColorDist(c[0], c[2]) > ColorDist(c[1], c[2]))
                                                                {
                                                                        int bitindex = pindex * 2;
-                                                                       out[bitindex / 8 + 12] |= (1 << (bitindex % 8));
+                                                                       setbit(&out[12], bitindex);
                                                                        if(refine != REFINE_NEVER)
                                                                        {
                                                                                if(!alpha_0_is_unimportant<ColorDist>() || visible)
@@ -628,12 +628,12 @@ namespace
                                                case DXT3:
                                                        {
                                                                int bitindex = pindex * 4;
-                                                               out[bitindex / 8 + 0] |= (ca[2] << (bitindex % 8));
+                                                               setbit(&out[0], bitindex, ca[2]);
                                                        }
                                                        if(ColorDist(c[0], c[2]) > ColorDist(c[1], c[2]))
                                                        {
                                                                int bitindex = pindex * 2;
-                                                               out[bitindex / 8 + 12] |= (1 << (bitindex % 8));
+                                                               setbit(&out[12], bitindex);
                                                                if(refine != REFINE_NEVER)
                                                                {
                                                                        if(!alpha_0_is_unimportant<ColorDist>() || ca[2])
@@ -664,10 +664,10 @@ namespace
                                                                // the normalmap-uses-alpha-0 hack cannot be used here
                                                                int bitindex = pindex * 2;
                                                                if(!ca[2])
-                                                                       out[bitindex / 8 + 4] |= (3 << (bitindex % 8));
+                                                                       setbit(&out[4], bitindex, 3);
                                                                else if(ColorDist(c[0], c[2]) > ColorDist(c[1], c[2]))
                                                                {
-                                                                       out[bitindex / 8 + 4] |= (1 << (bitindex % 8));
+                                                                       setbit(&out[4], bitindex);
                                                                        if(refine != REFINE_NEVER)
                                                                        {
                                                                                ++nc1;
@@ -737,8 +737,8 @@ namespace
                                                                        // check ENCODED alpha
                                                                        int bitindex_0 = pindex * 3;
                                                                        int bitindex_1 = bitindex_0 + 2;
-                                                                       if(!(out[bitindex_0 / 8 + 2] & (1 << (bitindex_0 % 8))))
-                                                                               if(out[bitindex_1 / 8 + 2] & (1 << (bitindex_1 % 8)))
+                                                                       if(!testbit(&out[2], bitindex_0))
+                                                                               if(testbit(&out[2], bitindex_1))
                                                                                        continue;
                                                                }
                                                                else
@@ -750,7 +750,7 @@ namespace
                                                                }
                                                        }
                                                        int bitindex = pindex * 2;
-                                                       if(out[bitindex / 8 + (dxt == DXT1 ? 4 : 12)] & (1 << (bitindex % 8)))
+                                                       if(testbit(&out[(dxt == DXT1 ? 4 : 12)], bitindex))
                                                        {
                                                                // we picked an 1
                                                                score_01 += ColorDist(c[1], c[4]);
@@ -794,8 +794,8 @@ namespace
                                        {
                                                int bitindex_set = pindex * 3;
                                                int bitindex_test = bitindex_set + 2;
-                                               if(!(out[bitindex_test / 8 + 2] & (1 << (bitindex_test % 8))))
-                                                       out[bitindex_set / 8 + 2] ^= (1 << (bitindex_set % 8));
+                                               if(!testbit(&out[2], bitindex_test))
+                                                       xorbit(&out[2], bitindex_set);
                                        }
                                }
                        }
index f81229c..b9cdb8e 100644 (file)
@@ -7,16 +7,17 @@ extern "C"
 #include <stdio.h>
 #include <string.h>
 #include "s2tc_compressor.h"
+#include "s2tc_common.h"
 
 void fetch_2d_texel_rgb_dxt1(GLint srcRowStride, const GLubyte *pixdata,
                             GLint i, GLint j, GLvoid *texel)
 {
        // fetches a single texel (i,j) into pixdata (RGB)
        GLubyte *t = (GLubyte *) texel;
-       const GLubyte *blksrc = (pixdata + ((srcRowStride + 3) / 4 * (j / 4) + (i / 4)) * 8);
+       const GLubyte *blksrc = (pixdata + (((srcRowStride + 3) >> 2) * (j >> 2) + (i >> 2)) * 8);
        unsigned int c  = blksrc[0] + 256*blksrc[1];
        unsigned int c1 = blksrc[2] + 256*blksrc[3];
-       int b = (blksrc[4 + (j % 4)] >> (2 * (i % 4))) & 0x03;
+       int b = (blksrc[4 + (j & 3)] >> (2 * (i & 3))) & 0x03;
        switch(b)
        {
                case 0:                        break;
@@ -35,10 +36,10 @@ void fetch_2d_texel_rgba_dxt1(GLint srcRowStride, const GLubyte *pixdata,
 {
        // fetches a single texel (i,j) into pixdata (RGBA)
        GLubyte *t = (GLubyte *) texel;
-       const GLubyte *blksrc = (pixdata + ((srcRowStride + 3) / 4 * (j / 4) + (i / 4)) * 8);
+       const GLubyte *blksrc = (pixdata + (((srcRowStride + 3) >> 2) * (j >> 2) + (i >> 2)) * 8);
        unsigned int c  = blksrc[0] + 256*blksrc[1];
        unsigned int c1 = blksrc[2] + 256*blksrc[3];
-       int b = (blksrc[4 + (j % 4)] >> (2 * (i % 4))) & 0x03;
+       int b = (blksrc[4 + (j & 3)] >> (2 * (i & 3))) & 0x03;
        switch(b)
        {
                case 0:                        t[3] = 255; break;
@@ -56,10 +57,10 @@ void fetch_2d_texel_rgba_dxt3(GLint srcRowStride, const GLubyte *pixdata,
 {
        // fetches a single texel (i,j) into pixdata (RGBA)
        GLubyte *t = (GLubyte *) texel;
-       const GLubyte *blksrc = (pixdata + ((srcRowStride + 3) / 4 * (j / 4) + (i / 4)) * 16);
+       const GLubyte *blksrc = (pixdata + (((srcRowStride + 3) >> 2) * (j >> 2) + (i >> 2)) * 16);
        unsigned int c  = blksrc[8] + 256*blksrc[9];
        unsigned int c1 = blksrc[10] + 256*blksrc[11];
-       int b = (blksrc[12 + (j % 4)] >> (2 * (i % 4))) & 0x03;
+       int b = (blksrc[12 + (j & 3)] >> (2 * (i & 3))) & 0x03;
        switch(b)
        {
                case 0:                        break;
@@ -69,7 +70,7 @@ void fetch_2d_texel_rgba_dxt3(GLint srcRowStride, const GLubyte *pixdata,
        t[0] = ((c >> 11) & 0x1F); t[0] = (t[0] << 3) | (t[0] >> 2);
        t[1] = ((c >>  5) & 0x3F); t[1] = (t[1] << 2) | (t[1] >> 4);
        t[2] = ((c      ) & 0x1F); t[2] = (t[2] << 3) | (t[2] >> 2);
-       int a = (blksrc[(j % 4) * 2 + (i % 4) / 2] >> (4 * (i % 4) % 2)) & 0x0F;
+       int a = (blksrc[(j & 3) * 2 + ((i & 3) >> 1)] >> (4 * (i & 1))) & 0x0F;
        t[3] = a | (a << 4);
 }
 
@@ -78,10 +79,10 @@ void fetch_2d_texel_rgba_dxt5(GLint srcRowStride, const GLubyte *pixdata,
 {
        // fetches a single texel (i,j) into pixdata (RGBA)
        GLubyte *t = (GLubyte *) texel;
-       const GLubyte *blksrc = (pixdata + ((srcRowStride + 3) / 4 * (j / 4) + (i / 4)) * 16);
+       const GLubyte *blksrc = (pixdata + (((srcRowStride + 3) >> 2) * (j >> 2) + (i >> 2)) * 16);
        unsigned int c  = blksrc[8] + 256*blksrc[9];
        unsigned int c1 = blksrc[10] + 256*blksrc[11];
-       int b = (blksrc[12 + (j % 4)] >> (2 * (i % 4))) & 0x03;
+       int b = (blksrc[12 + (j & 3)] >> (2 * (i & 3))) & 0x03;
        switch(b)
        {
                case 0:                        break;
@@ -94,15 +95,15 @@ void fetch_2d_texel_rgba_dxt5(GLint srcRowStride, const GLubyte *pixdata,
 
        unsigned int a  = blksrc[0];
        unsigned int a1 = blksrc[1];
-       int abit = ((j % 4) * 4 + (i % 4)) * 3;
+       int abit = ((j & 3) * 4 + (i & 3)) * 3;
        int ab = 0;
-       if(blksrc[(abit / 8) + 2] & (1 << (abit % 8)))
+       if(testbit(&blksrc[2], abit))
                ab |= 1;
        ++abit;
-       if(blksrc[(abit / 8) + 2] & (1 << (abit % 8)))
+       if(testbit(&blksrc[2], abit))
                ab |= 2;
        ++abit;
-       if(blksrc[(abit / 8) + 2] & (1 << (abit % 8)))
+       if(testbit(&blksrc[2], abit))
                ab |= 4;
        switch(ab)
        {