Packages
evision
1.0.1-rc.0
1.0.1-rc.0
1.0.0
1.0.0-rc.0
0.2.17
0.2.17-rc2
0.2.17-rc1
0.2.16
0.2.16-pre.2
0.2.16-pre
0.2.15
0.2.14
0.2.13
0.2.12
0.2.11
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.2-rc2
0.2.1
0.2.0
0.1.39
0.1.38
0.1.37
0.1.36
0.1.35
0.1.34
0.1.33
0.1.32
retired
0.1.31
0.1.30
0.1.29
0.1.28
0.1.27
0.1.26
0.1.26-rc3
0.1.26-rc2
0.1.26-rc1
0.1.26-rc0
0.1.25
0.1.24
0.1.23
0.1.22
0.1.21
0.1.20
0.1.19
0.1.18
0.1.17
0.1.16
0.1.15
0.1.14
0.1.13
0.1.12
0.1.11
0.1.10
0.1.9
0.1.8
0.1.7
OpenCV-Erlang/Elixir binding.
Current section
Files
Jump to
Current section
Files
c_src/modules/evision_backend/shift.h
#ifndef EVISION_BACKEND_SHIFT_H
#define EVISION_BACKEND_SHIFT_H
#include <cstdint>
#include <type_traits>
#include <erl_nif.h>
#include "../../ArgInfo.hpp"
#include "../evision_mat_utils.hpp"
#include "parallel.h"
// Elementwise integer shift (Nx.left_shift / right_shift). `l` and `r` share the
// output type and either the same shape or one is a 1-element scalar, broadcast
// (stride 0) over the other. op 0 = left (<<), 1 = right (>>). Left shifts compute in
// the unsigned counterpart (modular, no signed-overflow UB); right shifts use the
// native >> (arithmetic for signed, logical for unsigned). A shift >= bit width
// yields 0, except a right shift of a negative signed value yields -1 -- matching
// Nx's arbitrary-precision shift truncated to the type.
template <typename T>
static void evision_shift(const cv::Mat &l, const cv::Mat &r, cv::Mat &dst, int op) {
typedef typename std::make_unsigned<T>::type U;
const T *lp = (const T *)l.data;
const T *rp = (const T *)r.data;
T *dp = (T *)dst.data;
size_t n = dst.total();
size_t ls = (l.total() == 1) ? 0 : 1, rs = (r.total() == 1) ? 0 : 1;
uint64_t width = (uint64_t)(sizeof(T) * 8);
evision_parallel_for((int64_t)n, (int64_t)n, EVISION_PARALLEL_SIMPLE_MIN_WORK, [&](int64_t begin, int64_t end) {
for (int64_t i = begin; i < end; i++) {
T a = lp[(size_t)i * ls];
T b = rp[(size_t)i * rs];
if (op == 0) {
dp[(size_t)i] = ((uint64_t)b >= width) ? (T)0 : (T)((U)a << b);
} else if ((uint64_t)b >= width) {
dp[(size_t)i] = (std::is_signed<T>::value && a < 0) ? (T)(-1) : (T)0;
} else {
dp[(size_t)i] = (T)(a >> b);
}
}
});
}
// @evision c: mat_shift, evision_cv_mat_shift, 1
// @evision nif: def mat_shift(_opts \\ []), do: :erlang.nif_error(:undefined)
static ERL_NIF_TERM evision_cv_mat_shift(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
using namespace cv;
using namespace evision::nif;
std::map<std::string, ERL_NIF_TERM> erl_terms;
int nif_opts_index = 0;
evision::nif::parse_arg(env, nif_opts_index, argv, erl_terms);
{
Mat l, r;
int op = 0;
if (evision_to_safe(env, evision_get_kw(env, erl_terms, "l"), l, ArgInfo("l", ArgInfo::INPUT_ONLY)) &&
evision_to_safe(env, evision_get_kw(env, erl_terms, "r"), r, ArgInfo("r", ArgInfo::INPUT_ONLY)) &&
evision_to_safe(env, evision_get_kw(env, erl_terms, "op"), op, ArgInfo("op", 0))) {
Mat lc = l.isContinuous() ? l : l.clone();
Mat rc = r.isContinuous() ? r : r.clone();
Mat dst = (rc.total() > lc.total() ? rc : lc).clone();
switch (dst.depth()) {
case CV_8U: evision_shift<uint8_t>(lc, rc, dst, op); break;
case CV_8S: evision_shift<int8_t>(lc, rc, dst, op); break;
case CV_16U: evision_shift<uint16_t>(lc, rc, dst, op); break;
case CV_16S: evision_shift<int16_t>(lc, rc, dst, op); break;
case CV_32S: evision_shift<int32_t>(lc, rc, dst, op); break;
case CV_32U: evision_shift<uint32_t>(lc, rc, dst, op); break;
case CV_64S: evision_shift<int64_t>(lc, rc, dst, op); break;
case CV_64U: evision_shift<uint64_t>(lc, rc, dst, op); break;
default: return evision::nif::error(env, "mat_shift: integer types only");
}
return evision_from(env, dst);
}
}
return enif_make_badarg(env);
}
#endif // EVISION_BACKEND_SHIFT_H