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