Skip to content

Commit

Permalink
Replacing template metaprogramming with constexpr for C++11.
Browse files Browse the repository at this point in the history
  • Loading branch information
emeryberger committed Dec 29, 2015
1 parent cfa4f1a commit a2849f0
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/archipelago/benchmarks/cfrac/Makefile
Expand Up @@ -16,8 +16,8 @@ CXXFLAGS = /Zi /MT /Ox /DNDEBUG /DNOMEMOPT=1
else
CC = gcc
CXX = g++
CFLAGS = -O3 -DNDEBUG # -g # -O
CXXFLAGS = -O3 -DNDEBUG -lm # -g # -O
CFLAGS = -m32 -O3 -DNDEBUG # -g # -O
CXXFLAGS = -m32 -O3 -DNDEBUG -lm # -g # -O
endif

BINSRC = cfrac.c
Expand Down
4 changes: 4 additions & 0 deletions src/include/dieharder-pagetable.h
Expand Up @@ -17,7 +17,11 @@
namespace DieHarder {

static uintptr_t computePageNumber (void * addr) {
#if __cplusplus > 199711L
return (uintptr_t) addr >> staticlog(CPUInfo::PageSize);
#else
return (uintptr_t) addr >> StaticLog<CPUInfo::PageSize>::VALUE;
#endif
}

class PageTable {
Expand Down
9 changes: 9 additions & 0 deletions src/include/diehardheap.h
Expand Up @@ -176,9 +176,13 @@ class DieHardHeap {
#else

/// The number of size classes managed by this heap.
#if __cplusplus > 199711L
enum { MAX_INDEX = staticlog(MaxSize) - staticlog(Alignment) + 1 };
#else
enum { MAX_INDEX =
StaticLog<MaxSize>::VALUE -
StaticLog<Alignment>::VALUE + 1 };
#endif

#endif

Expand All @@ -203,7 +207,12 @@ class DieHardHeap {
#if USE_HALF_LOG
int index = halflog2(sz); // - StaticHalfLog2<Alignment>::VALUE;
#else

#if __cplusplus > 199711L
int index = log2(sz) - staticlog(Alignment);
#else
int index = log2(sz) - StaticLog<Alignment>::VALUE;
#endif
#endif
return index;
}
Expand Down
23 changes: 23 additions & 0 deletions src/include/math/halflog2.h
Expand Up @@ -15,6 +15,28 @@
(e.g., 16, 32, 64) and the "halfway-between" sizes 3 * 2^n (e.g., 24, 48).
*/

#if __cplusplus > 199711L
template <class TYPE>
TYPE constexpr staticlog2ceiling(TYPE v) {
return ((1 << staticlog(v)) < v) ?
staticlog(v) + 1 :
staticlog(v);
}

template <class TYPE>
TYPE constexpr statichalflog2(TYPE v) {
return 2 * staticlog2ceiling(v) +
(staticlog2ceiling(v - (1 << (staticlog2ceiling(v)-1))) + 1 == staticlog2ceiling(v))
- 1;
}

template <class TYPE>
TYPE constexpr statichalfpow2(TYPE v) {
return (1 << (v/2)) + (v & 1) * ((1 << (v/2)) / 2);
}

#else

template <size_t v>
class StaticLog2Ceiling {
public:
Expand Down Expand Up @@ -42,6 +64,7 @@ class StaticHalfPow2 {
enum { VALUE = SZ1 + SZ2 };
};

#endif

inline int halflog2 (size_t v) {
int a = log2(v);
Expand Down
4 changes: 4 additions & 0 deletions src/include/randomheap.h
Expand Up @@ -66,7 +66,11 @@ class RandomHeap : public RandomHeapBase<Numerator, Denominator> {

/// The most miniheaps we will use without overflowing.
/// We will support at most 2GB per size class.
#if __cplusplus > 199711L
enum { MAX_MINIHEAPS = (int) 31 - staticlog(AllocationGrain) };
#else
enum { MAX_MINIHEAPS = (int) 31 - StaticLog<AllocationGrain>::VALUE };
#endif

/// The smallest miniheap size, which may be larger than AllocationGrain.
enum { MIN_SIZE = (AllocationGrain > (Numerator * ObjectSize) / Denominator)
Expand Down
4 changes: 4 additions & 0 deletions src/include/randomminiheap-diehard.h
Expand Up @@ -77,7 +77,11 @@ class RandomMiniHeapDieHard :
unsigned int computeIndex (void * ptr) const {
size_t offset = computeOffset (ptr);
if (IsPowerOfTwo<ObjectSize>::VALUE) {
#if __cplusplus > 199711L
return (offset >> staticlog(ObjectSize));
#else
return (offset >> StaticLog<ObjectSize>::VALUE);
#endif
} else {
return (offset / ObjectSize);
}
Expand Down
13 changes: 13 additions & 0 deletions src/include/randomminiheap-dieharder.h
Expand Up @@ -150,8 +150,13 @@ class RandomMiniHeapDieHarderBase :

// Now compute its index: the number of objects in PRECEDING pages
// PLUS the object index WITHIN the page.
#if __cplusplus > 199711L
unsigned int ret =
pageIdx + (offset >> staticlog(ObjectSize));
#else
unsigned int ret =
pageIdx + (offset >> StaticLog<ObjectSize>::VALUE);
#endif

assert (ret < NObjects);
return ret;
Expand Down Expand Up @@ -190,14 +195,22 @@ class RandomMiniHeapDieHarderBase :
void setPageFromIndex (unsigned int index, void * pageAddr) {
assert (((uintptr_t) pageAddr & (CPUInfo::PageSize - 1)) == 0);
assert (modulo<ObjectsPerPage>(index) == 0);
#if __cplusplus > 199711L
_mapEntryToPage(index >> staticlog(ObjectsPerPage)) = pageAddr;
#else
_mapEntryToPage(index >> StaticLog<ObjectsPerPage>::VALUE) = pageAddr;
#endif
}

//@brief Get the page corresponding to this object index.
void * getPageFromIndex (unsigned int index) {
// Pick the index that is at the start of a page.
index -= modulo<ObjectsPerPage>(index);
#if __cplusplus > 199711L
void * pageAddr = _mapEntryToPage(index >> staticlog(ObjectsPerPage));
#else
void * pageAddr = _mapEntryToPage(index >> StaticLog<ObjectsPerPage>::VALUE);
#endif
assert (((uintptr_t) pageAddr & (CPUInfo::PageSize - 1)) == 0);
return pageAddr;
}
Expand Down
5 changes: 5 additions & 0 deletions src/include/randommmap.h
Expand Up @@ -109,7 +109,12 @@ class RandomMmap {
RandomMmap(const RandomMmap& r);
RandomMmap& operator=(const RandomMmap& r);

#if __cplusplus > 199711L
enum { BITS = 31 - staticlog(CPUInfo::PageSize) }; // size of address space, minus bits for pages.
#else
enum { BITS = 31 - StaticLog<CPUInfo::PageSize>::VALUE }; // size of address space, minus bits for pages.
#endif

enum { PAGES = (1ULL << BITS) }; // Must be a power of two.

/// Random number generator for probing. note that it needs to produce 64-bit RNGs.
Expand Down
34 changes: 22 additions & 12 deletions src/include/rng/mwc64.h
Expand Up @@ -9,24 +9,28 @@

class MWC64 {

unsigned long long x, c, t;
unsigned long long _x, _c, _t;

void init (unsigned long long seed1, unsigned long long seed2)
{
x = seed1;
x <<= 32;
x += seed2;
c = 123456123456123456ULL;
_x = seed1;
_x <<= 32;
_x += seed2;
_c = 123456123456123456ULL;
_index = 2;
}

unsigned long long MWC() {
t = (x << 58) + c;
c = x >> 6;
x += t;
c += (x < t);
return x;
_t = (_x << 58) + _c;
_c = _x >> 6;
_x += _t;
_c += (_x < _t);
return _x;
}

int _index;
unsigned long long _value;

public:

MWC64()
Expand All @@ -41,9 +45,15 @@ class MWC64 {
init (seed1, seed2);
}

inline unsigned long long next()
inline unsigned long next()
{
return MWC();
if (_index == 2) {
_value = MWC();
_index = 0;
}
unsigned long v = ((unsigned long *) &_value)[_index];
_index++;
return v;
}

};
Expand Down
12 changes: 5 additions & 7 deletions src/include/rng/randomnumbergenerator.h
Expand Up @@ -10,26 +10,24 @@
#ifndef DH_RANDOMNUMBERGENERATOR_H
#define DH_RANDOMNUMBERGENERATOR_H


#include "mwc.h"
#include "realrandomvalue.h"


class RandomNumberGenerator {
public:

RandomNumberGenerator() // (unsigned int seed1, unsigned int seed2)
: mt (RealRandomValue::value(), RealRandomValue::value()) {
// : mt (362436069, 521288629) {
RandomNumberGenerator()
: mt (RealRandomValue::value(), RealRandomValue::value())
{
}

inline unsigned int next (void) {
return mt.next();
}

private:

MWC mt;
MWC mt;

};

Expand Down
14 changes: 12 additions & 2 deletions src/include/static/staticif.h
Expand Up @@ -7,8 +7,17 @@
* @note Copyright (C) 2005 by Emery Berger, University of Massachusetts Amherst.
*/

#ifndef _STATICIF_H_
#define _STATICIF_H_
#ifndef DH_STATICIF_H
#define DH_STATICIF_H

#if __cplusplus > 199711L

template <class TYPE>
TYPE constexpr staticif(bool v, TYPE a, TYPE b) {
return (v ? a : b);
}

#else

template <bool b, int a, int c>
class StaticIf;
Expand All @@ -25,5 +34,6 @@ class StaticIf<false, a, b> {
enum { VALUE = b };
};

#endif

#endif
13 changes: 13 additions & 0 deletions src/include/static/staticlog.h
Expand Up @@ -12,6 +12,17 @@

#include "staticif.h"

#if __cplusplus > 199711L

int constexpr staticlog(int v) {
return ((v == 1) ? 0 :
(v == 2) ? 1 :
(v > 1) ? staticlog(v / 2) + 1 :
0);
}

#else

template <int Number>
class StaticLog;

Expand All @@ -34,3 +45,5 @@ class StaticLog {
};

#endif

#endif
4 changes: 4 additions & 0 deletions src/include/util/bitmap.h
Expand Up @@ -116,7 +116,11 @@ class BitMap : private Heap {
enum { WORDBYTES = sizeof(WORD) };

/// The log of the number of bits in a WORD, for shifting.
#if __cplusplus > 199711L
enum { WORDBITSHIFT = staticlog(WORDBITS) };
#else
enum { WORDBITSHIFT = StaticLog<WORDBITS>::VALUE };
#endif

/// The bit array itself.
WORD * _bitarray;
Expand Down

0 comments on commit a2849f0

Please sign in to comment.