blob: 1552c877add5d12f598814aaf44325ff5c352674 [file] [log] [blame]
Corentin Wallezf07e3bd2017-04-20 14:38:20 -04001// 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 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
Corentin Wallez0b186b12017-07-12 12:56:05 -040019#if defined(NXT_COMPILER_MSVC)
Corentin Wallez40fb17d2017-05-30 18:02:38 -040020 #include <intrin.h>
21#endif
Corentin Wallez944b60f2017-05-29 11:33:33 -070022
Corentin Wallezfd589f32017-07-10 13:46:05 -040023uint32_t ScanForward(uint32_t bits) {
24 ASSERT(bits != 0);
Corentin Wallez0b186b12017-07-12 12:56:05 -040025 #if defined(NXT_COMPILER_MSVC)
Corentin Wallezfd589f32017-07-10 13:46:05 -040026 unsigned long firstBitIndex = 0ul;
27 unsigned char ret = _BitScanForward(&firstBitIndex, bits);
28 ASSERT(ret != 0);
29 return firstBitIndex;
30 #else
Kai Ninomiya78c8b832017-07-21 17:00:22 -070031 return static_cast<uint32_t>(__builtin_ctz(bits));
Corentin Wallezfd589f32017-07-10 13:46:05 -040032 #endif
33}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040034
Corentin Wallezfd589f32017-07-10 13:46:05 -040035uint32_t Log2(uint32_t value) {
36 ASSERT(value != 0);
Corentin Wallez0b186b12017-07-12 12:56:05 -040037 #if defined(NXT_COMPILER_MSVC)
Corentin Wallezfd589f32017-07-10 13:46:05 -040038 unsigned long firstBitIndex = 0ul;
39 unsigned char ret = _BitScanReverse(&firstBitIndex, value);
40 ASSERT(ret != 0);
41 return firstBitIndex;
42 #else
Kai Ninomiya78c8b832017-07-21 17:00:22 -070043 return 31 - static_cast<uint32_t>(__builtin_clz(value));
Corentin Wallezfd589f32017-07-10 13:46:05 -040044 #endif
45}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040046
Corentin Wallezfd589f32017-07-10 13:46:05 -040047bool IsPowerOfTwo(size_t n) {
48 ASSERT(n != 0);
49 return (n & (n - 1)) == 0;
50}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040051
Corentin Wallezfd589f32017-07-10 13:46:05 -040052bool IsAligned(const void* ptr, size_t alignment) {
53 ASSERT(IsPowerOfTwo(alignment));
54 ASSERT(alignment != 0);
Kai Ninomiya78c8b832017-07-21 17:00:22 -070055 return (reinterpret_cast<size_t>(ptr) & (alignment - 1)) == 0;
Corentin Wallezfd589f32017-07-10 13:46:05 -040056}
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040057
Corentin Wallezfd589f32017-07-10 13:46:05 -040058void* AlignVoidPtr(void* ptr, size_t alignment) {
Austin Eng8867e5d2017-07-14 18:53:07 -040059 ASSERT(IsPowerOfTwo(alignment));
Corentin Wallezfd589f32017-07-10 13:46:05 -040060 ASSERT(alignment != 0);
Kai Ninomiya78c8b832017-07-21 17:00:22 -070061 return reinterpret_cast<void*>((reinterpret_cast<size_t>(ptr) + (alignment - 1)) & ~(alignment - 1));
Corentin Wallezf07e3bd2017-04-20 14:38:20 -040062}
Austin Eng98b78152017-07-14 10:58:50 -040063
64uint32_t Align(uint32_t value, size_t alignment) {
Kai Ninomiya59dc03f2017-07-20 07:28:00 -070065 ASSERT(alignment <= UINT32_MAX);
Austin Eng98b78152017-07-14 10:58:50 -040066 ASSERT(IsPowerOfTwo(alignment));
67 ASSERT(alignment != 0);
Kai Ninomiya59dc03f2017-07-20 07:28:00 -070068 uint32_t alignment32 = static_cast<uint32_t>(alignment);
69 return (value + (alignment32 - 1)) & ~(alignment32 - 1);
Austin Eng98b78152017-07-14 10:58:50 -040070}