NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
static.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (c) 2013, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the
12  * names of its contributors may be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  ******************************************************************************/
27 
28 /******************************************************************************
29  *
30  * Code and text by Sean Baxter, NVIDIA Research
31  * See http://nvlabs.github.io/moderngpu for repository and documentation.
32  *
33  ******************************************************************************/
34 
35 #pragma once
36 
37 #include <functional>
38 #include <iterator>
39 #include <cfloat>
40 #include <typeinfo>
41 #include <vector>
42 #include <list>
43 #include <map>
44 #include <algorithm>
45 #include <cassert>
46 #include <memory>
47 #include <cmath>
48 #include <cstdio>
49 #include <cstdlib>
50 
51 #ifndef MGPU_MIN
52 #define MGPU_MIN(x, y) (((x) <= (y)) ? (x) : (y))
53 #define MGPU_MAX(x, y) (((x) >= (y)) ? (x) : (y))
54 #define MGPU_MAX0(x) (((x) >= 0) ? (x) : 0)
55 #define MGPU_ABS(x) (((x) >= 0) ? (x) : (-x))
56 
57 #define MGPU_DIV_UP(x, y) (((x) + (y) - 1) / (y))
58 #define MGPU_DIV_ROUND(x, y) (((x) + (y) / 2) / (y))
59 #define MGPU_ROUND_UP(x, y) ((y) * MGPU_DIV_UP(x, y))
60 #define MGPU_SHIFT_DIV_UP(x, y) (((x) + ((1<< (y)) - 1))>> y)
61 #define MGPU_ROUND_UP_POW2(x, y) (((x) + (y) - 1) & ~((y) - 1))
62 #define MGPU_ROUND_DOWN_POW2(x, y) ((x) & ~((y) - 1))
63 #define MGPU_IS_POW_2(x) (0 == ((x) & ((x) - 1)))
64 
65 #endif // MGPU_MIN
66 
67 namespace mgpu {
68 
69 
70 typedef unsigned char byte;
71 
72 typedef unsigned int uint;
73 typedef signed short int16;
74 
75 typedef unsigned short ushort;
76 typedef unsigned short uint16;
77 
78 typedef long long int64;
79 typedef unsigned long long uint64;
80 
81 // IsPow2<X>::value is true if X is a power of 2.
82 template<int X> struct sIsPow2 {
83  enum { value = 0 == (X & (X - 1)) };
84 };
85 
86 // Finds the base-2 logarithm of X. value is -1 if X is not a power of 2.
87 template<int X, bool roundUp = true> struct sLogPow2 {
88  enum { extra = sIsPow2<X>::value ? 0 : (roundUp ? 1 : 0) };
89  enum { inner = sLogPow2<X / 2>::inner + 1 };
90  enum { value = inner + extra };
91 };
92 template<bool roundUp> struct sLogPow2<0, roundUp> {
93  enum { inner = 0 };
94  enum { value = 0 };
95 };
96 template<bool roundUp> struct sLogPow2<1, roundUp> {
97  enum { inner = 0 };
98  enum { value = 0 };
99 };
100 
101 template<int X, int Y>
102 struct sDivUp {
103  enum { value = (X + Y - 1) / Y };
104 };
105 
106 template<int count, int levels> struct sDiv2RoundUp {
108 };
109 template<int count> struct sDiv2RoundUp<count, 0> {
110  enum { value = count };
111 };
112 
113 template<int X, int Y>
114 struct sDivSafe {
115  enum { value = X / Y };
116 };
117 template<int X>
118 struct sDivSafe<X, 0> {
119  enum { value = 0 };
120 };
121 
122 template<int X, int Y>
123 struct sRoundUp {
124  enum { rem = X % Y };
125  enum { value = X + (rem ? (Y - rem) : 0) };
126 };
127 
128 template<int X, int Y>
129 struct sRoundDown {
130  enum { rem = X % Y };
131  enum { value = X - rem };
132 };
133 
134 // IntegerDiv is a template for avoiding divisions by zero in template
135 // evaluation. Templates always evaluate both b and c in an expression like
136 // a ? b : c, and will error if either rhs contains an illegal expression,
137 // even if the ternary is explictly designed to guard against that.
138 template<int X, int Y>
139 struct sIntegerDiv {
140  enum { value = X / (Y ? Y : (X + 1)) };
141 };
142 
143 template<int X, int Y>
144 struct sMax {
145  enum { value = (X >= Y) ? X : Y };
146 };
147 template<int X, int Y>
148 struct sMin {
149  enum { value = (X <= Y) ? X : Y };
150 };
151 
152 template<int X>
153 struct sAbs {
154  enum { value = (X >= 0) ? X : -X };
155 };
156 
157 
158 // Finds the number of powers of 2 in the prime factorization of X.
159 template<int X, int LSB = 1 & X> struct sNumFactorsOf2 {
160  enum { shifted = X >> 1 };
162 };
163 template<int X> struct sNumFactorsOf2<X, 1> {
164  enum { value = 0 };
165 };
166 
167 // Returns the divisor for a conflict-free transpose.
168 template<int X, int NumBanks = 32> struct sBankConflictDivisor {
169  enum { value =
170  (1 & X) ? 0 :
171  (sIsPow2<X>::value ? NumBanks :
172  (1<< sNumFactorsOf2<X>::value)) };
173  enum { log_value = sLogPow2<value>::value };
174 };
175 
176 template<int NT, int X, int NumBanks = 32> struct sConflictFreeStorage {
177  enum { count = NT * X };
180  enum { value = count + padding };
181 };
182 
183 } // namespace mgpu