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) |
Austin Eng | f19c328 | 2019-08-26 16:50:05 +0000 | [diff] [blame] | 64 | if (n <= 1) { |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | unsigned long firstBitIndex = 0ul; |
| 69 | unsigned char ret = _BitScanReverse64(&firstBitIndex, n - 1); |
| 70 | ASSERT(ret != 0); |
| 71 | return 1ull << (firstBitIndex + 1); |
Bryan Bernhart | 4079323 | 2019-07-22 21:42:18 +0000 | [diff] [blame] | 72 | #else |
Brian Ho | ee3de1e | 2019-08-16 18:44:14 +0000 | [diff] [blame] | 73 | return n <= 1 ? 1 : 1ull << (64 - __builtin_clzll(n - 1)); |
Bryan Bernhart | 4079323 | 2019-07-22 21:42:18 +0000 | [diff] [blame] | 74 | #endif |
| 75 | } |
| 76 | |
Yan, Shaobo | 9286adc | 2019-04-26 15:25:18 +0000 | [diff] [blame] | 77 | uint16_t Float32ToFloat16(float fp32) { |
| 78 | uint32_t fp32i = BitCast<uint32_t>(fp32); |
| 79 | uint32_t sign16 = (fp32i & 0x80000000) >> 16; |
| 80 | uint32_t mantissaAndExponent = fp32i & 0x7FFFFFFF; |
| 81 | |
Corentin Wallez | 77aa5b5 | 2019-06-26 20:26:13 +0000 | [diff] [blame] | 82 | if (mantissaAndExponent > 0x7F800000) { // NaN |
| 83 | return 0x7FFF; |
| 84 | } else if (mantissaAndExponent > 0x47FFEFFF) { // Infinity |
| 85 | return static_cast<uint16_t>(sign16 | 0x7C00); |
Yan, Shaobo | 9286adc | 2019-04-26 15:25:18 +0000 | [diff] [blame] | 86 | } else if (mantissaAndExponent < 0x38800000) { // Denormal |
| 87 | uint32_t mantissa = (mantissaAndExponent & 0x007FFFFF) | 0x00800000; |
| 88 | int32_t exponent = 113 - (mantissaAndExponent >> 23); |
| 89 | |
| 90 | if (exponent < 24) { |
| 91 | mantissaAndExponent = mantissa >> exponent; |
| 92 | } else { |
| 93 | mantissaAndExponent = 0; |
| 94 | } |
| 95 | |
| 96 | return static_cast<uint16_t>( |
| 97 | sign16 | (mantissaAndExponent + 0x00000FFF + ((mantissaAndExponent >> 13) & 1)) >> 13); |
| 98 | } else { |
| 99 | return static_cast<uint16_t>(sign16 | (mantissaAndExponent + 0xC8000000 + 0x00000FFF + |
| 100 | ((mantissaAndExponent >> 13) & 1)) >> |
| 101 | 13); |
| 102 | } |
| 103 | } |
Corentin Wallez | 431d618 | 2019-07-01 09:58:07 +0000 | [diff] [blame] | 104 | |
Corentin Wallez | 2d8ba5f | 2019-07-12 07:13:41 +0000 | [diff] [blame] | 105 | bool IsFloat16NaN(uint16_t fp16) { |
| 106 | return (fp16 & 0x7FFF) > 0x7C00; |
| 107 | } |
| 108 | |
Corentin Wallez | 431d618 | 2019-07-01 09:58:07 +0000 | [diff] [blame] | 109 | // Based on the Khronos Data Format Specification 1.2 Section 13.3 sRGB transfer functions |
| 110 | float SRGBToLinear(float srgb) { |
| 111 | // sRGB is always used in unsigned normalized formats so clamp to [0.0, 1.0] |
| 112 | if (srgb <= 0.0f) { |
| 113 | return 0.0f; |
| 114 | } else if (srgb > 1.0f) { |
| 115 | return 1.0f; |
| 116 | } |
| 117 | |
| 118 | if (srgb < 0.04045f) { |
| 119 | return srgb / 12.92f; |
| 120 | } else { |
| 121 | return std::pow((srgb + 0.055f) / 1.055f, 2.4f); |
| 122 | } |
| 123 | } |