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> |
| 20 | |
Corentin Wallez | 83a9c9d | 2018-07-18 13:37:54 +0200 | [diff] [blame] | 21 | #if defined(DAWN_COMPILER_MSVC) |
Corentin Wallez | 9d01c6c | 2017-11-24 11:45:29 -0500 | [diff] [blame] | 22 | # include <intrin.h> |
Corentin Wallez | 40fb17d | 2017-05-30 18:02:38 -0400 | [diff] [blame] | 23 | #endif |
Corentin Wallez | 944b60f | 2017-05-29 11:33:33 -0700 | [diff] [blame] | 24 | |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 25 | uint32_t ScanForward(uint32_t bits) { |
| 26 | ASSERT(bits != 0); |
Corentin Wallez | 83a9c9d | 2018-07-18 13:37:54 +0200 | [diff] [blame] | 27 | #if defined(DAWN_COMPILER_MSVC) |
Corentin Wallez | 9d01c6c | 2017-11-24 11:45:29 -0500 | [diff] [blame] | 28 | unsigned long firstBitIndex = 0ul; |
| 29 | unsigned char ret = _BitScanForward(&firstBitIndex, bits); |
| 30 | ASSERT(ret != 0); |
| 31 | return firstBitIndex; |
| 32 | #else |
| 33 | return static_cast<uint32_t>(__builtin_ctz(bits)); |
| 34 | #endif |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 35 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 36 | |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 37 | uint32_t Log2(uint32_t value) { |
| 38 | ASSERT(value != 0); |
Corentin Wallez | 83a9c9d | 2018-07-18 13:37:54 +0200 | [diff] [blame] | 39 | #if defined(DAWN_COMPILER_MSVC) |
Corentin Wallez | 9d01c6c | 2017-11-24 11:45:29 -0500 | [diff] [blame] | 40 | unsigned long firstBitIndex = 0ul; |
| 41 | unsigned char ret = _BitScanReverse(&firstBitIndex, value); |
| 42 | ASSERT(ret != 0); |
| 43 | return firstBitIndex; |
| 44 | #else |
| 45 | return 31 - static_cast<uint32_t>(__builtin_clz(value)); |
| 46 | #endif |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 47 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 48 | |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 49 | bool IsPowerOfTwo(size_t n) { |
| 50 | ASSERT(n != 0); |
| 51 | return (n & (n - 1)) == 0; |
| 52 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 53 | |
Austin Eng | ae48c95 | 2017-08-17 14:02:16 -0400 | [diff] [blame] | 54 | bool IsPtrAligned(const void* ptr, size_t alignment) { |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 55 | ASSERT(IsPowerOfTwo(alignment)); |
| 56 | ASSERT(alignment != 0); |
Kai Ninomiya | 78c8b83 | 2017-07-21 17:00:22 -0700 | [diff] [blame] | 57 | return (reinterpret_cast<size_t>(ptr) & (alignment - 1)) == 0; |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 58 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 59 | |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 60 | void* AlignVoidPtr(void* ptr, size_t alignment) { |
Austin Eng | 8867e5d | 2017-07-14 18:53:07 -0400 | [diff] [blame] | 61 | ASSERT(IsPowerOfTwo(alignment)); |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 62 | ASSERT(alignment != 0); |
Corentin Wallez | 9d01c6c | 2017-11-24 11:45:29 -0500 | [diff] [blame] | 63 | return reinterpret_cast<void*>((reinterpret_cast<size_t>(ptr) + (alignment - 1)) & |
| 64 | ~(alignment - 1)); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 65 | } |
Austin Eng | 98b7815 | 2017-07-14 10:58:50 -0400 | [diff] [blame] | 66 | |
Austin Eng | ae48c95 | 2017-08-17 14:02:16 -0400 | [diff] [blame] | 67 | bool IsAligned(uint32_t value, size_t alignment) { |
| 68 | ASSERT(alignment <= UINT32_MAX); |
| 69 | ASSERT(IsPowerOfTwo(alignment)); |
| 70 | ASSERT(alignment != 0); |
| 71 | uint32_t alignment32 = static_cast<uint32_t>(alignment); |
| 72 | return (value & (alignment32 - 1)) == 0; |
| 73 | } |
| 74 | |
Austin Eng | 98b7815 | 2017-07-14 10:58:50 -0400 | [diff] [blame] | 75 | uint32_t Align(uint32_t value, size_t alignment) { |
Kai Ninomiya | 59dc03f | 2017-07-20 07:28:00 -0700 | [diff] [blame] | 76 | ASSERT(alignment <= UINT32_MAX); |
Austin Eng | 98b7815 | 2017-07-14 10:58:50 -0400 | [diff] [blame] | 77 | ASSERT(IsPowerOfTwo(alignment)); |
| 78 | ASSERT(alignment != 0); |
Kai Ninomiya | 59dc03f | 2017-07-20 07:28:00 -0700 | [diff] [blame] | 79 | uint32_t alignment32 = static_cast<uint32_t>(alignment); |
| 80 | return (value + (alignment32 - 1)) & ~(alignment32 - 1); |
Austin Eng | 98b7815 | 2017-07-14 10:58:50 -0400 | [diff] [blame] | 81 | } |
Yan, Shaobo | 9286adc | 2019-04-26 15:25:18 +0000 | [diff] [blame] | 82 | |
| 83 | uint16_t Float32ToFloat16(float fp32) { |
| 84 | uint32_t fp32i = BitCast<uint32_t>(fp32); |
| 85 | uint32_t sign16 = (fp32i & 0x80000000) >> 16; |
| 86 | uint32_t mantissaAndExponent = fp32i & 0x7FFFFFFF; |
| 87 | |
| 88 | if (mantissaAndExponent > 0x47FFEFFF) { // Infinity |
| 89 | return static_cast<uint16_t>(sign16 | 0x7FFF); |
| 90 | } else if (mantissaAndExponent < 0x38800000) { // Denormal |
| 91 | uint32_t mantissa = (mantissaAndExponent & 0x007FFFFF) | 0x00800000; |
| 92 | int32_t exponent = 113 - (mantissaAndExponent >> 23); |
| 93 | |
| 94 | if (exponent < 24) { |
| 95 | mantissaAndExponent = mantissa >> exponent; |
| 96 | } else { |
| 97 | mantissaAndExponent = 0; |
| 98 | } |
| 99 | |
| 100 | return static_cast<uint16_t>( |
| 101 | sign16 | (mantissaAndExponent + 0x00000FFF + ((mantissaAndExponent >> 13) & 1)) >> 13); |
| 102 | } else { |
| 103 | return static_cast<uint16_t>(sign16 | (mantissaAndExponent + 0xC8000000 + 0x00000FFF + |
| 104 | ((mantissaAndExponent >> 13) & 1)) >> |
| 105 | 13); |
| 106 | } |
| 107 | } |