Skip to content

Commit

Permalink
refactor(simd.h): Simplify vbool16 casting (#4105)
Browse files Browse the repository at this point in the history
Some of the ways we were using unions for type punning in the vbool16
class were unnecessary, as both union members were basically the same
type. Simplify by getting rid of the unnecessary m_bits union member.

This also makes Sonar static analysis happier about these dubious uses
of unions for type punning.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
  • Loading branch information
lgritz authored Jan 19, 2024
1 parent b558b96 commit d9702e1
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions src/include/OpenImageIO/simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -920,10 +920,7 @@ class vbool16 {

private:
// The actual data representation
union {
simd_t m_simd;
uint16_t m_bits;
};
simd_t m_simd;
};


Expand Down Expand Up @@ -3920,19 +3917,15 @@ OIIO_FORCEINLINE bool none (const vbool8& v) { return reduce_or(v) == false; }

OIIO_FORCEINLINE int vbool16::operator[] (int i) const {
OIIO_DASSERT(i >= 0 && i < elements);
#if OIIO_SIMD_AVX >= 512
return (int(m_simd) >> i) & 1;
#else
return (m_bits >> i) & 1;
#endif
return (static_cast<uint16_t>(m_simd) >> i) & 1;
}

OIIO_FORCEINLINE void vbool16::setcomp (int i, bool value) {
OIIO_DASSERT(i >= 0 && i < elements);
int bits = m_bits;
int bits = static_cast<uint16_t>(m_simd);
bits &= (0xffff ^ (1<<i));
bits |= (int(value)<<i);
m_bits = bits;
m_simd = static_cast<simd_t>(bits);
}


Expand Down Expand Up @@ -4015,11 +4008,7 @@ OIIO_FORCEINLINE const vbool16& vbool16::operator= (const vbool16 & other) {


OIIO_FORCEINLINE int vbool16::bitmask () const {
#if OIIO_SIMD_AVX >= 512
return int(m_simd);
#else
return int(m_bits);
#endif
return static_cast<int>(m_simd);
}


Expand All @@ -4038,13 +4027,13 @@ OIIO_FORCEINLINE const vbool16 vbool16::True () {


OIIO_FORCEINLINE void vbool16::store (bool *values) const {
SIMD_DO (values[i] = m_bits & (1<<i));
SIMD_DO (values[i] = m_simd & (1<<i));
}

OIIO_FORCEINLINE void vbool16::store (bool *values, int n) const {
OIIO_DASSERT (n >= 0 && n <= elements);
for (int i = 0; i < n; ++i)
values[i] = m_bits & (1<<i);
values[i] = m_simd & (1<<i);
}


Expand All @@ -4070,31 +4059,31 @@ OIIO_FORCEINLINE vbool16 operator! (const vbool16 & a) {
#if OIIO_SIMD_AVX >= 512
return _mm512_knot (a.simd());
#else
return vbool16 (a.m_bits ^ 0xffff);
return vbool16 (a.m_simd ^ 0xffff);
#endif
}

OIIO_FORCEINLINE vbool16 operator& (const vbool16 & a, const vbool16 & b) {
#if OIIO_SIMD_AVX >= 512
return _mm512_kand (a.simd(), b.simd());
#else
return vbool16 (a.m_bits & b.m_bits);
return vbool16 (a.m_simd & b.m_simd);
#endif
}

OIIO_FORCEINLINE vbool16 operator| (const vbool16 & a, const vbool16 & b) {
#if OIIO_SIMD_AVX >= 512
return _mm512_kor (a.simd(), b.simd());
#else
return vbool16 (a.m_bits | b.m_bits);
return vbool16 (a.m_simd | b.m_simd);
#endif
}

OIIO_FORCEINLINE vbool16 operator^ (const vbool16& a, const vbool16& b) {
#if OIIO_SIMD_AVX >= 512
return _mm512_kxor (a.simd(), b.simd());
#else
return vbool16 (a.m_bits ^ b.m_bits);
return vbool16 (a.m_simd ^ b.m_simd);
#endif
}

Expand All @@ -4121,15 +4110,15 @@ OIIO_FORCEINLINE vbool16 operator== (const vbool16 & a, const vbool16 & b) {
#if OIIO_SIMD_AVX >= 512
return _mm512_kxnor (a.simd(), b.simd());
#else
return vbool16 (!(a.m_bits ^ b.m_bits));
return vbool16 (!(a.m_simd ^ b.m_simd));
#endif
}

OIIO_FORCEINLINE vbool16 operator!= (const vbool16 & a, const vbool16 & b) {
#if OIIO_SIMD_AVX >= 512
return _mm512_kxor (a.simd(), b.simd());
#else
return vbool16 (a.m_bits ^ b.m_bits);
return vbool16 (a.m_simd ^ b.m_simd);
#endif
}

Expand Down

0 comments on commit d9702e1

Please sign in to comment.