gfx/angle/src/libGLESv2/mathutil.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/angle/src/libGLESv2/mathutil.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,161 @@
     1.4 +//
     1.5 +// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
     1.6 +// Use of this source code is governed by a BSD-style license that can be
     1.7 +// found in the LICENSE file.
     1.8 +//
     1.9 +
    1.10 +// mathutil.h: Math and bit manipulation functions.
    1.11 +
    1.12 +#ifndef LIBGLESV2_MATHUTIL_H_
    1.13 +#define LIBGLESV2_MATHUTIL_H_
    1.14 +
    1.15 +#include <intrin.h>
    1.16 +
    1.17 +#include "common/system.h"
    1.18 +#include "common/debug.h"
    1.19 +
    1.20 +namespace gl
    1.21 +{
    1.22 +struct Vector4
    1.23 +{
    1.24 +    Vector4() {}
    1.25 +    Vector4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
    1.26 +
    1.27 +    float x;
    1.28 +    float y;
    1.29 +    float z;
    1.30 +    float w;
    1.31 +};
    1.32 +
    1.33 +inline bool isPow2(int x)
    1.34 +{
    1.35 +    return (x & (x - 1)) == 0 && (x != 0);
    1.36 +}
    1.37 +
    1.38 +inline int log2(int x)
    1.39 +{
    1.40 +    int r = 0;
    1.41 +    while ((x >> r) > 1) r++;
    1.42 +    return r;
    1.43 +}
    1.44 +
    1.45 +inline unsigned int ceilPow2(unsigned int x)
    1.46 +{
    1.47 +    if (x != 0) x--;
    1.48 +    x |= x >> 1;
    1.49 +    x |= x >> 2;
    1.50 +    x |= x >> 4;
    1.51 +    x |= x >> 8;
    1.52 +    x |= x >> 16;
    1.53 +    x++;
    1.54 +
    1.55 +    return x;
    1.56 +}
    1.57 +
    1.58 +template<typename T, typename MIN, typename MAX>
    1.59 +inline T clamp(T x, MIN min, MAX max)
    1.60 +{
    1.61 +    // Since NaNs fail all comparison tests, a NaN value will default to min
    1.62 +    return x > min ? (x > max ? max : x) : min;
    1.63 +}
    1.64 +
    1.65 +inline float clamp01(float x)
    1.66 +{
    1.67 +    return clamp(x, 0.0f, 1.0f);
    1.68 +}
    1.69 +
    1.70 +template<const int n>
    1.71 +inline unsigned int unorm(float x)
    1.72 +{
    1.73 +    const unsigned int max = 0xFFFFFFFF >> (32 - n);
    1.74 +
    1.75 +    if (x > 1)
    1.76 +    {
    1.77 +        return max;
    1.78 +    }
    1.79 +    else if (x < 0)
    1.80 +    {
    1.81 +        return 0;
    1.82 +    }
    1.83 +    else
    1.84 +    {
    1.85 +        return (unsigned int)(max * x + 0.5f);
    1.86 +    }
    1.87 +}
    1.88 +
    1.89 +inline bool supportsSSE2()
    1.90 +{
    1.91 +    static bool checked = false;
    1.92 +    static bool supports = false;
    1.93 +
    1.94 +    if (checked)
    1.95 +    {
    1.96 +        return supports;
    1.97 +    }
    1.98 +
    1.99 +    int info[4];
   1.100 +    __cpuid(info, 0);
   1.101 +    
   1.102 +    if (info[0] >= 1)
   1.103 +    {
   1.104 +        __cpuid(info, 1);
   1.105 +
   1.106 +        supports = (info[3] >> 26) & 1;
   1.107 +    }
   1.108 +
   1.109 +    checked = true;
   1.110 +
   1.111 +    return supports;
   1.112 +}
   1.113 +
   1.114 +inline unsigned short float32ToFloat16(float fp32)
   1.115 +{
   1.116 +    unsigned int fp32i = (unsigned int&)fp32;
   1.117 +    unsigned int sign = (fp32i & 0x80000000) >> 16;
   1.118 +    unsigned int abs = fp32i & 0x7FFFFFFF;
   1.119 +
   1.120 +    if(abs > 0x47FFEFFF)   // Infinity
   1.121 +    {
   1.122 +        return sign | 0x7FFF;
   1.123 +    }
   1.124 +    else if(abs < 0x38800000)   // Denormal
   1.125 +    {
   1.126 +        unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000;   
   1.127 +        int e = 113 - (abs >> 23);
   1.128 +
   1.129 +        if(e < 24)
   1.130 +        {
   1.131 +            abs = mantissa >> e;
   1.132 +        }
   1.133 +        else
   1.134 +        {
   1.135 +            abs = 0;
   1.136 +        }
   1.137 +
   1.138 +        return sign | (abs + 0x00000FFF + ((abs >> 13) & 1)) >> 13;
   1.139 +    }
   1.140 +    else
   1.141 +    {
   1.142 +        return sign | (abs + 0xC8000000 + 0x00000FFF + ((abs >> 13) & 1)) >> 13;
   1.143 +    }
   1.144 +}
   1.145 +
   1.146 +float float16ToFloat32(unsigned short h);
   1.147 +
   1.148 +}
   1.149 +
   1.150 +namespace rx
   1.151 +{
   1.152 +
   1.153 +struct Range
   1.154 +{
   1.155 +    Range() {}
   1.156 +    Range(int lo, int hi) : start(lo), end(hi) { ASSERT(lo <= hi); }
   1.157 +
   1.158 +    int start;
   1.159 +    int end;
   1.160 +};
   1.161 +
   1.162 +}
   1.163 +
   1.164 +#endif   // LIBGLESV2_MATHUTIL_H_

mercurial