Corentin Wallez | 4a9ef4e | 2018-07-18 11:40:26 +0200 | [diff] [blame] | 1 | // Copyright 2017 The Dawn Authors |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 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 | |
Corentin Wallez | fffe6df | 2017-07-06 14:41:13 -0400 | [diff] [blame] | 15 | #include "common/Math.h" |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 16 | |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 17 | #include "common/Assert.h" |
| 18 | |
Yan, Shaobo | 9286adc | 2019-04-26 15:25:18 +0000 | [diff] [blame] | 19 | #include <algorithm> |
Corentin Wallez | 431d618 | 2019-07-01 09:58:07 +0000 | [diff] [blame] | 20 | #include <cmath> |
Yan, Shaobo | 9286adc | 2019-04-26 15:25:18 +0000 | [diff] [blame] | 21 | |
Corentin Wallez | 83a9c9d | 2018-07-18 13:37:54 +0200 | [diff] [blame] | 22 | #if defined(DAWN_COMPILER_MSVC) |
Corentin Wallez | 9d01c6c | 2017-11-24 11:45:29 -0500 | [diff] [blame] | 23 | # include <intrin.h> |
Corentin Wallez | 40fb17d | 2017-05-30 18:02:38 -0400 | [diff] [blame] | 24 | #endif |
Corentin Wallez | 944b60f | 2017-05-29 11:33:33 -0700 | [diff] [blame] | 25 | |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 26 | uint32_t ScanForward(uint32_t bits) { |
| 27 | ASSERT(bits != 0); |
Corentin Wallez | 83a9c9d | 2018-07-18 13:37:54 +0200 | [diff] [blame] | 28 | #if defined(DAWN_COMPILER_MSVC) |
Corentin Wallez | 9d01c6c | 2017-11-24 11:45:29 -0500 | [diff] [blame] | 29 | unsigned long firstBitIndex = 0ul; |
| 30 | unsigned char ret = _BitScanForward(&firstBitIndex, bits); |
| 31 | ASSERT(ret != 0); |
| 32 | return firstBitIndex; |
| 33 | #else |
| 34 | return static_cast<uint32_t>(__builtin_ctz(bits)); |
| 35 | #endif |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 36 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 37 | |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 38 | uint32_t Log2(uint32_t value) { |
| 39 | ASSERT(value != 0); |
Corentin Wallez | 83a9c9d | 2018-07-18 13:37:54 +0200 | [diff] [blame] | 40 | #if defined(DAWN_COMPILER_MSVC) |
Corentin Wallez | 9d01c6c | 2017-11-24 11:45:29 -0500 | [diff] [blame] | 41 | unsigned long firstBitIndex = 0ul; |
| 42 | unsigned char ret = _BitScanReverse(&firstBitIndex, value); |
| 43 | ASSERT(ret != 0); |
| 44 | return firstBitIndex; |
| 45 | #else |
| 46 | return 31 - static_cast<uint32_t>(__builtin_clz(value)); |
| 47 | #endif |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 48 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 49 | |
Bryan Bernhart | 4079323 | 2019-07-22 21:42:18 +0000 | [diff] [blame] | 50 | uint32_t Log2(uint64_t value) { |
| 51 | ASSERT(value != 0); |
| 52 | #if defined(DAWN_COMPILER_MSVC) |
| 53 | unsigned long firstBitIndex = 0ul; |
| 54 | unsigned char ret = _BitScanReverse64(&firstBitIndex, value); |
| 55 | ASSERT(ret != 0); |
| 56 | return firstBitIndex; |
| 57 | #else |
| 58 | return 63 - static_cast<uint32_t>(__builtin_clzll(value)); |
| 59 | #endif |
| 60 | } |
| 61 | |
Bryan Bernhart | 506ce9b | 2019-07-26 11:20:08 +0000 | [diff] [blame] | 62 | uint64_t NextPowerOfTwo(uint64_t n) { |
Bryan Bernhart | 4079323 | 2019-07-22 21:42:18 +0000 | [diff] [blame] | 63 | #if defined(DAWN_COMPILER_MSVC) |
Bryan Bernhart | 506ce9b | 2019-07-26 11:20:08 +0000 | [diff] [blame] | 64 | return n <= 1 ? 1 : 1ull << (64 - __lzcnt64(n - 1)); |
Bryan Bernhart | 4079323 | 2019-07-22 21:42:18 +0000 | [diff] [blame] | 65 | #else |
Bryan Bernhart | 506ce9b | 2019-07-26 11:20:08 +0000 | [diff] [blame] | 66 | return n == 1 ? 1 : 1ull << (64 - __builtin_clzll(n - 1)); |
Bryan Bernhart | 4079323 | 2019-07-22 21:42:18 +0000 | [diff] [blame] | 67 | #endif |
| 68 | } |
| 69 | |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 70 | bool IsPowerOfTwo(size_t n) { |
| 71 | ASSERT(n != 0); |
| 72 | return (n & (n - 1)) == 0; |
| 73 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 74 | |
Austin Eng | ae48c95 | 2017-08-17 14:02:16 -0400 | [diff] [blame] | 75 | bool IsPtrAligned(const void* ptr, size_t alignment) { |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 76 | ASSERT(IsPowerOfTwo(alignment)); |
| 77 | ASSERT(alignment != 0); |
Kai Ninomiya | 78c8b83 | 2017-07-21 17:00:22 -0700 | [diff] [blame] | 78 | return (reinterpret_cast<size_t>(ptr) & (alignment - 1)) == 0; |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 79 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 80 | |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 81 | void* AlignVoidPtr(void* ptr, size_t alignment) { |
Austin Eng | 8867e5d | 2017-07-14 18:53:07 -0400 | [diff] [blame] | 82 | ASSERT(IsPowerOfTwo(alignment)); |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 83 | ASSERT(alignment != 0); |
Corentin Wallez | 9d01c6c | 2017-11-24 11:45:29 -0500 | [diff] [blame] | 84 | return reinterpret_cast<void*>((reinterpret_cast<size_t>(ptr) + (alignment - 1)) & |
| 85 | ~(alignment - 1)); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 86 | } |
Austin Eng | 98b7815 | 2017-07-14 10:58:50 -0400 | [diff] [blame] | 87 | |
Austin Eng | ae48c95 | 2017-08-17 14:02:16 -0400 | [diff] [blame] | 88 | bool IsAligned(uint32_t value, size_t alignment) { |
| 89 | ASSERT(alignment <= UINT32_MAX); |
| 90 | ASSERT(IsPowerOfTwo(alignment)); |
| 91 | ASSERT(alignment != 0); |
| 92 | uint32_t alignment32 = static_cast<uint32_t>(alignment); |
| 93 | return (value & (alignment32 - 1)) == 0; |
| 94 | } |
| 95 | |
Austin Eng | 98b7815 | 2017-07-14 10:58:50 -0400 | [diff] [blame] | 96 | uint32_t Align(uint32_t value, size_t alignment) { |
Kai Ninomiya | 59dc03f | 2017-07-20 07:28:00 -0700 | [diff] [blame] | 97 | ASSERT(alignment <= UINT32_MAX); |
Austin Eng | 98b7815 | 2017-07-14 10:58:50 -0400 | [diff] [blame] | 98 | ASSERT(IsPowerOfTwo(alignment)); |
| 99 | ASSERT(alignment != 0); |
Kai Ninomiya | 59dc03f | 2017-07-20 07:28:00 -0700 | [diff] [blame] | 100 | uint32_t alignment32 = static_cast<uint32_t>(alignment); |
| 101 | return (value + (alignment32 - 1)) & ~(alignment32 - 1); |
Austin Eng | 98b7815 | 2017-07-14 10:58:50 -0400 | [diff] [blame] | 102 | } |
Yan, Shaobo | 9286adc | 2019-04-26 15:25:18 +0000 | [diff] [blame] | 103 | |
| 104 | uint16_t Float32ToFloat16(float fp32) { |
| 105 | uint32_t fp32i = BitCast<uint32_t>(fp32); |
| 106 | uint32_t sign16 = (fp32i & 0x80000000) >> 16; |
| 107 | uint32_t mantissaAndExponent = fp32i & 0x7FFFFFFF; |
| 108 | |
Corentin Wallez | 77aa5b5 | 2019-06-26 20:26:13 +0000 | [diff] [blame] | 109 | if (mantissaAndExponent > 0x7F800000) { // NaN |
| 110 | return 0x7FFF; |
| 111 | } else if (mantissaAndExponent > 0x47FFEFFF) { // Infinity |
| 112 | return static_cast<uint16_t>(sign16 | 0x7C00); |
Yan, Shaobo | 9286adc | 2019-04-26 15:25:18 +0000 | [diff] [blame] | 113 | } else if (mantissaAndExponent < 0x38800000) { // Denormal |
| 114 | uint32_t mantissa = (mantissaAndExponent & 0x007FFFFF) | 0x00800000; |
| 115 | int32_t exponent = 113 - (mantissaAndExponent >> 23); |
| 116 | |
| 117 | if (exponent < 24) { |
| 118 | mantissaAndExponent = mantissa >> exponent; |
| 119 | } else { |
| 120 | mantissaAndExponent = 0; |
| 121 | } |
| 122 | |
| 123 | return static_cast<uint16_t>( |
| 124 | sign16 | (mantissaAndExponent + 0x00000FFF + ((mantissaAndExponent >> 13) & 1)) >> 13); |
| 125 | } else { |
| 126 | return static_cast<uint16_t>(sign16 | (mantissaAndExponent + 0xC8000000 + 0x00000FFF + |
| 127 | ((mantissaAndExponent >> 13) & 1)) >> |
| 128 | 13); |
| 129 | } |
| 130 | } |
Corentin Wallez | 431d618 | 2019-07-01 09:58:07 +0000 | [diff] [blame] | 131 | |
Corentin Wallez | 2d8ba5f | 2019-07-12 07:13:41 +0000 | [diff] [blame] | 132 | bool IsFloat16NaN(uint16_t fp16) { |
| 133 | return (fp16 & 0x7FFF) > 0x7C00; |
| 134 | } |
| 135 | |
Corentin Wallez | 431d618 | 2019-07-01 09:58:07 +0000 | [diff] [blame] | 136 | // Based on the Khronos Data Format Specification 1.2 Section 13.3 sRGB transfer functions |
| 137 | float SRGBToLinear(float srgb) { |
| 138 | // sRGB is always used in unsigned normalized formats so clamp to [0.0, 1.0] |
| 139 | if (srgb <= 0.0f) { |
| 140 | return 0.0f; |
| 141 | } else if (srgb > 1.0f) { |
| 142 | return 1.0f; |
| 143 | } |
| 144 | |
| 145 | if (srgb < 0.04045f) { |
| 146 | return srgb / 12.92f; |
| 147 | } else { |
| 148 | return std::pow((srgb + 0.055f) / 1.055f, 2.4f); |
| 149 | } |
| 150 | } |