gfx/angle/src/libGLESv2/mathutil.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 //
michael@0 2 // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
michael@0 3 // Use of this source code is governed by a BSD-style license that can be
michael@0 4 // found in the LICENSE file.
michael@0 5 //
michael@0 6
michael@0 7 // mathutil.h: Math and bit manipulation functions.
michael@0 8
michael@0 9 #ifndef LIBGLESV2_MATHUTIL_H_
michael@0 10 #define LIBGLESV2_MATHUTIL_H_
michael@0 11
michael@0 12 #include <intrin.h>
michael@0 13
michael@0 14 #include "common/system.h"
michael@0 15 #include "common/debug.h"
michael@0 16
michael@0 17 namespace gl
michael@0 18 {
michael@0 19 struct Vector4
michael@0 20 {
michael@0 21 Vector4() {}
michael@0 22 Vector4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
michael@0 23
michael@0 24 float x;
michael@0 25 float y;
michael@0 26 float z;
michael@0 27 float w;
michael@0 28 };
michael@0 29
michael@0 30 inline bool isPow2(int x)
michael@0 31 {
michael@0 32 return (x & (x - 1)) == 0 && (x != 0);
michael@0 33 }
michael@0 34
michael@0 35 inline int log2(int x)
michael@0 36 {
michael@0 37 int r = 0;
michael@0 38 while ((x >> r) > 1) r++;
michael@0 39 return r;
michael@0 40 }
michael@0 41
michael@0 42 inline unsigned int ceilPow2(unsigned int x)
michael@0 43 {
michael@0 44 if (x != 0) x--;
michael@0 45 x |= x >> 1;
michael@0 46 x |= x >> 2;
michael@0 47 x |= x >> 4;
michael@0 48 x |= x >> 8;
michael@0 49 x |= x >> 16;
michael@0 50 x++;
michael@0 51
michael@0 52 return x;
michael@0 53 }
michael@0 54
michael@0 55 template<typename T, typename MIN, typename MAX>
michael@0 56 inline T clamp(T x, MIN min, MAX max)
michael@0 57 {
michael@0 58 // Since NaNs fail all comparison tests, a NaN value will default to min
michael@0 59 return x > min ? (x > max ? max : x) : min;
michael@0 60 }
michael@0 61
michael@0 62 inline float clamp01(float x)
michael@0 63 {
michael@0 64 return clamp(x, 0.0f, 1.0f);
michael@0 65 }
michael@0 66
michael@0 67 template<const int n>
michael@0 68 inline unsigned int unorm(float x)
michael@0 69 {
michael@0 70 const unsigned int max = 0xFFFFFFFF >> (32 - n);
michael@0 71
michael@0 72 if (x > 1)
michael@0 73 {
michael@0 74 return max;
michael@0 75 }
michael@0 76 else if (x < 0)
michael@0 77 {
michael@0 78 return 0;
michael@0 79 }
michael@0 80 else
michael@0 81 {
michael@0 82 return (unsigned int)(max * x + 0.5f);
michael@0 83 }
michael@0 84 }
michael@0 85
michael@0 86 inline bool supportsSSE2()
michael@0 87 {
michael@0 88 static bool checked = false;
michael@0 89 static bool supports = false;
michael@0 90
michael@0 91 if (checked)
michael@0 92 {
michael@0 93 return supports;
michael@0 94 }
michael@0 95
michael@0 96 int info[4];
michael@0 97 __cpuid(info, 0);
michael@0 98
michael@0 99 if (info[0] >= 1)
michael@0 100 {
michael@0 101 __cpuid(info, 1);
michael@0 102
michael@0 103 supports = (info[3] >> 26) & 1;
michael@0 104 }
michael@0 105
michael@0 106 checked = true;
michael@0 107
michael@0 108 return supports;
michael@0 109 }
michael@0 110
michael@0 111 inline unsigned short float32ToFloat16(float fp32)
michael@0 112 {
michael@0 113 unsigned int fp32i = (unsigned int&)fp32;
michael@0 114 unsigned int sign = (fp32i & 0x80000000) >> 16;
michael@0 115 unsigned int abs = fp32i & 0x7FFFFFFF;
michael@0 116
michael@0 117 if(abs > 0x47FFEFFF) // Infinity
michael@0 118 {
michael@0 119 return sign | 0x7FFF;
michael@0 120 }
michael@0 121 else if(abs < 0x38800000) // Denormal
michael@0 122 {
michael@0 123 unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000;
michael@0 124 int e = 113 - (abs >> 23);
michael@0 125
michael@0 126 if(e < 24)
michael@0 127 {
michael@0 128 abs = mantissa >> e;
michael@0 129 }
michael@0 130 else
michael@0 131 {
michael@0 132 abs = 0;
michael@0 133 }
michael@0 134
michael@0 135 return sign | (abs + 0x00000FFF + ((abs >> 13) & 1)) >> 13;
michael@0 136 }
michael@0 137 else
michael@0 138 {
michael@0 139 return sign | (abs + 0xC8000000 + 0x00000FFF + ((abs >> 13) & 1)) >> 13;
michael@0 140 }
michael@0 141 }
michael@0 142
michael@0 143 float float16ToFloat32(unsigned short h);
michael@0 144
michael@0 145 }
michael@0 146
michael@0 147 namespace rx
michael@0 148 {
michael@0 149
michael@0 150 struct Range
michael@0 151 {
michael@0 152 Range() {}
michael@0 153 Range(int lo, int hi) : start(lo), end(hi) { ASSERT(lo <= hi); }
michael@0 154
michael@0 155 int start;
michael@0 156 int end;
michael@0 157 };
michael@0 158
michael@0 159 }
michael@0 160
michael@0 161 #endif // LIBGLESV2_MATHUTIL_H_

mercurial