blob: c7ff48b33673a4e6e5584181f560bd5b57122206 [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
Yan, Shaobo9286adc2019-04-26 15:25:18 +000077uint16_t Float32ToFloat16(float fp32) {
78 uint32_t fp32i = BitCast<uint32_t>(fp32);
79 uint32_t sign16 = (fp32i & 0x80000000) >> 16;
80 uint32_t mantissaAndExponent = fp32i & 0x7FFFFFFF;
81
Corentin Wallez77aa5b52019-06-26 20:26:13 +000082 if (mantissaAndExponent > 0x7F800000) { // NaN
83 return 0x7FFF;
84 } else if (mantissaAndExponent > 0x47FFEFFF) { // Infinity
85 return static_cast<uint16_t>(sign16 | 0x7C00);
Yan, Shaobo9286adc2019-04-26 15:25:18 +000086 } else if (mantissaAndExponent < 0x38800000) { // Denormal
87 uint32_t mantissa = (mantissaAndExponent & 0x007FFFFF) | 0x00800000;
88 int32_t exponent = 113 - (mantissaAndExponent >> 23);
89
90 if (exponent < 24) {
91 mantissaAndExponent = mantissa >> exponent;
92 } else {
93 mantissaAndExponent = 0;
94 }
95
96 return static_cast<uint16_t>(
97 sign16 | (mantissaAndExponent + 0x00000FFF + ((mantissaAndExponent >> 13) & 1)) >> 13);
98 } else {
99 return static_cast<uint16_t>(sign16 | (mantissaAndExponent + 0xC8000000 + 0x00000FFF +
100 ((mantissaAndExponent >> 13) & 1)) >>
101 13);
102 }
103}
Corentin Wallez431d6182019-07-01 09:58:07 +0000104
Corentin Wallez2d8ba5f2019-07-12 07:13:41 +0000105bool IsFloat16NaN(uint16_t fp16) {
106 return (fp16 & 0x7FFF) > 0x7C00;
107}
108
Corentin Wallez431d6182019-07-01 09:58:07 +0000109// Based on the Khronos Data Format Specification 1.2 Section 13.3 sRGB transfer functions
110float SRGBToLinear(float srgb) {
111 // sRGB is always used in unsigned normalized formats so clamp to [0.0, 1.0]
112 if (srgb <= 0.0f) {
113 return 0.0f;
114 } else if (srgb > 1.0f) {
115 return 1.0f;
116 }
117
118 if (srgb < 0.04045f) {
119 return srgb / 12.92f;
120 } else {
121 return std::pow((srgb + 0.055f) / 1.055f, 2.4f);
122 }
123}