• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Dawn Authors
2 //
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 
15 #ifndef COMMON_MATH_H_
16 #define COMMON_MATH_H_
17 
18 #include "common/Assert.h"
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <cstring>
23 
24 #include <limits>
25 #include <type_traits>
26 
27 // The following are not valid for 0
28 uint32_t ScanForward(uint32_t bits);
29 uint32_t Log2(uint32_t value);
30 uint32_t Log2(uint64_t value);
31 bool IsPowerOfTwo(uint64_t n);
32 uint64_t RoundUp(uint64_t n, uint64_t m);
33 
ConstexprLog2(uint64_t v)34 constexpr uint32_t ConstexprLog2(uint64_t v) {
35     return v <= 1 ? 0 : 1 + ConstexprLog2(v / 2);
36 }
37 
ConstexprLog2Ceil(uint64_t v)38 constexpr uint32_t ConstexprLog2Ceil(uint64_t v) {
39     return v <= 1 ? 0 : ConstexprLog2(v - 1) + 1;
40 }
41 
Log2Ceil(uint32_t v)42 inline uint32_t Log2Ceil(uint32_t v) {
43     return v <= 1 ? 0 : Log2(v - 1) + 1;
44 }
45 
Log2Ceil(uint64_t v)46 inline uint32_t Log2Ceil(uint64_t v) {
47     return v <= 1 ? 0 : Log2(v - 1) + 1;
48 }
49 
50 uint64_t NextPowerOfTwo(uint64_t n);
51 bool IsPtrAligned(const void* ptr, size_t alignment);
52 void* AlignVoidPtr(void* ptr, size_t alignment);
53 bool IsAligned(uint32_t value, size_t alignment);
54 
55 template <typename T>
Align(T value,size_t alignment)56 T Align(T value, size_t alignment) {
57     ASSERT(value <= std::numeric_limits<T>::max() - (alignment - 1));
58     ASSERT(IsPowerOfTwo(alignment));
59     ASSERT(alignment != 0);
60     T alignmentT = static_cast<T>(alignment);
61     return (value + (alignmentT - 1)) & ~(alignmentT - 1);
62 }
63 
64 template <typename T>
AlignPtr(T * ptr,size_t alignment)65 DAWN_FORCE_INLINE T* AlignPtr(T* ptr, size_t alignment) {
66     ASSERT(IsPowerOfTwo(alignment));
67     ASSERT(alignment != 0);
68     return reinterpret_cast<T*>((reinterpret_cast<size_t>(ptr) + (alignment - 1)) &
69                                 ~(alignment - 1));
70 }
71 
72 template <typename T>
AlignPtr(const T * ptr,size_t alignment)73 DAWN_FORCE_INLINE const T* AlignPtr(const T* ptr, size_t alignment) {
74     ASSERT(IsPowerOfTwo(alignment));
75     ASSERT(alignment != 0);
76     return reinterpret_cast<const T*>((reinterpret_cast<size_t>(ptr) + (alignment - 1)) &
77                                       ~(alignment - 1));
78 }
79 
80 template <typename destType, typename sourceType>
BitCast(const sourceType & source)81 destType BitCast(const sourceType& source) {
82     static_assert(sizeof(destType) == sizeof(sourceType), "BitCast: cannot lose precision.");
83     destType output;
84     std::memcpy(&output, &source, sizeof(destType));
85     return output;
86 }
87 
88 uint16_t Float32ToFloat16(float fp32);
89 float Float16ToFloat32(uint16_t fp16);
90 bool IsFloat16NaN(uint16_t fp16);
91 
92 float SRGBToLinear(float srgb);
93 
94 template <typename T1,
95           typename T2,
96           typename Enable = typename std::enable_if<sizeof(T1) == sizeof(T2)>::type>
IsSubset(T1 subset,T2 set)97 constexpr bool IsSubset(T1 subset, T2 set) {
98     T2 bitsAlsoInSet = subset & set;
99     return bitsAlsoInSet == subset;
100 }
101 
102 #endif  // COMMON_MATH_H_
103