blob: bb5a564dd74c1031a8f605215c97d321279bf559 (
plain) (
blame)
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
|
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
* nl_util.h
*
*/
#ifndef NL_UTIL_H_
#define NL_UTIL_H_
#include <cmath>
#include <cstring>
#include <cstdlib>
#include "plib/pstring.h"
#include "plib/plists.h"
class nl_util
{
// this is purely static
private:
nl_util() {};
public:
static const pstring environment(const pstring &var, const pstring &default_val = "")
{
if (getenv(var.cstr()) == NULL)
return default_val;
else
return pstring(getenv(var.cstr()));
}
};
class nl_math
{
// this is purely static
private:
nl_math() {};
public:
ATTR_HOT inline static float exp(const float x) { return std::exp(x); }
ATTR_HOT inline static double abs(const double x) { return std::abs(x); }
ATTR_HOT inline static float abs(const float x) { return std::abs(x); }
ATTR_HOT inline static double log(const double x) { return std::log(x); }
ATTR_HOT inline static float log(const float x) { return std::log(x); }
#if defined(_MSC_VER) && _MSC_VER < 1800
ATTR_HOT inline static double e_log1p(const double x) { return nl_math::log(1.0 + x); }
ATTR_HOT inline static float e_log1p(const float x) { return nl_math::log(1.0 + x); }
#else
ATTR_HOT inline static double e_log1p(const double x) { return log1p(x); }
ATTR_HOT inline static float e_log1p(const float x) { return log1pf(x); }
#endif
ATTR_HOT inline static double sqrt(const double x) { return std::sqrt(x); }
ATTR_HOT inline static float sqrt(const float x) { return std::sqrt(x); }
// this one has an accuracy of better than 5%. That's enough for our purpose
// add c3 and it'll be better than 1%
#if 0
inline static double fastexp_h(const double x)
{
/* static */ const double ln2r = 1.442695040888963387;
/* static */ const double ln2 = 0.693147180559945286;
/* static */ const double c3 = 0.166666666666666667;
/* static */ const double c4 = 1.0 / 24.0;
/* static */ const double c5 = 1.0 / 120.0;
const double y = x * ln2r;
const UINT32 t = y;
const double z = (x - ln2 * (double) t);
const double e = (1.0 + z * (1.0 + z * (0.5 + z * (c3 + z * (c4 + c5*z)))));
if (t < 63)
//return (double)((UINT64) 1 << t)*(1.0 + z + 0.5 * zz + c3 * zzz+c4*zzzz+c5*zzzzz);
return (double)((UINT64) 1 << t) * e;
else
return pow(2.0, t)*e;
}
ATTR_HOT inline static double exp(const double x)
{
if (x<0)
return 1.0 / fastexp_h(-x);
else
return fastexp_h(x);
}
#else
ATTR_HOT inline static double exp(const double x) { return std::exp(x); }
#endif
};
#endif /* NL_UTIL_H_ */
|