Skip to content

Commit df6e25e

Browse files
committed
w
1 parent 743f594 commit df6e25e

File tree

4 files changed

+82
-33
lines changed

4 files changed

+82
-33
lines changed

src/expression.cpp

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,38 @@ int count_expression_blobs(const std::string& expr)
6161
return count;
6262
}
6363

64+
struct typed_value
65+
{
66+
int type; // 0=i 1=f
67+
union
68+
{
69+
int i;
70+
float f;
71+
};
72+
73+
typed_value()
74+
: type(0), i(0)
75+
{
76+
}
77+
typed_value(int _i)
78+
: type(0), i(_i)
79+
{
80+
}
81+
typed_value(float _f)
82+
: type(1), f(_f)
83+
{
84+
}
85+
86+
int to_int()
87+
{
88+
if (type == 0)
89+
return i;
90+
91+
// trunc by default
92+
return (int)f;
93+
}
94+
};
95+
6496
std::vector<int> eval_list_expression(const std::string& expr, const std::vector<Mat>& blobs)
6597
{
6698
// /(0w,2),*(0h,2),0c
@@ -115,38 +147,6 @@ std::vector<int> eval_list_expression(const std::string& expr, const std::vector
115147

116148
// / 0w 2 * 0h 2 0c
117149

118-
struct typed_value
119-
{
120-
int type; // 0=i 1=f
121-
union
122-
{
123-
int i;
124-
float f;
125-
};
126-
127-
typed_value()
128-
: type(0), i(0)
129-
{
130-
}
131-
typed_value(int _i)
132-
: type(0), i(_i)
133-
{
134-
}
135-
typed_value(float _f)
136-
: type(1), f(_f)
137-
{
138-
}
139-
140-
int to_int()
141-
{
142-
if (type == 0)
143-
return i;
144-
145-
// trunc by default
146-
return (int)f;
147-
}
148-
};
149-
150150
// scan and stack
151151
std::stack<typed_value> exprstack;
152152
for (int i = (int)tokens.size() - 1; i >= 0; i--)
@@ -379,23 +379,35 @@ std::vector<int> eval_list_expression(const std::string& expr, const std::vector
379379
}
380380
else if (t == "acosh")
381381
{
382+
#if NCNN_SIMPLEMATH
383+
NCNN_LOGE("acosh not implemented in simplemath!");
384+
#else
382385
r = acoshf(a);
386+
#endif
383387
}
384388
else if (t == "asin")
385389
{
386390
r = asinf(a);
387391
}
388392
else if (t == "asinh")
389393
{
394+
#if NCNN_SIMPLEMATH
395+
NCNN_LOGE("asinh not implemented in simplemath!");
396+
#else
390397
r = asinhf(a);
398+
#endif
391399
}
392400
else if (t == "atan")
393401
{
394402
r = atanf(a);
395403
}
396404
else if (t == "atanh")
397405
{
406+
#if NCNN_SIMPLEMATH
407+
NCNN_LOGE("atanh not implemented in simplemath!");
408+
#else
398409
r = atanhf(a);
410+
#endif
399411
}
400412
else if (t == "cos")
401413
{

src/simplemath.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ float frac(float x)
136136
return x - floor(x);
137137
}
138138

139+
float fmodf(float x, float y)
140+
{
141+
float m = frac(fabsf(x / y)) * fabsf(y);
142+
return (x < 0) ? -m : m;
143+
}
144+
139145
/*
140146
* ====================================================
141147
* trigonometric functions
@@ -314,6 +320,16 @@ float atan2f(float y, float x)
314320
}
315321
}
316322

323+
float sinhf(float v)
324+
{
325+
return 0.5 * (expf(v) - expf(-v));
326+
}
327+
328+
float coshf(float v)
329+
{
330+
return 0.5 * (expf(v) + expf(-v));
331+
}
332+
317333
float tanhf(float v)
318334
{
319335
if (v >= 8 || v <= -8)

src/simplemath.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ NCNN_EXPORT float ceilf(float);
3939
NCNN_EXPORT float fmaxf(float, float);
4040
NCNN_EXPORT float truncf(float);
4141
NCNN_EXPORT float frac(float);
42+
NCNN_EXPORT float fmodf(float, float);
4243
/*
4344
* ====================================================
4445
* trigonometric functions
@@ -51,6 +52,8 @@ NCNN_EXPORT float asinf(float);
5152
NCNN_EXPORT float acosf(float);
5253
NCNN_EXPORT float atanf(float);
5354
NCNN_EXPORT float atan2f(float, float);
55+
NCNN_EXPORT float sinhf(float);
56+
NCNN_EXPORT float coshf(float);
5457
NCNN_EXPORT float tanhf(float);
5558

5659
/*

tests/test_expression.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,25 @@ static int test_eval_list_expression(const std::string& expr, std::vector<ncnn::
5050
va_end(ap);
5151

5252
std::vector<int> list = ncnn::eval_list_expression(expr, blobs);
53-
if (list != true_list)
53+
54+
bool failed = false;
55+
if (list.size() != true_list.size())
56+
{
57+
failed = true;
58+
}
59+
else
60+
{
61+
for (size_t i = 0; i < list.size(); i++)
62+
{
63+
if (list[i] != true_list[i])
64+
{
65+
failed = true;
66+
break;
67+
}
68+
}
69+
}
70+
71+
if (failed)
5472
{
5573
fprintf(stderr, "test_eval_list_expression failed expr=%s got [", expr.c_str());
5674
for (size_t i = 0; i < list.size(); i++)

0 commit comments

Comments
 (0)