ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
StaticUtils.cuh
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <cuda.h>
11 
12 // allow usage for non-CUDA files
13 #ifndef __host__
14 #define __host__
15 #define __device__
16 #endif
17 
18 namespace cloudViewer {
19 namespace core {
20 
21 template <typename U, typename V>
22 constexpr __host__ __device__ auto divDown(U a, V b) -> decltype(a + b) {
23  return (a / b);
24 }
25 
26 template <typename U, typename V>
27 constexpr __host__ __device__ auto divUp(U a, V b) -> decltype(a + b) {
28  return (a + b - 1) / b;
29 }
30 
31 template <typename U, typename V>
32 constexpr __host__ __device__ auto roundDown(U a, V b) -> decltype(a + b) {
33  return divDown(a, b) * b;
34 }
35 
36 template <typename U, typename V>
37 constexpr __host__ __device__ auto roundUp(U a, V b) -> decltype(a + b) {
38  return divUp(a, b) * b;
39 }
40 
41 template <class T>
42 constexpr __host__ __device__ T pow(T n, T power) {
43  return (power > 0 ? n * pow(n, power - 1) : 1);
44 }
45 
46 template <class T>
47 constexpr __host__ __device__ T pow2(T n) {
48  return pow(2, (T)n);
49 }
50 
51 static_assert(pow2(8) == 256, "pow2");
52 
53 template <typename T>
54 constexpr __host__ __device__ int log2(T n, int p = 0) {
55  return (n <= 1) ? p : log2(n / 2, p + 1);
56 }
57 
58 static_assert(log2(2) == 1, "log2");
59 static_assert(log2(3) == 1, "log2");
60 static_assert(log2(4) == 2, "log2");
61 
62 template <typename T>
63 constexpr __host__ __device__ bool isPowerOf2(T v) {
64  return (v && !(v & (v - 1)));
65 }
66 
67 static_assert(isPowerOf2(2048), "isPowerOf2");
68 static_assert(!isPowerOf2(3333), "isPowerOf2");
69 
70 template <typename T>
71 constexpr __host__ __device__ T nextHighestPowerOf2(T v) {
72  return (isPowerOf2(v) ? (T)2 * v : ((T)1 << (log2(v) + 1)));
73 }
74 
75 static_assert(nextHighestPowerOf2(1) == 2, "nextHighestPowerOf2");
76 static_assert(nextHighestPowerOf2(2) == 4, "nextHighestPowerOf2");
77 static_assert(nextHighestPowerOf2(3) == 4, "nextHighestPowerOf2");
78 static_assert(nextHighestPowerOf2(4) == 8, "nextHighestPowerOf2");
79 
80 static_assert(nextHighestPowerOf2(15) == 16, "nextHighestPowerOf2");
81 static_assert(nextHighestPowerOf2(16) == 32, "nextHighestPowerOf2");
82 static_assert(nextHighestPowerOf2(17) == 32, "nextHighestPowerOf2");
83 
84 static_assert(nextHighestPowerOf2(1536000000u) == 2147483648u,
85  "nextHighestPowerOf2");
86 static_assert(nextHighestPowerOf2((size_t)2147483648ULL) ==
87  (size_t)4294967296ULL,
88  "nextHighestPowerOf2");
89 
90 } // namespace core
91 } // namespace cloudViewer