blob: d9217c8e44325fee1996cda3b45ba88e47501b05 [file] [log] [blame]
Corentin Wallez4a9ef4e2018-07-18 11:40:26 +02001// Copyright 2017 The Dawn Authors
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04002//
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 Wallezfffe6df2017-07-06 14:41:13 -040015#include "common/Math.h"
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040016
Corentin Wallezfd589f32017-07-10 13:46:05 -040017#include "common/Assert.h"
18
Yan, Shaobo9286adc2019-04-26 15:25:18 +000019#include <algorithm>
20
Corentin Wallez83a9c9d2018-07-18 13:37:54 +020021#if defined(DAWN_COMPILER_MSVC)
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050022# include <intrin.h>
Corentin Wallez40fb17d2017-05-30 18:02:38 -040023#endif
Corentin Wallez944b60f2017-05-29 11:33:33 -070024
Corentin Wallezfd589f32017-07-10 13:46:05 -040025uint32_t ScanForward(uint32_t bits) {
26 ASSERT(bits != 0);
Corentin Wallez83a9c9d2018-07-18 13:37:54 +020027#if defined(DAWN_COMPILER_MSVC)
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050028 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 Wallezfd589f32017-07-10 13:46:05 -040035}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040036
Corentin Wallezfd589f32017-07-10 13:46:05 -040037uint32_t Log2(uint32_t value) {
38 ASSERT(value != 0);
Corentin Wallez83a9c9d2018-07-18 13:37:54 +020039#if defined(DAWN_COMPILER_MSVC)
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050040 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 Wallezfd589f32017-07-10 13:46:05 -040047}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040048
Corentin Wallezfd589f32017-07-10 13:46:05 -040049bool IsPowerOfTwo(size_t n) {
50 ASSERT(n != 0);
51 return (n & (n - 1)) == 0;
52}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040053
Austin Engae48c952017-08-17 14:02:16 -040054bool IsPtrAligned(const void* ptr, size_t alignment) {
Corentin Wallezfd589f32017-07-10 13:46:05 -040055 ASSERT(IsPowerOfTwo(alignment));
56 ASSERT(alignment != 0);
Kai Ninomiya78c8b832017-07-21 17:00:22 -070057 return (reinterpret_cast<size_t>(ptr) & (alignment - 1)) == 0;
Corentin Wallezfd589f32017-07-10 13:46:05 -040058}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040059
Corentin Wallezfd589f32017-07-10 13:46:05 -040060void* AlignVoidPtr(void* ptr, size_t alignment) {
Austin Eng8867e5d2017-07-14 18:53:07 -040061 ASSERT(IsPowerOfTwo(alignment));
Corentin Wallezfd589f32017-07-10 13:46:05 -040062 ASSERT(alignment != 0);
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050063 return reinterpret_cast<void*>((reinterpret_cast<size_t>(ptr) + (alignment - 1)) &
64 ~(alignment - 1));
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040065}
Austin Eng98b78152017-07-14 10:58:50 -040066
Austin Engae48c952017-08-17 14:02:16 -040067bool 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 Eng98b78152017-07-14 10:58:50 -040075uint32_t Align(uint32_t value, size_t alignment) {
Kai Ninomiya59dc03f2017-07-20 07:28:00 -070076 ASSERT(alignment <= UINT32_MAX);
Austin Eng98b78152017-07-14 10:58:50 -040077 ASSERT(IsPowerOfTwo(alignment));
78 ASSERT(alignment != 0);
Kai Ninomiya59dc03f2017-07-20 07:28:00 -070079 uint32_t alignment32 = static_cast<uint32_t>(alignment);
80 return (value + (alignment32 - 1)) & ~(alignment32 - 1);
Austin Eng98b78152017-07-14 10:58:50 -040081}
Yan, Shaobo9286adc2019-04-26 15:25:18 +000082
83uint16_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}