mirror of
https://github.com/opencv/opencv.git
synced 2026-11-28 20:37:06 -06:00
* experimental new arithmetics; work-in-progress * continue working on new-gen arithmetic expressions * * improved performance of the new add on small arrays * added sub * extended tests * fixed potential bug when adding multi-channel array and a single-channel scalar * improved const handling * * added copyMask * added mul/dev (without scale so far) * * accelerated mul * addedd scale to mul and div * * done substantial refactoring; however a few more rounds of refactoring are ahead. * added min, max, absdiff, addweighted. * improved performance of the new arithmetic functions, but some of them are still slow, e.g. operations with mask have some bugs (that affect speed, not accuracy). * * further (significantly) accelerated several functions, especially on small arrays: mul, binary ops with mask * further polished the new arithmetic engine * started integration of the new element-wise arithmetic engine into core * big step forward. We now use the new engine inside cv::add, subtract, multiply, divide, absdiff, min and max. * big progress: * added bitwise operations * fixed and accelerated compare * ported regression tests to test new broadcasting behaviour of arithmetic functions * lot's of improvements in compare, divide, addWeighted! * lot's of small and big performance improvements in the new arithmetics * * some more optimizations; parsing texpr-expressions is now faster as well * port new_arithm to Linux/x86: dispatch guards, scalar-Mat compat fallback, dnn shape-contract fixes Core: - arithm.simd.hpp: CV_CPU_OPTIMIZATION_DECLARATIONS_ONLY guards (the file is included once per dispatched mode on x86), vx_load_expand instead of the 128-bit v_load_expand, VTraits::vlanes() instead of ::nlanes - arithm.cpp/precomp.hpp: compat fallback for scalar-like Mat operands (1x1, 1xcn/cnx1, 4x1 CV_64F - java/python tuples, operator-(Mat, Matx)): treated as a per-channel scalar ONLY when the shapes are not broadcast-compatible, so every valid numpy-style broadcast keeps its meaning and calls that would otherwise throw get the 4.x semantics DNN (fallout of the stricter shape semantics, found by the new engine): - dict.hpp: DictValue relied on fresh AutoBuffer having size()==fixed_size; allocate explicitly - batch_norm: weights_/bias_ are 1-D [n] now; 0/1-D forward runs on exact-shape 1-D views - net_impl2: extend the post-forward sanity check to non-temp outputs - a layer that reallocates its preallocated output tensor now fails loudly instead of silently detaching the result from the graph - LSTM/LSTM2 batchwise (layout=1): getMemoryShapes now matches what forward() writes (ONNX: Y=(batch,seq,dirs,hid), Yh/Yc=(batch,dirs,hid)); forward assembles seq-major results in a local buffer and transposes INTO the preallocated outputs in place Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * u8/s8 multiply: exact integer SIMD path on non-FP16 builds (3-10x vs 5.x) The unit-scale branch of vecBinaryKernel already supported a separate work-vector type Wvec1 (used by the ARM f16 build and by u16/s16 everywhere), but on x86 the u8/s8 same-type multiply still went through the f32 hub. Route it through v_uint16/v_int16: products of 8-bit values fit exactly (255^2 < 2^16), the saturating pack on store gives bit-exact results at half the vector traffic. Also fix a latent kernel bug this exposed: the unit-scale branch stepped by Wvec's lane count while loading/storing Wvec1 vectors. All previous Wvec1 instantiations had equal lane counts, but u8's v_uint16 has 2x the lanes of v_float32 - the pairs overlapped (50% redundant work) and the tail backoff could write VECSZ bytes past the row end. The branch now derives its step, offsets and tail condition from Wvec1 itself. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * vecBinaryKernel: constexpr Op::useScalar instead of a runtime-only scale check Every binary op functor now declares whether it consumes the scale scalar (params[0]): true only for mul and the two div variants. Ops that ignore it (add/sub/min/max/absdiff) take the fast 2-arg branch unconditionally - the 'scalar == 1' check used to fail for them (their params[0] is 0), sending them through the preproc branch, and the condition now folds at compile time. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * restore cv::hal::mul8u as a wrapper over the element-wise engine The symbol is still declared in core/hal/hal.hpp and called directly by external code (the G-API fluid backend in opencv_contrib), but its implementation went away with the old arithm kernels. Forward it to getMulFunc(CV_8U, CV_8U) - with scale==1 it lands on the new exact integer SIMD path. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * silence every new-arithm warning reported by CI (ARM64/Mac) and gcc 15 - arithm.cpp: bitwise_op_ocl and the actualScalarDepth/coerceTypes helpers are consumed only by the OpenCL paths - guard them with HAVE_OPENCL; haveScalar in cv::compare is read only inside CV_OCL_RUN - CV_UNUSED for OpenCL-less builds - arithm_expr.hpp: declare getBitwiseFunc/getNotFunc/getAddWeightedFunc next to the other per-op entry points (-Wmissing-prototypes in arithm.dispatch.cpp) - arithm.simd.hpp: define CV_SIMD_16F to 0 when FP16 SIMD is absent (-Wundef); {}-init the expandScalar staging buffers (-Wmaybe-uninitialized: they are fully written before use, but the compiler cannot prove it with runtime vector widths); rename the compare kernel's lambda parameter (-Wshadow) - arithm_expr.cpp: rename the exec tile-lambda's hot-field locals that shadowed TExpr members and outer locals (-Wshadow) - test_new_arithm_extensive.cpp: rename the name-generator lambdas' parameter shadowing the INSTANTIATE macro's own (-Wshadow), drop an unused variable Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * restore 4.x scalar semantics for the bindings' 4x1 CV_64F Scalar columns The python/java bindings materialize numbers and tuples as a 2-D 4x1 CV_64F Mat - or UMat, when the call carries UMat arguments. Three CI-reported python failures came from those pseudo-scalars reaching the engine as arrays: - absdiff(int_arr, 0): the (4,1) column is broadcast-COMPATIBLE with a 1-D array, so numpy semantics silently won - an outer-product f64 result instead of the int per-channel-scalar one; - subtract(u8 4x8x4, (40,)): same, by the rows==4 coincidence; - multiply(UMat, 2., dst=UMat): the scalar arrives as a UMAT, which the scalar detection did not recognize at all. isScalarArg now treats the exact bindings shape - 2-D 4x1 CV_64F single-channel Mat/UMat against a <=4-channel array - as a scalar UNCONDITIONALLY (a 1-D [4] array has dims==1 and still broadcasts). One exception, decided in arithm_op: when the partner is itself a tiny scalar-shaped array, both are honest data and ride the broadcast (compare(Mat 4x1, Mat 1x1) - issue #8999 - stays elementwise). Small-array discipline, this all runs per engine call: the probes read Mat/UMat fields directly (rows == 4 alone rejects almost everything, no _InputArray getter dispatch), and a UMAT scalar's 32 bytes are copied into a caller-stack buffer - no heap, no getMat mapping. Measured: no latency change on 4x4/16x16 element-wise calls. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * cv::texpr: std::string_view -> const std::string& in the public API string_view in an exported signature breaks some CUDA toolchain builds, and for short expression strings the difference is immaterial (SSO, parsed once). The parser internals keep string_view - the argument converts implicitly. Also drop the now-unused <string_view> include from cvstd.hpp, so the header does not reach every nvcc TU. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * compare boundary-rewrite: fixed 4-slot kind/bound arrays instead of AutoBuffers A CONST operand is capped at 4 channels (addConst), so the per-channel kind/bound staging needs no dynamic buffers - plain int[4]/double[4], with a CV_Assert on the contract. This is also what gcc's -Wmaybe-uninitialized was flagging (it could not see the AutoBuffer's inline storage get filled). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * element-wise engine: unary math kernels (sqrt/exp/log/sin/cos/tanh/erf/relu) + select New dispatched pair math.simd.hpp / math.dispatch.cpp - the unary/ternary sibling of arithm.simd.hpp: - vecUnaryKernel: T -> T over f16/bf16/f32/f64 on top of the intrin_math primitives (v_exp/v_log/v_sin/v_cos/v_sqrt/v_erf/v_max). f32/f64 compute natively, f16/bf16 ride the f32 hub inside the kernel (vx_load_pair_as / v_store_pair_as) - no materialized casts. Continuity collapse + the halide right-edge backoff, suppressed in-place (it would re-apply Op to already-written values). tanh = (e^2x-1)/(e^2x+1) with the input clamped to +/-10 (f32) / +/-20 (f64) - unclamped saturation hits inf/inf = NaN. erf has no f64 SIMD primitive: std::erf per lane. - selectKernel(mask, x, y): 1-byte mask expanded to lane width and tested against zero in the INTEGER domain (immune to DAZ/FTZ), branches of any depth by element size, broadcast branches supported. - emitUnary: math over a float input is T -> T now (f16 in -> f16 out, native kernel when input and result depths match); integer inputs still compute in the float domain and land in f32. - emitTernary/select: literal branches are typed via typedConstFrom (an OP_CAST of a depth-less flex const crashed); a non-1-byte mask is normalized by an explicit "mask != 0" compare, never a value cast. texpr already parsed the function names - they now execute. Tests: per-depth accuracy of all 8 ops against the double std:: reference, integer input, in-place, select over 4 depths / const branch / float mask. Perf vs the classic kernels (1920x1080 f32, 16 threads, AVX2): exp 3.7x, log 2.6x, sqrt 5.7x faster; polarToCart expressed as (r*cos(a), r*sin(a)) 4.0x. Accuracy improves too (max rel err vs f64 reference): exp 8.1e-8 vs 2.1e-7, log 8.0e-8 vs 1.5e-7. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * engine: OP_COPY_MASK folded into OP_SELECT; selectKernel moved to arithm.simd.hpp copyMask(dst, mask, src) is select(mask, src, dst) - one masking primitive instead of two. The compiler emits the masked-op tail as addInsn(OP_SELECT, mask, r, out, out): the output slot rides as both arg2 and the result, so unmasked elements are preserved by reading them back through the b-branch. OP_COPY_MASK, copyMaskKernel and getCopyMaskFunc are gone. selectKernel (moved from math.simd.hpp to arithm.simd.hpp) inherits every copyMaskKernel optimization: - the interleaved multichannel fast path (2..4 channels under a per-pixel mask: expand the mask once per VECSZ rows, v_store_interleave across lanes); - the per-row scalar path with the row-skip when the selected source row IS dst (the "leave the output untouched" half of copyMask); - plus the select-specific ones: branch broadcasts (stepx == 0) and the right-edge tail backoff under dst-aliases-a-branch - safe because re-running select over already-blended elements is idempotent; only dst == mask keeps the backoff off (the store would rewrite mask bytes before the re-read). Masked-add perf is on par with the old copyMask (1280x720, 1 thread: 8UC3 204 -> 198 us, 32FC3 1395 -> 1344, 8UC1/32FC1 within noise). Full core suite 24117 green. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * engine: dedicated vectorized pow kernel (moved from arithm to math.simd.hpp) pow was the last scalar-only binary op (scalarBinaryKernel + std::pow, f32/f64 only). The new powKernel keeps exact std::pow semantics and is T x T -> T over all four float depths (f16/bf16 via the f32 hub): - scalar exponent (the dominant call shape - texpr literals ride as 0-dim broadcast consts) is dispatched PER ROW to the special cases: y==2 -> x*x, y==3 -> x*x*x, y==0.5 -> v_sqrt, y==1 -> copy, y==0 -> fill 1; - everything else - including a per-element exponent array - runs the general vectorized exp(y * log(x)) path, valid for x > 0; a vector pair containing any x <= 0 lane falls back to scalar std::pow for that pair (v_check_any), which preserves every std::pow subtlety: signed results for integer y on negative bases, NaN for fractional y, the x == 0 family; - no right-edge tail backoff: pow is not idempotent, in-place calls finish rows in the scalar tail. Perf vs the classic cv::pow (1920x1080 f32, 16 threads, AVX2): p=2 1.4x (classic special-cases it too), p=3 6.1x, p=0.5 6.3x, fractional p 4.5x with slightly better accuracy (6.1e-7 vs 7.4e-7 max rel err). Tests: exponent sweep 2/3/0.5/1/0/2.5/-1.5 vs the double std::pow reference on f32/f64, negative bases (exact signed cubes, NaN for fractional), array exponent. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * powKernel: halide right-edge tail backoff in every SIMD loop Same shape as vecBinaryKernel: the final partial vector re-processes [width - VECSZ*2, width) instead of finishing scalar, suppressed when dst aliases an input (pow is not idempotent - the overlap region must be recomputed from an untouched source, which the no-alias case guarantees). Modest measured win (~2% on ROI rows for the special-cased exponents; the general path tail was already cheap - modern libm powf is fast), no regressions; mainly aligns the kernel with the house style, where every SIMD loop ends vector-wide. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix MSVC 2019 C2975: function-local constexpr as a template argument inside a lambda MSVC 2019 loses the constexpr-ness of function-local constants (LOCAL_OPS, MAX_DIMS, ...) when they are used as template arguments inside a lambda body (AutoBuffer<Slice, LOCAL_OPS> / std::array<int, MAX_DIMS> in the parallel bodies of BroadcastOp::run and TExpr::exec). Hoist them to namespace scope - no behavior change. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * ocl_arithm_op: route 16U multiply to the CPU engine on Apple OpenCL The Apple OpenCL driver miscompiles the 16U multiply kernel: products near the top of the u16 range come back wrapped instead of saturated (CPU vs GPU NORM_INF up to 65535 in OCL_Arithm/Mul.Mat CV_16U cases). The same arithm.cl kernel is correct on Intel NEO and NVIDIA drivers - verified not to reproduce on Linux/Intel iGPU - so gate the decline to __APPLE__ only; the CPU engine computes 16u multiply exactly (integer SIMD path). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * engine: neg/abs as compositions, clamp kernel, ** and ?: operators, abs(a-b) peephole - OP_NEG and OP_ABS need no kernels: neg = sub(0, a), abs = absdiff(a, 0) - including the engine absdiff auto-type rule (signed |a| lands in the UNSIGNED type of the same width: |SHRT_MIN| fits u16 exactly instead of saturating; NB the public cv::absdiff auto depth keeps the source type for 4.x compatibility - values agree, the depth rule is the engine own). - peephole: abs(x - y) rewrites to absdiff(x, y) ALWAYS. On integers the literal semantics differ (the subtract saturates first: u8 gives max(x-y, 0)), but whoever writes abs(a - b) means absdiff - we deliberately hand out the useful semantics instead of the saturation artifact. The just- emitted OP_SUB is retired via the moveToOutput manoeuvre, so the program shrinks to the single absdiff instruction. abs(x), abs(x - 0) and absdiff(x, 0) all give one result. - OP_CLAMP kernel (arithm.simd.hpp): v_min(v_max(x, lo), hi) over u8/s8/u16/s16/u32/s32/f32 (+f64 with 64-bit SIMD), scalar f16/bf16/64-bit ints; lo/hi may broadcast (the common clamp(img, a, b) shape) or be full arrays; the tail backoff stays on under dst-aliases-x (clamp is idempotent). emitTernary types literal bounds via typedConstFrom (same flex-const crash select had) and keeps the auto result type pinned to x. - parser: "a ** b" == pow(a, b), precedence above * /, RIGHT-associative (a ** 2 ** 3 == a ** 8); "cond ? a : b" == select(cond, a, b), precedence below everything, right-associative chains (f1 ? a : f2 ? b : c) work without parentheses. parseTernary() is the expression entry point now. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * cv::exp/log/sqrt on the engine via math_op; hal functions wrapped as engine kernels math_op is the master function of the unary math family (the arithm_op analogue): same-shape same-type output over f16/bf16/f32/f64 (classic exp/log accepted f32/f64 only - the half floats are new), two tiers: - small (<= 100000 elements) and continuous: call the kernel DIRECTLY over the flattened data - no TExpr, no broadcastOp, no parallel_for setup; - everything else: the usual single-instruction program via compile()/exec() (parallelism for large arrays, real steps for ROIs). getMathFunc routes OP_EXP/OP_LOG at f32/f64 through the full cv::hal stack - an external vendor HAL (CALL_HAL), IPP, or the built-in table kernels, whichever is installed - by wrapping hal::exp32f/exp64f/log32f/log64f as engine kernels with the function pointer in TKernel::userdata, the same mechanism castKernel uses for core BinaryFuncs. The engine adds tiling and parallelism on top, so every tier gets the best available scalar-span implementation. v_exp/v_log remain for f16/bf16 (the f32 hub) and the ops hal has no entry points for. v_log_default_32f: the degree-8 polynomial is evaluated by Estrin pairing (4 dependent levels) instead of an 8-FMA Horner chain (~5% on the f16 hub path). An exp64 Taylor-without-division rewrite was tried and benched SLOWER than the Cephes Pade scheme (the evaluation is FMA-throughput-bound, and vdivpd pipelines well enough) - reverted; a table-based reduction is the only way further there. cv::exp f32 (16 threads, AVX2+IPP build), old -> new: 640 elements 0.16 -> 0.15 us, 16k 2.79 -> 2.49, 640x480 47 -> 20, 1920x1080 452 -> 60 us (the old CPU loops were single-threaded); f64 exp 1080p 1295 -> 161 us. No size regresses; small arrays now run at installed-HAL speed. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * getMathFunc: IPP tier + raw-HAL probing; the exp/log table kernels are deleted The raw cv_hal_* entry points return int for a reason: without an installed HAL they are stubs returning CV_HAL_ERROR_NOT_IMPLEMENTED. getMathFunc now selects the exp/log implementation in three tiers: 1. HAVE_IPP && ipp::useIPP(): ippsExp/ippsLn through thin int adapters (IPP is not routed through the cv_hal_ hooks, so it needs its own tier); 2. the raw cv_hal_exp32f/... hook, PROBED once with a 1-element call on the safe input 1.0 (cached in magic statics): implemented -> wrapped as an engine kernel with the function pointer in TKernel::userdata; 3. the engine own v_exp/v_log kernels. Whichever wins, the engine adds tiling and parallelism on top. The EXPTAB/LOGTAB table kernels and their tables (~790 lines in mathfuncs_core.simd.hpp + mathfuncs.cpp) are DELETED: they benched within ~15% of v_exp/v_log, not worth a second implementation. The public cv::hal::exp32f/exp64f/log32f/log64f keep their contract - CALL_HAL, then IPP, then the built-in implementation - but the built-in is now the engine vector kernel via ew::mathSpanEngine (one contiguous span, exported from math.dispatch.cpp). All unary math kernels (vec/scalar/hal wrappers, pow) also handle the vertical-broadcast tile (s0y == 0, a row expanded into a matrix): the first row is computed, the rest are memcpy of it - transcendentals cost far more than a row copy. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * texpr: hypot(x, y) binary op (alias: mag) hypot = sqrt(x^2 + y^2), NAIVE like cv::magnitude (not the overflow-safe std::hypot), computed in the float work type; kernels for the four float depths only (T x T -> T; integer inputs ride the usual f32-compute + cast). A 10-line EwHypot functor on top of vecBinaryKernel in arithm.simd.hpp - broadcast branches, continuity collapse and the tail backoff come for free. Registered in the parser as both "hypot" (the C/numpy name) and "mag" (the cv::magnitude-flavored alias). A building block for the future cartToPolar. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * texpr: atan2(y, x) binary op - radians, standard C range v_atan2 (arithm.simd.hpp, generic over the universal-intrinsic float vector): the fastAtan2 minimax polynomial from mathfuncs_core v_atan_f32 reworked to plain radians - the 180/pi factor dropped from the coefficients and the C quadrant logic instead of the [0, 360) wrap, so the result matches std::atan2 over (-pi, pi]. Measured absolute accuracy ~1.6e-4 rad. (v_atan_f32 itself is untouched - cv::phase/fastAtan2 keep their degree semantics.) EwAtan2 rides vecBinaryKernel: f16/bf16/f32 through v_atan2 (the f32 hub), f64 through exact scalar std::atan2. arg0 = y, arg1 = x, like std::atan2; float depths only, same emitBinary policy as pow/hypot. Parser name "atan2". Together with hypot this completes the cartToPolar building blocks. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix the RISC-V RVV build and two ARM64 warnings The new f64 kernel registrations (hypot, pow, the unary math family) gate on CV_SIMD_64F || CV_SIMD_SCALABLE_64F, but the vx_setall_as(const double*, v_float64&) helper family in arithm.simd.hpp was still CV_SIMD_64F-only - scalable platforms (RVV) have v_float64 with CV_SIMD_64F == 0, so vecBinaryKernel<double, ...> failed to instantiate there. Widen the helper gate to match (verified with a riscv64 rv64gcv cross-build of opencv_core - the engine f64 paths now vectorize on RVV instead of not compiling). cv::exp/cv::log: the depth local is consumed by CV_OCL_RUN only - CV_UNUSED for OpenCL-less builds (ARM64 -Wunused-variable). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * silence the remaining ARM64 gcc warnings - compare boundary-rewrite: {}-init the fixed kind/bound arrays (filled for every channel used below, but gcc cannot prove it across the cn <= 4 loop); - cv::exp/log: [[maybe_unused]] on the depth local (consumed by CV_OCL_RUN only), instead of the CV_UNUSED idiom; - AutoBuffer::reserve: a targeted -Wmaybe-uninitialized suppression around the live-element copy loop - only [0, sz) is read, all written before, but gcc inlining a grow-from-inline-storage chain cannot see that. An annotation for the analyzer, no behavior change. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * saturating 32-bit add/sub kernels; cv::texpr python binding; two CI warnings - v_add_sat/v_sub_sat for v_int32/v_uint32, local to arithm.simd.hpp for now (the plan is to grow them into proper universal intrinsics later): NEON single-instruction vqadd/vqsub, elsewhere the Hacker Delight bit tricks over universal intrinsics (u32 add is 2 ops: or with the wrapped-compare mask). EwAdd/EwSub overload vec() for the 32-bit lanes and getAddSubFunc routes 32S/32U T->T through vecBinaryKernel instead of the former pure scalar kernel. Semantics unchanged - the scalar int64 tail already saturated; directed boundary tests added (both rails, 0 - INT_MIN, u32 cases, a full-range random block vs an exact int64 reference). 640x480 32S add: 0.48x of 5.x -> parity (memory-bound); 1080p: 6-8x. - cv::texpr becomes CV_EXPORTS_W: python gets cv.texpr(expr, [inputs]) -> tuple of ndarrays, so `res, = cv.texpr(...)` and `mag, ang = cv.texpr(...)` unpacking both work. modules/python/test/test_expr.py covers arithmetic, the fused abs(a-b), casts, broadcasting, ?: and ** operators, math functions vs numpy, clamp, named temporaries, tuple outputs, the one-line cartToPolar and the int32 saturation cases. - warnings: {}-init the parser args array (gcc -Wmaybe-uninitialized on Ubuntu 20/22); the compare short-row block gates sizeof(T) <= 4 as constexpr so the f64 instantiation does not leave set-but-unused locals (gcc 9 -Wunused-but-set-variable). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * arithm_op: direct-kernel fast path for small continuous arrays Building and compiling the 1-instruction program plus the BroadcastOp setup costs ~40-250ns per call - negligible on big images, dominant at 127x61-class sizes where the classic 5.x functions were 1.3-2x faster. Mirror math_op two tiers in arithm_op: two same-type same-shape continuous arrays, no mask, no scalar, result depth == input depth, <= 100k elements -> call the T x T -> T kernel directly over the flattened elements (checks ordered cheapest-first). Applies to add/subtract/min/max/absdiff/multiply/addWeighted/and/or/xor; compare and divide lower to more than a single kernel (boundary rewrites, int guards) and keep the ordinary path. addWeighted falls through automatically for the 32/64-bit int types whose lowering is wide-compute + cast (getElemwiseFunc returns no direct kernel there). 127x61 vs 5.x, was -> now: add/subtract 8UC1 0.76x -> 1.3x, min/max u8 0.6x -> ~1x, addWeighted 1.0x -> 1.1-1.4x (32SC1 stays 4.5x); the one remaining laggard is add/sub 32SC1 (0.74-0.82x) - the price of the new SATURATING semantics (7-instruction AVX2 emulation vs the wrapping single add of 5.x; single-instruction on NEON). The 127x61 size is ADDED PERMANENTLY to the arithmetic/addWeighted/compare perf grids: per-call overhead regressions in these base functions must be caught by CI, not discovered by users. dst creation goes through createSameSize (whole-shape transfer including layout and future metadata, not piecemeal dims+sizes) here and in math_op. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * cv::pow rebuilt on the engine; integer-exponent and 1/sqrt(x) kernel branches Routing: p = 0/1/2 keep their early special cases (fill/copy/multiply); an INTEGER array with an INTEGER power keeps the classic iPow multiply chain - bit-exact compatibility, including its wrap-around quirks (iPow squares in int, so e.g. pow(255,4) on u8 wraps negative and saturates to 0 - somebody may rely on that). Everything else goes through the engine with the math_op two-tier scheme: small continuous arrays call powKernel directly (the exponent rides as a broadcast T scalar), the rest run the tiled parallel program. Integer arrays with fractional powers compute in the float domain and saturate back; the 32U/64-bit depths (classic iPow asserted on them) and f16/bf16 (the classic float path misread them) now just work. powKernel gets two new per-row exponent branches: - p == -0.5: 1/v_sqrt(x) (the classic path used IPP ippsInvSqrt_A21, a 21-bit approximation; ours is exact - slightly slower on small arrays, 4.5x faster at 1080p via parallelism); - any other INTEGER p (|p| <= 65536): LSB-first binary exponentiation, the same multiply chain and order as iPow, fully vectorized - a few ulp accurate vs ~2e-7 of exp(p*log x), and exact on non-positive bases (the sign falls out of the multiplies, 0^negative divides to inf) - no scalar patching. cv::pow f32 vs 5.x: p=0.5 365 -> 61 us at 1080p (6x), p=3 7.7x, p=5 7x AND faster at every size (the old scalar chain: 0.81 -> 0.47 us at 127x61), p=2.5 5.3x. The s16^5 iPow path is untouched (118.7 == 118.5 us). pow_exponents accuracy tests extended to 11 exponents x f32/f64 against the double std::pow reference. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * perf: SANITY_CHECK_NOTHING for the tests whose grids got the 127x61 size The 127x61 entry added to guard per-call overhead has no regression data in opencv_extra, so the legacy SANITY_CHECK in addWeighted/compare failed on CI (locally it passes silently without the test-data path). Accuracy of both functions is covered by the accuracy suite; the perf tests should measure time. PatchNaNs/finiteMask keep their SANITY_CHECK - their grids are untouched. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix a temp-buffer double release in the liveness pass ("d*d" crash) When the same temp is passed as SEVERAL arguments of its last-use instruction (e.g. the named-intermediate expression "d = {0} - {1}; d*d", where the MUL consumes slot d twice), the buffer-reuse scan pushed the temp's physical buffer onto the free list once per argument. That overflows the ntemps-sized freeBufs array (caught by the AutoBuffer range check in Debug: python test_expr.py::test_named_temporary) and, in larger programs, would hand the same physical buffer to two live temps. Release the buffer once by retiring lastUse[t] after the first hit. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * executor: recalibrate opCost to vectorized cycles + clamp the stripe count The per-element op costs fed into the parallel_for_ stripe hint were scalar-era estimates (~20x above the vectorized reality: the atan2/exp polynomials run at ~1.5 cycles/element, not 30). The hint therefore split transcendental/divide work into hundreds of ~1 us jobs, which the macOS/GCD backend dispatches poorly under sustained load, on top of the P/E-core equal-share straggler effect. Measured on M4 Max (12P+4E), sustained medians @1920x1080 f32: texpr atan2 320 -> 133 us, cv::exp 280 -> 141 us, cv::log 301 -> ~200 us, cv::pow(x,2.5) 388 -> 376 us; the PR tables' math rows improved ~1.5-2x across the board. - opCost is now in units of ~1/4 cycle/element of the SIMD kernels: cheap ops 1 (unchanged), div/sqrt/hypot/convert_scale 10 -> 3, transcendentals 30 -> 6. - the stripe hint is clamped by min(4*nthreads, max(32, 3*nthreads)): ~4 stripes/thread is plenty of granularity for element-wise work, and the ceiling is 32 pieces except on machines with many (heterogeneous) cores, where anything coarser than ~3 pieces/worker turns the slow cores into equal-share bottlenecks (measured: 32 stripes on 16 threads is the worst point of the curve - 193 us vs 137 us at 48 for atan2). getNumThreads() is clamped from below (WINRT/plugin backends may report 0). Not addressed here (needs cross-machine data, M2/M3 Ultra): streaming memory-bound ops saturate the M4 Max fabric at ~8 fat stripes and E-core participation only adds contention - a cost model cannot express that; candidate follow-up is a bytes-aware clamp or a GCD-backend-level fix. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * multiply: v_mul_sat integer kernels (full product clamped to the type) v_mul_sat(V, V) -> V for u8/s8/u16/s16/u32/s32 - the full-precision product clamped to the lane type, which is exactly cv::multiply's integer semantics at scale == 1. Local to arithm.simd.hpp for now, next to v_add_sat/v_sub_sat, to be promoted into proper universal intrinsics later. NEON: widening vmull + saturating narrow (vqmovn); other backends: the portable v_mul_expand + saturating v_pack composition for 8/16-bit lanes. 32-bit lanes have no universal widening multiply (no v_mul_expand for s32), so the 32-bit integer fast path is NEON-only for now and the other backends keep the previous f64 work-vector kernels (which measure well on x86 with IPP-free AVX2). EwMul::vec() now routes the integer lane types through v_mul_sat, and getMulFunc_ passes the NATIVE lane vector as the scale==1 fast-path type: whole-register loads/stores, the widening happens inside the multiply. Replaces both the half-register widening loads (u8/s8/u16/s16) and the scalar-equivalent f64 path for 32S/32U on NEON. M4 Max, 640x480 (the sizes where the old kernels lost to carotene): 8U 22.0 -> 13.6 us, 8S 15.0 -> 11.1, 16S 21.2 -> 17.4, 32S 84.8 -> 30.5 (parity with the classic path everywhere, 32S was 0.38x). 1920x1080: 8S 1.30x -> 1.78x, 16S 2.57x -> 2.98x, 32S 2.20x -> 4.02x vs 5.x. Correctness: exhaustive 8-bit (all 65536 pairs per sign), directed saturation corners for 16/32-bit (46341^2, INT_MIN*-1, 65536*65536, all sign combinations) against an exact int64 reference. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * NEON: make v_cvt_f64(v_int32) exact (was via f32, losing bits > 2^24) The NEON implementations of v_cvt_f64/v_cvt_f64_high for v_int32 did s32 -> f32 -> f64 (vcvt_f32_s32 + vcvt_f64_f32), silently rounding any |x| > 2^24. Every vectorized f64 work path with int32 inputs on AArch64 was affected: the engine's addWeighted/divide 32S kernels, convertTo 32S -> 64F, etc. Found via addWeighted 32SC1 on values ~1e9: max error was 32 vs the exact double reference (the classic carotene path is worse still - it computes in f32 end-to-end with f32-truncated weights, max error 96 on the same data). The exact sequence sxtl + scvtf (vmovl_s32 + vcvtq_f64_s64) is the same 2 instructions, so there is no cost. addWeighted 32SC1 on the engine now matches the exact-double reference bit-for-bit and stays at parity/1.4x vs the classic path (640x480/1920x1080). Pre-existing upstream bug (same code in 4.x) - worth a standalone backport with directed large-value tests. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * math_op: drop the temporary direct-IPP tier for exp/log Upstream moved the IPP math wrappers into the hal/ipp HAL module (cv_hal_exp32f/ log32f & co now resolve to ipp_hal_* which honor cv::ipp::useIPP via CV_HAL_CHECK_USE_IPP). The engine's single probeHalUnary(cv_hal_*) probe already picks that up uniformly, so the stopgap #ifdef HAVE_IPP ippExp/ippLog tier and its ipp::useIPP() branch in getMathFunc are now redundant - removed. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * arithm: restore cv::hal::and8u/or8u/xor8u/not8u as engine wrappers These public CV_EXPORTS entry points (core/hal/hal.hpp) lost their definitions when the bitwise ops moved to the element-wise engine, but they are still declared and called by other modules (opencv_objdetect's aruco) and external code - the link broke with undefined references to cv::hal::and8u/xor8u. Restore them as thin forwarders over the engine's byte-wise bitwise kernels (getBitwiseFunc / getNotFunc), mirroring the existing mul8u wrapper. CV_Assert guards the kernel lookup. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * blobdetector: guard empty contour when computing blob radius The new AutoBuffer leaves its tail uninitialized for trivial types, which surfaced a -Wmaybe-uninitialized in findBlobs where the median of per-point distances is read. Use a std::vector, default the radius to 0, and compute the median only for a non-empty contour - no unproven size invariant. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2005 lines
101 KiB
C++
2005 lines
101 KiB
C++
// This file is part of OpenCV project.
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
// of this distribution and at http://opencv.org/license.html.
|
|
|
|
// Element-wise kernels for the new arithmetic engine, SIMD-dispatched per CPU baseline.
|
|
//
|
|
// This file is compiled once per SIMD baseline (registered via ocv_add_dispatched_file). The per-op
|
|
// entry points get*Func_(...) live in cv::ew::CV_CPU_OPTIMIZATION_NAMESPACE and return the kernel
|
|
// optimized for that baseline; the regular get*Func / getElemwiseFunc dispatchers (which forward here
|
|
// through CV_CPU_DISPATCH) live in arithm.dispatch.cpp.
|
|
//
|
|
// Kernel shape (house style of arithm.simd.hpp + convert.simd.hpp):
|
|
// - one 2D tile; per-row outer loop with stepy; dst contiguous in x.
|
|
// - stepx is restricted to {0,1}: 1 = contiguous (SIMD), 0 = broadcast-scalar along x.
|
|
// - continuity collapse 2D->1D when every operand+dst is gap-free.
|
|
// - halide right-edge backoff for the SIMD tail; scalar tail / scalar path for width<VECSZ.
|
|
// - IN-PLACE SAFE: the right-edge backoff is suppressed (scalar tail) when dst aliases an input.
|
|
//
|
|
// One template (vecBinaryKernel) covers the whole matrix: the work-vector type selects the native
|
|
// same-type saturating path vs the widening f32-hub path. Adding a binary op = add one Op functor.
|
|
|
|
#include "opencv2/core/hal/intrin.hpp"
|
|
#include "convert.hpp" // typed load/store-as helpers (cv::), and getConvert*Func
|
|
#include "arithm_expr.hpp" // the kernel contract: TOp / TKernel / KernelFunc
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstring>
|
|
|
|
namespace cv {
|
|
|
|
// Everything outside cv::ew::CV_CPU_OPTIMIZATION_NAMESPACE must be skipped in the
|
|
// declarations-only re-includes (one per dispatched mode), or it gets redefined.
|
|
#ifndef CV_CPU_OPTIMIZATION_DECLARATIONS_ONLY
|
|
|
|
// BinaryFunc and getConvertFunc / getConvertScaleFunc come from core (precomp.hpp / convert.hpp).
|
|
|
|
#if CV_SIMD128_FP16
|
|
#undef CV_SIMD_16F
|
|
#define CV_SIMD_16F 1
|
|
#elif !defined(CV_SIMD_16F)
|
|
#define CV_SIMD_16F 0 // keep `#if CV_SIMD_16F` -Wundef-clean on builds without FP16 SIMD
|
|
#endif
|
|
|
|
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
|
static inline void vx_setall_as(const uchar* p, v_uint8& a)
|
|
{ a = vx_setall_u8(*p); }
|
|
static inline void vx_setall_as(const schar* p, v_int8& a)
|
|
{ a = vx_setall_s8(*p); }
|
|
static inline void vx_setall_as(const uchar* p, v_int16& a)
|
|
{ a = vx_setall_s16(*p); }
|
|
static inline void vx_setall_as(const schar* p, v_int16& a)
|
|
{ a = vx_setall_s16(*p); }
|
|
|
|
static inline void vx_setall_as(const ushort* p, v_uint16& a)
|
|
{ a = vx_setall_u16(*p); }
|
|
static inline void vx_setall_as(const short* p, v_int16& a)
|
|
{ a = vx_setall_s16(*p); }
|
|
static inline void vx_setall_as(const ushort* p, v_int32& a)
|
|
{ a = vx_setall_s32(*p); }
|
|
static inline void vx_setall_as(const short* p, v_int32& a)
|
|
{ a = vx_setall_s32(*p); }
|
|
|
|
static inline void vx_setall_as(const unsigned* p, v_uint32& a)
|
|
{ a = vx_setall_u32(*p); }
|
|
static inline void vx_setall_as(const int* p, v_int32& a)
|
|
{ a = vx_setall_s32(*p); }
|
|
|
|
static inline void vx_setall_as(const uchar* p, v_float32& a)
|
|
{ a = vx_setall_f32(float(*p)); }
|
|
static inline void vx_setall_as(const schar* p, v_float32& a)
|
|
{ a = vx_setall_f32(float(*p)); }
|
|
static inline void vx_setall_as(const ushort* p, v_float32& a)
|
|
{ a = vx_setall_f32(float(*p)); }
|
|
static inline void vx_setall_as(const short* p, v_float32& a)
|
|
{ a = vx_setall_f32(float(*p)); }
|
|
static inline void vx_setall_as(const float* p, v_float32& a)
|
|
{ a = vx_setall_f32(*p); }
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Saturating integer helpers the universal intrinsics lack: v_add_sat/v_sub_sat for 32-bit lanes
|
|
// (v_add/v_sub saturate 8/16-bit but WRAP 32-bit), and v_mul_sat - the FULL-precision product
|
|
// clamped to the lane type, which is exactly cv::multiply's integer semantics at scale == 1.
|
|
// Local for now - the plan is to promote these into core/hal/intrin_*.hpp as proper universal
|
|
// intrinsics for all integer types. On NEON: single-instruction add/sub (vqadd/vqsub) and the
|
|
// widening-multiply + saturating-narrow pattern (vmull + vqmovn; the vget/vcombine forms compile
|
|
// to smull2/sqxtn2 on AArch64). Elsewhere: the Hacker's Delight (2-13) bit tricks for add/sub and
|
|
// the portable v_mul_expand + saturating v_pack composition for the 8/16-bit multiplies (RVV/LSX
|
|
// also have native ones - later, in the intrinsics). 32-bit lanes have no universal widening
|
|
// multiply (no v_mul_expand for s32), so 32-bit v_mul_sat is NEON-only (EW_HAVE_MULSAT32) and
|
|
// getMulFunc_ keeps the f64 work-vector kernels on the other backends.
|
|
#if defined(__ARM_NEON)
|
|
static inline v_int32 v_add_sat(const v_int32& a, const v_int32& b) { return v_int32(vqaddq_s32(a.val, b.val)); }
|
|
static inline v_int32 v_sub_sat(const v_int32& a, const v_int32& b) { return v_int32(vqsubq_s32(a.val, b.val)); }
|
|
static inline v_uint32 v_add_sat(const v_uint32& a, const v_uint32& b) { return v_uint32(vqaddq_u32(a.val, b.val)); }
|
|
static inline v_uint32 v_sub_sat(const v_uint32& a, const v_uint32& b) { return v_uint32(vqsubq_u32(a.val, b.val)); }
|
|
#define EW_HAVE_MULSAT32 1
|
|
static inline v_uint8 v_mul_sat(const v_uint8& a, const v_uint8& b)
|
|
{
|
|
uint16x8_t p0 = vmull_u8(vget_low_u8(a.val), vget_low_u8(b.val));
|
|
uint16x8_t p1 = vmull_u8(vget_high_u8(a.val), vget_high_u8(b.val));
|
|
return v_uint8(vcombine_u8(vqmovn_u16(p0), vqmovn_u16(p1)));
|
|
}
|
|
static inline v_int8 v_mul_sat(const v_int8& a, const v_int8& b)
|
|
{
|
|
int16x8_t p0 = vmull_s8(vget_low_s8(a.val), vget_low_s8(b.val));
|
|
int16x8_t p1 = vmull_s8(vget_high_s8(a.val), vget_high_s8(b.val));
|
|
return v_int8(vcombine_s8(vqmovn_s16(p0), vqmovn_s16(p1)));
|
|
}
|
|
static inline v_uint16 v_mul_sat(const v_uint16& a, const v_uint16& b)
|
|
{
|
|
uint32x4_t p0 = vmull_u16(vget_low_u16(a.val), vget_low_u16(b.val));
|
|
uint32x4_t p1 = vmull_u16(vget_high_u16(a.val), vget_high_u16(b.val));
|
|
return v_uint16(vcombine_u16(vqmovn_u32(p0), vqmovn_u32(p1)));
|
|
}
|
|
static inline v_int16 v_mul_sat(const v_int16& a, const v_int16& b)
|
|
{
|
|
int32x4_t p0 = vmull_s16(vget_low_s16(a.val), vget_low_s16(b.val));
|
|
int32x4_t p1 = vmull_s16(vget_high_s16(a.val), vget_high_s16(b.val));
|
|
return v_int16(vcombine_s16(vqmovn_s32(p0), vqmovn_s32(p1)));
|
|
}
|
|
static inline v_uint32 v_mul_sat(const v_uint32& a, const v_uint32& b)
|
|
{
|
|
uint64x2_t p0 = vmull_u32(vget_low_u32(a.val), vget_low_u32(b.val));
|
|
uint64x2_t p1 = vmull_u32(vget_high_u32(a.val), vget_high_u32(b.val));
|
|
return v_uint32(vcombine_u32(vqmovn_u64(p0), vqmovn_u64(p1)));
|
|
}
|
|
static inline v_int32 v_mul_sat(const v_int32& a, const v_int32& b)
|
|
{
|
|
int64x2_t p0 = vmull_s32(vget_low_s32(a.val), vget_low_s32(b.val));
|
|
int64x2_t p1 = vmull_s32(vget_high_s32(a.val), vget_high_s32(b.val));
|
|
return v_int32(vcombine_s32(vqmovn_s64(p0), vqmovn_s64(p1)));
|
|
}
|
|
#else
|
|
static inline v_int32 v_add_sat(const v_int32& a, const v_int32& b)
|
|
{
|
|
v_int32 res = v_add(a, b);
|
|
v_int32 ov = v_and(v_xor(a, res), v_xor(b, res)); // sign bit set iff overflow
|
|
v_int32 sat = v_xor(v_shr<31>(a), vx_setall_s32(INT_MAX)); // a >= 0 ? INT_MAX : INT_MIN
|
|
return v_select(v_lt(ov, vx_setzero_s32()), sat, res);
|
|
}
|
|
static inline v_int32 v_sub_sat(const v_int32& a, const v_int32& b)
|
|
{
|
|
v_int32 res = v_sub(a, b);
|
|
v_int32 ov = v_and(v_xor(a, b), v_xor(a, res));
|
|
v_int32 sat = v_xor(v_shr<31>(a), vx_setall_s32(INT_MAX));
|
|
return v_select(v_lt(ov, vx_setzero_s32()), sat, res);
|
|
}
|
|
static inline v_uint32 v_add_sat(const v_uint32& a, const v_uint32& b)
|
|
{
|
|
v_uint32 res = v_add(a, b);
|
|
return v_or(res, v_lt(res, a)); // wrapped => all-ones
|
|
}
|
|
static inline v_uint32 v_sub_sat(const v_uint32& a, const v_uint32& b)
|
|
{
|
|
return v_and(v_sub(a, b), v_ge(a, b)); // borrow => zero
|
|
}
|
|
static inline v_uint8 v_mul_sat(const v_uint8& a, const v_uint8& b)
|
|
{ v_uint16 p0, p1; v_mul_expand(a, b, p0, p1); return v_pack(p0, p1); }
|
|
static inline v_int8 v_mul_sat(const v_int8& a, const v_int8& b)
|
|
{ v_int16 p0, p1; v_mul_expand(a, b, p0, p1); return v_pack(p0, p1); }
|
|
static inline v_uint16 v_mul_sat(const v_uint16& a, const v_uint16& b)
|
|
{ v_uint32 p0, p1; v_mul_expand(a, b, p0, p1); return v_pack(p0, p1); }
|
|
static inline v_int16 v_mul_sat(const v_int16& a, const v_int16& b)
|
|
{ v_int32 p0, p1; v_mul_expand(a, b, p0, p1); return v_pack(p0, p1); }
|
|
#endif
|
|
#if CV_SIMD_64F || CV_SIMD_SCALABLE_64F // scalable (RVV) has v_float64 without CV_SIMD_64F
|
|
static inline void vx_setall_as(const double* p, v_float64& a) { a = vx_setall_f64(*p); }
|
|
static inline void vx_setall_as(const int* p, v_float64& a) { a = vx_setall_f64((double)*p); }
|
|
static inline void vx_setall_as(const unsigned* p, v_float64& a) { a = vx_setall_f64((double)*p); }
|
|
static inline void vx_setall_as(const int64_t* p, v_float64& a) { a = vx_setall_f64((double)*p); }
|
|
static inline void vx_setall_as(const uint64_t* p, v_float64& a) { a = vx_setall_f64((double)*p); }
|
|
#endif
|
|
static inline void vx_setall_as(const bfloat* p, v_float32& a)
|
|
{ a = vx_setall_f32(float(*p)); }
|
|
static inline void vx_setall_as(const hfloat* p, v_float32& a)
|
|
{ a = vx_setall_f32(float(*p)); }
|
|
|
|
static inline void vx_load_pair_as(const uchar* p, v_uint8& a, v_uint8& b)
|
|
{
|
|
a = vx_load(p);
|
|
b = vx_load(p + VTraits<v_uint8>::vlanes());
|
|
}
|
|
|
|
static inline void vx_load_pair_as(const schar* p, v_int8& a, v_int8& b)
|
|
{
|
|
a = vx_load(p);
|
|
b = vx_load(p + VTraits<v_int8>::vlanes());
|
|
}
|
|
|
|
static inline void vx_load_pair_as(const uchar* p, v_uint32& a, v_uint32& b)
|
|
{
|
|
v_uint16 w = vx_load_expand(p);
|
|
v_expand(w, a, b);
|
|
}
|
|
|
|
static inline void v_store_pair_as(uchar* p, const v_uint8& a, const v_uint8& b)
|
|
{
|
|
v_store(p, a);
|
|
v_store(p + VTraits<v_uint8>::vlanes(), b);
|
|
}
|
|
|
|
static inline void v_store_pair_as(schar* p, const v_int8& a, const v_int8& b)
|
|
{
|
|
v_store(p, a);
|
|
v_store(p + VTraits<v_int8>::vlanes(), b);
|
|
}
|
|
|
|
|
|
static inline void v_store_pair_as(int* p, const v_int16& a, const v_int16& b)
|
|
{
|
|
const int nlanes32 = VTraits<v_int32>::vlanes();
|
|
v_int32 v0, v1, v2, v3;
|
|
v_expand(a, v0, v1);
|
|
v_expand(b, v2, v3);
|
|
v_store(p, v0);
|
|
v_store(p + nlanes32, v1);
|
|
v_store(p + nlanes32*2, v2);
|
|
v_store(p + nlanes32*3, v3);
|
|
}
|
|
|
|
static inline void v_store_pair_as(float* p, const v_int16& a, const v_int16& b)
|
|
{
|
|
const int nlanes32 = VTraits<v_float32>::vlanes();
|
|
v_int32 v0, v1, v2, v3;
|
|
v_expand(a, v0, v1);
|
|
v_expand(b, v2, v3);
|
|
v_store(p, v_cvt_f32(v0));
|
|
v_store(p + nlanes32, v_cvt_f32(v1));
|
|
v_store(p + nlanes32*2, v_cvt_f32(v2));
|
|
v_store(p + nlanes32*3, v_cvt_f32(v3));
|
|
}
|
|
|
|
static inline void v_store_pair_as(float* p, const v_int32& a, const v_int32& b)
|
|
{
|
|
v_store(p, v_cvt_f32(a));
|
|
v_store(p + VTraits<v_float32>::vlanes(), v_cvt_f32(b));
|
|
}
|
|
|
|
static inline void v_store_pair_as(hfloat* p, const v_float32& a, const v_float32& b)
|
|
{
|
|
v_pack_store(p, a);
|
|
v_pack_store(p + VTraits<v_float32>::vlanes(), b);
|
|
}
|
|
|
|
static inline void v_store_pair_as(bfloat* p, const v_float32& a, const v_float32& b)
|
|
{
|
|
v_pack_store(p, a);
|
|
v_pack_store(p + VTraits<v_float32>::vlanes(), b);
|
|
}
|
|
|
|
// Reinterpret a comparison result (whose lane type matches the operands) as the UNSIGNED integer
|
|
// vector of the same width: u8->u8, s8->u8, u16/s16->u16, u32/s32/f32->u32. The destination type
|
|
// drives the overload, so the compare kernel needs no per-width if constexpr.
|
|
static inline void v_reinterpret_as(const v_uint8& s, v_uint8& d) { d = s; }
|
|
static inline void v_reinterpret_as(const v_int8& s, v_uint8& d) { d = v_reinterpret_as_u8(s); }
|
|
static inline void v_reinterpret_as(const v_uint16& s, v_uint16& d) { d = s; }
|
|
static inline void v_reinterpret_as(const v_int16& s, v_uint16& d) { d = v_reinterpret_as_u16(s); }
|
|
static inline void v_reinterpret_as(const v_uint32& s, v_uint32& d) { d = s; }
|
|
static inline void v_reinterpret_as(const v_int32& s, v_uint32& d) { d = v_reinterpret_as_u32(s); }
|
|
static inline void v_reinterpret_as(const v_float32& s, v_uint32& d) { d = v_reinterpret_as_u32(s); }
|
|
#if CV_SIMD_16F
|
|
static inline void v_reinterpret_as(const v_float16& s, v_uint16& d) { d = v_reinterpret_as_u16(s); }
|
|
#endif
|
|
#if CV_SIMD_64F
|
|
static inline void v_reinterpret_as(const v_float64& s, v_uint64& d) { d = v_reinterpret_as_u64(s); }
|
|
#endif
|
|
|
|
// Broadcast the compare mask value (1 or 255) across the unsigned work vector.
|
|
static inline void v_setall_mask(v_uint8& v, uchar t) { v = vx_setall_u8(t); }
|
|
static inline void v_setall_mask(v_uint16& v, uchar t) { v = vx_setall_u16(t); }
|
|
static inline void v_setall_mask(v_uint32& v, uchar t) { v = vx_setall_u32(t); }
|
|
|
|
// NOTE: v_store_pair_as(uchar*, v_uint16/v_uint32, ...) - the narrowing pack-to-u8 stores we need -
|
|
// already come from core's convert.hpp (included above), so they are not redefined here.
|
|
|
|
#if CV_SIMD_16F
|
|
static inline void vx_setall_as(const hfloat* p, v_float16& a)
|
|
{ a = vx_setall_f16(*p); }
|
|
static inline void vx_setall_as(const float* p, v_float16& a)
|
|
{ a = vx_setall_f16(hfloat(*p)); }
|
|
static inline void vx_setall_as(const uchar* p, v_float16& a)
|
|
{ a = vx_setall_f16(hfloat(float(*p))); }
|
|
static inline void vx_setall_as(const schar* p, v_float16& a)
|
|
{ a = vx_setall_f16(hfloat(float(*p))); }
|
|
static inline void vx_load_pair_as(const uchar* p, v_float16& a, v_float16& b)
|
|
{
|
|
v_uint8 v = vx_load(p);
|
|
v_uint16 v0, v1;
|
|
v_expand(v, v0, v1);
|
|
a = v_cvt_f16(v_reinterpret_as_s16(v0));
|
|
b = v_cvt_f16(v_reinterpret_as_s16(v1));
|
|
}
|
|
static inline void vx_load_pair_as(const schar* p, v_float16& a, v_float16& b)
|
|
{
|
|
v_int8 v = vx_load(p);
|
|
v_int16 v0, v1;
|
|
v_expand(v, v0, v1);
|
|
a = v_cvt_f16(v0);
|
|
b = v_cvt_f16(v1);
|
|
}
|
|
static inline void vx_load_pair_as(const hfloat* p, v_float16& a, v_float16& b)
|
|
{
|
|
a = vx_load(p);
|
|
b = vx_load(p + VTraits<v_float16>::vlanes());
|
|
}
|
|
static inline void v_store_pair_as(uchar* p, const v_float16& a, const v_float16& b)
|
|
{
|
|
v_int16 v0 = v_round(a), v1 = v_round(b);
|
|
v_store(p, v_pack_u(v0, v1));
|
|
}
|
|
static inline void v_store_pair_as(schar* p, const v_float16& a, const v_float16& b)
|
|
{
|
|
v_int16 v0 = v_round(a), v1 = v_round(b);
|
|
v_store(p, v_pack(v0, v1));
|
|
}
|
|
static inline void v_store_pair_as(hfloat* p, const v_float16& a, const v_float16& b)
|
|
{
|
|
v_store(p, a);
|
|
v_store(p + VTraits<v_float16>::vlanes(), b);
|
|
}
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif // CV_CPU_OPTIMIZATION_DECLARATIONS_ONLY
|
|
|
|
namespace ew {
|
|
CV_CPU_OPTIMIZATION_NAMESPACE_BEGIN
|
|
|
|
// ---- per-op kernel entry points for THIS baseline (the regular dispatchers in
|
|
// arithm.dispatch.cpp reach them through CV_CPU_DISPATCH). ----
|
|
TKernel getAddFunc_(int T, int R);
|
|
TKernel getSubFunc_(int T, int R);
|
|
TKernel getMulFunc_(int T, int R);
|
|
TKernel getDivFunc_(int T, int R, bool checked);
|
|
TKernel getMinFunc_(int T, int R);
|
|
TKernel getMaxFunc_(int T, int R);
|
|
TKernel getAbsdiffFunc_(int T, int R);
|
|
TKernel getHypotFunc_(int T, int R); // hypot = sqrt(x^2+y^2), float depths, T x T -> T
|
|
TKernel getAtan2Func_(int T, int R); // atan2(y, x), radians (-pi, pi], float depths
|
|
TKernel getCmpFunc_(TOp op, int T);
|
|
TKernel getBitwiseFunc_(TOp op, int esz); // OP_AND / OP_OR / OP_XOR, by element size
|
|
TKernel getNotFunc_(int esz); // OP_NOT, by element size
|
|
TKernel getAddWeightedFunc_(int T, int R); // OP_ADDW, a*alpha+b*beta+gamma (T x T -> R)
|
|
TKernel getSelectFunc_(int mdepth, int T); // OP_SELECT: 1-byte mask, a/b/dst of T (by esz)
|
|
TKernel getClampFunc_(int T); // OP_CLAMP: min(max(x, lo), hi), all operands of T
|
|
TKernel getCastFunc_(int sdepth, int ddepth, bool scaled); // OP_CAST / OP_CONVERT_SCALE
|
|
|
|
#ifndef CV_CPU_OPTIMIZATION_DECLARATIONS_ONLY
|
|
|
|
// ===========================================================================
|
|
// Op functors (vector + scalar). New binary ops slot in here.
|
|
// ===========================================================================
|
|
struct EwAdd {
|
|
// useScalar: does the op consume the scale scalar (params[0]) in vec()/preproc()? When false,
|
|
// vecBinaryKernel's fast 2-arg branch is taken unconditionally (the check folds at compile time);
|
|
// when true, it is taken only if the runtime scale is exactly 1.
|
|
static constexpr bool useScalar = false;
|
|
template<typename V> static V vec(const V& a, const V& b) { return v_add(a, b); }
|
|
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
|
// 32-bit lanes: v_add wraps - use the saturating version to match the scalar int64+clamp tail
|
|
static v_int32 vec(const v_int32& a, const v_int32& b) { return v_add_sat(a, b); }
|
|
static v_uint32 vec(const v_uint32& a, const v_uint32& b) { return v_add_sat(a, b); }
|
|
#endif
|
|
template<typename V, typename S> static V vec(const V& a, const V& b, const S&) { return vec(a, b); }
|
|
template<typename V> static V preproc(const V& a, const V&) { return a; }
|
|
// Accumulate in the promoted type, NOT in W: for the native saturating path W is the narrow
|
|
// lane type (schar/short/...), and (W)(a+b) would wrap in 8/16 bits before saturate_cast<Tr>
|
|
// could clamp. Letting a+b promote (narrow -> int) keeps saturation for 8/16-bit outputs and
|
|
// the natural wrap for 32/64-bit (both matching cv::add). The SIMD path already saturates.
|
|
template<typename W, typename ST> static W scl(W a, W b, ST) { return W(a + b); }
|
|
};
|
|
|
|
struct EwSub {
|
|
static constexpr bool useScalar = false;
|
|
template<typename V> static V vec(const V& a, const V& b) { return v_sub(a, b); }
|
|
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
|
// 32-bit lanes: see EwAdd::vec
|
|
static v_int32 vec(const v_int32& a, const v_int32& b) { return v_sub_sat(a, b); }
|
|
static v_uint32 vec(const v_uint32& a, const v_uint32& b) { return v_sub_sat(a, b); }
|
|
#endif
|
|
template<typename V, typename S> static V vec(const V& a, const V& b, const S&) { return vec(a, b); }
|
|
template<typename V> static V preproc(const V& a, const V&) { return a; }
|
|
template<typename W, typename ST> static W scl(W a, W b, ST) { return W(a - b); } // see EwAdd::scl
|
|
// 64-bit unsigned has no wider WT to hold a-b, so the generic path would wrap on underflow.
|
|
// Saturate at 0 to match cv::subtract (8/16/32-bit already saturate via SIMD floor / wide WT).
|
|
static uint64_t scl(uint64_t a, uint64_t b, uint64_t) { return uint64_t(a >= b)*(a - b); }
|
|
};
|
|
|
|
// mul/div compute in a wide FLOAT work type (W = float for <=16-bit/f16/bf/f32, double for
|
|
// 32/64-bit/f64), matching cv::multiply/divide; the executor casts the work-type result down to
|
|
// rdepth. scalar (reference) path only for now - SIMD can follow. (No vec(): never instantiated.)
|
|
// vec(a, b, scale): the 3rd arg is the scale vector. COMMUTATIVE ops (mul, absdiff) ignore it - their
|
|
// scale is folded into the (cheaper) preproc of one operand. DIVISION is NOT commutative, so its scale
|
|
// MUST stick to the numerator; it takes the scale in vec (numerator*scale/denominator) and leaves
|
|
// preproc as identity. vecBinaryKernel always passes vscalar to vec, so every branch (incl. a broadcast
|
|
// denominator) divides correctly. The 2-arg vec (scale==1) is the both-contiguous fast path.
|
|
struct EwMul {
|
|
static constexpr bool useScalar = true;
|
|
template<typename V> static V vec(const V& a, const V& b) { return v_mul(a, b); }
|
|
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
|
// integer lanes: full product clamped to the lane type (v_mul_sat) == cv::multiply semantics
|
|
// at scale 1. These drive the scale==1 fast path on FULL-width registers (Wvec1 = the native
|
|
// lane vector): whole-register loads/stores, widening happens inside the multiply itself.
|
|
// The guard matches the v_mul_sat definitions above (like EwAdd's 32-bit overloads): in
|
|
// no-SIMD builds the vector typedefs still exist (intrin_cpp), but the helpers do not.
|
|
static v_uint8 vec(const v_uint8& a, const v_uint8& b) { return v_mul_sat(a, b); }
|
|
static v_int8 vec(const v_int8& a, const v_int8& b) { return v_mul_sat(a, b); }
|
|
static v_uint16 vec(const v_uint16& a, const v_uint16& b) { return v_mul_sat(a, b); }
|
|
static v_int16 vec(const v_int16& a, const v_int16& b) { return v_mul_sat(a, b); }
|
|
#ifdef EW_HAVE_MULSAT32
|
|
static v_uint32 vec(const v_uint32& a, const v_uint32& b) { return v_mul_sat(a, b); }
|
|
static v_int32 vec(const v_int32& a, const v_int32& b) { return v_mul_sat(a, b); }
|
|
#endif
|
|
#endif
|
|
template<typename V, typename S> static V vec(const V& a, const V& b, const S&) { return vec(a, b); }
|
|
template<typename V> static V preproc(const V& a, const V& s) { return v_mul(a, s); }
|
|
template<typename W, typename ST> static W scl(W a, W b, ST s) { return a * b * s; }
|
|
};
|
|
// div has two variants by the COMMON INPUT type (matching cv::'s per-type kernel choice): integer
|
|
// inputs guard divide-by-zero -> 0 (cv:: iscalar_div); float inputs do NOT guard (cv:: fscalar_div,
|
|
// a/0 -> inf), which then saturates on the cast to an integer output exactly like cv::divide.
|
|
struct EwDivInt {
|
|
static constexpr bool useScalar = true;
|
|
// integer inputs computed in the float work type: guard b==0 -> 0. Scale rides the numerator (vec).
|
|
template<typename V> static V vec(const V& a, const V& b) {
|
|
const V z = v_setzero_<V>();
|
|
return v_select(v_eq(b, z), z, v_div(a, b));
|
|
}
|
|
template<typename V> static V vec(const V& a, const V& b, const V& s) {
|
|
const V z = v_setzero_<V>();
|
|
return v_select(v_eq(b, z), z, v_div(v_mul(a, s), b));
|
|
}
|
|
template<typename V> static V preproc(const V& a, const V&) { return a; } // identity: scale is in vec
|
|
template<typename W, typename ST> static W scl(W a, W b, ST s) { return b != W(0) ? a * s / b : W(0); }
|
|
};
|
|
struct EwDivFlt {
|
|
static constexpr bool useScalar = true;
|
|
template<typename V> static V vec(const V& a, const V& b) { return v_div(a, b); }
|
|
template<typename V> static V vec(const V& a, const V& b, const V& s) { return v_div(v_mul(a, s), b); }
|
|
template<typename V> static V preproc(const V& a, const V&) { return a; } // identity: scale is in vec
|
|
template<typename W, typename ST> static W scl(W a, W b, ST s) { return a * s / b; }
|
|
};
|
|
|
|
// atan2(y, x) in RADIANS over the standard C range (-pi, pi] - the fastAtan2 minimax polynomial
|
|
// (mathfuncs_core.simd.hpp v_atan_f32) with the 180/pi factor dropped and the C quadrant logic
|
|
// (fastAtan2 returns degrees in [0, 360)). Absolute accuracy ~1e-5 rad, same as cv::fastAtan2.
|
|
// Generic over the universal-intrinsic float vector type.
|
|
template<typename V>
|
|
static inline V v_atan2(const V& y, const V& x)
|
|
{
|
|
using LT = typename VTraits<V>::lane_type;
|
|
const V eps = v_setall_<V>((LT)DBL_EPSILON);
|
|
const V z = v_setzero_<V>();
|
|
const V p7 = v_setall_<V>((LT)-0.04432655554792128);
|
|
const V p5 = v_setall_<V>((LT)0.1555786518463281);
|
|
const V p3 = v_setall_<V>((LT)-0.3258083974640975);
|
|
const V p1 = v_setall_<V>((LT)0.9997878412794807);
|
|
const V vpi2 = v_setall_<V>((LT)(CV_PI/2));
|
|
const V vpi = v_setall_<V>((LT)CV_PI);
|
|
|
|
V ax = v_abs(x), ay = v_abs(y);
|
|
V c = v_div(v_min(ax, ay), v_add(v_max(ax, ay), eps));
|
|
V c2 = v_mul(c, c);
|
|
V a = v_mul(v_fma(v_fma(v_fma(p7, c2, p5), c2, p3), c2, p1), c);
|
|
a = v_select(v_ge(ax, ay), a, v_sub(vpi2, a));
|
|
a = v_select(v_lt(x, z), v_sub(vpi, a), a);
|
|
a = v_select(v_lt(y, z), v_sub(z, a), a);
|
|
return a;
|
|
}
|
|
|
|
struct EwAtan2 {
|
|
static constexpr bool useScalar = false;
|
|
template<typename V> static V vec(const V& a, const V& b) { return v_atan2(a, b); } // a = y, b = x
|
|
template<typename V, typename S> static V vec(const V& a, const V& b, const S&) { return vec(a, b); }
|
|
template<typename V> static V preproc(const V& a, const V&) { return a; }
|
|
template<typename W, typename ST> static W scl(W a, W b, ST) { return std::atan2(a, b); }
|
|
};
|
|
|
|
// hypot(x, y) = sqrt(x^2 + y^2): NAIVE (matches cv::magnitude; overflow at |x| ~ 1e19+ for f32
|
|
// inputs is accepted), computed in the float work type; T x T -> T over the float depths.
|
|
struct EwHypot {
|
|
static constexpr bool useScalar = false;
|
|
template<typename V> static V vec(const V& a, const V& b) { return v_sqrt(v_fma(a, a, v_mul(b, b))); }
|
|
template<typename V, typename S> static V vec(const V& a, const V& b, const S&) { return vec(a, b); }
|
|
template<typename V> static V preproc(const V& a, const V&) { return a; }
|
|
template<typename W, typename ST> static W scl(W a, W b, ST) { return std::sqrt(a*a + b*b); }
|
|
};
|
|
|
|
// min / max / absdiff: T x T -> T (same depth in and out, no scale). v_min/v_max exist for every
|
|
// vector lane type (64-bit ints fall back to scalar). absdiff uses v_absdiff (defined for the
|
|
// UNSIGNED and float lane types - signed/wide depths go through the scalar path), and the scalar
|
|
// |a-b| is computed branch-wise so it never underflows an unsigned work type.
|
|
struct EwMin {
|
|
static constexpr bool useScalar = false;
|
|
template<typename V> static V vec(const V& a, const V& b) { return v_min(a, b); }
|
|
template<typename V, typename S> static V vec(const V& a, const V& b, const S&) { return vec(a, b); }
|
|
template<typename V> static V preproc(const V& a, const V&) { return a; }
|
|
template<typename W, typename ST> static W scl(W a, W b, ST) { return std::min(a, b); }
|
|
};
|
|
struct EwMax {
|
|
static constexpr bool useScalar = false;
|
|
template<typename V> static V vec(const V& a, const V& b) { return v_max(a, b); }
|
|
template<typename V, typename S> static V vec(const V& a, const V& b, const S&) { return vec(a, b); }
|
|
template<typename V> static V preproc(const V& a, const V&) { return a; }
|
|
template<typename W, typename ST> static W scl(W a, W b, ST) { return std::max(a, b); }
|
|
};
|
|
struct EwAbsdiff {
|
|
static constexpr bool useScalar = false;
|
|
// The absdiff RESULT is the UNSIGNED type of the input width: v_absdiff(v_int8/16/32) already returns
|
|
// v_uint8/16/32 (the true |a-b|, which can exceed the signed max), and v_absdiff on unsigned/float
|
|
// returns the same type. vec() therefore returns THAT type (deduced), not the input V, so the kernel
|
|
// stores it through the matching UNSIGNED v_store_pair_as (an honest same-type store) - no reinterpret,
|
|
// no touching the saturating-narrow overloads' semantics. (This is why the kernel keeps the vec
|
|
// result in its own variable rather than reusing the input operand.)
|
|
template<typename V> static auto vec(const V& a, const V& b) { return v_absdiff(a, b); }
|
|
template<typename V, typename S> static auto vec(const V& a, const V& b, const S&) { return v_absdiff(a, b); }
|
|
template<typename V> static V preproc(const V& a, const V&) { return a; }
|
|
template<typename W, typename ST> static W scl(W a, W b, ST) { return a > b ? W(a - b) : W(b - a); }
|
|
};
|
|
|
|
// Signed T -> SAME signed T (rdepth==T): |a-b| saturated into the signed range, in ONE pass. cv::absdiff
|
|
// keeps the signed depth, so the generic path computes absdiff->unsigned then casts back down (2 insns);
|
|
// this fuses it. v_absdiff yields the unsigned |a-b|; v_min clamps to the signed max (already >=0), then
|
|
// a same-width reinterpret to signed (all values now fit).
|
|
struct EwAbsdiffS {
|
|
static constexpr bool useScalar = false;
|
|
static v_int8 vec(const v_int8& a, const v_int8& b) { return v_reinterpret_as_s8 (v_min(v_absdiff(a, b), vx_setall_u8 (0x7f))); }
|
|
static v_int16 vec(const v_int16& a, const v_int16& b) { return v_reinterpret_as_s16(v_min(v_absdiff(a, b), vx_setall_u16(0x7fff))); }
|
|
static v_int32 vec(const v_int32& a, const v_int32& b) { return v_reinterpret_as_s32(v_min(v_absdiff(a, b), vx_setall_u32(0x7fffffff))); }
|
|
template<typename V, typename S> static V vec(const V& a, const V& b, const S&) { return vec(a, b); }
|
|
template<typename V> static V preproc(const V& a, const V&) { return a; }
|
|
template<typename W, typename ST> static W scl(W a, W b, ST) { return a > b ? W(a - b) : W(b - a); }
|
|
};
|
|
|
|
// compare: T x T -> u8 mask. cmp(a,b) is the scalar relation over a work type W (integers compare
|
|
// directly, f16/bf16 through float); vec(a,b) is the vector relation returning an all-ones/zero mask
|
|
// in the operand's own lane type. The kernel turns either into 0 / trueVal (1 or 255 via TKernel::flags).
|
|
struct EwCmpEq { template<typename W> static bool cmp(W a, W b) { return a == b; }
|
|
template<typename V> static V vec(const V& a, const V& b) { return v_eq(a, b); } };
|
|
struct EwCmpNe { template<typename W> static bool cmp(W a, W b) { return a != b; }
|
|
template<typename V> static V vec(const V& a, const V& b) { return v_ne(a, b); } };
|
|
// LT/LE are synthesized from GT/GE via the EW_KERNEL_SWAP01 flag (a<b == b>a), so no Lt/Le kernels.
|
|
struct EwCmpGt { template<typename W> static bool cmp(W a, W b) { return a > b; }
|
|
template<typename V> static V vec(const V& a, const V& b) { return v_gt(a, b); } };
|
|
struct EwCmpGe { template<typename W> static bool cmp(W a, W b) { return a >= b; }
|
|
template<typename V> static V vec(const V& a, const V& b) { return v_ge(a, b); } };
|
|
|
|
// bitwise AND/OR/XOR: bit-pattern op, type-agnostic. Run on the UNSIGNED integer whose width matches
|
|
// the element (u8/u16/u32/u64), so one functor set covers every depth. No scale, no widening (T x T ->
|
|
// T, exactly like min/max); preproc is the identity (min/max share this shape). 64-bit uses the scalar
|
|
// path (no widening vector helpers), the rest ride vecBinaryKernel's native same-type path.
|
|
struct EwAnd {
|
|
static constexpr bool useScalar = false;
|
|
template<typename V> static V vec(const V& a, const V& b) { return v_and(a, b); }
|
|
template<typename V, typename S> static V vec(const V& a, const V& b, const S&) { return vec(a, b); }
|
|
template<typename V> static V preproc(const V& a, const V&) { return a; }
|
|
template<typename W, typename ST> static W scl(W a, W b, ST) { return W(a & b); }
|
|
};
|
|
struct EwOr {
|
|
static constexpr bool useScalar = false;
|
|
template<typename V> static V vec(const V& a, const V& b) { return v_or(a, b); }
|
|
template<typename V, typename S> static V vec(const V& a, const V& b, const S&) { return vec(a, b); }
|
|
template<typename V> static V preproc(const V& a, const V&) { return a; }
|
|
template<typename W, typename ST> static W scl(W a, W b, ST) { return W(a | b); }
|
|
};
|
|
struct EwXor {
|
|
static constexpr bool useScalar = false;
|
|
template<typename V> static V vec(const V& a, const V& b) { return v_xor(a, b); }
|
|
template<typename V, typename S> static V vec(const V& a, const V& b, const S&) { return vec(a, b); }
|
|
template<typename V> static V preproc(const V& a, const V&) { return a; }
|
|
template<typename W, typename ST> static W scl(W a, W b, ST) { return W(a ^ b); }
|
|
};
|
|
|
|
// Collapse a gap-free 2D tile to 1D (call with the per-operand x/y-steps).
|
|
#define EW_TRY_COLLAPSE(NSRC) \
|
|
if (height > 1 && dsty == (size_t)width && \
|
|
s0y == s0x*(size_t)width && (NSRC < 2 || s1y == s1x*(size_t)width)) \
|
|
{ width *= height; height = 1; }
|
|
|
|
// Unified binary kernel: T0 x T1 -> Tr (operands same depth for arithmetic; cast is separate).
|
|
// Wvec = work vector. Native (v_uint8/...) drives the same-type saturating path
|
|
// (v_add saturates 8/16-bit, wraps 32-bit); v_float32 drives the widening hub.
|
|
// WT = scalar work type for the tail.
|
|
// Op = operation functor (vec()/scl()).
|
|
// use_simd = compile-time switch; false => pure scalar (32/64-bit widened outputs, f64).
|
|
// stepx in {0,1}; dst contiguous. In-place safe (see file header).
|
|
template<typename T, typename Tr, typename WT, class Op, typename ST=WT>
|
|
static int scalarBinaryKernel(const void* src0_, size_t s0y, size_t s0x,
|
|
const void* src1_, size_t s1y, size_t s1x,
|
|
const void*, size_t, size_t, void* dst_, size_t dsty,
|
|
int width, int height, const double* params, int, void*)
|
|
{
|
|
s0y /= sizeof(T);
|
|
s1y /= sizeof(T);
|
|
dsty /= sizeof(Tr);
|
|
CV_Assert((s0x|s1x) == 1u || (s0x|s1x) + (size_t)width == 1u);
|
|
|
|
const T* src0 = (const T*)src0_;
|
|
const T* src1 = (const T*)src1_;
|
|
Tr* dst = (Tr*)dst_;
|
|
[[maybe_unused]] ST scalar = saturate_cast<ST>(params[0]); // mul/div scale; ignored by add/sub
|
|
|
|
EW_TRY_COLLAPSE(2);
|
|
for (int y = 0; y < height; y++, src0 += s0y, src1 += s1y, dst += dsty)
|
|
{
|
|
if (s0x == s1x) {
|
|
for (int x = 0; x < width; x++)
|
|
dst[x] = saturate_cast<Tr>(Op::scl((WT)src0[x], (WT)src1[x], scalar));
|
|
}
|
|
else if (s0x == 0) {
|
|
WT sc0 = (WT)src0[0];
|
|
for (int x = 0; x < width; x++)
|
|
dst[x] = saturate_cast<Tr>(Op::scl(sc0, (WT)src1[x], scalar));
|
|
}
|
|
else {
|
|
WT sc1 = (WT)src1[0];
|
|
for (int x = 0; x < width; x++)
|
|
dst[x] = saturate_cast<Tr>(Op::scl((WT)src0[x], sc1, scalar));
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
template<typename T, typename WT>
|
|
static void expandScalar(const T* sc, size_t sx, int n0, WT* scbuf, int n)
|
|
{
|
|
int i = 0;
|
|
for (; i < n0; i++) scbuf[i] = (WT)sc[i*sx];
|
|
for (; i < n; i++) scbuf[i] = scbuf[i - n0];
|
|
}
|
|
|
|
// Decode the per-channel patch bytes (mask + value) from the kernel flags into small arrays. Uniform
|
|
// (no EW_CMP_PATCH): mask = trueVal, value = 0 (an ordinary compare). Per-channel: from the 4-bit
|
|
// fields. The compare kernels apply result = (rawmask & mask) | value per channel - folding the
|
|
// former separate patch pass into the compare (one pass).
|
|
static inline void cmpUnpackPatch(int flags, uchar trueVal, uchar mvals[4], uchar vvals[4])
|
|
{
|
|
if (flags & EW_CMP_PATCH)
|
|
for (int c = 0; c < 4; c++)
|
|
{
|
|
const int f = (flags >> (EW_CMP_PATCH_SHIFT + c*4)) & 0xF;
|
|
mvals[c] = (uchar)cmpPatchByte(f & 3);
|
|
vvals[c] = (uchar)cmpPatchByte((f >> 2) & 3);
|
|
}
|
|
else
|
|
for (int c = 0; c < 4; c++) { mvals[c] = trueVal; vvals[c] = 0; }
|
|
}
|
|
|
|
// Scalar comparison kernel: T x T -> u8 mask. Result is 0 (false) or `trueVal` (true), trueVal read
|
|
// from TKernel::flags - 255 (default, matching cv::compare) or 1 (a numpy-style 0/1 mask). Integers
|
|
// compare directly (WT == T); f16/bf16 compare through a float WT. This is the fallback for the depths
|
|
// the SIMD kernel doesn't cover (f16/bf16/64-bit). stepx in {0,1}; dst is u8, byte step == element step.
|
|
template<typename T, typename WT, class Cmp>
|
|
static int scalarCompareKernel(const void* src0_, size_t s0y, size_t s0x,
|
|
const void* src1_, size_t s1y, size_t s1x,
|
|
const void*, size_t, size_t,
|
|
void* dst_, size_t dsty, int width, int height,
|
|
const double*, int flags, void*)
|
|
{
|
|
// LT/LE reuse the GT/GE kernel with the operands swapped (a<b == b>a, a<=b == b>=a).
|
|
if (flags & EW_KERNEL_SWAP01) { std::swap(src0_, src1_); std::swap(s0y, s1y); std::swap(s0x, s1x); }
|
|
s0y /= sizeof(T);
|
|
s1y /= sizeof(T);
|
|
CV_Assert((s0x|s1x) == 1u || (s0x|s1x) + (size_t)width == 1u);
|
|
|
|
const T* src0 = (const T*)src0_;
|
|
const T* src1 = (const T*)src1_;
|
|
uchar* dst = (uchar*)dst_;
|
|
const uchar trueVal = (flags & EW_KERNEL_MASK1) ? 1 : 255;
|
|
uchar mvals[4], vvals[4];
|
|
cmpUnpackPatch(flags, trueVal, mvals, vvals);
|
|
const bool perch = (flags & EW_CMP_PATCH) != 0; // per-channel fix-up (width=cn<=4)
|
|
CV_Assert(!perch || width <= 4); // a per-channel patch is a short-row tile
|
|
#define EW_CMP_APPLY(cond, x) (perch ? (uchar)(((cond) ? mvals[x] : 0) | vvals[x]) \
|
|
: (uchar)((cond) ? trueVal : 0))
|
|
|
|
EW_TRY_COLLAPSE(2);
|
|
for (int y = 0; y < height; y++, src0 += s0y, src1 += s1y, dst += dsty)
|
|
{
|
|
if (s0x == s1x) {
|
|
for (int x = 0; x < width; x++) dst[x] = EW_CMP_APPLY(Cmp::cmp((WT)src0[x], (WT)src1[x]), x);
|
|
}
|
|
else if (s0x == 0) {
|
|
WT a = (WT)src0[0];
|
|
for (int x = 0; x < width; x++) dst[x] = EW_CMP_APPLY(Cmp::cmp(a, (WT)src1[x]), x);
|
|
}
|
|
else {
|
|
WT b = (WT)src1[0];
|
|
for (int x = 0; x < width; x++) dst[x] = EW_CMP_APPLY(Cmp::cmp((WT)src0[x], b), x);
|
|
}
|
|
}
|
|
#undef EW_CMP_APPLY
|
|
return 0;
|
|
}
|
|
|
|
// SIMD comparison kernel: T x T -> u8 mask, for the directly-comparable depths (u8/s8/u16/s16/u32/
|
|
// s32/f32). vec(a,b) gives an all-ones/zero mask in the operand's lane type; v_reinterpret_as turns it
|
|
// into the unsigned int of the same width; cmpFuse packs to u8 and applies (rawmask & M) | V. The
|
|
// per-row SIMD body runs when an operand is contiguous or broadcast; a per-channel patch (M/V differ
|
|
// by channel) only ever arrives as a short-row (width=cn) tile and is handled there.
|
|
template<typename T, typename Vvec, typename Uvec, class Cmp>
|
|
static int vecCompareKernel(const void* src0_, size_t s0y, size_t s0x,
|
|
const void* src1_, size_t s1y, size_t s1x,
|
|
const void*, size_t, size_t,
|
|
void* dst_, size_t dsty, int width, int height,
|
|
const double*, int flags, void*)
|
|
{
|
|
// LT/LE reuse the GT/GE kernel with the operands swapped (a<b == b>a, a<=b == b>=a).
|
|
if (flags & EW_KERNEL_SWAP01) { std::swap(src0_, src1_); std::swap(s0y, s1y); std::swap(s0x, s1x); }
|
|
s0y /= sizeof(T);
|
|
s1y /= sizeof(T);
|
|
CV_Assert((s0x|s1x) == 1u || (s0x|s1x) + (size_t)width == 1u);
|
|
|
|
const T* src0 = (const T*)src0_;
|
|
const T* src1 = (const T*)src1_;
|
|
uchar* dst = (uchar*)dst_;
|
|
const uchar trueVal = (flags & EW_KERNEL_MASK1) ? 1 : 255;
|
|
uchar mvals[4], vvals[4]; // per-channel fix-up: (rawmask & m) | v
|
|
cmpUnpackPatch(flags, trueVal, mvals, vvals);
|
|
const bool perch = (flags & EW_CMP_PATCH) != 0;
|
|
CV_Assert(!perch || width <= 4); // a per-channel patch is a short-row tile
|
|
|
|
EW_TRY_COLLAPSE(2);
|
|
int y = 0;
|
|
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
|
// Short rows (2/3/4 elements): a per-channel scalar over a multi-channel image arrives as a
|
|
// (width=cn) x (height=pixels) tile with the scalar broadcast over rows (s?y==0). The per-row SIMD
|
|
// below never triggers at such a tiny width, so expand the width<=4 broadcast operand (threshold
|
|
// AND the interleaved per-channel mask/value bytes) across the lanes and compare many rows at once.
|
|
// This is also the ONLY path a per-channel patch (M/V differ by channel) reaches.
|
|
// constexpr: the short-row loads T natively (vx_load->Vvec), so it exists ONLY at native width. The
|
|
// widened f16/bf16->f32 path (sizeof(T) < Vvec lane) and 8-byte f64 fall through to per-row + scalar.
|
|
// (sizeof(T) <= 4 is part of the constexpr gate: for f64 the body would compile to nothing but
|
|
// dead stores - gcc 9 flags scbuf/bsrc/bsx as set-but-not-used there.)
|
|
if constexpr (sizeof(T) == sizeof(typename VTraits<Vvec>::lane_type) && sizeof(T) <= 4)
|
|
if (height > 1 && width <= 4 &&
|
|
((s0y == 0 && s1y == (size_t)width*s1x) || (s1y == 0 && s0y == (size_t)width*s0x)) &&
|
|
dsty == (size_t)width)
|
|
{
|
|
const int VECSZ = VTraits<Vvec>::vlanes(); // sizeof(T)-type lanes
|
|
const int VECSZ8 = VTraits<v_uint8>::vlanes(); // u8 output lanes
|
|
constexpr int MAXV8 = VTraits<v_uint8>::max_nlanes;
|
|
T scbuf[MAXV8 * 3] = {}; // interleaved threshold (elements)
|
|
uchar mbuf[MAXV8 * 3] = {}, vbuf[MAXV8 * 3] = {}; // interleaved mask / value (bytes)
|
|
// ^ the {} inits are for -Wmaybe-uninitialized only: expandScalar/decode fill every lane that
|
|
// is later read, but the compiler cannot prove it with a runtime VECSZ
|
|
const bool bc0 = (s0y == 0); // src0 is the broadcast (short) operand
|
|
const T* bsrc = bc0 ? src0 : src1; const size_t bsx = bc0 ? s0x : s1x;
|
|
const T*& src = bc0 ? src1 : src0; // the row-stepping operand (advanced below)
|
|
const size_t sy = bc0 ? s1y : s0y;
|
|
// The compare direction (scalar-first when bc0, array-first otherwise) is hoisted OUT of the row
|
|
// loop with an explicit if(bc0) - the loop is written twice so no per-iteration branch remains.
|
|
// Per branch: own unroll, threshold + interleaved M/V loaded ONCE. sizeof==1 keeps 12 vectors.
|
|
#define EW_CMPR(r, a, b) v_reinterpret_as(Cmp::vec(a, b), r) // rawmask -> sizeof(T)-byte uint
|
|
if constexpr (sizeof(T) == 1) // 3 u8 masks -> 3 u8 stores
|
|
{
|
|
const int ewidth = VECSZ8 * 3;
|
|
expandScalar(bsrc, bsx, width, scbuf, ewidth);
|
|
expandScalar(mvals, (size_t)1, width, mbuf, ewidth);
|
|
expandScalar(vvals, (size_t)1, width, vbuf, ewidth);
|
|
const int dy = ewidth / width;
|
|
Vvec sc0=vx_load(scbuf), sc1=vx_load(scbuf+VECSZ), sc2=vx_load(scbuf+VECSZ*2);
|
|
v_uint8 M0=vx_load(mbuf), M1=vx_load(mbuf+VECSZ8), M2=vx_load(mbuf+VECSZ8*2);
|
|
v_uint8 V0=vx_load(vbuf), V1=vx_load(vbuf+VECSZ8), V2=vx_load(vbuf+VECSZ8*2);
|
|
#define EW_ROW1(A0,B0,A1,B1,A2,B2) \
|
|
for (; y + dy <= height; y += dy, src += sy*dy, dst += dsty*dy) { \
|
|
Uvec r0,r1,r2; EW_CMPR(r0,A0,B0); EW_CMPR(r1,A1,B1); EW_CMPR(r2,A2,B2); \
|
|
v_uint8 o0=v_or(v_and(r0,M0),V0),o1=v_or(v_and(r1,M1),V1),o2=v_or(v_and(r2,M2),V2); \
|
|
v_store(dst, o0); v_store(dst+VECSZ8, o1); v_store(dst+VECSZ8*2, o2); }
|
|
if (bc0) EW_ROW1(sc0,vx_load(src), sc1,vx_load(src+VECSZ), sc2,vx_load(src+VECSZ*2))
|
|
else EW_ROW1(vx_load(src),sc0, vx_load(src+VECSZ),sc1, vx_load(src+VECSZ*2),sc2)
|
|
#undef EW_ROW1
|
|
}
|
|
else if constexpr (sizeof(T) == 2) // 3 u16 masks -> 1 full + 1 half u8 store
|
|
{
|
|
const int ewidth = VECSZ * 3;
|
|
expandScalar(bsrc, bsx, width, scbuf, ewidth);
|
|
expandScalar(mvals, (size_t)1, width, mbuf, ewidth);
|
|
expandScalar(vvals, (size_t)1, width, vbuf, ewidth);
|
|
const int dy = ewidth / width;
|
|
Vvec sc0=vx_load(scbuf), sc1=vx_load(scbuf+VECSZ), sc2=vx_load(scbuf+VECSZ*2);
|
|
v_uint8 M0=vx_load(mbuf), M1=vx_load(mbuf+VECSZ8);
|
|
v_uint8 V0=vx_load(vbuf), V1=vx_load(vbuf+VECSZ8);
|
|
#define EW_ROW2(A0,B0,A1,B1,A2,B2) \
|
|
for (; y + dy <= height; y += dy, src += sy*dy, dst += dsty*dy) { \
|
|
Uvec r0,r1,r2; EW_CMPR(r0,A0,B0); EW_CMPR(r1,A1,B1); EW_CMPR(r2,A2,B2); \
|
|
v_uint8 b0=v_pack(r0,r1), b1=v_pack(r2,r2); \
|
|
v_uint8 o0=v_or(v_and(b0,M0),V0), o1=v_or(v_and(b1,M1),V1); \
|
|
v_store(dst, o0); v_store_low(dst+VECSZ8, o1); }
|
|
if (bc0) EW_ROW2(sc0,vx_load(src), sc1,vx_load(src+VECSZ), sc2,vx_load(src+VECSZ*2))
|
|
else EW_ROW2(vx_load(src),sc0, vx_load(src+VECSZ),sc1, vx_load(src+VECSZ*2),sc2)
|
|
#undef EW_ROW2
|
|
}
|
|
else if constexpr (sizeof(T) == 4) // sizeof==4: 6 u32 -> 3 u16 -> 1 full + 1 half u8
|
|
{
|
|
const int ewidth = VECSZ * 6;
|
|
expandScalar(bsrc, bsx, width, scbuf, ewidth);
|
|
expandScalar(mvals, (size_t)1, width, mbuf, ewidth);
|
|
expandScalar(vvals, (size_t)1, width, vbuf, ewidth);
|
|
const int dy = ewidth / width;
|
|
Vvec sc0=vx_load(scbuf),sc1=vx_load(scbuf+VECSZ),sc2=vx_load(scbuf+VECSZ*2),
|
|
sc3=vx_load(scbuf+VECSZ*3),sc4=vx_load(scbuf+VECSZ*4),sc5=vx_load(scbuf+VECSZ*5);
|
|
v_uint8 M0=vx_load(mbuf), M1=vx_load(mbuf+VECSZ8);
|
|
v_uint8 V0=vx_load(vbuf), V1=vx_load(vbuf+VECSZ8);
|
|
#define EW_ROW4(A0,B0,A1,B1,A2,B2,A3,B3,A4,B4,A5,B5) \
|
|
for (; y + dy <= height; y += dy, src += sy*dy, dst += dsty*dy) { \
|
|
Uvec r0,r1,r2,r3,r4,r5; \
|
|
EW_CMPR(r0,A0,B0); EW_CMPR(r1,A1,B1); EW_CMPR(r2,A2,B2); \
|
|
EW_CMPR(r3,A3,B3); EW_CMPR(r4,A4,B4); EW_CMPR(r5,A5,B5); \
|
|
v_uint16 p0=v_pack(r0,r1), p1=v_pack(r2,r3), p2=v_pack(r4,r5); \
|
|
v_uint8 q0=v_pack(p0,p1), q1=v_pack(p2,p2); \
|
|
v_uint8 o0=v_or(v_and(q0,M0),V0), o1=v_or(v_and(q1,M1),V1); \
|
|
v_store(dst, o0); v_store_low(dst+VECSZ8, o1); }
|
|
if (bc0) EW_ROW4(sc0,vx_load(src),sc1,vx_load(src+VECSZ),sc2,vx_load(src+VECSZ*2),
|
|
sc3,vx_load(src+VECSZ*3),sc4,vx_load(src+VECSZ*4),sc5,vx_load(src+VECSZ*5))
|
|
else EW_ROW4(vx_load(src),sc0,vx_load(src+VECSZ),sc1,vx_load(src+VECSZ*2),sc2,
|
|
vx_load(src+VECSZ*3),sc3,vx_load(src+VECSZ*4),sc4,vx_load(src+VECSZ*5),sc5)
|
|
#undef EW_ROW4
|
|
}
|
|
#undef EW_CMPR
|
|
}
|
|
#endif
|
|
for (; y < height; y++, src0 += s0y, src1 += s1y, dst += dsty)
|
|
{
|
|
int x = 0;
|
|
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
|
// SIMD for BOTH the contiguous case AND a broadcast operand (a per-channel scalar const has
|
|
// step 0 - without this the scalar-vs-array compare, incl. every multi-channel scalar compare,
|
|
// fell to the scalar tail below, ~5x slower). vx_setall broadcasts the step-0 operand once.
|
|
// 4-vector unroll + halide right-edge backoff (reprocess the last 4*VECSZ when width is not a
|
|
// multiple); dst is a separate mask buffer so the overlap is a harmless idempotent rewrite.
|
|
const int VECSZ = VTraits<Vvec>::vlanes();
|
|
const int VECSZ8 = VTraits<v_uint8>::vlanes();
|
|
v_uint8 vT; v_setall_mask(vT, trueVal); // uniform mask (per-channel patch is short-row)
|
|
const bool tailTrick = width >= 4*VECSZ && src0_ != dst_ && src1_ != dst_;
|
|
// Narrow the 4 masks to u8 FIRST, then apply trueVal on the u8 result (1 and per u8 vector, not
|
|
// per wide lane-group). One block per MASK lane width (== sizeof(T) natively, but 4 for the
|
|
// f16/bf16->f32 widened path); a 4-vector unroll spans exactly 4*VECSZ elements.
|
|
constexpr size_t MW = sizeof(typename VTraits<Uvec>::lane_type);
|
|
#define PACK_STORE_CMP_RESULT(m0,m1,m2,m3, D) do { \
|
|
if constexpr (MW == 1u) { \
|
|
v_uint8 o0=v_and(m0,vT),o1=v_and(m1,vT),o2=v_and(m2,vT),o3=v_and(m3,vT); \
|
|
v_store((D),o0); v_store((D)+VECSZ8,o1); v_store((D)+VECSZ8*2,o2); v_store((D)+VECSZ8*3,o3); \
|
|
} else if constexpr (MW == 2u) { \
|
|
v_uint8 o0=v_and(v_pack(m0,m1),vT), o1=v_and(v_pack(m2,m3),vT); \
|
|
v_store((D),o0); v_store((D)+VECSZ8,o1); \
|
|
} else if constexpr (MW == 4u) { \
|
|
v_uint8 o0=v_and(v_pack(v_pack(m0,m1),v_pack(m2,m3)),vT); \
|
|
v_store((D),o0); \
|
|
} else { /* sizeof==8: 4 u64 -> 2 u32 -> 1 u16 -> u8 (low half), 4*VECSZ elems -> store_low */ \
|
|
v_uint16 g=v_pack(v_pack(m0,m1),v_pack(m2,m3)); \
|
|
v_uint8 o0=v_and(v_pack(g,g),vT); \
|
|
v_store_low((D),o0); \
|
|
} } while(0)
|
|
if (s0x == 1u && s1x == 1u)
|
|
{
|
|
for (; x < width; x += 4*VECSZ)
|
|
{
|
|
if (x + 4*VECSZ > width) { if (!tailTrick) break; x = width - 4*VECSZ; }
|
|
Vvec a0, a1, a2, a3, b0, b1, b2, b3; // vx_load_pair_as widens f16/bf16->f32, else native
|
|
vx_load_pair_as(src0+x, a0, a1); vx_load_pair_as(src0+x+2*VECSZ, a2, a3);
|
|
vx_load_pair_as(src1+x, b0, b1); vx_load_pair_as(src1+x+2*VECSZ, b2, b3);
|
|
Uvec m0, m1, m2, m3;
|
|
v_reinterpret_as(Cmp::vec(a0, b0), m0); v_reinterpret_as(Cmp::vec(a1, b1), m1);
|
|
v_reinterpret_as(Cmp::vec(a2, b2), m2); v_reinterpret_as(Cmp::vec(a3, b3), m3);
|
|
PACK_STORE_CMP_RESULT(m0, m1, m2, m3, dst+x);
|
|
}
|
|
}
|
|
else if (s1x == 0u) // src1 (e.g. the scalar) broadcast
|
|
{
|
|
Vvec b0; vx_setall_as(src1, b0);
|
|
for (; x < width; x += 4*VECSZ)
|
|
{
|
|
if (x + 4*VECSZ > width) { if (!tailTrick) break; x = width - 4*VECSZ; }
|
|
Vvec a0, a1, a2, a3;
|
|
vx_load_pair_as(src0+x, a0, a1); vx_load_pair_as(src0+x+2*VECSZ, a2, a3);
|
|
Uvec m0, m1, m2, m3;
|
|
v_reinterpret_as(Cmp::vec(a0, b0), m0); v_reinterpret_as(Cmp::vec(a1, b0), m1);
|
|
v_reinterpret_as(Cmp::vec(a2, b0), m2); v_reinterpret_as(Cmp::vec(a3, b0), m3);
|
|
PACK_STORE_CMP_RESULT(m0, m1, m2, m3, dst+x);
|
|
}
|
|
}
|
|
else if (s0x == 0u) // src0 broadcast
|
|
{
|
|
Vvec a0; vx_setall_as(src0, a0);
|
|
for (; x < width; x += 4*VECSZ)
|
|
{
|
|
if (x + 4*VECSZ > width) { if (!tailTrick) break; x = width - 4*VECSZ; }
|
|
Vvec b0, b1, b2, b3;
|
|
vx_load_pair_as(src1+x, b0, b1); vx_load_pair_as(src1+x+2*VECSZ, b2, b3);
|
|
Uvec m0, m1, m2, m3;
|
|
v_reinterpret_as(Cmp::vec(a0, b0), m0); v_reinterpret_as(Cmp::vec(a0, b1), m1);
|
|
v_reinterpret_as(Cmp::vec(a0, b2), m2); v_reinterpret_as(Cmp::vec(a0, b3), m3);
|
|
PACK_STORE_CMP_RESULT(m0, m1, m2, m3, dst+x);
|
|
}
|
|
}
|
|
#undef PACK_STORE_CMP_RESULT
|
|
#endif
|
|
// Fold the fix-up: raw all-ones/zero mask -> (raw & M) | V. Uniform => raw & trueVal (V=0); a
|
|
// per-channel patch (width=cn<=4 here) indexes M/V by the channel x.
|
|
#define EW_CMP_APPLY(cond, x) (perch ? (uchar)(((cond) ? mvals[x] : 0) | vvals[x]) \
|
|
: (uchar)((cond) ? trueVal : 0))
|
|
if (s0x == s1x) {
|
|
for (; x < width; x++) dst[x] = EW_CMP_APPLY(Cmp::cmp(src0[x], src1[x]), x);
|
|
}
|
|
else if (s0x == 0) {
|
|
T a = src0[0];
|
|
for (; x < width; x++) dst[x] = EW_CMP_APPLY(Cmp::cmp(a, src1[x]), x);
|
|
}
|
|
else {
|
|
T b = src1[0];
|
|
for (; x < width; x++) dst[x] = EW_CMP_APPLY(Cmp::cmp(src0[x], b), x);
|
|
}
|
|
#undef EW_CMP_APPLY
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// Unified binary kernel: T0 x T1 -> Tr (operands same depth for arithmetic; cast is separate).
|
|
// Wvec = work vector. Native (v_uint8/...) drives the same-type saturating path
|
|
// (v_add saturates 8/16-bit, wraps 32-bit); v_float32 drives the widening hub.
|
|
// WT = scalar work type for the tail.
|
|
// Op = operation functor (vec()/scl()).
|
|
// use_simd = compile-time switch; false => pure scalar (32/64-bit widened outputs, f64).
|
|
// stepx in {0,1}; dst contiguous. In-place safe (see file header).
|
|
template<typename T, typename Tr, typename Wvec, typename WT, class Op, typename ST=WT, typename Wvec1=Wvec>
|
|
static int vecBinaryKernel(const void* src0_, size_t s0y, size_t s0x,
|
|
const void* src1_, size_t s1y, size_t s1x,
|
|
const void*, size_t, size_t,
|
|
void* dst_, size_t dsty, int width, int height,
|
|
const double* params, int, void*)
|
|
{
|
|
s0y /= sizeof(T);
|
|
s1y /= sizeof(T);
|
|
dsty /= sizeof(Tr);
|
|
|
|
CV_Assert((s0x|s1x) == 1u || (s0x|s1x) + (size_t)width == 1u);
|
|
|
|
const T* src0 = (const T*)src0_;
|
|
const T* src1 = (const T*)src1_;
|
|
Tr* dst = (Tr*)dst_;
|
|
[[maybe_unused]] ST scalar = saturate_cast<ST>(params[0]); // mul/div scale; ignored by add/sub
|
|
|
|
EW_TRY_COLLAPSE(2);
|
|
int y = 0;
|
|
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
|
using Wlane = typename VTraits<Wvec>::lane_type;
|
|
const int VECSZ = VTraits<Wvec>::vlanes();
|
|
const bool use_tail_trick = width >= VECSZ*4 && src0_ != dst_ && src1_ != dst_;
|
|
[[maybe_unused]] Wvec vscalar;
|
|
vx_setall_as(&scalar, vscalar);
|
|
if (height > 1 && width <= 4 &&
|
|
((s0y == 0 && s1y == width*s1x) ||
|
|
(s1y == 0 && s0y == width*s0x)) &&
|
|
dsty == (size_t)width) {
|
|
constexpr int MAXVECSZ = VTraits<Wvec>::max_nlanes;
|
|
Wlane scbuf[MAXVECSZ*6] = {}; // {} for -Wmaybe-uninitialized only (filled up to VECSZ*6)
|
|
const int ewidth = VECSZ*6;
|
|
expandScalar(s0y == 0 ? src0 : src1, s0y == 0 ? s0x : s1x, width, scbuf, ewidth);
|
|
int dy = ewidth / width;
|
|
Wvec sc0, sc1, sc2, sc3, sc4, sc5;
|
|
sc0 = Op::preproc(vx_load(scbuf), vscalar);
|
|
sc1 = Op::preproc(vx_load(scbuf + VECSZ), vscalar);
|
|
sc2 = Op::preproc(vx_load(scbuf + VECSZ*2), vscalar);
|
|
sc3 = Op::preproc(vx_load(scbuf + VECSZ*3), vscalar);
|
|
sc4 = Op::preproc(vx_load(scbuf + VECSZ*4), vscalar);
|
|
sc5 = Op::preproc(vx_load(scbuf + VECSZ*5), vscalar);
|
|
|
|
if (s0y == 0) {
|
|
for (; y + dy <= height; y += dy, src1 += s1y*dy, dst += dsty*dy) {
|
|
Wvec v0, v1, v2, v3, v4, v5;
|
|
vx_load_pair_as(src1, v0, v1);
|
|
vx_load_pair_as(src1 + VECSZ*2, v2, v3);
|
|
vx_load_pair_as(src1 + VECSZ*4, v4, v5);
|
|
auto w0 = Op::vec(sc0, v0, vscalar), w1 = Op::vec(sc1, v1, vscalar),
|
|
w2 = Op::vec(sc2, v2, vscalar), w3 = Op::vec(sc3, v3, vscalar),
|
|
w4 = Op::vec(sc4, v4, vscalar), w5 = Op::vec(sc5, v5, vscalar);
|
|
v_store_pair_as(dst, w0, w1);
|
|
v_store_pair_as(dst + VECSZ*2, w2, w3);
|
|
v_store_pair_as(dst + VECSZ*4, w4, w5);
|
|
}
|
|
}
|
|
else {
|
|
for (; y + dy <= height; y += dy, src0 += s0y*dy, dst += dsty*dy) {
|
|
Wvec v0, v1, v2, v3, v4, v5;
|
|
vx_load_pair_as(src0, v0, v1);
|
|
vx_load_pair_as(src0 + VECSZ*2, v2, v3);
|
|
vx_load_pair_as(src0 + VECSZ*4, v4, v5);
|
|
auto w0 = Op::vec(v0, sc0, vscalar), w1 = Op::vec(v1, sc1, vscalar),
|
|
w2 = Op::vec(v2, sc2, vscalar), w3 = Op::vec(v3, sc3, vscalar),
|
|
w4 = Op::vec(v4, sc4, vscalar), w5 = Op::vec(v5, sc5, vscalar);
|
|
v_store_pair_as(dst, w0, w1);
|
|
v_store_pair_as(dst + VECSZ*2, w2, w3);
|
|
v_store_pair_as(dst + VECSZ*4, w4, w5);
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
for (; y < height; y++, src0 += s0y, src1 += s1y, dst += dsty)
|
|
{
|
|
int x = 0;
|
|
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
|
Wvec a0, a1, a2, a3, b0, b1, b2, b3;
|
|
if (s0x == s1x) {
|
|
if (!Op::useScalar || scalar == ST(1)) {
|
|
// this branch runs on Wvec1, which may be WIDER-laned than Wvec (e.g. the u8 mul
|
|
// path: Wvec1=v_uint16 is 2x the lanes of Wvec=v_float32) - step by ITS lane count,
|
|
// or the pairs overlap and the tail backoff writes past the row end
|
|
const int VECSZ1 = VTraits<Wvec1>::vlanes();
|
|
const bool tail_trick1 = width >= VECSZ1*4 && src0_ != dst_ && src1_ != dst_;
|
|
for (; x < width; x += VECSZ1*4) {
|
|
Wvec1 a0_, a1_, a2_, a3_, b0_, b1_, b2_, b3_;
|
|
if (x + VECSZ1*4 > width) { if (!tail_trick1) break; x = width - VECSZ1*4; }
|
|
vx_load_pair_as(src0 + x, a0_, a1_);
|
|
vx_load_pair_as(src0 + x + VECSZ1*2, a2_, a3_);
|
|
vx_load_pair_as(src1 + x, b0_, b1_);
|
|
vx_load_pair_as(src1 + x + VECSZ1*2, b2_, b3_);
|
|
auto c0 = Op::vec(a0_, b0_), c1 = Op::vec(a1_, b1_),
|
|
c2 = Op::vec(a2_, b2_), c3 = Op::vec(a3_, b3_);
|
|
v_store_pair_as(dst + x, c0, c1);
|
|
v_store_pair_as(dst + x + VECSZ1*2, c2, c3);
|
|
}
|
|
}
|
|
else {
|
|
for (; x < width; x += VECSZ*4) {
|
|
if (x + VECSZ*4 > width) { if (!use_tail_trick) break; x = width - VECSZ*4; }
|
|
vx_load_pair_as(src0 + x, a0, a1);
|
|
vx_load_pair_as(src0 + x + VECSZ*2, a2, a3);
|
|
vx_load_pair_as(src1 + x, b0, b1);
|
|
vx_load_pair_as(src1 + x + VECSZ*2, b2, b3);
|
|
auto c0 = Op::vec(Op::preproc(a0, vscalar), b0, vscalar),
|
|
c1 = Op::vec(Op::preproc(a1, vscalar), b1, vscalar),
|
|
c2 = Op::vec(Op::preproc(a2, vscalar), b2, vscalar),
|
|
c3 = Op::vec(Op::preproc(a3, vscalar), b3, vscalar);
|
|
v_store_pair_as(dst + x, c0, c1);
|
|
v_store_pair_as(dst + x + VECSZ*2, c2, c3);
|
|
}
|
|
}
|
|
}
|
|
else if (s1x == 0) {
|
|
vx_setall_as(src1, b0);
|
|
b0 = Op::preproc(b0, vscalar);
|
|
for (; x < width; x += VECSZ*4) {
|
|
if (x + VECSZ*4 > width) { if (!use_tail_trick) break; x = width - VECSZ*4; }
|
|
vx_load_pair_as(src0 + x, a0, a1);
|
|
vx_load_pair_as(src0 + x + VECSZ*2, a2, a3);
|
|
auto c0 = Op::vec(a0, b0, vscalar), c1 = Op::vec(a1, b0, vscalar),
|
|
c2 = Op::vec(a2, b0, vscalar), c3 = Op::vec(a3, b0, vscalar);
|
|
v_store_pair_as(dst + x, c0, c1);
|
|
v_store_pair_as(dst + x + VECSZ*2, c2, c3);
|
|
}
|
|
}
|
|
else {
|
|
vx_setall_as(src0, b0);
|
|
b0 = Op::preproc(b0, vscalar);
|
|
for (; x < width; x += VECSZ*4) {
|
|
if (x + VECSZ*4 > width) { if (!use_tail_trick) break; x = width - VECSZ*4; }
|
|
vx_load_pair_as(src1 + x, a0, a1);
|
|
vx_load_pair_as(src1 + x + VECSZ*2, a2, a3);
|
|
auto c0 = Op::vec(b0, a0, vscalar), c1 = Op::vec(b0, a1, vscalar),
|
|
c2 = Op::vec(b0, a2, vscalar), c3 = Op::vec(b0, a3, vscalar);
|
|
v_store_pair_as(dst + x, c0, c1);
|
|
v_store_pair_as(dst + x + VECSZ*2, c2, c3);
|
|
}
|
|
}
|
|
#endif
|
|
for (; x < width; x++)
|
|
dst[x] = saturate_cast<Tr>(Op::scl((WT)src0[x*s0x], (WT)src1[x*s1x], scalar));
|
|
}
|
|
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
|
vx_cleanup();
|
|
#endif
|
|
return 0;
|
|
}
|
|
|
|
// ===========================================================================
|
|
// OP_ADD
|
|
// ===========================================================================
|
|
|
|
// add dispatch: T x T -> R (operands already same depth T).
|
|
// - native saturating path (Wvec = native, use_simd=true) for T -> T on <=32-bit ints + f32
|
|
// (32-bit lanes via the local v_add_sat/v_sub_sat - v_add/v_sub wrap there);
|
|
// - widening f32-hub (Wvec = v_float32, use_simd=true) for the widened/half/float outputs;
|
|
// - scalar (use_simd=false) for 64-bit results and f64 (no vector path yet).
|
|
template<class Op>
|
|
TKernel getAddSubFunc(int T, int R)
|
|
{
|
|
KernelFunc fptr = nullptr;
|
|
switch (T)
|
|
{
|
|
case CV_8U:
|
|
fptr = R == CV_8U ? vecBinaryKernel<uchar, uchar, v_uint8, short, Op, uchar> :
|
|
R == CV_16S ? vecBinaryKernel<uchar, short, v_int16, short, Op, short> :
|
|
R == CV_32S ? vecBinaryKernel<uchar, int, v_int16, short, Op, short> :
|
|
R == CV_32F ? vecBinaryKernel<uchar, float, v_int16, short, Op, short> : nullptr;
|
|
break;
|
|
case CV_8S:
|
|
fptr =
|
|
R == CV_8S ? vecBinaryKernel<schar, schar, v_int8, short, Op, schar> :
|
|
R == CV_16S ? vecBinaryKernel<schar, short, v_int16, short, Op, short> :
|
|
R == CV_32S ? vecBinaryKernel<schar, int, v_int16, short, Op, short> :
|
|
R == CV_32F ? vecBinaryKernel<schar, float, v_int16, short, Op, short> : nullptr;
|
|
break;
|
|
case CV_16U:
|
|
fptr = R == CV_16U ? vecBinaryKernel<ushort, ushort, v_uint16, int, Op, ushort> :
|
|
R == CV_32S ? vecBinaryKernel<ushort, int, v_int32, int, Op, int> :
|
|
R == CV_32F ? vecBinaryKernel<ushort, float, v_int32, int, Op, int> : nullptr;
|
|
break;
|
|
case CV_16S:
|
|
fptr = R == CV_16S ? vecBinaryKernel<short, short, v_int16, int, Op, short> :
|
|
R == CV_32S ? vecBinaryKernel<short, int, v_int32, int, Op, int> :
|
|
R == CV_32F ? vecBinaryKernel<short, float, v_int32, int, Op, int> : nullptr;
|
|
break;
|
|
case CV_32U:
|
|
fptr = R == CV_32U ? vecBinaryKernel<unsigned, unsigned, v_uint32, int64_t, Op, unsigned> :
|
|
R == CV_64S ? scalarBinaryKernel<unsigned, int64_t, int64_t, Op> :
|
|
R == CV_64F ? scalarBinaryKernel<unsigned, double, int64_t, Op> : nullptr;
|
|
break;
|
|
case CV_32S:
|
|
fptr = R == CV_32S ? vecBinaryKernel<int, int, v_int32, int64_t, Op, int> :
|
|
R == CV_64S ? scalarBinaryKernel<int, int64_t, int64_t, Op> :
|
|
R == CV_64F ? scalarBinaryKernel<int, double, int64_t, Op> : nullptr;
|
|
break;
|
|
case CV_64U:
|
|
fptr = R == CV_64U ? scalarBinaryKernel<uint64_t, uint64_t, uint64_t, Op> :
|
|
R == CV_64F ? scalarBinaryKernel<uint64_t, double, double, Op> : nullptr;
|
|
break;
|
|
case CV_64S:
|
|
fptr = R == CV_64S ? scalarBinaryKernel<int64_t, int64_t, int64_t, Op> :
|
|
R == CV_64F ? scalarBinaryKernel<int64_t, double, double, Op> : nullptr;
|
|
break;
|
|
case CV_16F:
|
|
fptr =
|
|
#if CV_SIMD_16F
|
|
R == CV_16F ? vecBinaryKernel<hfloat, hfloat, v_float16, float, Op, hfloat> :
|
|
#else
|
|
R == CV_16F ? vecBinaryKernel<hfloat, hfloat, v_float32, float, Op, float> :
|
|
#endif
|
|
R == CV_32F ? vecBinaryKernel<hfloat, float, v_float32, float, Op, float> : nullptr;
|
|
break;
|
|
case CV_16BF:
|
|
fptr =
|
|
R == CV_16BF ? vecBinaryKernel<bfloat, bfloat, v_float32, float, Op, float> :
|
|
R == CV_32F ? vecBinaryKernel<bfloat, float, v_float32, float, Op, float> : nullptr;
|
|
break;
|
|
case CV_32F:
|
|
fptr = R == CV_32F ? vecBinaryKernel<float, float, v_float32, float, Op, float> : nullptr;
|
|
break;
|
|
case CV_64F:
|
|
#if CV_SIMD_64F
|
|
fptr = R == CV_64F ? vecBinaryKernel<double, double, v_float64, double, Op> : nullptr;
|
|
#else
|
|
fptr = R == CV_64F ? scalarBinaryKernel<double, double, double, Op> : nullptr;
|
|
#endif
|
|
break;
|
|
default:
|
|
;
|
|
}
|
|
return {fptr, nullptr, 0};
|
|
}
|
|
|
|
TKernel getMulFunc_(int T, int R)
|
|
{
|
|
KernelFunc fptr = nullptr;
|
|
switch (T)
|
|
{
|
|
case CV_8U:
|
|
fptr = // scale==1 fast path: whole u8 registers, v_mul_sat widens+clamps inside;
|
|
// Wvec (f16 where available, else f32) for the scale path
|
|
#if CV_SIMD_16F
|
|
R == CV_8U ? vecBinaryKernel<uchar, uchar, v_float16, float, EwMul, float, v_uint8> :
|
|
#else
|
|
R == CV_8U ? vecBinaryKernel<uchar, uchar, v_float32, float, EwMul, float, v_uint8> :
|
|
#endif
|
|
R == CV_32F ? vecBinaryKernel<uchar, float, v_float32, float, EwMul> : nullptr;
|
|
break;
|
|
case CV_8S:
|
|
fptr = // scale==1 fast path: whole s8 registers via v_mul_sat
|
|
#if CV_SIMD_16F
|
|
R == CV_8S ? vecBinaryKernel<schar, schar, v_float16, float, EwMul, float, v_int8> :
|
|
#else
|
|
R == CV_8S ? vecBinaryKernel<schar, schar, v_float32, float, EwMul, float, v_int8> :
|
|
#endif
|
|
R == CV_32F ? vecBinaryKernel<schar, float, v_float32, float, EwMul> : nullptr;
|
|
break;
|
|
case CV_16U:
|
|
fptr = // scale==1 fast path: whole u16 registers via v_mul_sat; Wvec=f32 for scale
|
|
R == CV_16U ? vecBinaryKernel<ushort, ushort, v_float32, float, EwMul, float, v_uint16> :
|
|
R == CV_32F ? vecBinaryKernel<ushort, float, v_float32, float, EwMul> : nullptr;
|
|
break;
|
|
case CV_16S:
|
|
fptr = // scale==1 fast path: whole s16 registers via v_mul_sat; Wvec=f32 for scale
|
|
R == CV_16S ? vecBinaryKernel<short, short, v_float32, float, EwMul, float, v_int16> :
|
|
R == CV_32F ? vecBinaryKernel<short, float, v_float32, float, EwMul> : nullptr;
|
|
break;
|
|
case CV_16F:
|
|
fptr =
|
|
R == CV_16F ? vecBinaryKernel<hfloat, hfloat, v_float32, float, EwMul> :
|
|
R == CV_32F ? vecBinaryKernel<hfloat, float, v_float32, float, EwMul> : nullptr;
|
|
break;
|
|
case CV_16BF:
|
|
fptr =
|
|
R == CV_16BF ? vecBinaryKernel<bfloat, bfloat, v_float32, float, EwMul> :
|
|
R == CV_32F ? vecBinaryKernel<bfloat, float, v_float32, float, EwMul> : nullptr;
|
|
break;
|
|
case CV_32F:
|
|
fptr = R == CV_32F ? vecBinaryKernel<float, float, v_float32, float, EwMul> : nullptr;
|
|
break;
|
|
case CV_32U:
|
|
fptr = // scale==1 fast path (where v_mul_sat32 exists): whole u32 registers, widening
|
|
// multiply + saturating narrow; the f64 work vector serves the scale path
|
|
#if defined(EW_HAVE_MULSAT32) && CV_SIMD_64F
|
|
R == CV_32U ? vecBinaryKernel<unsigned, unsigned, v_float64, double, EwMul, double, v_uint32> :
|
|
R == CV_64F ? vecBinaryKernel<unsigned, double, v_float64, double, EwMul> : nullptr;
|
|
#elif CV_SIMD_64F
|
|
R == CV_32U ? vecBinaryKernel<unsigned, unsigned, v_float64, double, EwMul> :
|
|
R == CV_64F ? vecBinaryKernel<unsigned, double, v_float64, double, EwMul> : nullptr;
|
|
#else
|
|
R == CV_32U ? scalarBinaryKernel<unsigned, unsigned, double, EwMul> :
|
|
R == CV_64F ? scalarBinaryKernel<unsigned, double, double, EwMul> : nullptr;
|
|
#endif
|
|
break;
|
|
case CV_32S:
|
|
fptr = // scale==1 fast path: see CV_32U
|
|
#if defined(EW_HAVE_MULSAT32) && CV_SIMD_64F
|
|
R == CV_32S ? vecBinaryKernel<int, int, v_float64, double, EwMul, double, v_int32> :
|
|
R == CV_64F ? vecBinaryKernel<int, double, v_float64, double, EwMul> : nullptr;
|
|
#elif CV_SIMD_64F
|
|
R == CV_32S ? vecBinaryKernel<int, int, v_float64, double, EwMul> :
|
|
R == CV_64F ? vecBinaryKernel<int, double, v_float64, double, EwMul> : nullptr;
|
|
#else
|
|
R == CV_32S ? scalarBinaryKernel<int, int, double, EwMul> :
|
|
R == CV_64F ? scalarBinaryKernel<int, double, double, EwMul> : nullptr;
|
|
#endif
|
|
break;
|
|
case CV_64U:
|
|
fptr = R == CV_64F ? scalarBinaryKernel<uint64_t, double, double, EwMul> : nullptr;
|
|
break;
|
|
case CV_64S:
|
|
fptr = R == CV_64F ? scalarBinaryKernel<int64_t, double, double, EwMul> : nullptr;
|
|
break;
|
|
case CV_64F:
|
|
#if CV_SIMD_64F
|
|
fptr = R == CV_64F ? vecBinaryKernel<double, double, v_float64, double, EwMul> : nullptr;
|
|
#else
|
|
fptr = R == CV_64F ? scalarBinaryKernel<double, double, double, EwMul> : nullptr;
|
|
#endif
|
|
break;
|
|
default:
|
|
;
|
|
}
|
|
return {fptr, nullptr, 0};
|
|
}
|
|
|
|
// `checked` (decided by the CALLER from the ORIGINAL input types) selects the divide-by-zero
|
|
// policy, INDEPENDENT of the work type R: EwDivInt guards /0 -> 0 for integer-semantics division,
|
|
// EwDivFlt does not (a/0 -> inf, saturating on a later cast, as cv::divide does for float inputs).
|
|
// It must apply on EVERY row - two wide integers (e.g. 32U / 64S) promote to a 64F work type yet
|
|
// still need the integer guard, so the float-work rows can't hardcode EwDivFlt.
|
|
TKernel getDivFunc_(int T, int R, bool checked)
|
|
{
|
|
KernelFunc fptr = nullptr;
|
|
// SIMD via vecBinaryKernel: div scales the numerator in vec (a*scale/b), EwDivFlt (float a/0 -> inf,
|
|
// saturates on cast) / EwDivInt (v_select guards b==0 -> 0 post-facto). Prefer a DIRECT T->T kernel
|
|
// (1 pass, saturate on store) - emitBinary probes result==T first; a T->f32/f64 kernel serves an
|
|
// explicit float dtype. Work type: f16 for 8-bit (CV_SIMD_16F), else f32; f64 for 32/64-bit.
|
|
#define DIV(T_, Tr_, Wv_, W_) (checked ? vecBinaryKernel<T_, Tr_, Wv_, W_, EwDivInt> \
|
|
: vecBinaryKernel<T_, Tr_, Wv_, W_, EwDivFlt>)
|
|
#if CV_SIMD_16F
|
|
#define DIV8(T_) DIV(T_, T_, v_float16, float) // 8-bit T->T, f16 work
|
|
#else
|
|
#define DIV8(T_) DIV(T_, T_, v_float32, float)
|
|
#endif
|
|
#if CV_SIMD_64F
|
|
#define DIVW(T_, Tr_) DIV(T_, Tr_, v_float64, double) // 32/64-bit, f64 SIMD
|
|
#else
|
|
#define DIVW(T_, Tr_) (checked ? scalarBinaryKernel<T_, Tr_, double, EwDivInt> \
|
|
: scalarBinaryKernel<T_, Tr_, double, EwDivFlt>)
|
|
#endif
|
|
switch (T)
|
|
{
|
|
case CV_8U: fptr = R==CV_8U ? DIV8(uchar) : R==CV_32F ? DIV(uchar, float, v_float32, float) : nullptr; break;
|
|
case CV_8S: fptr = R==CV_8S ? DIV8(schar) : R==CV_32F ? DIV(schar, float, v_float32, float) : nullptr; break;
|
|
case CV_16U: fptr = R==CV_16U ? DIV(ushort, ushort, v_float32, float) : R==CV_32F ? DIV(ushort, float, v_float32, float) : nullptr; break;
|
|
case CV_16S: fptr = R==CV_16S ? DIV(short, short, v_float32, float) : R==CV_32F ? DIV(short, float, v_float32, float) : nullptr; break;
|
|
case CV_16F: fptr = R==CV_16F ? DIV(hfloat, hfloat, v_float32, float) : R==CV_32F ? DIV(hfloat, float, v_float32, float) : nullptr; break;
|
|
case CV_16BF: fptr = R==CV_16BF? DIV(bfloat, bfloat, v_float32, float) : R==CV_32F ? DIV(bfloat, float, v_float32, float) : nullptr; break;
|
|
case CV_32F: fptr = R==CV_32F ? DIV(float, float, v_float32, float) : nullptr; break;
|
|
case CV_32U: fptr = R==CV_32U ? DIVW(unsigned, unsigned) : R==CV_64F ? DIVW(unsigned, double) : nullptr; break;
|
|
case CV_32S: fptr = R==CV_32S ? DIVW(int, int) : R==CV_64F ? DIVW(int, double) : nullptr; break;
|
|
case CV_64U: fptr = R==CV_64F ? DIVW(uint64_t, double) : nullptr; break;
|
|
case CV_64S: fptr = R==CV_64F ? DIVW(int64_t, double) : nullptr; break;
|
|
case CV_64F: fptr = R==CV_64F ? DIVW(double, double) : nullptr; break;
|
|
default: ;
|
|
}
|
|
#undef DIV
|
|
#undef DIV8
|
|
#undef DIVW
|
|
return {fptr, nullptr, 0};
|
|
}
|
|
|
|
|
|
// min / max: T x T -> T for every depth. Native v_min/v_max on the matching lane type (8/16/32-bit
|
|
// ints, f16/bf16/f32); 64-bit ints and f64 use the scalar path. Op = EwMin or EwMax.
|
|
template<class Op>
|
|
static TKernel getMinMaxFunc(int T)
|
|
{
|
|
KernelFunc fptr = nullptr;
|
|
switch (T)
|
|
{
|
|
case CV_8U: fptr = vecBinaryKernel<uchar, uchar, v_uint8, short, Op, uchar>; break;
|
|
case CV_8S: fptr = vecBinaryKernel<schar, schar, v_int8, short, Op, schar>; break;
|
|
case CV_16U: fptr = vecBinaryKernel<ushort, ushort, v_uint16, int, Op, ushort>; break;
|
|
case CV_16S: fptr = vecBinaryKernel<short, short, v_int16, int, Op, short>; break;
|
|
case CV_32U: fptr = vecBinaryKernel<unsigned, unsigned, v_uint32, int64_t, Op, unsigned>; break;
|
|
case CV_32S: fptr = vecBinaryKernel<int, int, v_int32, int64_t, Op, int>; break;
|
|
case CV_16F:
|
|
#if CV_SIMD_16F
|
|
fptr = vecBinaryKernel<hfloat, hfloat, v_float16, float, Op, hfloat>;
|
|
#else
|
|
fptr = vecBinaryKernel<hfloat, hfloat, v_float32, float, Op, float>;
|
|
#endif
|
|
break;
|
|
case CV_16BF: fptr = vecBinaryKernel<bfloat, bfloat, v_float32, float, Op, float>; break;
|
|
case CV_32F: fptr = vecBinaryKernel<float, float, v_float32, float, Op, float>; break;
|
|
case CV_64U: fptr = scalarBinaryKernel<uint64_t, uint64_t, uint64_t, Op>; break;
|
|
case CV_64S: fptr = scalarBinaryKernel<int64_t, int64_t, int64_t, Op>; break;
|
|
#if CV_SIMD_64F
|
|
case CV_64F: fptr = vecBinaryKernel<double, double, v_float64, double, Op, double>; break;
|
|
#else
|
|
case CV_64F: fptr = scalarBinaryKernel<double, double, double, Op>; break;
|
|
#endif
|
|
default: ;
|
|
}
|
|
return {fptr, nullptr, 0};
|
|
}
|
|
|
|
// absdiff: |a-b|, T x T -> R.
|
|
TKernel getAbsdiffFunc_(int T, int R)
|
|
{
|
|
// Signed T has TWO outputs: R==T -> EwAbsdiffS (saturating |a-b| kept in the signed range, native work,
|
|
// ONE pass - the depth cv::absdiff keeps); R==(the unsigned type of the same width) -> EwAbsdiff (the
|
|
// true |a-b| via v_absdiff, for an explicit unsigned dst). Unsigned/float T only produce R==T. Each case
|
|
// returns nullptr for a wrong R (no separate rdepth guard).
|
|
KernelFunc fptr = nullptr;
|
|
switch (T)
|
|
{
|
|
case CV_8U: fptr = R == CV_8U ? vecBinaryKernel<uchar, uchar, v_uint8, short, EwAbsdiff, uchar> : nullptr; break;
|
|
case CV_16U: fptr = R == CV_16U ? vecBinaryKernel<ushort, ushort, v_uint16, int, EwAbsdiff, ushort> : nullptr; break;
|
|
case CV_32U: fptr = R == CV_32U ? vecBinaryKernel<unsigned, unsigned, v_uint32, int64_t, EwAbsdiff, unsigned> : nullptr; break;
|
|
case CV_8S: fptr = R == CV_8S ? vecBinaryKernel<schar, schar, v_int8, short, EwAbsdiffS, schar> :
|
|
R == CV_8U ? vecBinaryKernel<schar, uchar, v_int8, short, EwAbsdiff, schar> : nullptr; break;
|
|
case CV_16S: fptr = R == CV_16S ? vecBinaryKernel<short, short, v_int16, int, EwAbsdiffS, short> :
|
|
R == CV_16U ? vecBinaryKernel<short, ushort, v_int16, int, EwAbsdiff, short> : nullptr; break;
|
|
case CV_32S: fptr = R == CV_32S ? vecBinaryKernel<int, int, v_int32, int64_t, EwAbsdiffS, int> :
|
|
R == CV_32U ? vecBinaryKernel<int, unsigned, v_int32, int64_t, EwAbsdiff, int> : nullptr; break;
|
|
#if CV_SIMD_16F
|
|
case CV_16F: fptr = R == CV_16F ? vecBinaryKernel<hfloat, hfloat, v_float16, float, EwAbsdiff, hfloat> : nullptr; break;
|
|
#else
|
|
case CV_16F: fptr = R == CV_16F ? vecBinaryKernel<hfloat, hfloat, v_float32, float, EwAbsdiff, float> : nullptr; break;
|
|
#endif
|
|
case CV_16BF: fptr = R == CV_16BF ? vecBinaryKernel<bfloat, bfloat, v_float32, float, EwAbsdiff, float> : nullptr; break;
|
|
case CV_32F: fptr = R == CV_32F ? vecBinaryKernel<float, float, v_float32, float, EwAbsdiff, float> : nullptr; break;
|
|
case CV_64U: fptr = R == CV_64U ? scalarBinaryKernel<uint64_t, uint64_t, uint64_t, EwAbsdiff> : nullptr; break;
|
|
case CV_64S: fptr = R == CV_64U ? scalarBinaryKernel<int64_t, uint64_t, int64_t, EwAbsdiff> : nullptr; break;
|
|
#if CV_SIMD_64F
|
|
case CV_64F: fptr = R == CV_64F ? vecBinaryKernel<double, double, v_float64, double, EwAbsdiff, double> : nullptr; break;
|
|
#else
|
|
case CV_64F: fptr = R == CV_64F ? scalarBinaryKernel<double, double, double, EwAbsdiff> : nullptr; break;
|
|
#endif
|
|
default: ;
|
|
}
|
|
return {fptr, nullptr, 0};
|
|
}
|
|
|
|
// compare: T x T -> u8 mask. Directly-comparable depths (u8/s8/u16/s16/u32/s32/f32) take the SIMD
|
|
// vecCompareKernel; f16/bf16 and 64-bit depths fall back to scalarCompareKernel. The returned kernel
|
|
// defaults to a 255 mask in TKernel::flags (cv::compare-compatible); kernel.flags=1 gives a 0/1 mask.
|
|
template<class Cmp>
|
|
static KernelFunc compareByType(int T)
|
|
{
|
|
switch (T)
|
|
{
|
|
case CV_8U: return vecCompareKernel<uchar, v_uint8, v_uint8, Cmp>;
|
|
case CV_8S: return vecCompareKernel<schar, v_int8, v_uint8, Cmp>;
|
|
case CV_16U: return vecCompareKernel<ushort, v_uint16, v_uint16, Cmp>;
|
|
case CV_16S: return vecCompareKernel<short, v_int16, v_uint16, Cmp>;
|
|
case CV_32U: return vecCompareKernel<unsigned, v_uint32, v_uint32, Cmp>;
|
|
case CV_32S: return vecCompareKernel<int, v_int32, v_uint32, Cmp>;
|
|
case CV_32F: return vecCompareKernel<float, v_float32, v_uint32, Cmp>;
|
|
#if CV_SIMD_16F
|
|
case CV_16F: return vecCompareKernel<hfloat, v_float16, v_uint16, Cmp>;
|
|
#else
|
|
case CV_16F: return vecCompareKernel<hfloat, v_float32, v_uint32, Cmp>; // widen f16->f32
|
|
#endif
|
|
case CV_16BF: return vecCompareKernel<bfloat, v_float32, v_uint32, Cmp>; // widen bf16->f32 (no native)
|
|
case CV_64U: return scalarCompareKernel<uint64_t, uint64_t, Cmp>;
|
|
case CV_64S: return scalarCompareKernel<int64_t, int64_t, Cmp>;
|
|
#if CV_SIMD_64F
|
|
case CV_64F: return vecCompareKernel<double, v_float64, v_uint64, Cmp>;
|
|
#else
|
|
case CV_64F: return scalarCompareKernel<double, double, Cmp>;
|
|
#endif
|
|
default: return nullptr;
|
|
}
|
|
}
|
|
|
|
TKernel getCmpFunc_(TOp op, int T)
|
|
{
|
|
// Only 4 physical kernels (eq/ne/gt/ge): LT/LE reuse GT/GE with the operands swapped
|
|
// (a<b == b>a, a<=b == b>=a) via the EW_KERNEL_SWAP01 flag, honored by the executor.
|
|
KernelFunc f = nullptr;
|
|
int flags = 0; // mask value: 0 flag bits => 0/255 (cv::compare); EW_KERNEL_MASK1 => 0/1
|
|
switch (op)
|
|
{
|
|
case OP_CMP_EQ: f = compareByType<EwCmpEq>(T); break;
|
|
case OP_CMP_NE: f = compareByType<EwCmpNe>(T); break;
|
|
case OP_CMP_GT: f = compareByType<EwCmpGt>(T); break;
|
|
case OP_CMP_GE: f = compareByType<EwCmpGe>(T); break;
|
|
case OP_CMP_LT: f = compareByType<EwCmpGt>(T); flags = EW_KERNEL_SWAP01; break;
|
|
case OP_CMP_LE: f = compareByType<EwCmpGe>(T); flags = EW_KERNEL_SWAP01; break;
|
|
default: ;
|
|
}
|
|
return {f, nullptr, flags};
|
|
}
|
|
|
|
// ===========================================================================
|
|
// OP_AND / OP_OR / OP_XOR / OP_NOT: bitwise, type-agnostic (by element size)
|
|
// ===========================================================================
|
|
// A bit-pattern op ignores the operand's semantic type, so we run it on the UNSIGNED integer whose
|
|
// width matches the element (1/2/4/8 bytes). One functor set (EwAnd/EwOr/EwXor) times four widths
|
|
// covers every depth; the dispatchers below pick by element size. AND/OR/XOR reuse vecBinaryKernel's
|
|
// native same-type path (as min/max do); 64-bit falls to the scalar kernel. NOT is unary.
|
|
|
|
// bitwise NOT: ~x. Single operand -> always a full contiguous array (no broadcast), so just a flat
|
|
// per-row complement. SIMD for 1/2/4-byte elements; 8-byte uses the scalar tail (Vvec unused there).
|
|
template<typename T, typename Vvec>
|
|
static int notKernel(const void* src0_, size_t s0y, size_t s0x,
|
|
const void*, size_t, size_t, const void*, size_t, size_t,
|
|
void* dst_, size_t dsty, int width, int height,
|
|
const double*, int, void*)
|
|
{
|
|
s0y /= sizeof(T);
|
|
dsty /= sizeof(T);
|
|
CV_Assert(s0x == 1u || width == 1);
|
|
const T* src0 = (const T*)src0_;
|
|
T* dst = (T*)dst_;
|
|
if (height > 1 && dsty == (size_t)width && s0y == (size_t)width) { width *= height; height = 1; }
|
|
for (int y = 0; y < height; y++, src0 += s0y, dst += dsty)
|
|
{
|
|
int x = 0;
|
|
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
|
if constexpr (sizeof(T) <= 4)
|
|
{
|
|
const int VECSZ = VTraits<Vvec>::vlanes();
|
|
for (; x <= width - VECSZ; x += VECSZ)
|
|
v_store(dst + x, v_not(vx_load(src0 + x)));
|
|
}
|
|
#endif
|
|
for (; x < width; x++) dst[x] = (T)~src0[x];
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
template<class Op>
|
|
static KernelFunc bitwiseByEsz(int esz)
|
|
{
|
|
switch (esz)
|
|
{
|
|
case 1: return vecBinaryKernel<uint8_t, uint8_t, v_uint8, uint8_t, Op, uint8_t>;
|
|
case 2: return vecBinaryKernel<uint16_t, uint16_t, v_uint16, uint16_t, Op, uint16_t>;
|
|
case 4: return vecBinaryKernel<uint32_t, uint32_t, v_uint32, uint32_t, Op, uint32_t>;
|
|
case 8: return scalarBinaryKernel<uint64_t, uint64_t, uint64_t, Op>;
|
|
default: return nullptr;
|
|
}
|
|
}
|
|
|
|
TKernel getBitwiseFunc_(TOp op, int esz)
|
|
{
|
|
KernelFunc f = nullptr;
|
|
switch (op)
|
|
{
|
|
case OP_AND: f = bitwiseByEsz<EwAnd>(esz); break;
|
|
case OP_OR: f = bitwiseByEsz<EwOr >(esz); break;
|
|
case OP_XOR: f = bitwiseByEsz<EwXor>(esz); break;
|
|
default: ;
|
|
}
|
|
return {f, nullptr, 0};
|
|
}
|
|
|
|
TKernel getNotFunc_(int esz)
|
|
{
|
|
KernelFunc f = nullptr;
|
|
switch (esz)
|
|
{
|
|
case 1: f = notKernel<uint8_t, v_uint8 >; break;
|
|
case 2: f = notKernel<uint16_t, v_uint16>; break;
|
|
case 4: f = notKernel<uint32_t, v_uint32>; break;
|
|
case 8: f = notKernel<uint64_t, v_uint32>; break; // SIMD path compiled out for 8-byte -> scalar ~
|
|
default: ;
|
|
}
|
|
return {f, nullptr, 0};
|
|
}
|
|
|
|
// ===========================================================================
|
|
// OP_ADDW (addWeighted): dst = a*alpha + b*beta + gamma, params[0..2] = {alpha, beta, gamma}. Two fused
|
|
// v_fma in the work type Wvec - f32 SIMD for u8/s8/u16/s16/f16/bf16/f32; the 32-bit-int/64-bit group
|
|
// works in f64 (v_float64 SIMD under CV_SIMD_64F, else use_simd=false scalar). Like vecBinaryKernel but
|
|
// WITHOUT its multi-channel short-row
|
|
// branch: addWeighted takes plain scalar coefficients (a multi-channel scalar is not optimized, matching
|
|
// the classic function). The broadcast branches fold the constant operand's contribution once.
|
|
// ===========================================================================
|
|
template<typename T, typename Tr, typename Wvec, typename WT, bool use_simd>
|
|
static int addWeightedKernel(const void* src0_, size_t s0y, size_t s0x,
|
|
const void* src1_, size_t s1y, size_t s1x,
|
|
const void*, size_t, size_t,
|
|
void* dst_, size_t dsty, int width, int height,
|
|
const double* params, int, void*)
|
|
{
|
|
s0y /= sizeof(T); s1y /= sizeof(T); dsty /= sizeof(Tr);
|
|
CV_Assert((s0x|s1x) == 1u || (s0x|s1x) + (size_t)width == 1u);
|
|
const T* src0 = (const T*)src0_;
|
|
const T* src1 = (const T*)src1_;
|
|
Tr* dst = (Tr*)dst_;
|
|
const WT alpha = (WT)params[0], beta = (WT)params[1], gamma = (WT)params[2];
|
|
EW_TRY_COLLAPSE(2);
|
|
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
|
Wvec va{}, vb{}, vg{};
|
|
const int VECSZ = VTraits<Wvec>::vlanes();
|
|
const bool tail = width >= VECSZ*4 && src0_ != dst_ && src1_ != dst_;
|
|
if constexpr (use_simd) {
|
|
WT fa=alpha, fb=beta, fg=gamma;
|
|
vx_setall_as(&fa, va); vx_setall_as(&fb, vb); vx_setall_as(&fg, vg);
|
|
}
|
|
#endif
|
|
for (int y = 0; y < height; y++, src0 += s0y, src1 += s1y, dst += dsty)
|
|
{
|
|
int x = 0;
|
|
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
|
if constexpr (use_simd)
|
|
{
|
|
Wvec a0,a1,a2,a3,b0,b1,b2,b3;
|
|
if (s0x == s1x) { // both arrays contiguous
|
|
for (; x < width; x += VECSZ*4) {
|
|
if (x+VECSZ*4 > width) { if (!tail) break; x = width-VECSZ*4; }
|
|
vx_load_pair_as(src0+x, a0, a1); vx_load_pair_as(src0+x+VECSZ*2, a2, a3);
|
|
vx_load_pair_as(src1+x, b0, b1); vx_load_pair_as(src1+x+VECSZ*2, b2, b3);
|
|
a0=v_fma(a0,va,v_fma(b0,vb,vg)); a1=v_fma(a1,va,v_fma(b1,vb,vg));
|
|
a2=v_fma(a2,va,v_fma(b2,vb,vg)); a3=v_fma(a3,va,v_fma(b3,vb,vg));
|
|
v_store_pair_as(dst+x, a0, a1); v_store_pair_as(dst+x+VECSZ*2, a2, a3);
|
|
}
|
|
}
|
|
else if (s1x == 0) { // src1 broadcast: b*beta+gamma is constant
|
|
Wvec bb; vx_setall_as(src1, bb); Wvec bc = v_fma(bb, vb, vg);
|
|
for (; x < width; x += VECSZ*4) {
|
|
if (x+VECSZ*4 > width) { if (!tail) break; x = width-VECSZ*4; }
|
|
vx_load_pair_as(src0+x, a0, a1); vx_load_pair_as(src0+x+VECSZ*2, a2, a3);
|
|
a0=v_fma(a0,va,bc); a1=v_fma(a1,va,bc); a2=v_fma(a2,va,bc); a3=v_fma(a3,va,bc);
|
|
v_store_pair_as(dst+x, a0, a1); v_store_pair_as(dst+x+VECSZ*2, a2, a3);
|
|
}
|
|
}
|
|
else { // src0 broadcast: a*alpha+gamma is constant
|
|
Wvec aa; vx_setall_as(src0, aa); Wvec acg = v_fma(aa, va, vg);
|
|
for (; x < width; x += VECSZ*4) {
|
|
if (x+VECSZ*4 > width) { if (!tail) break; x = width-VECSZ*4; }
|
|
vx_load_pair_as(src1+x, b0, b1); vx_load_pair_as(src1+x+VECSZ*2, b2, b3);
|
|
b0=v_fma(b0,vb,acg); b1=v_fma(b1,vb,acg); b2=v_fma(b2,vb,acg); b3=v_fma(b3,vb,acg);
|
|
v_store_pair_as(dst+x, b0, b1); v_store_pair_as(dst+x+VECSZ*2, b2, b3);
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
if (s0x == s1x) {
|
|
for (; x < width; x++) dst[x] = saturate_cast<Tr>((WT)src0[x]*alpha + (WT)src1[x]*beta + gamma);
|
|
} else if (s0x == 0) {
|
|
const WT ac = (WT)src0[0]*alpha + gamma;
|
|
for (; x < width; x++) dst[x] = saturate_cast<Tr>((WT)src1[x]*beta + ac);
|
|
} else {
|
|
const WT bc = (WT)src1[0]*beta + gamma;
|
|
for (; x < width; x++) dst[x] = saturate_cast<Tr>((WT)src0[x]*alpha + bc);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
TKernel getAddWeightedFunc_(int T, int R)
|
|
{
|
|
KernelFunc f = nullptr;
|
|
#define AWS(Tt, Rr) addWeightedKernel<Tt, Rr, v_float32, float, true>
|
|
#if CV_SIMD_16F
|
|
#define AWH(Tt) addWeightedKernel<Tt, Tt, v_float16, float, true> // u8/s8 -> same, f16 work
|
|
#else
|
|
#define AWH(Tt) AWS(Tt, Tt) // no f16: f32 work
|
|
#endif
|
|
#if CV_SIMD_64F
|
|
#define AWD(Tt, Rr) addWeightedKernel<Tt, Rr, v_float64, double, true> // f64 SIMD
|
|
#else
|
|
#define AWD(Tt, Rr) addWeightedKernel<Tt, Rr, v_float32, double, false> // scalar (v_float32 unused)
|
|
#endif
|
|
switch (T)
|
|
{
|
|
case CV_8U: f = R==CV_8U ? AWH(uint8_t) : R==CV_32F ? AWS(uint8_t, float) : nullptr; break;
|
|
case CV_8S: f = R==CV_8S ? AWH(int8_t) : R==CV_32F ? AWS(int8_t, float) : nullptr; break;
|
|
case CV_16U: f = R==CV_16U ? AWS(uint16_t, uint16_t) : R==CV_32F ? AWS(uint16_t, float) : nullptr; break;
|
|
case CV_16S: f = R==CV_16S ? AWS(int16_t, int16_t) : R==CV_32F ? AWS(int16_t, float) : nullptr; break;
|
|
case CV_16F: f = R==CV_16F ? AWS(hfloat, hfloat) : R==CV_32F ? AWS(hfloat, float) : nullptr; break;
|
|
case CV_16BF:f = R==CV_32F ? AWS(bfloat, float) : nullptr; break;
|
|
case CV_32F: f = R==CV_32F ? AWS(float, float) : nullptr; break;
|
|
case CV_32U: f = R==CV_32U ? AWD(unsigned, unsigned) : R==CV_64F ? AWD(unsigned, double) : nullptr; break;
|
|
case CV_32S: f = R==CV_32S ? AWD(int, int) : R==CV_64F ? AWD(int, double) : nullptr; break;
|
|
case CV_64U: f = R==CV_64F ? AWD(uint64_t, double) : nullptr; break;
|
|
case CV_64S: f = R==CV_64F ? AWD(int64_t, double) : nullptr; break;
|
|
case CV_64F: f = R==CV_64F ? AWD(double, double) : nullptr; break;
|
|
default: ;
|
|
}
|
|
#undef AWS
|
|
#undef AWH
|
|
#undef AWD
|
|
return {f, nullptr, 0};
|
|
}
|
|
|
|
// ===========================================================================
|
|
// OP_SELECT: dst = (mask != 0) ? a : b
|
|
// ===========================================================================
|
|
// The one masking primitive of the engine. It serves both the public texpr select() and the
|
|
// masked-op tail: `cv::add(..., mask)` computes the full result into a temp `r`, then a final
|
|
// select(mask, r, dst) -> dst lands the masked subset in the (pre-existing) output and PRESERVES
|
|
// the rest - dst rides as both an input and the result. That aliasing is safe even under the
|
|
// right-edge tail backoff: re-running select over already-blended elements is IDEMPOTENT
|
|
// (mask!=0 lanes stay a, mask==0 lanes stay b/dst). Only dst == mask would break (the store
|
|
// rewrites the mask before the backoff re-reads it) - that combination falls to the scalar tail.
|
|
//
|
|
// The mask is one byte per element (bool/u8/s8 - never parameterized by its depth, we just test
|
|
// the byte != 0); a/b/dst share one depth, the kernel is templated by the element SIZE only.
|
|
// Channels fall out of the broadcast machinery: single-channel data arrives as a (width x 1) tile
|
|
// with a per-element mask (smx == 1); n-channel data as a tall-thin tile - channel axis = width,
|
|
// the 1-channel mask broadcasting across it (smx == 0, one mask byte per row) - handled by the
|
|
// interleaved fast path for 2..4 channels, per-row otherwise. Branches may broadcast (s1x/s2x == 0).
|
|
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
|
// VECSZ(T) mask bytes -> T-width lanes (u8 direct, u16/u32 via expand)
|
|
static inline v_uint8 loadSelectMask(const uchar* m, const v_uint8&) { return vx_load(m); }
|
|
static inline v_uint16 loadSelectMask(const uchar* m, const v_uint16&) { return vx_load_expand(m); }
|
|
static inline v_uint32 loadSelectMask(const uchar* m, const v_uint32&) { return vx_load_expand_q(m); }
|
|
|
|
static inline void setallSelect(const uint8_t* p, v_uint8& a) { a = vx_setall_u8(*p); }
|
|
static inline void setallSelect(const uint16_t* p, v_uint16& a) { a = vx_setall_u16(*p); }
|
|
static inline void setallSelect(const uint32_t* p, v_uint32& a) { a = vx_setall_u32(*p); }
|
|
#endif
|
|
|
|
template<typename T, typename Tvec>
|
|
static int selectKernel(const void* mask_, size_t smy, size_t smx,
|
|
const void* src1_, size_t s1y, size_t s1x,
|
|
const void* src2_, size_t s2y, size_t s2x,
|
|
void* dst_, size_t dsty, int width, int height,
|
|
const double*, int, void*)
|
|
{
|
|
s1y /= sizeof(T); // the 1-byte mask's smy stays in bytes == elements
|
|
s2y /= sizeof(T);
|
|
dsty /= sizeof(T);
|
|
CV_Assert(smx <= 1u && s1x <= 1u && s2x <= 1u);
|
|
|
|
const uchar* mask = (const uchar*)mask_;
|
|
const T* src1 = (const T*)src1_;
|
|
const T* src2 = (const T*)src2_;
|
|
T* dst = (T*)dst_;
|
|
|
|
if (height > 1 && dsty == (size_t)width && smy == smx*(size_t)width &&
|
|
s1y == s1x*(size_t)width && s2y == s2x*(size_t)width)
|
|
{ width *= height; height = 1; }
|
|
|
|
int y = 0;
|
|
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
|
if constexpr (sizeof(T) <= 4)
|
|
{
|
|
const int VECSZ = VTraits<Tvec>::vlanes();
|
|
const Tvec z = v_setzero_<Tvec>();
|
|
|
|
// n-channel data under a per-pixel mask (the masked-op shape): channel axis = width (2..4),
|
|
// one mask byte per row. Process VECSZ rows per iteration - expand the mask once and
|
|
// interleave it across the channel lanes.
|
|
if (height > VECSZ && 2 <= width && width <= 4 && smx == 0u && smy == 1u &&
|
|
s1x == 1u && s1y == (size_t)width && s2x == 1u && s2y == (size_t)width &&
|
|
dsty == (size_t)width)
|
|
{
|
|
constexpr int MAXVECSZ = VTraits<Tvec>::max_nlanes;
|
|
T maskbuf[MAXVECSZ*4] = {}; // {} for -Wmaybe-uninitialized only (fully written)
|
|
const int dy = VECSZ;
|
|
for (; y + dy <= height; y += dy, mask += dy, src1 += width*dy, src2 += width*dy,
|
|
dst += width*dy)
|
|
{
|
|
Tvec m0 = v_ne(loadSelectMask(mask, z), z), m1, m2, m3;
|
|
if (width == 2) v_store_interleave(maskbuf, m0, m0);
|
|
else if (width == 3) v_store_interleave(maskbuf, m0, m0, m0);
|
|
else v_store_interleave(maskbuf, m0, m0, m0, m0);
|
|
m0 = vx_load(maskbuf);
|
|
m1 = vx_load(maskbuf + VECSZ);
|
|
if (width > 2) m2 = vx_load(maskbuf + VECSZ*2);
|
|
if (width > 3) m3 = vx_load(maskbuf + VECSZ*3);
|
|
v_store(dst, v_select(m0, vx_load(src1), vx_load(src2)));
|
|
v_store(dst + VECSZ, v_select(m1, vx_load(src1 + VECSZ), vx_load(src2 + VECSZ)));
|
|
if (width > 2)
|
|
v_store(dst + VECSZ*2,
|
|
v_select(m2, vx_load(src1 + VECSZ*2), vx_load(src2 + VECSZ*2)));
|
|
if (width > 3)
|
|
v_store(dst + VECSZ*3,
|
|
v_select(m3, vx_load(src1 + VECSZ*3), vx_load(src2 + VECSZ*3)));
|
|
}
|
|
// the remaining < VECSZ rows fall through to the per-row path below
|
|
}
|
|
else if (smx == 1) // per-element mask - the common case
|
|
{
|
|
// a branch aliasing dst is fine under the backoff (idempotent select); dst == mask is not
|
|
const bool use_tail_trick = width >= VECSZ*2 && dst_ != mask_;
|
|
Tvec a, b;
|
|
if (s1x == 0) setallSelect(src1, a);
|
|
if (s2x == 0) setallSelect(src2, b);
|
|
for (; y < height; y++, mask += smy, src1 += s1y, src2 += s2y, dst += dsty)
|
|
{
|
|
int x = 0;
|
|
for (; x < width; x += VECSZ)
|
|
{
|
|
if (x + VECSZ > width) { if (!use_tail_trick || x == 0) break; x = width - VECSZ; }
|
|
Tvec m = v_ne(loadSelectMask(mask + x, z), z);
|
|
if (s1x) a = vx_load(src1 + x);
|
|
if (s2x) b = vx_load(src2 + x);
|
|
v_store(dst + x, v_select(m, a, b));
|
|
}
|
|
for (; x < width; x++)
|
|
dst[x] = mask[x] != 0 ? src1[x*s1x] : src2[x*s2x];
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
#endif
|
|
for (; y < height; y++, mask += smy, src1 += s1y, src2 += s2y, dst += dsty)
|
|
{
|
|
if (smx == 0) // one mask byte per row (n-channel data / broadcast mask)
|
|
{
|
|
const T* s = mask[0] != 0 ? src1 : src2;
|
|
const size_t sx = mask[0] != 0 ? s1x : s2x;
|
|
if ((const void*)s != (const void*)dst) // row select from dst itself is a no-op
|
|
for (int x = 0; x < width; x++) dst[x] = s[x*sx];
|
|
}
|
|
else
|
|
for (int x = 0; x < width; x++)
|
|
dst[x] = mask[x] != 0 ? src1[x*s1x] : src2[x*s2x];
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
TKernel getSelectFunc_(int mdepth, int T)
|
|
{
|
|
if (CV_ELEM_SIZE1(mdepth) != 1) // the mask must be a 1-byte type (u8/s8/bool)
|
|
return {};
|
|
KernelFunc fptr = nullptr;
|
|
switch (CV_ELEM_SIZE1(T))
|
|
{
|
|
case 1: fptr = selectKernel<uint8_t, v_uint8 >; break;
|
|
case 2: fptr = selectKernel<uint16_t, v_uint16>; break;
|
|
case 4: fptr = selectKernel<uint32_t, v_uint32>; break;
|
|
case 8: fptr = selectKernel<uint64_t, v_uint32>; break; // SIMD path compiled out -> scalar
|
|
default: ;
|
|
}
|
|
return {fptr, nullptr, 0};
|
|
}
|
|
|
|
// ===========================================================================
|
|
// OP_CLAMP: dst = min(max(x, lo), hi)
|
|
// ===========================================================================
|
|
// All four operands share one depth (emitTernary unifies them); lo/hi are usually broadcast
|
|
// scalars (clamp(img, 10, 200) - literals ride as 0-dim consts with stepx == 0) but may be full
|
|
// arrays. clamp is IDEMPOTENT, so the right-edge tail backoff stays on when dst aliases x; only
|
|
// dst aliasing lo/hi suppresses it (the store would corrupt the bounds before the re-read).
|
|
// NaN note: v_min/v_max lane behavior on NaN is ISA-specific, matching the scalar std::min/max
|
|
// unspecifiedness - clamp of NaN is not a contract either way.
|
|
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
|
static inline void setallClamp(const uchar* p, v_uint8& a) { a = vx_setall_u8(*p); }
|
|
static inline void setallClamp(const schar* p, v_int8& a) { a = vx_setall_s8(*p); }
|
|
static inline void setallClamp(const ushort* p, v_uint16& a) { a = vx_setall_u16(*p); }
|
|
static inline void setallClamp(const short* p, v_int16& a) { a = vx_setall_s16(*p); }
|
|
static inline void setallClamp(const unsigned* p, v_uint32& a) { a = vx_setall_u32(*p); }
|
|
static inline void setallClamp(const int* p, v_int32& a) { a = vx_setall_s32(*p); }
|
|
static inline void setallClamp(const float* p, v_float32& a) { a = vx_setall_f32(*p); }
|
|
#if CV_SIMD_64F || CV_SIMD_SCALABLE_64F
|
|
static inline void setallClamp(const double* p, v_float64& a) { a = vx_setall_f64(*p); }
|
|
#endif
|
|
#endif
|
|
|
|
template<typename T, typename Tvec>
|
|
static int clampKernel(const void* src0_, size_t s0y, size_t s0x,
|
|
const void* lo_, size_t s1y, size_t s1x,
|
|
const void* hi_, size_t s2y, size_t s2x,
|
|
void* dst_, size_t dsty, int width, int height,
|
|
const double*, int, void*)
|
|
{
|
|
s0y /= sizeof(T);
|
|
s1y /= sizeof(T);
|
|
s2y /= sizeof(T);
|
|
dsty /= sizeof(T);
|
|
CV_Assert(s0x <= 1u && s1x <= 1u && s2x <= 1u);
|
|
|
|
const T* src0 = (const T*)src0_;
|
|
const T* lo = (const T*)lo_;
|
|
const T* hi = (const T*)hi_;
|
|
T* dst = (T*)dst_;
|
|
|
|
if (height > 1 && dsty == (size_t)width && s0y == s0x*(size_t)width &&
|
|
s1y == s1x*(size_t)width && s2y == s2x*(size_t)width)
|
|
{ width *= height; height = 1; }
|
|
|
|
for (int y = 0; y < height; y++, src0 += s0y, lo += s1y, hi += s2y, dst += dsty)
|
|
{
|
|
int x = 0;
|
|
#if (CV_SIMD || CV_SIMD_SCALABLE)
|
|
if (s0x == 1)
|
|
{
|
|
const int VECSZ = VTraits<Tvec>::vlanes();
|
|
const bool use_tail_trick = width >= VECSZ*2 && lo_ != dst_ && hi_ != dst_;
|
|
Tvec vlo, vhi;
|
|
if (s1x == 0) setallClamp(lo, vlo);
|
|
if (s2x == 0) setallClamp(hi, vhi);
|
|
for (; x < width; x += VECSZ)
|
|
{
|
|
if (x + VECSZ > width) { if (!use_tail_trick || x == 0) break; x = width - VECSZ; }
|
|
if (s1x) vlo = vx_load(lo + x);
|
|
if (s2x) vhi = vx_load(hi + x);
|
|
v_store(dst + x, v_min(v_max(vx_load(src0 + x), vlo), vhi));
|
|
}
|
|
}
|
|
#endif
|
|
for (; x < width; x++)
|
|
{
|
|
T v = src0[x*s0x], l = lo[x*s1x], h = hi[x*s2x];
|
|
dst[x] = std::min(std::max(v, l), h);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// Scalar-only clamp for the depths without a native SIMD lane type here (f16/bf16 - compared in
|
|
// float; 64-bit ints).
|
|
template<typename T, typename WT>
|
|
static int scalarClampKernel(const void* src0_, size_t s0y, size_t s0x,
|
|
const void* lo_, size_t s1y, size_t s1x,
|
|
const void* hi_, size_t s2y, size_t s2x,
|
|
void* dst_, size_t dsty, int width, int height,
|
|
const double*, int, void*)
|
|
{
|
|
s0y /= sizeof(T); s1y /= sizeof(T); s2y /= sizeof(T); dsty /= sizeof(T);
|
|
CV_Assert(s0x <= 1u && s1x <= 1u && s2x <= 1u);
|
|
const T* src0 = (const T*)src0_;
|
|
const T* lo = (const T*)lo_;
|
|
const T* hi = (const T*)hi_;
|
|
T* dst = (T*)dst_;
|
|
if (height > 1 && dsty == (size_t)width && s0y == s0x*(size_t)width &&
|
|
s1y == s1x*(size_t)width && s2y == s2x*(size_t)width)
|
|
{ width *= height; height = 1; }
|
|
for (int y = 0; y < height; y++, src0 += s0y, lo += s1y, hi += s2y, dst += dsty)
|
|
for (int x = 0; x < width; x++)
|
|
{
|
|
WT v = (WT)src0[x*s0x], l = (WT)lo[x*s1x], h = (WT)hi[x*s2x];
|
|
dst[x] = saturate_cast<T>(std::min(std::max(v, l), h));
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
TKernel getClampFunc_(int T)
|
|
{
|
|
KernelFunc fptr = nullptr;
|
|
switch (T)
|
|
{
|
|
case CV_8U: fptr = clampKernel<uchar, v_uint8 >; break;
|
|
case CV_8S: fptr = clampKernel<schar, v_int8 >; break;
|
|
case CV_16U: fptr = clampKernel<ushort, v_uint16 >; break;
|
|
case CV_16S: fptr = clampKernel<short, v_int16 >; break;
|
|
case CV_32U: fptr = clampKernel<unsigned, v_uint32 >; break;
|
|
case CV_32S: fptr = clampKernel<int, v_int32 >; break;
|
|
case CV_32F: fptr = clampKernel<float, v_float32>; break;
|
|
#if CV_SIMD_64F || CV_SIMD_SCALABLE_64F
|
|
case CV_64F: fptr = clampKernel<double, v_float64>; break;
|
|
#else
|
|
case CV_64F: fptr = scalarClampKernel<double, double>; break;
|
|
#endif
|
|
case CV_16F: fptr = scalarClampKernel<hfloat, float>; break;
|
|
case CV_16BF: fptr = scalarClampKernel<bfloat, float>; break;
|
|
case CV_64U: fptr = scalarClampKernel<uint64_t, uint64_t>; break;
|
|
case CV_64S: fptr = scalarClampKernel<int64_t, int64_t >; break;
|
|
default: ;
|
|
}
|
|
return {fptr, nullptr, 0};
|
|
}
|
|
|
|
static int expandKernel(size_t s0y, size_t s0x,
|
|
void* dst_, size_t dsty, int width, int height,
|
|
int esz1)
|
|
{
|
|
uchar* dst = (uchar*)dst_;
|
|
if (s0x == 0u && width > 1) {
|
|
int h = s0y > 0u ? height : 1;
|
|
CV_Assert(esz1 == 1 || esz1 == 2 || esz1 == 4 || esz1 == 8);
|
|
for (int y = 0; y < h; y++) {
|
|
if (esz1 == 1) {
|
|
uchar* rowptr = dst + dsty*y;
|
|
uchar val = rowptr[0];
|
|
for (int x = 1; x < width; x++) rowptr[x] = val;
|
|
}
|
|
else if (esz1 == 2) {
|
|
ushort* rowptr = (ushort*)(dst + dsty*y);
|
|
ushort val = rowptr[0];
|
|
for (int x = 1; x < width; x++) rowptr[x] = val;
|
|
}
|
|
else if (esz1 == 4) {
|
|
unsigned* rowptr = (unsigned*)(dst + dsty*y);
|
|
unsigned val = rowptr[0];
|
|
for (int x = 1; x < width; x++) rowptr[x] = val;
|
|
}
|
|
else {
|
|
uint64_t* rowptr = (uint64_t*)(dst + dsty*y);
|
|
uint64_t val = rowptr[0];
|
|
for (int x = 1; x < width; x++) rowptr[x] = val;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (s0y == 0) {
|
|
for (int y = 1; y < height; y++)
|
|
memcpy(dst + dsty*y, dst, (size_t)esz1*width);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int castKernel(const void* src0_, size_t s0y, size_t s0x,
|
|
const void*, size_t, size_t,
|
|
const void*, size_t, size_t,
|
|
void* dst_, size_t dsty, int width, int height,
|
|
const double* params, int esz1, void* userdata)
|
|
{
|
|
BinaryFunc castfunc = (BinaryFunc)userdata;
|
|
castfunc((const uchar*)src0_, s0y, nullptr, 0, (uchar*)dst_, dsty,
|
|
Size((s0x > 0u ? width : 1), (s0y > 0u ? height : 1)), (void*)params);
|
|
return expandKernel(s0y, s0x, dst_, dsty, width, height, esz1);
|
|
}
|
|
|
|
// ===========================================================================
|
|
// Per-op entry points (this baseline). The op-level getElemwiseFunc() dispatcher and the regular
|
|
// get*Func() forwarders live in arithm.dispatch.cpp.
|
|
// ===========================================================================
|
|
|
|
// OP_CAST / OP_CONVERT_SCALE: wrap core's convert BinaryFunc (carried in kernel.userdata) in
|
|
// castKernel, which runs it over the distinct sub-region and then expands across broadcast axes.
|
|
TKernel getCastFunc_(int sdepth, int ddepth, bool scaled)
|
|
{
|
|
BinaryFunc cvt = scaled ? getConvertScaleFunc(sdepth, ddepth) : getConvertFunc(sdepth, ddepth);
|
|
return {castKernel, (void*)cvt, CV_ELEM_SIZE1(ddepth)};
|
|
}
|
|
|
|
// Non-template wrappers around the templated kernel selectors (ADD/SUB share getAddSubFunc, MIN/MAX
|
|
// share getMinMaxFunc). min/max are T x T -> T, so R is ignored (the dispatcher validates R == T).
|
|
TKernel getAddFunc_(int T, int R) { return getAddSubFunc<EwAdd>(T, R); }
|
|
TKernel getSubFunc_(int T, int R) { return getAddSubFunc<EwSub>(T, R); }
|
|
TKernel getMinFunc_(int T, int R) { (void)R; return getMinMaxFunc<EwMin>(T); }
|
|
TKernel getMaxFunc_(int T, int R) { (void)R; return getMinMaxFunc<EwMax>(T); }
|
|
|
|
TKernel getHypotFunc_(int T, int R)
|
|
{
|
|
if (R != T)
|
|
return {};
|
|
KernelFunc fptr = nullptr;
|
|
switch (T)
|
|
{
|
|
case CV_16F: fptr = vecBinaryKernel<hfloat, hfloat, v_float32, float, EwHypot>; break;
|
|
case CV_16BF: fptr = vecBinaryKernel<bfloat, bfloat, v_float32, float, EwHypot>; break;
|
|
case CV_32F: fptr = vecBinaryKernel<float, float, v_float32, float, EwHypot>; break;
|
|
#if CV_SIMD_64F || CV_SIMD_SCALABLE_64F
|
|
case CV_64F: fptr = vecBinaryKernel<double, double, v_float64, double, EwHypot>; break;
|
|
#else
|
|
case CV_64F: fptr = scalarBinaryKernel<double, double, double, EwHypot>; break;
|
|
#endif
|
|
default: ;
|
|
}
|
|
return {fptr, nullptr, 0};
|
|
}
|
|
|
|
TKernel getAtan2Func_(int T, int R)
|
|
{
|
|
if (R != T)
|
|
return {};
|
|
KernelFunc fptr = nullptr;
|
|
switch (T)
|
|
{
|
|
case CV_16F: fptr = vecBinaryKernel<hfloat, hfloat, v_float32, float, EwAtan2>; break;
|
|
case CV_16BF: fptr = vecBinaryKernel<bfloat, bfloat, v_float32, float, EwAtan2>; break;
|
|
case CV_32F: fptr = vecBinaryKernel<float, float, v_float32, float, EwAtan2>; break;
|
|
case CV_64F: fptr = scalarBinaryKernel<double, double, double, EwAtan2>; break; // exact std::atan2
|
|
default: ;
|
|
}
|
|
return {fptr, nullptr, 0};
|
|
}
|
|
|
|
#endif // CV_CPU_OPTIMIZATION_DECLARATIONS_ONLY
|
|
|
|
CV_CPU_OPTIMIZATION_NAMESPACE_END
|
|
}} // namespace cv::ew
|