blob: a096bead4d9ddb285e30c1557858dab14da37a91 [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
Corentin Wallezfd589f32017-07-10 13:46:05 -040050bool IsPowerOfTwo(size_t n) {
51 ASSERT(n != 0);
52 return (n & (n - 1)) == 0;
53}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040054
Austin Engae48c952017-08-17 14:02:16 -040055bool IsPtrAligned(const void* ptr, size_t alignment) {
Corentin Wallezfd589f32017-07-10 13:46:05 -040056 ASSERT(IsPowerOfTwo(alignment));
57 ASSERT(alignment != 0);
Kai Ninomiya78c8b832017-07-21 17:00:22 -070058 return (reinterpret_cast<size_t>(ptr) & (alignment - 1)) == 0;
Corentin Wallezfd589f32017-07-10 13:46:05 -040059}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040060
Corentin Wallezfd589f32017-07-10 13:46:05 -040061void* AlignVoidPtr(void* ptr, size_t alignment) {
Austin Eng8867e5d2017-07-14 18:53:07 -040062 ASSERT(IsPowerOfTwo(alignment));
Corentin Wallezfd589f32017-07-10 13:46:05 -040063 ASSERT(alignment != 0);
Corentin Wallez9d01c6c2017-11-24 11:45:29 -050064 return reinterpret_cast<void*>((reinterpret_cast<size_t>(ptr) + (alignment - 1)) &
65 ~(alignment - 1));
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040066}
Austin Eng98b78152017-07-14 10:58:50 -040067
Austin Engae48c952017-08-17 14:02:16 -040068bool IsAligned(uint32_t value, size_t alignment) {
69 ASSERT(alignment <= UINT32_MAX);
70 ASSERT(IsPowerOfTwo(alignment));
71 ASSERT(alignment != 0);
72 uint32_t alignment32 = static_cast<uint32_t>(alignment);
73 return (value & (alignment32 - 1)) == 0;
74}
75
Austin Eng98b78152017-07-14 10:58:50 -040076uint32_t Align(uint32_t value, size_t alignment) {
Kai Ninomiya59dc03f2017-07-20 07:28:00 -070077 ASSERT(alignment <= UINT32_MAX);
Austin Eng98b78152017-07-14 10:58:50 -040078 ASSERT(IsPowerOfTwo(alignment));
79 ASSERT(alignment != 0);
Kai Ninomiya59dc03f2017-07-20 07:28:00 -070080 uint32_t alignment32 = static_cast<uint32_t>(alignment);
81 return (value + (alignment32 - 1)) & ~(alignment32 - 1);
Austin Eng98b78152017-07-14 10:58:50 -040082}
Yan, Shaobo9286adc2019-04-26 15:25:18 +000083
84uint16_t Float32ToFloat16(float fp32) {
85 uint32_t fp32i = BitCast<uint32_t>(fp32);
86 uint32_t sign16 = (fp32i & 0x80000000) >> 16;
87 uint32_t mantissaAndExponent = fp32i & 0x7FFFFFFF;
88
Corentin Wallez77aa5b52019-06-26 20:26:13 +000089 if (mantissaAndExponent > 0x7F800000) { // NaN
90 return 0x7FFF;
91 } else if (mantissaAndExponent > 0x47FFEFFF) { // Infinity
92 return static_cast<uint16_t>(sign16 | 0x7C00);
Yan, Shaobo9286adc2019-04-26 15:25:18 +000093 } else if (mantissaAndExponent < 0x38800000) { // Denormal
94 uint32_t mantissa = (mantissaAndExponent & 0x007FFFFF) | 0x00800000;
95 int32_t exponent = 113 - (mantissaAndExponent >> 23);
96
97 if (exponent < 24) {
98 mantissaAndExponent = mantissa >> exponent;
99 } else {
100 mantissaAndExponent = 0;
101 }
102
103 return static_cast<uint16_t>(
104 sign16 | (mantissaAndExponent + 0x00000FFF + ((mantissaAndExponent >> 13) & 1)) >> 13);
105 } else {
106 return static_cast<uint16_t>(sign16 | (mantissaAndExponent + 0xC8000000 + 0x00000FFF +
107 ((mantissaAndExponent >> 13) & 1)) >>
108 13);
109 }
110}
Corentin Wallez431d6182019-07-01 09:58:07 +0000111
112// Based on the Khronos Data Format Specification 1.2 Section 13.3 sRGB transfer functions
113float SRGBToLinear(float srgb) {
114 // sRGB is always used in unsigned normalized formats so clamp to [0.0, 1.0]
115 if (srgb <= 0.0f) {
116 return 0.0f;
117 } else if (srgb > 1.0f) {
118 return 1.0f;
119 }
120
121 if (srgb < 0.04045f) {
122 return srgb / 12.92f;
123 } else {
124 return std::pow((srgb + 0.055f) / 1.055f, 2.4f);
125 }
126}