diff --git a/src/OpenColorIO/SSE2.h b/src/OpenColorIO/SSE2.h index 012862340..0d332973c 100644 --- a/src/OpenColorIO/SSE2.h +++ b/src/OpenColorIO/SSE2.h @@ -201,6 +201,18 @@ struct SSE2RGBAPack { 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); @@ -214,11 +226,23 @@ struct SSE2RGBAPack __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)); @@ -229,6 +253,7 @@ struct SSE2RGBAPack 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 } }; @@ -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)); @@ -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::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)); @@ -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 } }; @@ -316,50 +362,39 @@ struct SSE2RGBAPack { 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(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(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); @@ -380,16 +415,33 @@ struct SSE2RGBAPack { 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); @@ -397,6 +449,7 @@ struct SSE2RGBAPack _mm_storeu_ps(out + 4, rgba1); _mm_storeu_ps(out + 8, rgba2); _mm_storeu_ps(out + 12, rgba3); +#endif } }; diff --git a/src/OpenColorIO/ops/lut1d/Lut1DOpCPU_SSE2.cpp b/src/OpenColorIO/ops/lut1d/Lut1DOpCPU_SSE2.cpp index c170e1791..2a9a6bcc8 100644 --- a/src/OpenColorIO/ops/lut1d/Lut1DOpCPU_SSE2.cpp +++ b/src/OpenColorIO/ops/lut1d/Lut1DOpCPU_SSE2.cpp @@ -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]]; \ @@ -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) { @@ -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]); @@ -74,6 +103,7 @@ static inline __m128 apply_lut_sse2(const float *lut, __m128 v, const __m128& sc return v; } +#endif template static inline void linear1D(const float *lutR, const float *lutG,const float *lutB, int dim, const void *inImg, void *outImg, long numPixels) @@ -89,6 +119,7 @@ static inline void linear1D(const float *lutR, const float *lutG,const float *lu float rgb_scale = 1.0f / (float)BitDepthInfo::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(dim - 1) : 0u; if (inBD != outBD) alpha_scale = _mm_set1_ps((float)BitDepthInfo::maxValue / (float)BitDepthInfo::maxValue); @@ -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::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); @@ -129,9 +160,9 @@ static inline void linear1D(const float *lutR, const float *lutG,const float *lu SSE2RGBAPack::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); diff --git a/src/OpenColorIO/ops/lut3d/Lut3DOpCPU.cpp b/src/OpenColorIO/ops/lut3d/Lut3DOpCPU.cpp index d7ea8483a..0272da46a 100644 --- a/src/OpenColorIO/ops/lut3d/Lut3DOpCPU.cpp +++ b/src/OpenColorIO/ops/lut3d/Lut3DOpCPU.cpp @@ -196,6 +196,7 @@ class InvLut3DRenderer : public OpCPU #if OCIO_USE_SSE2 +#if !OCIO_USE_SSE2NEON //---------------------------------------------------------------------------- // RGB channel ordering. // Pixels ordered in such a way that the blue coordinate changes fastest, @@ -262,6 +263,7 @@ inline void LookupNearest4(float* optLut, res[2] = _mm_load_ps(optLut + offsetInt[2]); res[3] = _mm_load_ps(optLut + offsetInt[3]); } +#endif // !OCIO_USE_SSE2NEON #else @@ -641,7 +643,13 @@ void Lut3DRenderer::apply(const void * inImg, void * outImg, long numPixels) con __m128 step = _mm_set1_ps(m_step); __m128 maxIdx = _mm_set1_ps((float)(m_dim - 1)); +#if OCIO_USE_SSE2NEON + const size_t dim_sz = static_cast(m_dim); + const uint32_t maxIdxU = (m_dim > 1) ? static_cast(m_dim - 1) : 0u; + const float * const optLut = m_optLut; +#else __m128i dim = _mm_set1_epi32(m_dim); +#endif __m128 v[8]; @@ -649,13 +657,43 @@ void Lut3DRenderer::apply(const void * inImg, void * outImg, long numPixels) con { float newAlpha = (float)in[3]; - __m128 data = _mm_set_ps(in[3], in[2], in[1], in[0]); + __m128 data = _mm_loadu_ps(in); __m128 idx = _mm_mul_ps(data, step); idx = _mm_max_ps(idx, EZERO); // NaNs become 0 idx = _mm_min_ps(idx, maxIdx); +#if OCIO_USE_SSE2NEON + float32x4_t idx_f = vreinterpretq_f32_m128(idx); + float32x4_t lowIdx_f = vrndmq_f32(idx_f); + uint32x4_t lowIdx_u = vcvtq_u32_f32(lowIdx_f); + __m128 delta = vreinterpretq_m128_f32(vsubq_f32(idx_f, lowIdx_f)); + + const uint32_t l0 = vgetq_lane_u32(lowIdx_u, 0); + const uint32_t l1 = vgetq_lane_u32(lowIdx_u, 1); + const uint32_t l2 = vgetq_lane_u32(lowIdx_u, 2); + const uint32_t h0 = (l0 < maxIdxU) ? (l0 + 1u) : l0; + const uint32_t h1 = (l1 < maxIdxU) ? (l1 + 1u) : l1; + const uint32_t h2 = (l2 < maxIdxU) ? (l2 + 1u) : l2; + + const size_t r0 = static_cast(l0) * dim_sz; + const size_t r1 = static_cast(h0) * dim_sz; + const size_t rg00 = (r0 + static_cast(l1)) * dim_sz; + const size_t rg01 = (r0 + static_cast(h1)) * dim_sz; + const size_t rg10 = (r1 + static_cast(l1)) * dim_sz; + const size_t rg11 = (r1 + static_cast(h1)) * dim_sz; + + // Each voxel is 4 floats (16 bytes), folding lsl #4 directly into ldr q. + v[0] = vreinterpretq_m128_f32(vld1q_f32(optLut + (rg00 + l2) * 4)); + v[1] = vreinterpretq_m128_f32(vld1q_f32(optLut + (rg00 + h2) * 4)); + v[2] = vreinterpretq_m128_f32(vld1q_f32(optLut + (rg01 + l2) * 4)); + v[3] = vreinterpretq_m128_f32(vld1q_f32(optLut + (rg01 + h2) * 4)); + v[4] = vreinterpretq_m128_f32(vld1q_f32(optLut + (rg10 + l2) * 4)); + v[5] = vreinterpretq_m128_f32(vld1q_f32(optLut + (rg10 + h2) * 4)); + v[6] = vreinterpretq_m128_f32(vld1q_f32(optLut + (rg11 + l2) * 4)); + v[7] = vreinterpretq_m128_f32(vld1q_f32(optLut + (rg11 + h2) * 4)); +#else // lowIdxInt32 = floor(idx), with lowIdx in [0, maxIdx] __m128i lowIdxInt32 = _mm_cvttps_epi32(idx); __m128 lowIdx = _mm_cvtepi32_ps(lowIdxInt32); @@ -696,6 +734,7 @@ void Lut3DRenderer::apply(const void * inImg, void * outImg, long numPixels) con // Lookup 8 corners of cube LookupNearest4(m_optLut, idxR_L0, idxG, idxB, dim, v); LookupNearest4(m_optLut, idxR_H0, idxG, idxB, dim, v + 4); +#endif // Perform the trilinear interpolation __m128 wr = _mm_shuffle_ps(delta, delta, _MM_SHUFFLE(0, 0, 0, 0)); diff --git a/src/OpenColorIO/ops/lut3d/Lut3DOpCPU_SSE2.cpp b/src/OpenColorIO/ops/lut3d/Lut3DOpCPU_SSE2.cpp index e7b338d36..752d98348 100644 --- a/src/OpenColorIO/ops/lut3d/Lut3DOpCPU_SSE2.cpp +++ b/src/OpenColorIO/ops/lut3d/Lut3DOpCPU_SSE2.cpp @@ -22,6 +22,30 @@ struct rgbavec_sse2 { __m128 r, g, b, a; }; +#if OCIO_USE_SSE2NEON +static inline void gather_rgb_neon(const float *src, __m128i idx, + __m128 &sample_r, __m128 &sample_g, __m128 &sample_b) +{ + // idx holds 16-byte RGBA voxel indices; widen and shift by 4 (16 bytes/voxel) directly + // into 64-bit byte offsets to avoid per-lane GPR shifts before ldr q. + const uint8_t *base = reinterpret_cast(src); + uint32x4_t idx_u32 = vreinterpretq_u32_m128i(idx); + uint64x2_t off_lo = vshll_n_u32(vget_low_u32(idx_u32), 4); + uint64x2_t off_hi = vshll_n_u32(vget_high_u32(idx_u32), 4); + float32x4_t r0 = vld1q_f32(reinterpret_cast(base + vgetq_lane_u64(off_lo, 0))); + float32x4_t r1 = vld1q_f32(reinterpret_cast(base + vgetq_lane_u64(off_lo, 1))); + float32x4_t r2 = vld1q_f32(reinterpret_cast(base + vgetq_lane_u64(off_hi, 0))); + float32x4_t r3 = vld1q_f32(reinterpret_cast(base + vgetq_lane_u64(off_hi, 1))); + float32x4_t t0 = vzip1q_f32(r0, r1); + float32x4_t t2 = vzip1q_f32(r2, r3); + float32x4_t t1 = vzip2q_f32(r0, r1); + float32x4_t t3 = vzip2q_f32(r2, r3); + sample_r = vreinterpretq_m128_f64(vzip1q_f64(vreinterpretq_f64_f32(t0), vreinterpretq_f64_f32(t2))); + sample_g = vreinterpretq_m128_f64(vzip2q_f64(vreinterpretq_f64_f32(t0), vreinterpretq_f64_f32(t2))); + sample_b = vreinterpretq_m128_f64(vzip1q_f64(vreinterpretq_f64_f32(t1), vreinterpretq_f64_f32(t3))); +} +#define gather_rgb_sse2(src, idx) gather_rgb_neon(src, idx, sample_r, sample_g, sample_b) +#else #define gather_rgb_sse2(src, idx) \ _mm_store_si128((__m128i *)indices, idx); \ row0 = _mm_loadu_ps(src + indices[0]); \ @@ -35,6 +59,7 @@ struct rgbavec_sse2 { sample_r = _mm_movelh_ps(tmp0, tmp2); \ sample_g = _mm_movehl_ps(tmp2, tmp0); \ sample_b = _mm_movelh_ps(tmp1, tmp3) +#endif static inline __m128 floor_ps_sse2(__m128 v) { @@ -49,7 +74,12 @@ static inline __m128 floor_ps_sse2(__m128 v) static inline __m128 blendv_ps_sse2(__m128 a, __m128 b, __m128 mask) { #if OCIO_USE_SSE2NEON - return _mm_blendv_ps(a, b, mask); + // Callers always pass full-lane comparison masks (0x00000000 / 0xFFFFFFFF), + // so vbslq_f32 avoids the sign-bit broadcast shift emitted by _mm_blendv_ps. + return vreinterpretq_m128_f32( + vbslq_f32(vreinterpretq_u32_m128(mask), + vreinterpretq_f32_m128(b), + vreinterpretq_f32_m128(a))); #else return _mm_xor_ps(_mm_and_ps(_mm_xor_ps(a, b), mask), a); #endif @@ -68,15 +98,17 @@ static inline __m128 fmadd_ps_sse2(__m128 a, __m128 b, __m128 c) static inline rgbavec_sse2 interp_tetrahedral_sse2(const Lut3DContextSSE2 &ctx, __m128 r, __m128 g, __m128 b, __m128 a) { +#if !OCIO_USE_SSE2NEON SSE2_ALIGN(uint32_t indices[4]); + __m128 tmp0, tmp1, tmp2, tmp3; + __m128 row0, row1, row2, row3; +#endif __m128 x0, x1, x2; __m128 cxxxa; __m128 cxxxb; __m128 mask; - __m128 tmp0, tmp1, tmp2, tmp3; - __m128 row0, row1, row2, row3; __m128 sample_r, sample_g, sample_b; rgbavec_sse2 result; @@ -86,7 +118,9 @@ static inline rgbavec_sse2 interp_tetrahedral_sse2(const Lut3DContextSSE2 &ctx, __m128 lutsize2 = ctx.lutsize2; __m128 one_f = _mm_set1_ps(1.0f); +#if !OCIO_USE_SSE2NEON __m128 four_f = _mm_set1_ps(4.0f); +#endif __m128 prev_r = floor_ps_sse2(r); __m128 prev_g = floor_ps_sse2(g); @@ -108,8 +142,10 @@ static inline rgbavec_sse2 interp_tetrahedral_sse2(const Lut3DContextSSE2 &ctx, prev_g = _mm_mul_ps(prev_g, lutsize); next_g = _mm_mul_ps(next_g, lutsize); +#if !OCIO_USE_SSE2NEON prev_b = _mm_mul_ps(prev_b, four_f); next_b = _mm_mul_ps(next_b, four_f); +#endif // This is the tetrahedral blend equation // red = (1-x0) * c000.r + (x0-x1) * cxxxa.r + (x1-x2) * cxxxb.r + x2 * c111.r; @@ -235,8 +271,13 @@ static inline void applyTetrahedralSSE2Func(const float *lut3d, int dim, const f ctx.lut = lut3d; ctx.lutmax = _mm_set1_ps(lutmax); +#if OCIO_USE_SSE2NEON + ctx.lutsize = _mm_set1_ps((float)dim); + ctx.lutsize2 = _mm_set1_ps((float)dim * dim); +#else ctx.lutsize = _mm_set1_ps((float)dim * 4); ctx.lutsize2 = _mm_set1_ps((float)dim * dim * 4); +#endif int pixel_count = total_pixel_count / 4 * 4; int remainder = total_pixel_count - pixel_count; diff --git a/src/OpenColorIO/ops/matrix/MatrixOpCPU.cpp b/src/OpenColorIO/ops/matrix/MatrixOpCPU.cpp index a30e0d68e..796b88c2a 100644 --- a/src/OpenColorIO/ops/matrix/MatrixOpCPU.cpp +++ b/src/OpenColorIO/ops/matrix/MatrixOpCPU.cpp @@ -92,6 +92,18 @@ void ScaleRenderer::apply(const void * inImg, void * outImg, long numPixels) con const float * in = (const float *)inImg; float * out = (float *)outImg; +#if OCIO_USE_SSE2 + __m128 scale = _mm_loadu_ps(m_scale); + + for (long idx = 0; idx < numPixels; ++idx) + { + __m128 rgba = _mm_loadu_ps(in); + _mm_storeu_ps(out, _mm_mul_ps(rgba, scale)); + + in += 4; + out += 4; + } +#else for (long idx = 0; idx < numPixels; ++idx) { out[0] = in[0] * m_scale[0]; @@ -102,6 +114,7 @@ void ScaleRenderer::apply(const void * inImg, void * outImg, long numPixels) con in += 4; out += 4; } +#endif } ScaleWithOffsetRenderer::ScaleWithOffsetRenderer(ConstMatrixOpDataRcPtr & mat) @@ -127,6 +140,19 @@ void ScaleWithOffsetRenderer::apply(const void * inImg, void * outImg, long numP const float * in = (const float *)inImg; float * out = (float *)outImg; +#if OCIO_USE_SSE2 + __m128 scale = _mm_loadu_ps(m_scale); + __m128 offset = _mm_loadu_ps(m_offset); + + for (long idx = 0; idx < numPixels; ++idx) + { + __m128 rgba = _mm_loadu_ps(in); + _mm_storeu_ps(out, _mm_add_ps(_mm_mul_ps(rgba, scale), offset)); + + in += 4; + out += 4; + } +#else for (long idx = 0; idx < numPixels; ++idx) { out[0] = in[0] * m_scale[0] + m_offset[0]; @@ -137,6 +163,7 @@ void ScaleWithOffsetRenderer::apply(const void * inImg, void * outImg, long numP in += 4; out += 4; } +#endif } MatrixWithOffsetRenderer::MatrixWithOffsetRenderer(ConstMatrixOpDataRcPtr & mat)