FreeTensor
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1#ifndef FREE_TENSOR_MATH_UTILS
2#define FREE_TENSOR_MATH_UTILS
3
4#include <cmath>
5#include <cstdlib>
6#include <type_traits>
7#include <utility>
8
9namespace freetensor {
10
11template <typename T>
12concept IntegralExceptBool = requires {
13 requires std::integral<T>;
14 requires !std::same_as<T, bool>;
15};
16
17// NOTE: For floating-points, we always use double to deal with compile-time
18// operations
19
20inline auto floorDiv(IntegralExceptBool auto a, IntegralExceptBool auto b) {
21 auto res = a / b;
22 auto rem = a % b;
23 return res - (rem != 0 && ((rem < 0) != (b < 0)));
24}
25
26inline auto ceilDiv(IntegralExceptBool auto a, IntegralExceptBool auto b) {
27 auto res = a / b;
28 auto rem = a % b;
29 return res + (rem != 0 && ((rem < 0) == (b < 0)));
30}
31
32inline auto mod(IntegralExceptBool auto a, IntegralExceptBool auto b) {
33 auto m = a % b;
34 if ((m > 0 && b < 0) || (m < 0 && b > 0)) {
35 m += b;
36 }
37 return m;
38}
39
40template <class T> T square(T x) { return x * x; }
41inline bool square(bool x) { return x && x; }
42
43inline double sigmoid(double x) { return 1.0 / (1.0 + std::exp(-x)); }
44
48inline double realDiv(double a, double b) { return a / b; }
49
50} // namespace freetensor
51
52#endif // FREE_TENSOR_MATH_UTILS
Definition: utils.h:12
Definition: allocator.h:9
UpperBound ceilDiv(const UpperBound &b, int k)
Definition: bounds.cc:232
T square(T x)
Definition: utils.h:40
auto mod(IntegralExceptBool auto a, IntegralExceptBool auto b)
Definition: utils.h:32
UpperBound floorDiv(const UpperBound &b, int k)
Definition: bounds.cc:214
double sigmoid(double x)
Definition: utils.h:43
double realDiv(double a, double b)
Definition: utils.h:48