• 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 <cstddef>
19 #include <cstdint>
20 #include <cstring>
21 
22 #include <limits>
23 #include <type_traits>
24 
25 // The following are not valid for 0
26 uint32_t ScanForward(uint32_t bits);
27 uint32_t Log2(uint32_t value);
28 bool IsPowerOfTwo(size_t n);
29 
30 bool IsPtrAligned(const void* ptr, size_t alignment);
31 void* AlignVoidPtr(void* ptr, size_t alignment);
32 bool IsAligned(uint32_t value, size_t alignment);
33 uint32_t Align(uint32_t value, size_t alignment);
34 
35 template <typename T>
AlignPtr(T * ptr,size_t alignment)36 T* AlignPtr(T* ptr, size_t alignment) {
37     return static_cast<T*>(AlignVoidPtr(ptr, alignment));
38 }
39 
40 template <typename T>
AlignPtr(const T * ptr,size_t alignment)41 const T* AlignPtr(const T* ptr, size_t alignment) {
42     return static_cast<const T*>(AlignVoidPtr(const_cast<T*>(ptr), alignment));
43 }
44 
45 template <typename destType, typename sourceType>
BitCast(const sourceType & source)46 destType BitCast(const sourceType& source) {
47     static_assert(sizeof(destType) == sizeof(sourceType), "BitCast: cannot lose precision.");
48     destType output;
49     std::memcpy(&output, &source, sizeof(destType));
50     return output;
51 }
52 
53 uint16_t Float32ToFloat16(float fp32);
54 bool IsFloat16NaN(uint16_t fp16);
55 
56 float SRGBToLinear(float srgb);
57 
58 #endif  // COMMON_MATH_H_
59