user1906
user1906

Reputation: 2340

Eigen operations with unsigned type

I'm using Eigen to compute the difference between two matrices of type unsigned char.

 Eigen::Matrix<unsigned char, 4, 1> C0;
 Eigen::Matrix<unsigned char, 4, 1> C1;
 C0 << 255, 0, 127, 1;
 C1 << 0, 255, 128, 255;
 std::cout << (C0 - C1).cast<int>() << "\n";

The results are 255 1 255 2. I'd like the operation to saturate in case of underflow (or overflow if it was an addition), resulting in 255 0 0 0. Is that possible with Eigen?

Upvotes: 1

Views: 1733

Answers (1)

chtz
chtz

Reputation: 18817

There is nothing readily available inside Eigen for this. You could provide a custom type which overloads operator+, operator-, etc. like you intend.

Of course, ideally this should use saturating SIMD instructions (e.g., _mm_subs_epi8 if you have SSE2) internally, but that is not very easy to integrate into Eigen -- especially if your matrix sizes are not multiples of 16 bytes.

Upvotes: 2

Related Questions