Fix couple of instances of undefined behavior
authorPyry Haulos <phaulos@google.com>
Fri, 22 May 2015 17:27:29 +0000 (10:27 -0700)
committerPyry Haulos <phaulos@google.com>
Thu, 28 May 2015 21:46:29 +0000 (14:46 -0700)
These were found by running tests using clang's -fsanitize=undefined

Change-Id: I810be9c747504df671629201ba56b5b8ed2307d7

framework/common/tcuBilinearImageCompare.cpp
framework/delibs/debase/deFloat16.c
framework/delibs/dethread/deThreadTest.c
framework/randomshaders/rsgExpression.cpp
modules/gles3/functional/es3fShaderPrecisionTests.cpp

index 9b53ff9..df6f7c2 100644 (file)
@@ -173,8 +173,8 @@ bool comparePixelRGBA8 (const ConstPixelBufferAccess& reference, const ConstPixe
 
                for (int sampleNdx = 0; sampleNdx < DE_LENGTH_OF_ARRAY(s_offsets); sampleNdx++)
                {
-                       const int u = ((x-1)<<NUM_SUBPIXEL_BITS) + (int)s_offsets[sampleNdx][0];
-                       const int v = ((y-1)<<NUM_SUBPIXEL_BITS) + (int)s_offsets[sampleNdx][1];
+                       const int u = (x<<NUM_SUBPIXEL_BITS) + (int)s_offsets[sampleNdx][0] - (1<<NUM_SUBPIXEL_BITS);
+                       const int v = (y<<NUM_SUBPIXEL_BITS) + (int)s_offsets[sampleNdx][1] - (1<<NUM_SUBPIXEL_BITS);
 
                        if (!de::inBounds(u, 0, (reference.getWidth()-1)<<NUM_SUBPIXEL_BITS) ||
                                !de::inBounds(v, 0, (reference.getHeight()-1)<<NUM_SUBPIXEL_BITS))
index 375fdc3..e424395 100644 (file)
@@ -27,19 +27,19 @@ DE_BEGIN_EXTERN_C
 
 deFloat16 deFloat32To16 (float val32)
 {
-       int sign;
-       int expotent;
-       int mantissa;
+       deUint32        sign;
+       int                     expotent;
+       deUint32        mantissa;
        union
        {
-               float   f;
-               int             i;
+               float           f;
+               deUint32        u;
        } x;
 
        x.f             = val32;
-       sign            = (x.i >> 16) & 0x00008000;
-       expotent        = ((x.i >> 23) & 0x000000ff) - (127 - 15);
-       mantissa        = x.i & 0x007fffff;
+       sign            = (x.u >> 16u) & 0x00008000u;
+       expotent        = (int)((x.u >> 23u) & 0x000000ffu) - (127 - 15);
+       mantissa        = x.u & 0x007fffffu;
 
        if (expotent <= 0)
        {
@@ -50,13 +50,13 @@ deFloat16 deFloat32To16 (float val32)
                }
 
                /* Converted to denormalized half, add leading 1 to significand. */
-               mantissa = mantissa | 0x00800000;
+               mantissa = mantissa | 0x00800000u;
 
                /* Round mantissa to nearest (10+e) */
                {
-                       int t = 14 - expotent;
-                       int a = (1 << (t - 1)) - 1;
-                       int b = (mantissa >> t) & 1;
+                       deUint32 t = 14u - expotent;
+                       deUint32 a = (1u << (t - 1u)) - 1u;
+                       deUint32 b = (mantissa >> t) & 1u;
 
                        mantissa = (mantissa + a + b) >> t;
                }
@@ -65,99 +65,99 @@ deFloat16 deFloat32To16 (float val32)
        }
        else if (expotent == 0xff - (127 - 15))
        {
-               if (mantissa == 0)
+               if (mantissa == 0u)
                {
                        /* InF */
-                       return (deFloat16) (sign | 0x7c00);
+                       return (deFloat16) (sign | 0x7c00u);
                }
                else
                {
                        /* NaN */
-                       mantissa >>= 13;
-                       return (deFloat16) (sign | 0x7c00 | mantissa | (mantissa == 0));
+                       mantissa >>= 13u;
+                       return (deFloat16) (sign | 0x7c00u | mantissa | (mantissa == 0u));
                }
        }
        else
        {
                /* Normalized float. */
-               mantissa = mantissa + 0x00000fff + ((mantissa >> 13) & 1);
+               mantissa = mantissa + 0x00000fffu + ((mantissa >> 13u) & 1u);
 
-               if (mantissa & 0x00800000)
+               if (mantissa & 0x00800000u)
                {
                        /* Overflow in mantissa. */
-                       mantissa  = 0;
+                       mantissa  = 0u;
                        expotent += 1;
                }
 
                if (expotent > 30)
                {
                        /* \todo [pyry] Cause hw fp overflow */
-                       return (deFloat16) (sign | 0x7c00);
+                       return (deFloat16) (sign | 0x7c00u);
                }
 
-               return (deFloat16) (sign | (expotent << 10) | (mantissa >> 13));
+               return (deFloat16) (sign | ((deUint32)expotent << 10u) | (mantissa >> 13u));
        }
 }
 
 float deFloat16To32 (deFloat16 val16)
 {
-       int sign;
-       int expotent;
-       int mantissa;
+       deUint32 sign;
+       deUint32 expotent;
+       deUint32 mantissa;
        union
        {
-               float   f;
-               int             i;
+               float           f;
+               deUint32        u;
        } x;
 
-       x.i                     = 0;
+       x.u                     = 0u;
 
-       sign            = ((int) val16 >> 15) & 0x00000001;
-       expotent        = ((int) val16 >> 10) & 0x0000001f;
-       mantissa        = (int) val16 & 0x000003ff;
+       sign            = ((deUint32)val16 >> 15u) & 0x00000001u;
+       expotent        = ((deUint32)val16 >> 10u) & 0x0000001fu;
+       mantissa        = (deUint32)val16 & 0x000003ffu;
 
-       if (expotent == 0)
+       if (expotent == 0u)
        {
-               if (mantissa == 0)
+               if (mantissa == 0u)
                {
                        /* +/- 0 */
-                       x.i = sign << 31;
+                       x.u = sign << 31u;
                        return x.f;
                }
                else
                {
                        /* Denormalized, normalize it. */
 
-                       while (!(mantissa & 0x00000400))
+                       while (!(mantissa & 0x00000400u))
                        {
-                               mantissa <<= 1;
-                               expotent -=  1;
+                               mantissa <<= 1u;
+                               expotent -=  1u;
                        }
 
-                       expotent += 1;
-                       mantissa &= ~0x00000400;
+                       expotent += 1u;
+                       mantissa &= ~0x00000400u;
                }
        }
-       else if (expotent == 31)
+       else if (expotent == 31u)
        {
-               if (mantissa == 0)
+               if (mantissa == 0u)
                {
                        /* +/- InF */
-                       x.i = (sign << 31) | 0x7f800000;
+                       x.u = (sign << 31u) | 0x7f800000u;
                        return x.f;
                }
                else
                {
                        /* +/- NaN */
-                       x.i = (sign << 31) | 0x7f800000 | (mantissa << 13);
+                       x.u = (sign << 31u) | 0x7f800000u | (mantissa << 13u);
                        return x.f;
                }
        }
 
-       expotent = expotent + (127 - 15);
-       mantissa = mantissa << 13;
+       expotent = expotent + (127u - 15u);
+       mantissa = mantissa << 13u;
 
-       x.i = (sign << 31) | (expotent << 23) | mantissa;
+       x.u = (sign << 31u) | (expotent << 23u) | mantissa;
        return x.f;
 }
 
index 2af44de..dfd3b27 100644 (file)
@@ -355,12 +355,12 @@ void deMutex_selfTest (void)
 
 typedef struct TestBuffer_s
 {
-       deInt32                 buffer[32];
+       deUint32                buffer[32];
        deSemaphore             empty;
        deSemaphore             fill;
 
-       deInt32                 producerSum;
-       deInt32                 consumerSum;
+       deUint32                producerHash;
+       deUint32                consumerHash;
 } TestBuffer;
 
 void producerThread (void* arg)
@@ -375,16 +375,16 @@ void producerThread (void* arg)
 
        for (ndx = 0; ndx <= numToProduce; ndx++)
        {
-               deInt32 val;
+               deUint32 val;
 
                if (ndx == numToProduce)
                {
-                       val = 0; /* End. */
+                       val = 0u; /* End. */
                }
                else
                {
-                       val = (deInt32)deRandom_getUint32(&random);
-                       val = val ? val : 1;
+                       val = deRandom_getUint32(&random);
+                       val = val ? val : 1u;
                }
 
                deSemaphore_decrement(buffer->empty);
@@ -394,7 +394,7 @@ void producerThread (void* arg)
 
                deSemaphore_increment(buffer->fill);
 
-               buffer->producerSum += val;
+               buffer->producerHash ^= val;
        }
 }
 
@@ -414,7 +414,7 @@ void consumerThread (void* arg)
 
                deSemaphore_increment(buffer->empty);
 
-               buffer->consumerSum += val;
+               buffer->consumerHash ^= val;
 
                if (val == 0)
                        break;
@@ -463,7 +463,7 @@ void deSemaphore_selfTest (void)
 
                deSemaphore_destroy(testBuffer.empty);
                deSemaphore_destroy(testBuffer.fill);
-               DE_TEST_ASSERT(testBuffer.producerSum == testBuffer.consumerSum);
+               DE_TEST_ASSERT(testBuffer.producerHash == testBuffer.consumerHash);
        }
 }
 
index f91cbf5..7b22d38 100644 (file)
@@ -1046,14 +1046,14 @@ float computeEntryReadWeight (ConstValueRangeAccess entryValueRange, ConstValueR
                                        continue;
 
                                // Intersection to entry value range length ratio.
-                               int intersectionMin                     = deMax32(entryMin, readMin);
-                               int intersectionMax                     = deMin32(entryMax, readMax);
-                               int entryRangeLen                       = entryMax - entryMin;
-                               int readRangeLen                        = readMax - readMin;
-                               int intersectionLen                     = intersectionMax - intersectionMin;
-                               float entryRatio                        = (entryRangeLen        > 0) ? ((float)intersectionLen / (float)entryRangeLen)  : 1.0f;
-                               float readRatio                         = (readRangeLen         > 0) ? ((float)intersectionLen / (float)readRangeLen)   : 1.0f;
-                               float elementWeight                     = 0.5f*readRatio + 0.5f*entryRatio;
+                               int             intersectionMin                 = deMax32(entryMin, readMin);
+                               int             intersectionMax                 = deMin32(entryMax, readMax);
+                               deInt64 entryRangeLen                   = (deInt64)entryMax - (deInt64)entryMin;
+                               deInt64 readRangeLen                    = (deInt64)readMax - (deInt64)readMin;
+                               deInt64 intersectionLen                 = (deInt64)intersectionMax - (deInt64)intersectionMin;
+                               float   entryRatio                              = (entryRangeLen        > 0) ? ((float)intersectionLen / (float)entryRangeLen)  : 1.0f;
+                               float   readRatio                               = (readRangeLen         > 0) ? ((float)intersectionLen / (float)readRangeLen)   : 1.0f;
+                               float   elementWeight                   = 0.5f*readRatio + 0.5f*entryRatio;
 
                                weight = combineWeight(weight, elementWeight);
                        }
index e4ae760..cf4b4ff 100644 (file)
@@ -331,8 +331,8 @@ bool ShaderFloatPrecisionCase::compare (float in0, float in1, double reference,
        {
                const deUint64  refBits                         = tcu::Float64(reference).bits();
                const deUint64  resBits                         = tcu::Float64(result).bits();
-               const deUint64  accurateRefBits         = refBits >> maskBits;
-               const deUint64  accurateResBits         = resBits >> maskBits;
+               const deUint64  accurateRefBits         = maskBits < 32u ? refBits >> maskBits : 0u;
+               const deUint64  accurateResBits         = maskBits < 32u ? resBits >> maskBits : 0u;
                const deUint64  ulpDiff                         = (deUint64)de::abs((deInt64)accurateRefBits - (deInt64)accurateResBits);
 
                if (ulpDiff > (deUint64)roundingUlpError)
@@ -559,11 +559,6 @@ void ShaderIntPrecisionCase::deinit (void)
        m_renderbuffer  = 0;
 }
 
-inline int extendTo32Bit (int value, int bits)
-{
-       return (value & ((1<<(bits-1))-1)) | (((value & (1<<(bits-1))) << (32-bits)) >> (32-bits));
-}
-
 ShaderIntPrecisionCase::IterateResult ShaderIntPrecisionCase::iterate (void)
 {
        // Constant data.
@@ -604,10 +599,10 @@ ShaderIntPrecisionCase::IterateResult ShaderIntPrecisionCase::iterate (void)
        // Compute values and reference.
        for (int testNdx = 0; testNdx < m_numTestsPerIter; testNdx++)
        {
-               int             in0                     = extendTo32Bit(((isMaxRangeA ? (int)m_rnd.getUint32() : m_rnd.getInt(m_rangeA.x(), m_rangeA.y())) & mask), m_bits);
-               int             in1                     = extendTo32Bit(((isMaxRangeB ? (int)m_rnd.getUint32() : m_rnd.getInt(m_rangeB.x(), m_rangeB.y())) & mask), m_bits);
+               int             in0                     = deSignExtendTo32(((isMaxRangeA ? (int)m_rnd.getUint32() : m_rnd.getInt(m_rangeA.x(), m_rangeA.y())) & mask), m_bits);
+               int             in1                     = deSignExtendTo32(((isMaxRangeB ? (int)m_rnd.getUint32() : m_rnd.getInt(m_rangeB.x(), m_rangeB.y())) & mask), m_bits);
                int             refMasked       = m_evalFunc(in0, in1) & mask;
-               int             refOut          = extendTo32Bit(refMasked, m_bits);
+               int             refOut          = deSignExtendTo32(refMasked, m_bits);
 
                log << TestLog::Message << "iter " << m_iterNdx << ", test " << testNdx << ": "
                                                                << "in0 = " << in0 << ", in1 = " << in1 << ", ref out = " << refOut << " / " << tcu::toHex(refMasked)