forked from jeffhammond/stencil-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prk_util.h
123 lines (101 loc) · 2.68 KB
/
prk_util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef PRK_UTIL_H
#define PRK_UTIL_H
#include <cstdio>
#include <cstdlib> // atoi, getenv
#include <cstdint>
#include <cfloat> // FLT_MIN
#include <climits>
#include <cmath>
#include <string>
#include <iostream>
#include <iomanip> // std::setprecision
#include <exception>
#include <vector>
#include <numeric>
#include <algorithm>
#include <chrono>
#define RESTRICT __restrict__
namespace prk {
template<class I, class T>
const T reduce(I first, I last, T init) {
#if (defined(__cplusplus) && (__cplusplus >= 201703L)) && !defined(__GNUC__)
return std::reduce(first, last, init);
#elif (defined(__cplusplus) && (__cplusplus >= 201103L))
return std::accumulate(first, last, init);
#else
// unreachable, but preserved as reference implementation
T r(0);
for (I i=first; i!=last; ++i) {
r += *i;
}
return r;
#endif
}
static inline double wtime(void)
{
using t = std::chrono::high_resolution_clock;
auto c = t::now().time_since_epoch().count();
auto n = t::period::num;
auto d = t::period::den;
double r = static_cast<double>(c)/static_cast<double>(d)*static_cast<double>(n);
return r;
}
template <class T1, class T2>
static inline auto divceil(T1 numerator, T2 denominator) -> decltype(numerator / denominator) {
return ( numerator / denominator + (numerator % denominator > 0) );
}
bool parse_boolean(const std::string & s)
{
if (s=="t" || s=="T" || s=="y" || s=="Y" || s=="1") {
return true;
} else {
return false;
}
}
int get_max_matrix_size(void)
{
// std::floor( std::sqrt(INT_MAX) )
return 46340;
}
template <typename T>
T abs(T x) {
return (x >= 0 ? x : -x);
}
template <>
float abs(float x) {
return __builtin_fabsf(x);
}
template <>
double abs(double x) {
return __builtin_fabs(x);
}
template <typename T>
T sqrt(T x) {
double y = static_cast<double>(x);
double z = __builtin_sqrt(y);
return static_cast<T>(z);
}
template <>
float sqrt(float x) {
return __builtin_sqrtf(x);
}
template <>
double sqrt(double x) {
return __builtin_sqrt(x);
}
template <typename T>
T pow(T x, int n) {
double y = static_cast<double>(x);
double z = __builtin_pow(y,n);
return static_cast<T>(z);
}
template <>
double pow(double x, int n) {
return __builtin_pow(x,n);
}
template <>
float pow(float x, int n) {
return __builtin_pow(x,n);
}
} // namespace prk
#endif /* PRK_UTIL_H */