Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 1 | // Copyright 2017 The NXT 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 | |
Corentin Wallez | fffe6df | 2017-07-06 14:41:13 -0400 | [diff] [blame] | 15 | #include "common/Math.h" |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 16 | |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 17 | #include "common/Assert.h" |
| 18 | |
Corentin Wallez | 0b186b1 | 2017-07-12 12:56:05 -0400 | [diff] [blame] | 19 | #if defined(NXT_COMPILER_MSVC) |
Corentin Wallez | 40fb17d | 2017-05-30 18:02:38 -0400 | [diff] [blame] | 20 | #include <intrin.h> |
| 21 | #endif |
Corentin Wallez | 944b60f | 2017-05-29 11:33:33 -0700 | [diff] [blame] | 22 | |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 23 | uint32_t ScanForward(uint32_t bits) { |
| 24 | ASSERT(bits != 0); |
Corentin Wallez | 0b186b1 | 2017-07-12 12:56:05 -0400 | [diff] [blame] | 25 | #if defined(NXT_COMPILER_MSVC) |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 26 | unsigned long firstBitIndex = 0ul; |
| 27 | unsigned char ret = _BitScanForward(&firstBitIndex, bits); |
| 28 | ASSERT(ret != 0); |
| 29 | return firstBitIndex; |
| 30 | #else |
| 31 | return static_cast<unsigned long>(__builtin_ctz(bits)); |
| 32 | #endif |
| 33 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 34 | |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 35 | uint32_t Log2(uint32_t value) { |
| 36 | ASSERT(value != 0); |
Corentin Wallez | 0b186b1 | 2017-07-12 12:56:05 -0400 | [diff] [blame] | 37 | #if defined(NXT_COMPILER_MSVC) |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 38 | unsigned long firstBitIndex = 0ul; |
| 39 | unsigned char ret = _BitScanReverse(&firstBitIndex, value); |
| 40 | ASSERT(ret != 0); |
| 41 | return firstBitIndex; |
| 42 | #else |
| 43 | return 31 - __builtin_clz(value); |
| 44 | #endif |
| 45 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 46 | |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 47 | bool IsPowerOfTwo(size_t n) { |
| 48 | ASSERT(n != 0); |
| 49 | return (n & (n - 1)) == 0; |
| 50 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 51 | |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 52 | bool IsAligned(const void* ptr, size_t alignment) { |
| 53 | ASSERT(IsPowerOfTwo(alignment)); |
| 54 | ASSERT(alignment != 0); |
| 55 | return (reinterpret_cast<intptr_t>(ptr) & (alignment - 1)) == 0; |
| 56 | } |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 57 | |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 58 | void* AlignVoidPtr(void* ptr, size_t alignment) { |
Austin Eng | 8867e5d | 2017-07-14 18:53:07 -0400 | [diff] [blame^] | 59 | ASSERT(IsPowerOfTwo(alignment)); |
Corentin Wallez | fd589f3 | 2017-07-10 13:46:05 -0400 | [diff] [blame] | 60 | ASSERT(alignment != 0); |
| 61 | return reinterpret_cast<void*>((reinterpret_cast<intptr_t>(ptr) + (alignment - 1)) & ~(alignment - 1)); |
Corentin Wallez | f07e3bd | 2017-04-20 14:38:20 -0400 | [diff] [blame] | 62 | } |