blob: aab0e451ce9c3fc25e417b7acf37d86806296879 [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)
Bryan Bernhart506ce9b2019-07-26 11:20:08 +000064 return n <= 1 ? 1 : 1ull << (64 - __lzcnt64(n - 1));
Bryan Bernhart40793232019-07-22 21:42:18 +000065#else
Bryan Bernhart506ce9b2019-07-26 11:20:08 +000066 return n == 1 ? 1 : 1ull << (64 - __builtin_clzll(n - 1));
Bryan Bernhart40793232019-07-22 21:42:18 +000067#endif
68}
69
Corentin Wallezfd589f32017-07-10 13:46:05 -040070bool IsPowerOfTwo(size_t n) {
71 ASSERT(n != 0);
72 return (n & (n - 1)) == 0;
73}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040074
Austin Engae48c952017-08-17 14:02:16 -040075bool IsPtrAligned(const void* ptr, size_t alignment) {
Corentin Wallezfd589f32017-07-10 13:46:05 -040076 ASSERT(IsPowerOfTwo(alignment));
77 ASSERT(alignment != 0);
Kai Ninomiya78c8b832017-07-21 17:00:22 -070078 return (reinterpret_cast<size_t>(ptr) & (alignment - 1)) == 0;
Corentin Wallezfd589f32017-07-10 13:46:05 -040079}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040080
Corentin Wallezfd589f32017-07-10 13:46:05 -040081void* AlignVoidPtr(void* ptr, size_t alignment) {
Austin Eng8867e5d2017-07-14 18:53:07 -040082 ASSERT(IsPowerOfTwo(alignment));
Corentin Wallezfd589f32017-07-10 13:46:05 -040083 ASSERT(alignment != 0);
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050084 return reinterpret_cast<void*>((reinterpret_cast<size_t>(ptr) + (alignment - 1)) &
85 ~(alignment - 1));
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040086}
Austin Eng98b78152017-07-14 10:58:50 -040087
Austin Engae48c952017-08-17 14:02:16 -040088bool 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 Eng98b78152017-07-14 10:58:50 -040096uint32_t Align(uint32_t value, size_t alignment) {
Kai Ninomiya59dc03f2017-07-20 07:28:00 -070097 ASSERT(alignment <= UINT32_MAX);
Austin Eng98b78152017-07-14 10:58:50 -040098 ASSERT(IsPowerOfTwo(alignment));
99 ASSERT(alignment != 0);
Kai Ninomiya59dc03f2017-07-20 07:28:00 -0700100 uint32_t alignment32 = static_cast<uint32_t>(alignment);
101 return (value + (alignment32 - 1)) & ~(alignment32 - 1);
Austin Eng98b78152017-07-14 10:58:50 -0400102}
Yan, Shaobo9286adc2019-04-26 15:25:18 +0000103
104uint16_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 Wallez77aa5b52019-06-26 20:26:13 +0000109 if (mantissaAndExponent > 0x7F800000) { // NaN
110 return 0x7FFF;
111 } else if (mantissaAndExponent > 0x47FFEFFF) { // Infinity
112 return static_cast<uint16_t>(sign16 | 0x7C00);
Yan, Shaobo9286adc2019-04-26 15:25:18 +0000113 } 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 Wallez431d6182019-07-01 09:58:07 +0000131
Corentin Wallez2d8ba5f2019-07-12 07:13:41 +0000132bool IsFloat16NaN(uint16_t fp16) {
133 return (fp16 & 0x7FFF) > 0x7C00;
134}
135
Corentin Wallez431d6182019-07-01 09:58:07 +0000136// Based on the Khronos Data Format Specification 1.2 Section 13.3 sRGB transfer functions
137float 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}