libcommonc++  0.7
Numeric.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_Numeric_hxx
24 #define __ccxx_Numeric_hxx
25 
26 #include <limits>
27 
28 #ifdef min
29 #undef min
30 #endif
31 
32 #ifdef max
33 #undef max
34 #endif
35 
37 
38 namespace ccxx {
39 
46 class Numeric
47 {
48  public:
49 
57  static uint32_t nextPowerOf2(uint32_t value);
58 
66  static uint32_t leastSignificantSetBit(uint32_t value);
67 
75  static uint64_t leastSignificantSetBit(uint64_t value);
76 
84  static uint32_t mostSignificantSetBit(uint32_t value);
85 
93  static uint64_t mostSignificantSetBit(uint64_t value);
94 
101  static uint_t countSetBits(uint32_t value);
102 
109  static uint_t countSetBits(uint64_t value);
110 
117  static uint_t countLeadingZeroes(uint32_t value);
118 
125  static uint_t countLeadingZeroes(uint64_t value);
126 
133  static uint_t countTrailingZeroes(uint32_t value);
134 
141  static uint_t countTrailingZeroes(uint64_t value);
142 
143  private:
144 
145  Numeric();
146  CCXX_COPY_DECLS(Numeric);
147 };
148 
158 template<typename S, typename T>
159  inline T numeric_cast(S value)
160 {
161  if((value < std::numeric_limits<T>::min())
162  || (value > std::numeric_limits<T>::max()))
163  throw OutOfBoundsException();
164  else
165  return(static_cast<T>(value));
166 }
167 
175 template<typename S, typename T>
176  inline T numeric_clipping_cast(S value)
177 {
178  if(value < std::numeric_limits<T>::min())
179  return(std::numeric_limits<T>::min());
180  else if(value > std::numeric_limits<T>::max())
181  return(std::numeric_limits<T>::max());
182  else
183  return(static_cast<T>(value));
184 }
185 
195 template<typename T>
196  inline T numeric_clip(T value, T min, T max)
197 {
198  if(value < min)
199  return(min);
200  else if(value > max)
201  return(max);
202  else return(value);
203 }
204 
205 } // namespace ccxx
206 
207 #endif // __ccxx_Numeric_hxx
static uint32_t leastSignificantSetBit(uint32_t value)
Find the least significant bit that is set in a value.
Definition: Numeric.c++:52
static uint_t countSetBits(uint32_t value)
Count the number of bits that are set in a value.
Definition: Numeric.c++:99
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
static uint_t countLeadingZeroes(uint32_t value)
Count the number of leading zeroes in a value.
Definition: Numeric.c++:129
T numeric_clipping_cast(S value)
A range-clipping numeric cast.
Definition: Numeric.h++:176
An exception indicating that an attempted operation would result in an out-of-range array index acces...
Definition: OutOfBoundsException.h++:38
T numeric_clip(T value, T min, T max)
A numeric range-clipping function.
Definition: Numeric.h++:196
static uint32_t nextPowerOf2(uint32_t value)
Calculate the next higher power of 2 for a value.
Definition: Numeric.c++:35
static uint_t countTrailingZeroes(uint32_t value)
Count the number of trailing zeroes in a value.
Definition: Numeric.c++:158
static uint32_t mostSignificantSetBit(uint32_t value)
Find the most significant bit that is set in a value.
Definition: Numeric.c++:70
T numeric_cast(S value)
A range-checked numeric cast.
Definition: Numeric.h++:159
Definition: AllocationMap.c++:25
Numeric and bitwise utility functions.
Definition: Numeric.h++:46