TravisG
TravisG

Reputation: 2373

SIMD: Flip sign of four packed integer

Let's say I have four packed ints.

__m128i val = _mm_set_epi32(42,64,123,456);

What's the fastest way to flip the sign (multiply by -1) of the four integers in val?

Upvotes: 1

Views: 674

Answers (2)

Paul R
Paul R

Reputation: 213130

Just subtract from 0, e.g.

val = _mm_sub_epi32(_mm_set1_epi32(0), val);

Upvotes: 3

Mats Petersson
Mats Petersson

Reputation: 129514

You can subtract your actual values from a set of {0,0,0,0} [that's probably the fastest], you can XOR with {-1,-1,-1,-1} "all ones" and then add {1,1,1,1}. Or you can multiply by -1.

Upvotes: 6

Related Questions