1 // Copyright 2017 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "common/Math.h"
16
17 #include "common/Assert.h"
18 #include "common/Platform.h"
19
20 #include <algorithm>
21 #include <cmath>
22 #include <limits>
23
24 #if defined(DAWN_COMPILER_MSVC)
25 # include <intrin.h>
26 #endif
27
ScanForward(uint32_t bits)28 uint32_t ScanForward(uint32_t bits) {
29 ASSERT(bits != 0);
30 #if defined(DAWN_COMPILER_MSVC)
31 unsigned long firstBitIndex = 0ul;
32 unsigned char ret = _BitScanForward(&firstBitIndex, bits);
33 ASSERT(ret != 0);
34 return firstBitIndex;
35 #else
36 return static_cast<uint32_t>(__builtin_ctz(bits));
37 #endif
38 }
39
Log2(uint32_t value)40 uint32_t Log2(uint32_t value) {
41 ASSERT(value != 0);
42 #if defined(DAWN_COMPILER_MSVC)
43 unsigned long firstBitIndex = 0ul;
44 unsigned char ret = _BitScanReverse(&firstBitIndex, value);
45 ASSERT(ret != 0);
46 return firstBitIndex;
47 #else
48 return 31 - static_cast<uint32_t>(__builtin_clz(value));
49 #endif
50 }
51
Log2(uint64_t value)52 uint32_t Log2(uint64_t value) {
53 ASSERT(value != 0);
54 #if defined(DAWN_COMPILER_MSVC)
55 # if defined(DAWN_PLATFORM_64_BIT)
56 unsigned long firstBitIndex = 0ul;
57 unsigned char ret = _BitScanReverse64(&firstBitIndex, value);
58 ASSERT(ret != 0);
59 return firstBitIndex;
60 # else // defined(DAWN_PLATFORM_64_BIT)
61 unsigned long firstBitIndex = 0ul;
62 if (_BitScanReverse(&firstBitIndex, value >> 32)) {
63 return firstBitIndex + 32;
64 }
65 unsigned char ret = _BitScanReverse(&firstBitIndex, value & 0xFFFFFFFF);
66 ASSERT(ret != 0);
67 return firstBitIndex;
68 # endif // defined(DAWN_PLATFORM_64_BIT)
69 #else // defined(DAWN_COMPILER_MSVC)
70 return 63 - static_cast<uint32_t>(__builtin_clzll(value));
71 #endif // defined(DAWN_COMPILER_MSVC)
72 }
73
NextPowerOfTwo(uint64_t n)74 uint64_t NextPowerOfTwo(uint64_t n) {
75 if (n <= 1) {
76 return 1;
77 }
78
79 return 1ull << (Log2(n - 1) + 1);
80 }
81
IsPowerOfTwo(uint64_t n)82 bool IsPowerOfTwo(uint64_t n) {
83 ASSERT(n != 0);
84 return (n & (n - 1)) == 0;
85 }
86
IsPtrAligned(const void * ptr,size_t alignment)87 bool IsPtrAligned(const void* ptr, size_t alignment) {
88 ASSERT(IsPowerOfTwo(alignment));
89 ASSERT(alignment != 0);
90 return (reinterpret_cast<size_t>(ptr) & (alignment - 1)) == 0;
91 }
92
IsAligned(uint32_t value,size_t alignment)93 bool IsAligned(uint32_t value, size_t alignment) {
94 ASSERT(alignment <= UINT32_MAX);
95 ASSERT(IsPowerOfTwo(alignment));
96 ASSERT(alignment != 0);
97 uint32_t alignment32 = static_cast<uint32_t>(alignment);
98 return (value & (alignment32 - 1)) == 0;
99 }
100
Float32ToFloat16(float fp32)101 uint16_t Float32ToFloat16(float fp32) {
102 uint32_t fp32i = BitCast<uint32_t>(fp32);
103 uint32_t sign16 = (fp32i & 0x80000000) >> 16;
104 uint32_t mantissaAndExponent = fp32i & 0x7FFFFFFF;
105
106 if (mantissaAndExponent > 0x7F800000) { // NaN
107 return 0x7FFF;
108 } else if (mantissaAndExponent > 0x47FFEFFF) { // Infinity
109 return static_cast<uint16_t>(sign16 | 0x7C00);
110 } else if (mantissaAndExponent < 0x38800000) { // Denormal
111 uint32_t mantissa = (mantissaAndExponent & 0x007FFFFF) | 0x00800000;
112 int32_t exponent = 113 - (mantissaAndExponent >> 23);
113
114 if (exponent < 24) {
115 mantissaAndExponent = mantissa >> exponent;
116 } else {
117 mantissaAndExponent = 0;
118 }
119
120 return static_cast<uint16_t>(
121 sign16 | (mantissaAndExponent + 0x00000FFF + ((mantissaAndExponent >> 13) & 1)) >> 13);
122 } else {
123 return static_cast<uint16_t>(sign16 | (mantissaAndExponent + 0xC8000000 + 0x00000FFF +
124 ((mantissaAndExponent >> 13) & 1)) >>
125 13);
126 }
127 }
128
Float16ToFloat32(uint16_t fp16)129 float Float16ToFloat32(uint16_t fp16) {
130 uint32_t tmp = (fp16 & 0x7fff) << 13 | (fp16 & 0x8000) << 16;
131 float tmp2 = *reinterpret_cast<float*>(&tmp);
132 return pow(2, 127 - 15) * tmp2;
133 }
134
IsFloat16NaN(uint16_t fp16)135 bool IsFloat16NaN(uint16_t fp16) {
136 return (fp16 & 0x7FFF) > 0x7C00;
137 }
138
139 // Based on the Khronos Data Format Specification 1.2 Section 13.3 sRGB transfer functions
SRGBToLinear(float srgb)140 float SRGBToLinear(float srgb) {
141 // sRGB is always used in unsigned normalized formats so clamp to [0.0, 1.0]
142 if (srgb <= 0.0f) {
143 return 0.0f;
144 } else if (srgb > 1.0f) {
145 return 1.0f;
146 }
147
148 if (srgb < 0.04045f) {
149 return srgb / 12.92f;
150 } else {
151 return std::pow((srgb + 0.055f) / 1.055f, 2.4f);
152 }
153 }
154
RoundUp(uint64_t n,uint64_t m)155 uint64_t RoundUp(uint64_t n, uint64_t m) {
156 ASSERT(m > 0);
157 ASSERT(n > 0);
158 ASSERT(m <= std::numeric_limits<uint64_t>::max() - n);
159 return ((n + m - 1) / m) * m;
160 }
161