Home | History | Annotate | Download | only in common
      1 //
      2 // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
      3 // Use of this source code is governed by a BSD-style license that can be
      4 // found in the LICENSE file.
      5 //
      6 
      7 // mathutil.h: Math and bit manipulation functions.
      8 
      9 #ifndef LIBGLESV2_MATHUTIL_H_
     10 #define LIBGLESV2_MATHUTIL_H_
     11 
     12 #include "common/debug.h"
     13 
     14 #if defined(_WIN32)
     15 #include <intrin.h>
     16 #endif
     17 
     18 #include <limits>
     19 #include <algorithm>
     20 #include <string.h>
     21 
     22 namespace gl
     23 {
     24 
     25 const unsigned int Float32One = 0x3F800000;
     26 const unsigned short Float16One = 0x3C00;
     27 
     28 struct Vector4
     29 {
     30     Vector4() {}
     31     Vector4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
     32 
     33     float x;
     34     float y;
     35     float z;
     36     float w;
     37 };
     38 
     39 inline bool isPow2(int x)
     40 {
     41     return (x & (x - 1)) == 0 && (x != 0);
     42 }
     43 
     44 inline int log2(int x)
     45 {
     46     int r = 0;
     47     while ((x >> r) > 1) r++;
     48     return r;
     49 }
     50 
     51 inline unsigned int ceilPow2(unsigned int x)
     52 {
     53     if (x != 0) x--;
     54     x |= x >> 1;
     55     x |= x >> 2;
     56     x |= x >> 4;
     57     x |= x >> 8;
     58     x |= x >> 16;
     59     x++;
     60 
     61     return x;
     62 }
     63 
     64 inline int clampToInt(unsigned int x)
     65 {
     66     return static_cast<int>(std::min(x, static_cast<unsigned int>(std::numeric_limits<int>::max())));
     67 }
     68 
     69 template <typename DestT, typename SrcT>
     70 inline DestT clampCast(SrcT value)
     71 {
     72     // This assumes SrcT can properly represent DestT::min/max
     73     // Unfortunately we can't use META_ASSERT without C++11 constexpr support
     74     ASSERT(static_cast<DestT>(static_cast<SrcT>(std::numeric_limits<DestT>::min())) == std::numeric_limits<DestT>::min());
     75     ASSERT(static_cast<DestT>(static_cast<SrcT>(std::numeric_limits<DestT>::max())) == std::numeric_limits<DestT>::max());
     76 
     77     SrcT lo = static_cast<SrcT>(std::numeric_limits<DestT>::min());
     78     SrcT hi = static_cast<SrcT>(std::numeric_limits<DestT>::max());
     79     return static_cast<DestT>(value > lo ? (value > hi ? hi : value) : lo);
     80 }
     81 
     82 template<typename T, typename MIN, typename MAX>
     83 inline T clamp(T x, MIN min, MAX max)
     84 {
     85     // Since NaNs fail all comparison tests, a NaN value will default to min
     86     return x > min ? (x > max ? max : x) : min;
     87 }
     88 
     89 inline float clamp01(float x)
     90 {
     91     return clamp(x, 0.0f, 1.0f);
     92 }
     93 
     94 template<const int n>
     95 inline unsigned int unorm(float x)
     96 {
     97     const unsigned int max = 0xFFFFFFFF >> (32 - n);
     98 
     99     if (x > 1)
    100     {
    101         return max;
    102     }
    103     else if (x < 0)
    104     {
    105         return 0;
    106     }
    107     else
    108     {
    109         return (unsigned int)(max * x + 0.5f);
    110     }
    111 }
    112 
    113 inline bool supportsSSE2()
    114 {
    115 #if defined(_WIN32)
    116     static bool checked = false;
    117     static bool supports = false;
    118 
    119     if (checked)
    120     {
    121         return supports;
    122     }
    123 
    124     int info[4];
    125     __cpuid(info, 0);
    126 
    127     if (info[0] >= 1)
    128     {
    129         __cpuid(info, 1);
    130 
    131         supports = (info[3] >> 26) & 1;
    132     }
    133 
    134     checked = true;
    135 
    136     return supports;
    137 #else
    138     UNIMPLEMENTED();
    139     return false;
    140 #endif
    141 }
    142 
    143 template <typename destType, typename sourceType>
    144 destType bitCast(const sourceType &source)
    145 {
    146     size_t copySize = std::min(sizeof(destType), sizeof(sourceType));
    147     destType output;
    148     memcpy(&output, &source, copySize);
    149     return output;
    150 }
    151 
    152 inline unsigned short float32ToFloat16(float fp32)
    153 {
    154     unsigned int fp32i = (unsigned int&)fp32;
    155     unsigned int sign = (fp32i & 0x80000000) >> 16;
    156     unsigned int abs = fp32i & 0x7FFFFFFF;
    157 
    158     if(abs > 0x47FFEFFF)   // Infinity
    159     {
    160         return sign | 0x7FFF;
    161     }
    162     else if(abs < 0x38800000)   // Denormal
    163     {
    164         unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000;
    165         int e = 113 - (abs >> 23);
    166 
    167         if(e < 24)
    168         {
    169             abs = mantissa >> e;
    170         }
    171         else
    172         {
    173             abs = 0;
    174         }
    175 
    176         return sign | (abs + 0x00000FFF + ((abs >> 13) & 1)) >> 13;
    177     }
    178     else
    179     {
    180         return sign | (abs + 0xC8000000 + 0x00000FFF + ((abs >> 13) & 1)) >> 13;
    181     }
    182 }
    183 
    184 float float16ToFloat32(unsigned short h);
    185 
    186 unsigned int convertRGBFloatsTo999E5(float red, float green, float blue);
    187 void convert999E5toRGBFloats(unsigned int input, float *red, float *green, float *blue);
    188 
    189 inline unsigned short float32ToFloat11(float fp32)
    190 {
    191     const unsigned int float32MantissaMask = 0x7FFFFF;
    192     const unsigned int float32ExponentMask = 0x7F800000;
    193     const unsigned int float32SignMask = 0x80000000;
    194     const unsigned int float32ValueMask = ~float32SignMask;
    195     const unsigned int float32ExponentFirstBit = 23;
    196     const unsigned int float32ExponentBias = 127;
    197 
    198     const unsigned short float11Max = 0x7BF;
    199     const unsigned short float11MantissaMask = 0x3F;
    200     const unsigned short float11ExponentMask = 0x7C0;
    201     const unsigned short float11BitMask = 0x7FF;
    202     const unsigned int float11ExponentBias = 14;
    203 
    204     const unsigned int float32Maxfloat11 = 0x477E0000;
    205     const unsigned int float32Minfloat11 = 0x38800000;
    206 
    207     const unsigned int float32Bits = bitCast<unsigned int>(fp32);
    208     const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
    209 
    210     unsigned int float32Val = float32Bits & float32ValueMask;
    211 
    212     if ((float32Val & float32ExponentMask) == float32ExponentMask)
    213     {
    214         // INF or NAN
    215         if ((float32Val & float32MantissaMask) != 0)
    216         {
    217             return float11ExponentMask | (((float32Val >> 17) | (float32Val >> 11) | (float32Val >> 6) | (float32Val)) & float11MantissaMask);
    218         }
    219         else if (float32Sign)
    220         {
    221             // -INF is clamped to 0 since float11 is positive only
    222             return 0;
    223         }
    224         else
    225         {
    226             return float11ExponentMask;
    227         }
    228     }
    229     else if (float32Sign)
    230     {
    231         // float11 is positive only, so clamp to zero
    232         return 0;
    233     }
    234     else if (float32Val > float32Maxfloat11)
    235     {
    236         // The number is too large to be represented as a float11, set to max
    237         return float11Max;
    238     }
    239     else
    240     {
    241         if (float32Val < float32Minfloat11)
    242         {
    243             // The number is too small to be represented as a normalized float11
    244             // Convert it to a denormalized value.
    245             const unsigned int shift = (float32ExponentBias - float11ExponentBias) - (float32Val >> float32ExponentFirstBit);
    246             float32Val = ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
    247         }
    248         else
    249         {
    250             // Rebias the exponent to represent the value as a normalized float11
    251             float32Val += 0xC8000000;
    252         }
    253 
    254         return ((float32Val + 0xFFFF + ((float32Val >> 17) & 1)) >> 17) & float11BitMask;
    255     }
    256 }
    257 
    258 inline unsigned short float32ToFloat10(float fp32)
    259 {
    260     const unsigned int float32MantissaMask = 0x7FFFFF;
    261     const unsigned int float32ExponentMask = 0x7F800000;
    262     const unsigned int float32SignMask = 0x80000000;
    263     const unsigned int float32ValueMask = ~float32SignMask;
    264     const unsigned int float32ExponentFirstBit = 23;
    265     const unsigned int float32ExponentBias = 127;
    266 
    267     const unsigned short float10Max = 0x3DF;
    268     const unsigned short float10MantissaMask = 0x1F;
    269     const unsigned short float10ExponentMask = 0x3E0;
    270     const unsigned short float10BitMask = 0x3FF;
    271     const unsigned int float10ExponentBias = 14;
    272 
    273     const unsigned int float32Maxfloat10 = 0x477C0000;
    274     const unsigned int float32Minfloat10 = 0x38800000;
    275 
    276     const unsigned int float32Bits = bitCast<unsigned int>(fp32);
    277     const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
    278 
    279     unsigned int float32Val = float32Bits & float32ValueMask;
    280 
    281     if ((float32Val & float32ExponentMask) == float32ExponentMask)
    282     {
    283         // INF or NAN
    284         if ((float32Val & float32MantissaMask) != 0)
    285         {
    286             return float10ExponentMask | (((float32Val >> 18) | (float32Val >> 13) | (float32Val >> 3) | (float32Val)) & float10MantissaMask);
    287         }
    288         else if (float32Sign)
    289         {
    290             // -INF is clamped to 0 since float11 is positive only
    291             return 0;
    292         }
    293         else
    294         {
    295             return float10ExponentMask;
    296         }
    297     }
    298     else if (float32Sign)
    299     {
    300         // float10 is positive only, so clamp to zero
    301         return 0;
    302     }
    303     else if (float32Val > float32Maxfloat10)
    304     {
    305         // The number is too large to be represented as a float11, set to max
    306         return float10Max;
    307     }
    308     else
    309     {
    310         if (float32Val < float32Minfloat10)
    311         {
    312             // The number is too small to be represented as a normalized float11
    313             // Convert it to a denormalized value.
    314             const unsigned int shift = (float32ExponentBias - float10ExponentBias) - (float32Val >> float32ExponentFirstBit);
    315             float32Val = ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
    316         }
    317         else
    318         {
    319             // Rebias the exponent to represent the value as a normalized float11
    320             float32Val += 0xC8000000;
    321         }
    322 
    323         return ((float32Val + 0x1FFFF + ((float32Val >> 18) & 1)) >> 18) & float10BitMask;
    324     }
    325 }
    326 
    327 inline float float11ToFloat32(unsigned short fp11)
    328 {
    329     unsigned short exponent = (fp11 >> 6) & 0x1F;
    330     unsigned short mantissa = fp11 & 0x3F;
    331 
    332     if (exponent == 0x1F)
    333     {
    334         // INF or NAN
    335         return bitCast<float>(0x7f800000 | (mantissa << 17));
    336     }
    337     else
    338     {
    339         if (exponent != 0)
    340         {
    341             // normalized
    342         }
    343         else if (mantissa != 0)
    344         {
    345             // The value is denormalized
    346             exponent = 1;
    347 
    348             do
    349             {
    350                 exponent--;
    351                 mantissa <<= 1;
    352             }
    353             while ((mantissa & 0x40) == 0);
    354 
    355             mantissa = mantissa & 0x3F;
    356         }
    357         else // The value is zero
    358         {
    359             exponent = -112;
    360         }
    361 
    362         return bitCast<float>(((exponent + 112) << 23) | (mantissa << 17));
    363     }
    364 }
    365 
    366 inline float float10ToFloat32(unsigned short fp11)
    367 {
    368     unsigned short exponent = (fp11 >> 5) & 0x1F;
    369     unsigned short mantissa = fp11 & 0x1F;
    370 
    371     if (exponent == 0x1F)
    372     {
    373         // INF or NAN
    374         return bitCast<float>(0x7f800000 | (mantissa << 17));
    375     }
    376     else
    377     {
    378         if (exponent != 0)
    379         {
    380             // normalized
    381         }
    382         else if (mantissa != 0)
    383         {
    384             // The value is denormalized
    385             exponent = 1;
    386 
    387             do
    388             {
    389                 exponent--;
    390                 mantissa <<= 1;
    391             }
    392             while ((mantissa & 0x20) == 0);
    393 
    394             mantissa = mantissa & 0x1F;
    395         }
    396         else // The value is zero
    397         {
    398             exponent = -112;
    399         }
    400 
    401         return bitCast<float>(((exponent + 112) << 23) | (mantissa << 18));
    402     }
    403 }
    404 
    405 template <typename T>
    406 inline float normalizedToFloat(T input)
    407 {
    408     META_ASSERT(std::numeric_limits<T>::is_integer);
    409 
    410     const float inverseMax = 1.0f / std::numeric_limits<T>::max();
    411     return input * inverseMax;
    412 }
    413 
    414 template <unsigned int inputBitCount, typename T>
    415 inline float normalizedToFloat(T input)
    416 {
    417     META_ASSERT(std::numeric_limits<T>::is_integer);
    418     META_ASSERT(inputBitCount < (sizeof(T) * 8));
    419 
    420     const float inverseMax = 1.0f / ((1 << inputBitCount) - 1);
    421     return input * inverseMax;
    422 }
    423 
    424 template <typename T>
    425 inline T floatToNormalized(float input)
    426 {
    427     return std::numeric_limits<T>::max() * input + 0.5f;
    428 }
    429 
    430 template <unsigned int outputBitCount, typename T>
    431 inline T floatToNormalized(float input)
    432 {
    433     META_ASSERT(outputBitCount < (sizeof(T) * 8));
    434     return ((1 << outputBitCount) - 1) * input + 0.5f;
    435 }
    436 
    437 template <unsigned int inputBitCount, unsigned int inputBitStart, typename T>
    438 inline T getShiftedData(T input)
    439 {
    440     META_ASSERT(inputBitCount + inputBitStart <= (sizeof(T) * 8));
    441     const T mask = (1 << inputBitCount) - 1;
    442     return (input >> inputBitStart) & mask;
    443 }
    444 
    445 template <unsigned int inputBitCount, unsigned int inputBitStart, typename T>
    446 inline T shiftData(T input)
    447 {
    448     META_ASSERT(inputBitCount + inputBitStart <= (sizeof(T) * 8));
    449     const T mask = (1 << inputBitCount) - 1;
    450     return (input & mask) << inputBitStart;
    451 }
    452 
    453 
    454 inline unsigned char average(unsigned char a, unsigned char b)
    455 {
    456     return ((a ^ b) >> 1) + (a & b);
    457 }
    458 
    459 inline signed char average(signed char a, signed char b)
    460 {
    461     return ((short)a + (short)b) / 2;
    462 }
    463 
    464 inline unsigned short average(unsigned short a, unsigned short b)
    465 {
    466     return ((a ^ b) >> 1) + (a & b);
    467 }
    468 
    469 inline signed short average(signed short a, signed short b)
    470 {
    471     return ((int)a + (int)b) / 2;
    472 }
    473 
    474 inline unsigned int average(unsigned int a, unsigned int b)
    475 {
    476     return ((a ^ b) >> 1) + (a & b);
    477 }
    478 
    479 inline signed int average(signed int a, signed int b)
    480 {
    481     return ((long long)a + (long long)b) / 2;
    482 }
    483 
    484 inline float average(float a, float b)
    485 {
    486     return (a + b) * 0.5f;
    487 }
    488 
    489 inline unsigned short averageHalfFloat(unsigned short a, unsigned short b)
    490 {
    491     return float32ToFloat16((float16ToFloat32(a) + float16ToFloat32(b)) * 0.5f);
    492 }
    493 
    494 inline unsigned int averageFloat11(unsigned int a, unsigned int b)
    495 {
    496     return float32ToFloat11((float11ToFloat32(a) + float11ToFloat32(b)) * 0.5f);
    497 }
    498 
    499 inline unsigned int averageFloat10(unsigned int a, unsigned int b)
    500 {
    501     return float32ToFloat10((float10ToFloat32(a) + float10ToFloat32(b)) * 0.5f);
    502 }
    503 
    504 }
    505 
    506 namespace rx
    507 {
    508 
    509 struct Range
    510 {
    511     Range() {}
    512     Range(int lo, int hi) : start(lo), end(hi) { ASSERT(lo <= hi); }
    513 
    514     int start;
    515     int end;
    516 };
    517 
    518 template <typename T>
    519 T roundUp(const T value, const T alignment)
    520 {
    521     return value + alignment - 1 - (value - 1) % alignment;
    522 }
    523 
    524 template <class T>
    525 inline bool IsUnsignedAdditionSafe(T lhs, T rhs)
    526 {
    527     META_ASSERT(!std::numeric_limits<T>::is_signed);
    528     return (rhs <= std::numeric_limits<T>::max() - lhs);
    529 }
    530 
    531 template <class T>
    532 inline bool IsUnsignedMultiplicationSafe(T lhs, T rhs)
    533 {
    534     META_ASSERT(!std::numeric_limits<T>::is_signed);
    535     return (lhs == T(0) || rhs == T(0) || (rhs <= std::numeric_limits<T>::max() / lhs));
    536 }
    537 
    538 template <class SmallIntT, class BigIntT>
    539 inline bool IsIntegerCastSafe(BigIntT bigValue)
    540 {
    541     return (static_cast<BigIntT>(static_cast<SmallIntT>(bigValue)) == bigValue);
    542 }
    543 
    544 }
    545 
    546 #endif   // LIBGLESV2_MATHUTIL_H_
    547