VectorT.cpp 3.96 KB
Newer Older
1 2 3 4 5 6 7
/*
 * VectorT.cpp
 *
 *  Created on: Nov 19, 2015
 *      Author: ebke
 */

8
#ifndef BMPOSTFIX
9 10 11
#error "Do not compile directly."
#endif

12 13 14 15
#include <type_traits>

#define ASSEMBLE_(POSTFIX, PREFIX) PREFIX ## POSTFIX
#define ASSEMBLE(POSTFIX, PREFIX) ASSEMBLE_(POSTFIX, PREFIX)
16
#define MYBENCHMARK(NAME) BENCHMARK(NAME)
17
#define MYBENCHMARK_TEMPLATE(NAME, TYPE) BENCHMARK_TEMPLATE(NAME, TYPE)
18

19 20 21 22 23 24 25 26 27 28 29 30 31 32
template<class Vec>
static inline
typename std::enable_if<Vec::size_ == 3, Vec>::type
testVec() {
    return Vec(1.1, 1.2, 1.3);
}

template<class Vec>
static inline
typename std::enable_if<Vec::size_ == 4, Vec>::type
testVec() {
    return Vec(1.1, 1.2, 1.3, 1.4);
}

33
template<class Vec>
34
static void ASSEMBLE(BMPOSTFIX, Vec_add_compare)(benchmark::State& state) {
35 36
    Vec v1(0.0);
    Vec v2(1000.0);
37
    while (state.KeepRunning()) {
38 39
        v1 += testVec<Vec>();
        v2 -= testVec<Vec>();
40 41 42 43 44
        if (v1 == v2) {
            v1 -= v2;
            v2 += v1;
        }
    }
45 46 47
    // Just so nothing gets optimized away.
    static double dummy;
    dummy = v1.norm() + v2.norm();
48
    static_cast<void>(dummy);
49 50
}

51 52 53 54
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_add_compare), OpenMesh::Vec3d);
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_add_compare), OpenMesh::Vec3f);
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_add_compare), OpenMesh::Vec4d);
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_add_compare), OpenMesh::Vec4f);
55

56
template<class Vec>
57
static void ASSEMBLE(BMPOSTFIX, Vec_cross_product)(benchmark::State& state) {
58 59
    Vec v1(0.0);
    Vec v2(1000.0);
60
    while (state.KeepRunning()) {
61 62
        v1 += testVec<Vec>();
        v2 -= testVec<Vec>();
63
        v1 = (v1 % v2);
64
    }
65 66 67
    // Just so nothing gets optimized away.
    static double dummy;
    dummy = v1.norm() + v2.norm();
68
    static_cast<void>(dummy);
69 70
}

71 72
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_cross_product), OpenMesh::Vec3d);
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_cross_product), OpenMesh::Vec3f);
73

74
template<class Vec>
75
static void ASSEMBLE(BMPOSTFIX, Vec_scalar_product)(benchmark::State& state) {
76 77
    Vec v1(0.0);
    Vec v2(1000.0);
78
    double acc = 0;
79
    while (state.KeepRunning()) {
80 81
        v1 += testVec<Vec>();
        v2 -= testVec<Vec>();
82
        acc += (v1 | v2);
83
    }
84 85 86
    // Otherwise GCC will optimize everything away.
    static double dummy;
    dummy = acc;
87
    static_cast<void>(dummy);
88 89
}

90 91 92 93
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_scalar_product), OpenMesh::Vec3d);
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_scalar_product), OpenMesh::Vec3f);
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_scalar_product), OpenMesh::Vec4d);
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_scalar_product), OpenMesh::Vec4f);
94

95
template<class Vec>
96
static void ASSEMBLE(BMPOSTFIX, Vec_norm)(benchmark::State& state) {
97
    Vec v1(0.0);
98
    double acc = 0;
99
    while (state.KeepRunning()) {
100
        v1 += testVec<Vec>();
101
        acc += v1.norm();
102
    }
103 104 105
    // Otherwise GCC will optimize everything away.
    static double dummy;
    dummy = acc;
106
    static_cast<void>(dummy);
107 108
}

109 110 111 112
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_norm), OpenMesh::Vec3d);
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_norm), OpenMesh::Vec3f);
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_norm), OpenMesh::Vec4d);
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_norm), OpenMesh::Vec4f);
113 114 115 116 117 118 119 120 121 122 123 124

template<class Vec>
static void ASSEMBLE(BMPOSTFIX, Vec_times_scalar)(benchmark::State& state) {
    Vec v1(0.0);
    while (state.KeepRunning()) {
        v1 += testVec<Vec>();
        v1 *= static_cast<decltype(v1.norm())>(1.0)/v1[0];
        v1 *= v1[1];
    }
    // Otherwise GCC will optimize everything away.
    static double dummy;
    dummy = v1.norm();
125
    static_cast<void>(dummy);
126 127 128 129 130 131
}

MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_times_scalar), OpenMesh::Vec3d);
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_times_scalar), OpenMesh::Vec3f);
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_times_scalar), OpenMesh::Vec4d);
MYBENCHMARK_TEMPLATE (ASSEMBLE(BMPOSTFIX, Vec_times_scalar), OpenMesh::Vec4f);