Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 82 additions & 29 deletions src/OpenColorIO/SSE2.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ struct SSE2RGBAPack<BIT_DEPTH_UINT8>
{
static inline void Load(const uint8_t *in, __m128& r, __m128& g, __m128& b, __m128& a)
{
#if OCIO_USE_SSE2NEON
static constexpr uint8_t kDeinterleaveRGBA[16] = {
0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15
};
uint8x16_t planar = vqtbl1q_u8(vld1q_u8(in), vld1q_u8(kDeinterleaveRGBA));
uint16x8_t rg = vmovl_u8(vget_low_u8(planar));
uint16x8_t ba = vmovl_u8(vget_high_u8(planar));
r = vreinterpretq_m128_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(rg))));
g = vreinterpretq_m128_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(rg))));
b = vreinterpretq_m128_f32(vcvtq_f32_u32(vmovl_u16(vget_low_u16(ba))));
a = vreinterpretq_m128_f32(vcvtq_f32_u32(vmovl_u16(vget_high_u16(ba))));
#else
const __m128i zero = _mm_setzero_si128();
__m128i rgba_00_03 = _mm_loadu_si128((const __m128i*)in);

Expand All @@ -214,11 +226,23 @@ struct SSE2RGBAPack<BIT_DEPTH_UINT8>
__m128 rgba3 = _mm_cvtepi32_ps(_mm_unpackhi_epi16(rgba_02_03, zero));

sse2RGBATranspose_4x4(rgba0, rgba1, rgba2, rgba3, r, g, b, a);
#endif
}
static inline void Store(uint8_t *out, __m128 r_in, __m128 g_in, __m128 b_in, __m128 a_in)
{
const __m128 maxValue = _mm_set1_ps(255.0f);

#if OCIO_USE_SSE2NEON
uint32x4_t r = vcvtnq_u32_f32(vreinterpretq_f32_m128(sse2_clamp(r_in, maxValue)));
uint32x4_t g = vcvtnq_u32_f32(vreinterpretq_f32_m128(sse2_clamp(g_in, maxValue)));
uint32x4_t b = vcvtnq_u32_f32(vreinterpretq_f32_m128(sse2_clamp(b_in, maxValue)));
uint32x4_t a = vcvtnq_u32_f32(vreinterpretq_f32_m128(sse2_clamp(a_in, maxValue)));

uint32x4_t rg = vsliq_n_u32(r, g, 8);
uint32x4_t ba = vsliq_n_u32(b, a, 8);
uint32x4_t rgba = vsliq_n_u32(rg, ba, 16);
vst1q_u8(out, vreinterpretq_u8_u32(rgba));
#else
// NOTE note using cvtps which will round based on MXCSR register defaults to _MM_ROUND_NEAREST
__m128i r = _mm_cvtps_epi32(sse2_clamp(r_in, maxValue));
__m128i g = _mm_cvtps_epi32(sse2_clamp(g_in, maxValue));
Expand All @@ -229,6 +253,7 @@ struct SSE2RGBAPack<BIT_DEPTH_UINT8>
rgba = _mm_or_si128(rgba, _mm_slli_si128(b, 2));
rgba = _mm_or_si128(rgba, _mm_slli_si128(a, 3));
_mm_storeu_si128((__m128i*)out, rgba);
#endif
}
};

Expand All @@ -239,6 +264,13 @@ struct SSE2RGBAPack16

static inline void Load(const Type *in, __m128& r, __m128& g, __m128& b, __m128& a)
{
#if OCIO_USE_SSE2NEON
uint16x4x4_t rgba = vld4_u16(in);
r = vreinterpretq_m128_f32(vcvtq_f32_u32(vmovl_u16(rgba.val[0])));
g = vreinterpretq_m128_f32(vcvtq_f32_u32(vmovl_u16(rgba.val[1])));
b = vreinterpretq_m128_f32(vcvtq_f32_u32(vmovl_u16(rgba.val[2])));
a = vreinterpretq_m128_f32(vcvtq_f32_u32(vmovl_u16(rgba.val[3])));
#else
const __m128i zero = _mm_setzero_si128();
__m128i rgba_00_01 = _mm_loadu_si128((const __m128i*)(in + 0));
__m128i rgba_02_03 = _mm_loadu_si128((const __m128i*)(in + 8));
Expand All @@ -250,12 +282,25 @@ struct SSE2RGBAPack16
__m128 rgba3 = _mm_cvtepi32_ps(_mm_unpackhi_epi16(rgba_02_03, zero));

sse2RGBATranspose_4x4(rgba0, rgba1, rgba2, rgba3, r, g, b, a);
#endif
}

static inline void Store(Type *out, __m128 r, __m128 g, __m128 b, __m128 a)
{
const __m128 maxValue = _mm_set1_ps((float)BitDepthInfo<BD>::maxValue);

#if OCIO_USE_SSE2NEON
uint32x4_t rrrr = vcvtnq_u32_f32(vreinterpretq_f32_m128(sse2_clamp(r, maxValue)));
uint32x4_t gggg = vcvtnq_u32_f32(vreinterpretq_f32_m128(sse2_clamp(g, maxValue)));
uint32x4_t bbbb = vcvtnq_u32_f32(vreinterpretq_f32_m128(sse2_clamp(b, maxValue)));
uint32x4_t aaaa = vcvtnq_u32_f32(vreinterpretq_f32_m128(sse2_clamp(a, maxValue)));

uint32x4_t rgrg = vsliq_n_u32(rrrr, gggg, 16);
uint32x4_t baba = vsliq_n_u32(bbbb, aaaa, 16);

vst1q_u16(out + 0, vreinterpretq_u16_u32(vzip1q_u32(rgrg, baba)));
vst1q_u16(out + 8, vreinterpretq_u16_u32(vzip2q_u32(rgrg, baba)));
#else
__m128i rrrr = _mm_cvtps_epi32(sse2_clamp(r, maxValue));
__m128i gggg = _mm_cvtps_epi32(sse2_clamp(g, maxValue));
__m128i bbbb = _mm_cvtps_epi32(sse2_clamp(b, maxValue));
Expand All @@ -269,6 +314,7 @@ struct SSE2RGBAPack16

_mm_storeu_si128((__m128i*)(out + 0), rgba_00_01);
_mm_storeu_si128((__m128i*)(out + 8), rgba_02_03);
#endif
}
};

Expand Down Expand Up @@ -316,50 +362,39 @@ struct SSE2RGBAPack<BIT_DEPTH_F16>
{
static inline void Load(const half *in, __m128& r, __m128& g, __m128& b, __m128& a)
{
#if OCIO_USE_SSE2NEON
// Load 4 interleaved RGBA half pixels directly into planar R, G, B, A.
uint16x4x4_t rgba = vld4_u16(reinterpret_cast<const uint16_t *>(in));
r = vreinterpretq_m128_f32(vcvt_f32_f16(vreinterpret_f16_u16(rgba.val[0])));
g = vreinterpretq_m128_f32(vcvt_f32_f16(vreinterpret_f16_u16(rgba.val[1])));
b = vreinterpretq_m128_f32(vcvt_f32_f16(vreinterpret_f16_u16(rgba.val[2])));
a = vreinterpretq_m128_f32(vcvt_f32_f16(vreinterpret_f16_u16(rgba.val[3])));
#else
__m128i rgba_00_01 = _mm_loadu_si128((const __m128i*)(in + 0));
__m128i rgba_02_03 = _mm_loadu_si128((const __m128i*)(in + 8));

#if OCIO_USE_SSE2NEON
// use neon hardware support for f16 to f32
__m128 rgba0 = vreinterpretq_m128_f32(
vcvt_f32_f16(vget_low_f16(vreinterpretq_f16_s64(vreinterpretq_s64_m128i(rgba_00_01))))
);
__m128 rgba1 = vreinterpretq_m128_f32(
vcvt_f32_f16(vget_high_f16(vreinterpretq_f16_s64(vreinterpretq_s64_m128i(rgba_00_01))))
);
__m128 rgba2 = vreinterpretq_m128_f32(
vcvt_f32_f16(vget_low_f16(vreinterpretq_f16_s64(vreinterpretq_s64_m128i(rgba_02_03))))
);
__m128 rgba3 = vreinterpretq_m128_f32(
vcvt_f32_f16(vget_high_f16(vreinterpretq_f16_s64(vreinterpretq_s64_m128i(rgba_02_03))))
);
#else
__m128 rgba0 = sse2_cvtph_ps(rgba_00_01);
__m128 rgba1 = sse2_cvtph_ps(_mm_shuffle_epi32(rgba_00_01, _MM_SHUFFLE(1,0,3,2)));
__m128 rgba2 = sse2_cvtph_ps(rgba_02_03);
__m128 rgba3 = sse2_cvtph_ps(_mm_shuffle_epi32(rgba_02_03, _MM_SHUFFLE(1,0,3,2)));
#endif
sse2RGBATranspose_4x4(rgba0, rgba1, rgba2, rgba3, r, g, b, a);
#endif
}

static inline void Store(half *out, __m128 r, __m128 g, __m128 b, __m128 a)
{
#if OCIO_USE_SSE2NEON
uint16x4x4_t rgba = {{
vreinterpret_u16_f16(vcvt_f16_f32(vreinterpretq_f32_m128(r))),
vreinterpret_u16_f16(vcvt_f16_f32(vreinterpretq_f32_m128(g))),
vreinterpret_u16_f16(vcvt_f16_f32(vreinterpretq_f32_m128(b))),
vreinterpret_u16_f16(vcvt_f16_f32(vreinterpretq_f32_m128(a)))
}};
vst4_u16(reinterpret_cast<uint16_t *>(out), rgba);
#else
__m128 rgba0, rgba1, rgba2, rgba3;
sse2RGBATranspose_4x4(r, g, b, a, rgba0, rgba1, rgba2, rgba3);

#if OCIO_USE_SSE2NEON
// use neon hardware support for f32 to f16 (apart from in MSVC, which doesnt support it)
float16x8_t rgba;
float16x4_t rgba00_01 = vcvt_f16_f32(vreinterpretq_f32_m128(rgba0));
float16x4_t rgba03_03 = vcvt_f16_f32(vreinterpretq_f32_m128(rgba1));
float16x4_t rgba04_05 = vcvt_f16_f32(vreinterpretq_f32_m128(rgba2));
float16x4_t rgba06_07 = vcvt_f16_f32(vreinterpretq_f32_m128(rgba3));
rgba = vcombine_f16(rgba00_01, rgba03_03);
vst1q_f16((float16_t *)(out+0), rgba);

rgba = vcombine_f16(rgba04_05, rgba06_07);
vst1q_f16((float16_t *)(out+8), rgba);
#else
__m128i rgba;
__m128i rgba00_01 = sse2_cvtps_ph(rgba0);
__m128i rgba02_03 = sse2_cvtps_ph(rgba1);
Expand All @@ -380,23 +415,41 @@ struct SSE2RGBAPack<BIT_DEPTH_F32>
{
static inline void Load(const float *in, __m128& r, __m128& g, __m128& b, __m128& a)
{
#if OCIO_USE_SSE2NEON
float32x4x4_t rgba = vld4q_f32(in);
r = vreinterpretq_m128_f32(rgba.val[0]);
g = vreinterpretq_m128_f32(rgba.val[1]);
b = vreinterpretq_m128_f32(rgba.val[2]);
a = vreinterpretq_m128_f32(rgba.val[3]);
#else
__m128 rgba0 = _mm_loadu_ps(in + 0);
__m128 rgba1 = _mm_loadu_ps(in + 4);
__m128 rgba2 = _mm_loadu_ps(in + 8);
__m128 rgba3 = _mm_loadu_ps(in + 12);

sse2RGBATranspose_4x4(rgba0, rgba1, rgba2, rgba3, r, g, b, a);
#endif
}

static inline void Store(float *out, __m128 r, __m128 g, __m128 b, __m128 a)
{
#if OCIO_USE_SSE2NEON
float32x4x4_t rgba = {{
vreinterpretq_f32_m128(r),
vreinterpretq_f32_m128(g),
vreinterpretq_f32_m128(b),
vreinterpretq_f32_m128(a)
}};
vst4q_f32(out, rgba);
#else
__m128 rgba0, rgba1, rgba2, rgba3;
sse2RGBATranspose_4x4(r, g, b, a, rgba0, rgba1, rgba2, rgba3);

_mm_storeu_ps(out + 0, rgba0);
_mm_storeu_ps(out + 4, rgba1);
_mm_storeu_ps(out + 8, rgba2);
_mm_storeu_ps(out + 12, rgba3);
#endif
}
};

Expand Down
57 changes: 44 additions & 13 deletions src/OpenColorIO/ops/lut1d/Lut1DOpCPU_SSE2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace OCIO_NAMESPACE

namespace {

#if !OCIO_USE_SSE2NEON
#define i32gather_ps_sse2(src, dst, idx, indices, buffer) \
_mm_store_si128((__m128i *)indices, idx); \
buffer[0] = (src)[indices[0]]; \
Expand All @@ -24,14 +25,9 @@ namespace {

static inline __m128 fmadd_ps_sse2(__m128 a, __m128 b, __m128 c)
{
#if OCIO_USE_SSE2NEON
return vreinterpretq_m128_f32(
vfmaq_f32(vreinterpretq_f32_m128(c), vreinterpretq_f32_m128(a), vreinterpretq_f32_m128(b))
);
#else
return _mm_add_ps(_mm_mul_ps(a, b), c);
#endif
}
#endif

static inline __m128 floor_ps_sse2(__m128 v)
{
Expand All @@ -44,7 +40,40 @@ static inline __m128 floor_ps_sse2(__m128 v)
}


static inline __m128 apply_lut_sse2(const float *lut, __m128 v, const __m128& scale, const __m128& lut_max)
#if OCIO_USE_SSE2NEON
static inline __m128 apply_lut_sse2(const float *lut, __m128 v, const __m128& scale, const __m128& lut_max, uint32_t max_idx)
{
__m128 zero = _mm_setzero_ps();
__m128 scaled = _mm_mul_ps(v, scale);

// clamp, max first, NAN set to zero
__m128 x = _mm_min_ps(_mm_max_ps(scaled, zero), lut_max);
__m128 prev_f = floor_ps_sse2(x);
__m128 d = _mm_sub_ps(x, prev_f);

uint32x4_t prev_i = vcvtq_u32_f32(vreinterpretq_f32_m128(prev_f));
uint32_t i0 = vgetq_lane_u32(prev_i, 0);
uint32_t i1 = vgetq_lane_u32(prev_i, 1);
uint32_t i2 = vgetq_lane_u32(prev_i, 2);
uint32_t i3 = vgetq_lane_u32(prev_i, 3);

uint32_t n0 = (i0 < max_idx) ? (i0 + 1u) : i0;
uint32_t n1 = (i1 < max_idx) ? (i1 + 1u) : i1;
uint32_t n2 = (i2 < max_idx) ? (i2 + 1u) : i2;
uint32_t n3 = (i3 < max_idx) ? (i3 + 1u) : i3;

float32x4_t p = vcombine_f32(
vset_lane_f32(lut[i1], vdup_n_f32(lut[i0]), 1),
vset_lane_f32(lut[i3], vdup_n_f32(lut[i2]), 1));
float32x4_t n = vcombine_f32(
vset_lane_f32(lut[n1], vdup_n_f32(lut[n0]), 1),
vset_lane_f32(lut[n3], vdup_n_f32(lut[n2]), 1));

// lerp: a + (b - a) * t;
return vreinterpretq_m128_f32(vfmaq_f32(p, vsubq_f32(n, p), vreinterpretq_f32_m128(d)));
}
#else
static inline __m128 apply_lut_sse2(const float *lut, __m128 v, const __m128& scale, const __m128& lut_max, uint32_t /*max_idx*/)
{
SSE2_ALIGN(uint32_t indices_p[4]);
SSE2_ALIGN(uint32_t indices_n[4]);
Expand Down Expand Up @@ -74,6 +103,7 @@ static inline __m128 apply_lut_sse2(const float *lut, __m128 v, const __m128& sc

return v;
}
#endif

template <BitDepth inBD, BitDepth outBD>
static inline void linear1D(const float *lutR, const float *lutG,const float *lutB, int dim, const void *inImg, void *outImg, long numPixels)
Expand All @@ -89,6 +119,7 @@ static inline void linear1D(const float *lutR, const float *lutG,const float *lu
float rgb_scale = 1.0f / (float)BitDepthInfo<inBD>::maxValue * ((float)dim -1);
const __m128 lut_scale = _mm_set1_ps(rgb_scale);
const __m128 lut_max = _mm_set1_ps((float)dim -1);
const uint32_t max_idx = (dim > 1) ? static_cast<uint32_t>(dim - 1) : 0u;

if (inBD != outBD)
alpha_scale = _mm_set1_ps((float)BitDepthInfo<outBD>::maxValue / (float)BitDepthInfo<inBD>::maxValue);
Expand All @@ -99,9 +130,9 @@ static inline void linear1D(const float *lutR, const float *lutG,const float *lu
for (int i = 0; i < pixel_count; i += 4 ) {
SSE2RGBAPack<inBD>::Load(src, r, g, b, a);

r = apply_lut_sse2(lutR, r, lut_scale, lut_max);
g = apply_lut_sse2(lutG, g, lut_scale, lut_max);
b = apply_lut_sse2(lutB, b, lut_scale, lut_max);
r = apply_lut_sse2(lutR, r, lut_scale, lut_max, max_idx);
g = apply_lut_sse2(lutG, g, lut_scale, lut_max, max_idx);
b = apply_lut_sse2(lutB, b, lut_scale, lut_max, max_idx);

if (inBD != outBD)
a = _mm_mul_ps(a, alpha_scale);
Expand Down Expand Up @@ -129,9 +160,9 @@ static inline void linear1D(const float *lutR, const float *lutG,const float *lu

SSE2RGBAPack<inBD>::Load(in_buf, r, g, b, a);

r = apply_lut_sse2(lutR, r, lut_scale, lut_max);
g = apply_lut_sse2(lutG, g, lut_scale, lut_max);
b = apply_lut_sse2(lutB, b, lut_scale, lut_max);
r = apply_lut_sse2(lutR, r, lut_scale, lut_max, max_idx);
g = apply_lut_sse2(lutG, g, lut_scale, lut_max, max_idx);
b = apply_lut_sse2(lutB, b, lut_scale, lut_max, max_idx);

if (inBD != outBD)
a = _mm_mul_ps(a, alpha_scale);
Expand Down
Loading