Skip to content

Commit

Permalink
Compute function tables at compile time
Browse files Browse the repository at this point in the history
This requires C++ 14. Older compilers still use the old code.

Signed-off-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
stweil committed Feb 14, 2019
1 parent fd6e281 commit 3556152
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/lstm/functions.cpp
Expand Up @@ -20,6 +20,8 @@

namespace tesseract {

#if __cplusplus < 201402 // C++11

double TanhTable[kTableSize];
double LogisticTable[kTableSize];

Expand All @@ -35,4 +37,11 @@ class TableInit {

TableInit TableInit::tableInit;

#else // C++14 or newer

constexpr LUTTempl<kTableSize, LUTFuncTanh> TanhTable;
constexpr LUTTempl<kTableSize, LUTFuncLog> LogisticTable;

#endif

} // namespace tesseract.
36 changes: 34 additions & 2 deletions src/lstm/functions.h
Expand Up @@ -31,13 +31,45 @@
namespace tesseract {

// Size of static tables.
const int kTableSize = 4096;
constexpr int kTableSize = 4096;
// Scale factor for float arg to int index.
const double kScaleFactor = 256.0;
constexpr double kScaleFactor = 256.0;

#if __cplusplus < 201402 // C++11

extern double TanhTable[];
extern double LogisticTable[];

#else // C++14 or newer

typedef double (*LUT_FUNCTION)(int i);

constexpr double LUTFuncTanh(int i) {
return std::tanh(i / kScaleFactor);
}

constexpr double LUTFuncLog(int i) {
return 1 / (1 + std::exp(-i / kScaleFactor));
}

template<int n, LUT_FUNCTION f>
struct LUTTempl {
constexpr LUTTempl() : table_() {
for (auto i = 0; i < n; ++i) {
table_[i] = f(i);
}
}
const double& operator[](size_t i) const {
return table_[i];
}
double table_[n];
};

extern const LUTTempl<kTableSize, LUTFuncTanh> TanhTable;
extern const LUTTempl<kTableSize, LUTFuncLog> LogisticTable;

#endif

// Non-linearity (sigmoid) functions with cache tables and clipping.
inline double Tanh(double x) {
if (x < 0.0) return -Tanh(-x);
Expand Down

0 comments on commit 3556152

Please sign in to comment.