Skip to content

Commit

Permalink
README.md, expr2/exprfilter.cpp: add integer bitwise and/or/xor/not o…
Browse files Browse the repository at this point in the history
…perators

Signed-off-by: akarin <[email protected]>
  • Loading branch information
AkarinVS committed Oct 9, 2022
1 parent 35b7fb6 commit 530b489
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ This works just like [`std.Expr`](http://www.vapoursynth.com/doc/functions/expr.
- 0 means clamped
- 1 means mirrored
- (\*) Dynamic pixel access using absolute coordinates. Use `absX absY x[]` to access the pixel (absX, absY) in the current frame of clip x. absX and absY can be computed using arbitrary expressions, and they are clamped to be within their respecitive ranges (i.e. boundary pixels are repeated indefinitely.) Only use this as a last resort as the performance is likely worse than static relative pixel access, depending on access pattern.
- (\*) Bitwise operators (`bitand`, `bitor`, `bitxor`, `bitnot`): they operate on <24b integer clips by default. If you want to process 24-32 bit integer clips, you must set `opt=1` to force integer evaluation as much as possible (but beware that 32-bit signed integer overflow will wraparound.)
- Support more bases for constants
- hexadecimals: 0x123 or 0x123.4p5
- octals: 023 (however, invalid octal numbers will be parsed as floating points, so "09" will be parsed the same as "9.0")
Expand All @@ -98,6 +99,7 @@ Use this function to query the version and features of the plugin. It will retur
b'drop', # dropN support
b'sort', # sortN support
b'x[]', # dynamic pixel access
b'bitand', b'bitor', b'bitxor', b'bitnot', # bitwise operators
]
```

Expand Down
31 changes: 31 additions & 0 deletions expr2/exprfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ enum class ExprOpType {
// Logical operators.
AND, OR, XOR, NOT,

// Bitwise operators.
BITAND, BITOR, BITXOR, BITNOT,

// Transcendental functions.
EXP, LOG, POW, SIN, COS,

Expand All @@ -88,6 +91,7 @@ std::vector<std::string> features = {
"drop",
"sort",
"x[]",
"bitand", "bitor", "bitxor", "bitnot",
};

enum class ComparisonType {
Expand Down Expand Up @@ -221,6 +225,10 @@ ExprOp decodeToken(const std::string &token)
{ "or", { ExprOpType::OR } },
{ "xor", { ExprOpType::XOR } },
{ "not", { ExprOpType::NOT } },
{ "bitand", { ExprOpType::BITAND } },
{ "bitor", { ExprOpType::BITOR } },
{ "bitxor", { ExprOpType::BITXOR } },
{ "bitnot", { ExprOpType::BITNOT } },
{ "?", { ExprOpType::TERNARY } },
{ "exp", { ExprOpType::EXP } },
{ "log", { ExprOpType::LOG } },
Expand Down Expand Up @@ -720,6 +728,10 @@ void Compiler<lanes>::buildOneIter(const Helper &helpers, State &state)
2, // OR
2, // XOR
1, // NOT
2, // BITAND
2, // BITOR
2, // BITXOR
1, // BITNOT
1, // EXP
1, // LOG
2, // POW
Expand Down Expand Up @@ -1026,6 +1038,24 @@ void Compiler<lanes>::buildOneIter(const Helper &helpers, State &state)
break;
}

#define BITWISEOP(op) { \
LOAD2(l, r); \
IntV li = l.ensureInt(); \
IntV ri = r.ensureInt(); \
auto x = op(li, ri); \
OUT(x); \
break; \
}
case ExprOpType::BITAND: BITWISEOP(operator &);
case ExprOpType::BITOR: BITWISEOP(operator |);
case ExprOpType::BITXOR: BITWISEOP(operator ^);
case ExprOpType::BITNOT: {
LOAD1(x);
IntV xi = x.ensureInt();
OUT(~xi);
break;
}

case ExprOpType::TRUNC: UNARYOPF(Trunc);
case ExprOpType::ROUND: UNARYOPF(Round);
case ExprOpType::FLOOR: UNARYOPF(Floor);
Expand Down Expand Up @@ -1058,6 +1088,7 @@ void Compiler<lanes>::buildOneIter(const Helper &helpers, State &state)
OUT((t.i() & ci) | (f.i() & ~ci));
break;
}
#undef BITWISEOP
#undef LOGICOP
#undef UNARYOP
#undef BINARYOP
Expand Down

0 comments on commit 530b489

Please sign in to comment.