Commit b2098d24 authored by Stefano Sabatini's avatar Stefano Sabatini

lavu/eval: add bitor and bitand functions

Warning note suggested by Reimar.
parent d8dccf69
...@@ -32,6 +32,17 @@ Compute arcsine of @var{x}. ...@@ -32,6 +32,17 @@ Compute arcsine of @var{x}.
@item atan(x) @item atan(x)
Compute arctangent of @var{x}. Compute arctangent of @var{x}.
@item bitand(x, y)
@item bitor(x, y)
Compute bitwise and/or operation on @var{x} and @var{y}.
The results of the evaluation of @var{x} and @var{y} are converted to
integers before executing the bitwise operation.
Note that both the conversion to integer and the conversion back to
floating point can lose precision. Beware of unexpected results for
large numbers (usually 2^53 and larger).
@item ceil(expr) @item ceil(expr)
Round the value of expression @var{expr} upwards to the nearest Round the value of expression @var{expr} upwards to the nearest
integer. For example, "ceil(1.5)" is "2.0". integer. For example, "ceil(1.5)" is "2.0".
......
...@@ -145,7 +145,7 @@ struct AVExpr { ...@@ -145,7 +145,7 @@ struct AVExpr {
e_pow, e_mul, e_div, e_add, e_pow, e_mul, e_div, e_add,
e_last, e_st, e_while, e_taylor, e_root, e_floor, e_ceil, e_trunc, e_last, e_st, e_while, e_taylor, e_root, e_floor, e_ceil, e_trunc,
e_sqrt, e_not, e_random, e_hypot, e_gcd, e_sqrt, e_not, e_random, e_hypot, e_gcd,
e_if, e_ifnot, e_print, e_if, e_ifnot, e_print, e_bitand, e_bitor,
} type; } type;
double value; // is sign in other types double value; // is sign in other types
union { union {
...@@ -284,6 +284,8 @@ static double eval_expr(Parser *p, AVExpr *e) ...@@ -284,6 +284,8 @@ static double eval_expr(Parser *p, AVExpr *e)
case e_last:return e->value * d2; case e_last:return e->value * d2;
case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2); case e_st : return e->value * (p->var[av_clip(d, 0, VARS-1)]= d2);
case e_hypot:return e->value * (sqrt(d*d + d2*d2)); case e_hypot:return e->value * (sqrt(d*d + d2*d2));
case e_bitand: return isnan(d) || isnan(d2) ? NAN : e->value * ((long int)d & (long int)d2);
case e_bitor: return isnan(d) || isnan(d2) ? NAN : e->value * ((long int)d | (long int)d2);
} }
} }
} }
...@@ -424,6 +426,8 @@ static int parse_primary(AVExpr **e, Parser *p) ...@@ -424,6 +426,8 @@ static int parse_primary(AVExpr **e, Parser *p)
else if (strmatch(next, "gcd" )) d->type = e_gcd; else if (strmatch(next, "gcd" )) d->type = e_gcd;
else if (strmatch(next, "if" )) d->type = e_if; else if (strmatch(next, "if" )) d->type = e_if;
else if (strmatch(next, "ifnot" )) d->type = e_ifnot; else if (strmatch(next, "ifnot" )) d->type = e_ifnot;
else if (strmatch(next, "bitand")) d->type = e_bitand;
else if (strmatch(next, "bitor" )) d->type = e_bitor;
else { else {
for (i=0; p->func1_names && p->func1_names[i]; i++) { for (i=0; p->func1_names && p->func1_names[i]; i++) {
if (strmatch(next, p->func1_names[i])) { if (strmatch(next, p->func1_names[i])) {
...@@ -809,6 +813,9 @@ int main(int argc, char **argv) ...@@ -809,6 +813,9 @@ int main(int argc, char **argv)
"gauss(0.1)", "gauss(0.1)",
"hypot(4,3)", "hypot(4,3)",
"gcd(30,55)*print(min(9,1))", "gcd(30,55)*print(min(9,1))",
"bitor(42, 12)",
"bitand(42, 12)",
"bitand(NAN, 1)",
NULL NULL
}; };
......
...@@ -76,7 +76,7 @@ ...@@ -76,7 +76,7 @@
#define LIBAVUTIL_VERSION_MAJOR 52 #define LIBAVUTIL_VERSION_MAJOR 52
#define LIBAVUTIL_VERSION_MINOR 19 #define LIBAVUTIL_VERSION_MINOR 19
#define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_MICRO 101
#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
LIBAVUTIL_VERSION_MINOR, \ LIBAVUTIL_VERSION_MINOR, \
......
...@@ -252,3 +252,12 @@ Evaluating 'gcd(30,55)*print(min(9,1))' ...@@ -252,3 +252,12 @@ Evaluating 'gcd(30,55)*print(min(9,1))'
12.700000 == 12.7 12.700000 == 12.7
0.931323 == 0.931322575 0.931323 == 0.931322575
Evaluating 'bitor(42, 12)'
'bitor(42, 12)' -> 46.000000
Evaluating 'bitand(42, 12)'
'bitand(42, 12)' -> 8.000000
Evaluating 'bitand(NAN, 1)'
'bitand(NAN, 1)' -> nan
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment