Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
StaticUtils.h
1 
2 /**
3  * Copyright (c) 2015-present, Facebook, Inc.
4  * All rights reserved.
5  *
6  * This source code is licensed under the CC-by-NC license found in the
7  * LICENSE file in the root directory of this source tree.
8  */
9 
10 // Copyright 2004-present Facebook. All Rights Reserved.
11 
12 #pragma once
13 
14 #include <cuda.h>
15 
16 namespace faiss { namespace gpu { namespace utils {
17 
18 template <typename U, typename V>
19 constexpr __host__ __device__ auto divUp(U a, V b) -> decltype(a + b) {
20  return (a + b - 1) / b;
21 }
22 
23 template <typename U, typename V>
24 constexpr __host__ __device__ auto roundDown(U a, V b) -> decltype(a + b) {
25  return (a / b) * b;
26 }
27 
28 template <typename U, typename V>
29 constexpr __host__ __device__ auto roundUp(U a, V b) -> decltype(a + b) {
30  return divUp(a, b) * b;
31 }
32 
33 template <class T>
34 constexpr __host__ __device__ T pow(T n, T power) {
35  return (power > 0 ? n * pow(n, power - 1) : 1);
36 }
37 
38 template <class T>
39 constexpr __host__ __device__ T pow2(T n) {
40  return pow(2, (T) n);
41 }
42 
43 static_assert(pow2(8) == 256, "pow2");
44 
45 template <typename T>
46 constexpr __host__ __device__ int log2(T n, int p = 0) {
47  return (n <= 1) ? p : log2(n / 2, p + 1);
48 }
49 
50 static_assert(log2(2) == 1, "log2");
51 static_assert(log2(3) == 1, "log2");
52 static_assert(log2(4) == 2, "log2");
53 
54 template <typename T>
55 constexpr __host__ __device__ bool isPowerOf2(T v) {
56  return (v && !(v & (v - 1)));
57 }
58 
59 static_assert(isPowerOf2(2048), "isPowerOf2");
60 static_assert(!isPowerOf2(3333), "isPowerOf2");
61 
62 template <typename T>
63 constexpr __host__ __device__ T nextHighestPowerOf2(T v) {
64  return (isPowerOf2(v) ? (T) 2 * v : (1 << (log2(v) + 1)));
65 }
66 
67 static_assert(nextHighestPowerOf2(1) == 2, "nextHighestPowerOf2");
68 static_assert(nextHighestPowerOf2(2) == 4, "nextHighestPowerOf2");
69 static_assert(nextHighestPowerOf2(3) == 4, "nextHighestPowerOf2");
70 static_assert(nextHighestPowerOf2(4) == 8, "nextHighestPowerOf2");
71 
72 static_assert(nextHighestPowerOf2(15) == 16, "nextHighestPowerOf2");
73 static_assert(nextHighestPowerOf2(16) == 32, "nextHighestPowerOf2");
74 static_assert(nextHighestPowerOf2(17) == 32, "nextHighestPowerOf2");
75 
76 } } } // namespace