From: khizmax Date: Thu, 18 Sep 2014 15:43:59 +0000 (+0400) Subject: Move cds/numtraits.h to cds/details/numtraits.h X-Git-Tag: v2.0.0~341 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=0d6472a3a17a386de362769dbd0855b17f30855d;p=libcds.git Move cds/numtraits.h to cds/details/numtraits.h --- diff --git a/cds/details/defs.h b/cds/details/defs.h index 92d9c6ab..d6dc4d0c 100644 --- a/cds/details/defs.h +++ b/cds/details/defs.h @@ -408,7 +408,7 @@ namespace cds { Common things **************************************************************************/ -#include +#include namespace cds { diff --git a/cds/details/numtraits.h b/cds/details/numtraits.h new file mode 100644 index 00000000..a84a55f7 --- /dev/null +++ b/cds/details/numtraits.h @@ -0,0 +1,222 @@ +//$$CDS-header$$ + +#ifndef __CDS_DETAILS_NUMERIC_TRAITS_H +#define __CDS_DETAILS_NUMERIC_TRAITS_H + +/* + Filename: numtraits.h + Created 2007.04.22 by Maxim.Khiszinsky + + Description: + Various numeric constants and algorithms + Many algorithms are static (compile-time) + Result of static algorithm is the constant (enum) called "result". + + Editions: + 2007.04.22 Maxim.Khiszinsky Created + 2007.07.20 Maxim.Khiszinsky Added functions: exponent2, exp2Ceil +*/ + +namespace cds { + /// Some helper compile-time tricks + namespace beans { + + // @cond details + namespace details { + template struct Exponent2Helper; + template struct Exponent2Helper< N, 0 > { + enum { result = Exponent2Helper< N / 2, N % 2 >::result + 1 }; + }; + template <> struct Exponent2Helper< 1, 0 > { + enum { result = 0 }; + }; + } + // @endcond + + /*! Compile-time computing of log2(N) + + If N = 2**k for some natural k then Exponent2::result = k + If N != 2**k for any natural k then compile-time error has been encountered + */ + template struct Exponent2 { + enum { + native = N, + base = 2, + result = details::Exponent2Helper< N / 2, N % 2 >::result + 1 + }; + }; + //@cond details + template <> struct Exponent2<1> { + enum { + native = 1, + base = 2, + result = 0 + }; + }; + //@endcond + + /// Returns @a N: 2**N is nearest to @p nNumber, 2**N < nNumber + static inline size_t exp2Ceil( size_t nNumber ) + { + static_assert( sizeof(size_t) == (CDS_BUILD_BITS / 8), "Internal assumption error" ); + + size_t nExp = 0; + size_t nBit = CDS_BUILD_BITS - 1; +#if CDS_BUILD_BITS == 32 + size_t nMask = 0x80000000; +#else + size_t nMask = 0x8000000000000000; +#endif + while ( nMask != 0 ) { + if ( nNumber & nMask ) { + nExp = nBit; + break; + } + nMask = nMask >> 1; + --nBit; + } + if ( ( nNumber % ( ((size_t) 1) << nExp )) > ( ((size_t) 1) << (nExp - 1)) ) + ++nExp; + return nExp; + } + + /* ExponentN< int BASE, int N > + Exponent + If N = BASE**k then the algorithm returns k + Else compile-time error is encountered + */ + //@cond details + namespace details { + template struct ExponentNHelper; + template struct ExponentNHelper< N, BASE, 0 > { + enum { result = ExponentNHelper< N / BASE, BASE, N % BASE >::result + 1 }; + }; + template struct ExponentNHelper< 1, BASE, 0 > { + enum { result = 0 }; + }; + } + //@endcond + + /// Compile-time computing log(@p N) based @p BASE. Result in @a Exponent::result + template struct ExponentN { + enum { + native = N, + base = BASE, + result = details::ExponentNHelper< N / BASE, BASE, N % BASE >::result + 1 + }; + }; + //@cond + template struct ExponentN< BASE, 1 > { + enum { + native = 1, + base = BASE, + result = 0 + }; + }; + template struct ExponentN< BASE, 0 >; + //@endcond + + //@cond none + template struct Power2 { + enum { + exponent = N, + result = 1 << N + }; + }; + template <> struct Power2<0> { + enum { + exponent = 0, + result = 1 + }; + }; + //@endcond + + //@cond none + template struct PowerN { + enum { + exponent = N, + base = BASE, + result = PowerN< BASE, N - 1 >::result * BASE + }; + }; + template struct PowerN { + enum { + exponent = 0, + base = BASE, + result = 1 + }; + }; + //@endcond + + //@cond none + namespace details { + template struct NearestCeilHelper { + enum { result = N + ALIGN - MOD }; + }; + template struct NearestCeilHelper< N, ALIGN, 0> { + enum { result = N }; + }; + } + template struct NearestCeil { + enum { + native = N, + align = ALIGN, + result = details::NearestCeilHelper< N, ALIGN, N % ALIGN >::result + }; + }; + //@endcond + + //@cond none + template struct AlignedSize { + typedef T NativeType; + enum { + nativeSize = sizeof(T), + result = NearestCeil< sizeof(T), ALIGN >::result, + alignBytes = result - nativeSize, + alignedSize = result + }; + }; + //@endcond + + //@cond none + namespace details { + template < int N1, int N2, bool LESS > struct Max; + template < int N1, int N2 > + struct Max< N1, N2, true > { + enum { result = N2 }; + }; + + template < int N1, int N2 > + struct Max< N1, N2, false > { + enum { result = N1 }; + }; + + template < int N1, int N2, bool LESS > struct Min; + template < int N1, int N2 > + struct Min< N1, N2, true > { + enum { result = N1 }; + }; + + template < int N1, int N2 > + struct Min< N1, N2, false > { + enum { result = N2 }; + }; + } + //@endcond + + /// Returns max(N1, N2) as Max::result + template + struct Max { + enum { result = details::Max< N1, N2, N1 < N2 >::result }; + }; + + /// Returns min(N1, N2) as Min::result + template + struct Min { + enum { result = details::Min< N1, N2, N1 < N2 >::result }; + }; + + } // namespace beans +} // namespace cds + +#endif // __CDS_DETAILS_NUMERIC_TRAITS_H diff --git a/cds/numtraits.h b/cds/numtraits.h deleted file mode 100644 index df262a0a..00000000 --- a/cds/numtraits.h +++ /dev/null @@ -1,222 +0,0 @@ -//$$CDS-header$$ - -#ifndef __CDS_NUMERIC_TRAITS_H -#define __CDS_NUMERIC_TRAITS_H - -/* - Filename: numtraits.h - Created 2007.04.22 by Maxim.Khiszinsky - - Description: - Various numeric constants and algorithms - Many algorithms are static (compile-time) - Result of static algorithm is the constant (enum) called "result". - - Editions: - 2007.04.22 Maxim.Khiszinsky Created - 2007.07.20 Maxim.Khiszinsky Added functions: exponent2, exp2Ceil -*/ - -namespace cds { - /// Some helper compile-time tricks - namespace beans { - - // @cond details - namespace details { - template struct Exponent2Helper; - template struct Exponent2Helper< N, 0 > { - enum { result = Exponent2Helper< N / 2, N % 2 >::result + 1 }; - }; - template <> struct Exponent2Helper< 1, 0 > { - enum { result = 0 }; - }; - } - // @endcond - - /*! Compile-time computing of log2(N) - - If N = 2**k for some natural k then Exponent2::result = k - If N != 2**k for any natural k then compile-time error has been encountered - */ - template struct Exponent2 { - enum { - native = N, - base = 2, - result = details::Exponent2Helper< N / 2, N % 2 >::result + 1 - }; - }; - //@cond details - template <> struct Exponent2<1> { - enum { - native = 1, - base = 2, - result = 0 - }; - }; - //@endcond - - /// Returns @a N: 2**N is nearest to @p nNumber, 2**N < nNumber - static inline size_t exp2Ceil( size_t nNumber ) - { - static_assert( sizeof(size_t) == (CDS_BUILD_BITS / 8), "Internal assumption error" ); - - size_t nExp = 0; - size_t nBit = CDS_BUILD_BITS - 1; -#if CDS_BUILD_BITS == 32 - size_t nMask = 0x80000000; -#else - size_t nMask = 0x8000000000000000; -#endif - while ( nMask != 0 ) { - if ( nNumber & nMask ) { - nExp = nBit; - break; - } - nMask = nMask >> 1; - --nBit; - } - if ( ( nNumber % ( ((size_t) 1) << nExp )) > ( ((size_t) 1) << (nExp - 1)) ) - ++nExp; - return nExp; - } - - /* ExponentN< int BASE, int N > - Exponent - If N = BASE**k then the algorithm returns k - Else compile-time error is encountered - */ - //@cond details - namespace details { - template struct ExponentNHelper; - template struct ExponentNHelper< N, BASE, 0 > { - enum { result = ExponentNHelper< N / BASE, BASE, N % BASE >::result + 1 }; - }; - template struct ExponentNHelper< 1, BASE, 0 > { - enum { result = 0 }; - }; - } - //@endcond - - /// Compile-time computing log(@p N) based @p BASE. Result in @a Exponent::result - template struct ExponentN { - enum { - native = N, - base = BASE, - result = details::ExponentNHelper< N / BASE, BASE, N % BASE >::result + 1 - }; - }; - //@cond - template struct ExponentN< BASE, 1 > { - enum { - native = 1, - base = BASE, - result = 0 - }; - }; - template struct ExponentN< BASE, 0 >; - //@endcond - - //@cond none - template struct Power2 { - enum { - exponent = N, - result = 1 << N - }; - }; - template <> struct Power2<0> { - enum { - exponent = 0, - result = 1 - }; - }; - //@endcond - - //@cond none - template struct PowerN { - enum { - exponent = N, - base = BASE, - result = PowerN< BASE, N - 1 >::result * BASE - }; - }; - template struct PowerN { - enum { - exponent = 0, - base = BASE, - result = 1 - }; - }; - //@endcond - - //@cond none - namespace details { - template struct NearestCeilHelper { - enum { result = N + ALIGN - MOD }; - }; - template struct NearestCeilHelper< N, ALIGN, 0> { - enum { result = N }; - }; - } - template struct NearestCeil { - enum { - native = N, - align = ALIGN, - result = details::NearestCeilHelper< N, ALIGN, N % ALIGN >::result - }; - }; - //@endcond - - //@cond none - template struct AlignedSize { - typedef T NativeType; - enum { - nativeSize = sizeof(T), - result = NearestCeil< sizeof(T), ALIGN >::result, - alignBytes = result - nativeSize, - alignedSize = result - }; - }; - //@endcond - - //@cond none - namespace details { - template < int N1, int N2, bool LESS > struct Max; - template < int N1, int N2 > - struct Max< N1, N2, true > { - enum { result = N2 }; - }; - - template < int N1, int N2 > - struct Max< N1, N2, false > { - enum { result = N1 }; - }; - - template < int N1, int N2, bool LESS > struct Min; - template < int N1, int N2 > - struct Min< N1, N2, true > { - enum { result = N1 }; - }; - - template < int N1, int N2 > - struct Min< N1, N2, false > { - enum { result = N2 }; - }; - } - //@endcond - - /// Returns max(N1, N2) as Max::result - template - struct Max { - enum { result = details::Max< N1, N2, N1 < N2 >::result }; - }; - - /// Returns min(N1, N2) as Min::result - template - struct Min { - enum { result = details::Min< N1, N2, N1 < N2 >::result }; - }; - - } // namespace beans -} // namespace cds - -#endif // __CDS_NUMERIC_TRAITS_H diff --git a/projects/Win/vc12/cds.vcxproj b/projects/Win/vc12/cds.vcxproj index 2ef5e971..5a9ce158 100644 --- a/projects/Win/vc12/cds.vcxproj +++ b/projects/Win/vc12/cds.vcxproj @@ -634,6 +634,7 @@ + @@ -726,6 +727,7 @@ + @@ -810,7 +812,6 @@ - diff --git a/projects/Win/vc12/cds.vcxproj.filters b/projects/Win/vc12/cds.vcxproj.filters index 3f678be6..602db1cc 100644 --- a/projects/Win/vc12/cds.vcxproj.filters +++ b/projects/Win/vc12/cds.vcxproj.filters @@ -197,9 +197,6 @@ Header Files\cds - - Header Files\cds - Header Files\cds @@ -1316,5 +1313,11 @@ Header Files\cds\algo + + Header Files\cds\algo + + + Header Files\cds\details + \ No newline at end of file