blob: 1eeeb79cfd390a98cd3ee6ca0817d388f210987d [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>
Corentin Wallez431d6182019-07-01 09:58:07 +000020#include <cmath>
Yan, Shaobo9286adc2019-04-26 15:25:18 +000021
Corentin Wallez83a9c9d2018-07-18 13:37:54 +020022#if defined(DAWN_COMPILER_MSVC)
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050023# include <intrin.h>
Corentin Wallez40fb17d2017-05-30 18:02:38 -040024#endif
Corentin Wallez944b60f2017-05-29 11:33:33 -070025
Corentin Wallezfd589f32017-07-10 13:46:05 -040026uint32_t ScanForward(uint32_t bits) {
27 ASSERT(bits != 0);
Corentin Wallez83a9c9d2018-07-18 13:37:54 +020028#if defined(DAWN_COMPILER_MSVC)
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050029 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 Wallezfd589f32017-07-10 13:46:05 -040036}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040037
Corentin Wallezfd589f32017-07-10 13:46:05 -040038uint32_t Log2(uint32_t value) {
39 ASSERT(value != 0);
Corentin Wallez83a9c9d2018-07-18 13:37:54 +020040#if defined(DAWN_COMPILER_MSVC)
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050041 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 Wallezfd589f32017-07-10 13:46:05 -040048}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040049
Bryan Bernhart40793232019-07-22 21:42:18 +000050uint32_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 Bernhart506ce9b2019-07-26 11:20:08 +000062uint64_t NextPowerOfTwo(uint64_t n) {
Bryan Bernhart40793232019-07-22 21:42:18 +000063#if defined(DAWN_COMPILER_MSVC)
Austin Engf19c3282019-08-26 16:50:05 +000064 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 Bernhart40793232019-07-22 21:42:18 +000072#else
Brian Hoee3de1e2019-08-16 18:44:14 +000073 return n <= 1 ? 1 : 1ull << (64 - __builtin_clzll(n - 1));
Bryan Bernhart40793232019-07-22 21:42:18 +000074#endif
75}
76
Corentin Wallezfd589f32017-07-10 13:46:05 -040077bool IsPowerOfTwo(size_t n) {
78 ASSERT(n != 0);
79 return (n & (n - 1)) == 0;
80}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040081
Austin Engae48c952017-08-17 14:02:16 -040082bool IsPtrAligned(const void* ptr, size_t alignment) {
Corentin Wallezfd589f32017-07-10 13:46:05 -040083 ASSERT(IsPowerOfTwo(alignment));
84 ASSERT(alignment != 0);
Kai Ninomiya78c8b832017-07-21 17:00:22 -070085 return (reinterpret_cast<size_t>(ptr) & (alignment - 1)) == 0;
Corentin Wallezfd589f32017-07-10 13:46:05 -040086}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040087
Corentin Wallezfd589f32017-07-10 13:46:05 -040088void* AlignVoidPtr(void* ptr, size_t alignment) {
Austin Eng8867e5d2017-07-14 18:53:07 -040089 ASSERT(IsPowerOfTwo(alignment));
Corentin Wallezfd589f32017-07-10 13:46:05 -040090 ASSERT(alignment != 0);
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050091 return reinterpret_cast<void*>((reinterpret_cast<size_t>(ptr) + (alignment - 1)) &
92 ~(alignment - 1));
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040093}
Austin Eng98b78152017-07-14 10:58:50 -040094
Austin Engae48c952017-08-17 14:02:16 -040095bool IsAligned(uint32_t value, size_t alignment) {
96 ASSERT(alignment <= UINT32_MAX);
97 ASSERT(IsPowerOfTwo(alignment));
98 ASSERT(alignment != 0);
99 uint32_t alignment32 = static_cast<uint32_t>(alignment);
100 return (value & (alignment32 - 1)) == 0;
101}
102
Austin Eng98b78152017-07-14 10:58:50 -0400103uint32_t Align(uint32_t value, size_t alignment) {
Kai Ninomiya59dc03f2017-07-20 07:28:00 -0700104 ASSERT(alignment <= UINT32_MAX);
Austin Eng98b78152017-07-14 10:58:50 -0400105 ASSERT(IsPowerOfTwo(alignment));
106 ASSERT(alignment != 0);
Kai Ninomiya59dc03f2017-07-20 07:28:00 -0700107 uint32_t alignment32 = static_cast<uint32_t>(alignment);
108 return (value + (alignment32 - 1)) & ~(alignment32 - 1);
Austin Eng98b78152017-07-14 10:58:50 -0400109}
Yan, Shaobo9286adc2019-04-26 15:25:18 +0000110
111uint16_t Float32ToFloat16(float fp32) {
112 uint32_t fp32i = BitCast<uint32_t>(fp32);
113 uint32_t sign16 = (fp32i & 0x80000000) >> 16;
114 uint32_t mantissaAndExponent = fp32i & 0x7FFFFFFF;
115
Corentin Wallez77aa5b52019-06-26 20:26:13 +0000116 if (mantissaAndExponent > 0x7F800000) { // NaN
117 return 0x7FFF;
118 } else if (mantissaAndExponent > 0x47FFEFFF) { // Infinity
119 return static_cast<uint16_t>(sign16 | 0x7C00);
Yan, Shaobo9286adc2019-04-26 15:25:18 +0000120 } else if (mantissaAndExponent < 0x38800000) { // Denormal
121 uint32_t mantissa = (mantissaAndExponent & 0x007FFFFF) | 0x00800000;
122 int32_t exponent = 113 - (mantissaAndExponent >> 23);
123
124 if (exponent < 24) {
125 mantissaAndExponent = mantissa >> exponent;
126 } else {
127 mantissaAndExponent = 0;
128 }
129
130 return static_cast<uint16_t>(
131 sign16 | (mantissaAndExponent + 0x00000FFF + ((mantissaAndExponent >> 13) & 1)) >> 13);
132 } else {
133 return static_cast<uint16_t>(sign16 | (mantissaAndExponent + 0xC8000000 + 0x00000FFF +
134 ((mantissaAndExponent >> 13) & 1)) >>
135 13);
136 }
137}
Corentin Wallez431d6182019-07-01 09:58:07 +0000138
Corentin Wallez2d8ba5f2019-07-12 07:13:41 +0000139bool IsFloat16NaN(uint16_t fp16) {
140 return (fp16 & 0x7FFF) > 0x7C00;
141}
142
Corentin Wallez431d6182019-07-01 09:58:07 +0000143// Based on the Khronos Data Format Specification 1.2 Section 13.3 sRGB transfer functions
144float SRGBToLinear(float srgb) {
145 // sRGB is always used in unsigned normalized formats so clamp to [0.0, 1.0]
146 if (srgb <= 0.0f) {
147 return 0.0f;
148 } else if (srgb > 1.0f) {
149 return 1.0f;
150 }
151
152 if (srgb < 0.04045f) {
153 return srgb / 12.92f;
154 } else {
155 return std::pow((srgb + 0.055f) / 1.055f, 2.4f);
156 }
157}