michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "ImageScaling.h" michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: #include "SSEHelpers.h" michael@0: michael@0: /* The functions below use the following system for averaging 4 pixels: michael@0: * michael@0: * The first observation is that a half-adder is implemented as follows: michael@0: * R = S + 2C or in the case of a and b (a ^ b) + ((a & b) << 1); michael@0: * michael@0: * This can be trivially extended to three pixels by observaring that when michael@0: * doing (a ^ b ^ c) as the sum, the carry is simply the bitwise-or of the michael@0: * carries of the individual numbers, since the sum of 3 bits can only ever michael@0: * have a carry of one. michael@0: * michael@0: * We then observe that the average is then ((carry << 1) + sum) >> 1, or, michael@0: * assuming eliminating overflows and underflows, carry + (sum >> 1). michael@0: * michael@0: * We now average our existing sum with the fourth number, so we get: michael@0: * sum2 = (sum + d) >> 1 or (sum >> 1) + (d >> 1). michael@0: * michael@0: * We now observe that our sum has been moved into place relative to the michael@0: * carry, so we can now average with the carry to get the final 4 input michael@0: * average: avg = (sum2 + carry) >> 1; michael@0: * michael@0: * Or to reverse the proof: michael@0: * avg = ((sum >> 1) + carry + d >> 1) >> 1 michael@0: * avg = ((a + b + c) >> 1 + d >> 1) >> 1 michael@0: * avg = ((a + b + c + d) >> 2) michael@0: * michael@0: * An additional fact used in the SSE versions is the concept that we can michael@0: * trivially convert a rounded average to a truncated average: michael@0: * michael@0: * We have: michael@0: * f(a, b) = (a + b + 1) >> 1 michael@0: * michael@0: * And want: michael@0: * g(a, b) = (a + b) >> 1 michael@0: * michael@0: * Observe: michael@0: * ~f(~a, ~b) == ~((~a + ~b + 1) >> 1) michael@0: * == ~((-a - 1 + -b - 1 + 1) >> 1) michael@0: * == ~((-a - 1 + -b) >> 1) michael@0: * == ~((-(a + b) - 1) >> 1) michael@0: * == ~((~(a + b)) >> 1) michael@0: * == (a + b) >> 1 michael@0: * == g(a, b) michael@0: */ michael@0: michael@0: MOZ_ALWAYS_INLINE __m128i _mm_not_si128(__m128i arg) michael@0: { michael@0: __m128i minusone = _mm_set1_epi32(0xffffffff); michael@0: return _mm_xor_si128(arg, minusone); michael@0: } michael@0: michael@0: /* We have to pass pointers here, MSVC does not allow passing more than 3 michael@0: * __m128i arguments on the stack. And it does not allow 16-byte aligned michael@0: * stack variables. This inlines properly on MSVC 2010. It does -not- inline michael@0: * with just the inline directive. michael@0: */ michael@0: MOZ_ALWAYS_INLINE __m128i avg_sse2_8x2(__m128i *a, __m128i *b, __m128i *c, __m128i *d) michael@0: { michael@0: #define shuf1 _MM_SHUFFLE(2, 0, 2, 0) michael@0: #define shuf2 _MM_SHUFFLE(3, 1, 3, 1) michael@0: michael@0: // This cannot be an inline function as the __Imm argument to _mm_shuffle_ps michael@0: // needs to be a compile time constant. michael@0: #define shuffle_si128(arga, argb, imm) \ michael@0: _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps((arga)), _mm_castsi128_ps((argb)), (imm))); michael@0: michael@0: __m128i t = shuffle_si128(*a, *b, shuf1); michael@0: *b = shuffle_si128(*a, *b, shuf2); michael@0: *a = t; michael@0: t = shuffle_si128(*c, *d, shuf1); michael@0: *d = shuffle_si128(*c, *d, shuf2); michael@0: *c = t; michael@0: michael@0: #undef shuf1 michael@0: #undef shuf2 michael@0: #undef shuffle_si128 michael@0: michael@0: __m128i sum = _mm_xor_si128(*a, _mm_xor_si128(*b, *c)); michael@0: michael@0: __m128i carry = _mm_or_si128(_mm_and_si128(*a, *b), _mm_or_si128(_mm_and_si128(*a, *c), _mm_and_si128(*b, *c))); michael@0: michael@0: sum = _mm_avg_epu8(_mm_not_si128(sum), _mm_not_si128(*d)); michael@0: michael@0: return _mm_not_si128(_mm_avg_epu8(sum, _mm_not_si128(carry))); michael@0: } michael@0: michael@0: MOZ_ALWAYS_INLINE __m128i avg_sse2_4x2_4x1(__m128i a, __m128i b) michael@0: { michael@0: return _mm_not_si128(_mm_avg_epu8(_mm_not_si128(a), _mm_not_si128(b))); michael@0: } michael@0: michael@0: MOZ_ALWAYS_INLINE __m128i avg_sse2_8x1_4x1(__m128i a, __m128i b) michael@0: { michael@0: __m128i t = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), _MM_SHUFFLE(3, 1, 3, 1))); michael@0: b = _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), _MM_SHUFFLE(2, 0, 2, 0))); michael@0: a = t; michael@0: michael@0: return _mm_not_si128(_mm_avg_epu8(_mm_not_si128(a), _mm_not_si128(b))); michael@0: } michael@0: michael@0: MOZ_ALWAYS_INLINE uint32_t Avg2x2(uint32_t a, uint32_t b, uint32_t c, uint32_t d) michael@0: { michael@0: uint32_t sum = a ^ b ^ c; michael@0: uint32_t carry = (a & b) | (a & c) | (b & c); michael@0: michael@0: uint32_t mask = 0xfefefefe; michael@0: michael@0: // Not having a byte based average instruction means we should mask to avoid michael@0: // underflow. michael@0: sum = (((sum ^ d) & mask) >> 1) + (sum & d); michael@0: michael@0: return (((sum ^ carry) & mask) >> 1) + (sum & carry); michael@0: } michael@0: michael@0: // Simple 2 pixel average version of the function above. michael@0: MOZ_ALWAYS_INLINE uint32_t Avg2(uint32_t a, uint32_t b) michael@0: { michael@0: uint32_t sum = a ^ b; michael@0: uint32_t carry = (a & b); michael@0: michael@0: uint32_t mask = 0xfefefefe; michael@0: michael@0: return ((sum & mask) >> 1) + carry; michael@0: } michael@0: michael@0: namespace mozilla { michael@0: namespace gfx { michael@0: michael@0: void michael@0: ImageHalfScaler::HalfImage2D_SSE2(uint8_t *aSource, int32_t aSourceStride, michael@0: const IntSize &aSourceSize, uint8_t *aDest, michael@0: uint32_t aDestStride) michael@0: { michael@0: const int Bpp = 4; michael@0: michael@0: for (int y = 0; y < aSourceSize.height; y += 2) { michael@0: __m128i *storage = (__m128i*)(aDest + (y / 2) * aDestStride); michael@0: int x = 0; michael@0: // Run a loop depending on alignment. michael@0: if (!(uintptr_t(aSource + (y * aSourceStride)) % 16) && michael@0: !(uintptr_t(aSource + ((y + 1) * aSourceStride)) % 16)) { michael@0: for (; x < (aSourceSize.width - 7); x += 8) { michael@0: __m128i *upperRow = (__m128i*)(aSource + (y * aSourceStride + x * Bpp)); michael@0: __m128i *lowerRow = (__m128i*)(aSource + ((y + 1) * aSourceStride + x * Bpp)); michael@0: michael@0: __m128i a = _mm_load_si128(upperRow); michael@0: __m128i b = _mm_load_si128(upperRow + 1); michael@0: __m128i c = _mm_load_si128(lowerRow); michael@0: __m128i d = _mm_load_si128(lowerRow + 1); michael@0: michael@0: *storage++ = avg_sse2_8x2(&a, &b, &c, &d); michael@0: } michael@0: } else if (!(uintptr_t(aSource + (y * aSourceStride)) % 16)) { michael@0: for (; x < (aSourceSize.width - 7); x += 8) { michael@0: __m128i *upperRow = (__m128i*)(aSource + (y * aSourceStride + x * Bpp)); michael@0: __m128i *lowerRow = (__m128i*)(aSource + ((y + 1) * aSourceStride + x * Bpp)); michael@0: michael@0: __m128i a = _mm_load_si128(upperRow); michael@0: __m128i b = _mm_load_si128(upperRow + 1); michael@0: __m128i c = loadUnaligned128(lowerRow); michael@0: __m128i d = loadUnaligned128(lowerRow + 1); michael@0: michael@0: *storage++ = avg_sse2_8x2(&a, &b, &c, &d); michael@0: } michael@0: } else if (!(uintptr_t(aSource + ((y + 1) * aSourceStride)) % 16)) { michael@0: for (; x < (aSourceSize.width - 7); x += 8) { michael@0: __m128i *upperRow = (__m128i*)(aSource + (y * aSourceStride + x * Bpp)); michael@0: __m128i *lowerRow = (__m128i*)(aSource + ((y + 1) * aSourceStride + x * Bpp)); michael@0: michael@0: __m128i a = loadUnaligned128((__m128i*)upperRow); michael@0: __m128i b = loadUnaligned128((__m128i*)upperRow + 1); michael@0: __m128i c = _mm_load_si128((__m128i*)lowerRow); michael@0: __m128i d = _mm_load_si128((__m128i*)lowerRow + 1); michael@0: michael@0: *storage++ = avg_sse2_8x2(&a, &b, &c, &d); michael@0: } michael@0: } else { michael@0: for (; x < (aSourceSize.width - 7); x += 8) { michael@0: __m128i *upperRow = (__m128i*)(aSource + (y * aSourceStride + x * Bpp)); michael@0: __m128i *lowerRow = (__m128i*)(aSource + ((y + 1) * aSourceStride + x * Bpp)); michael@0: michael@0: __m128i a = loadUnaligned128(upperRow); michael@0: __m128i b = loadUnaligned128(upperRow + 1); michael@0: __m128i c = loadUnaligned128(lowerRow); michael@0: __m128i d = loadUnaligned128(lowerRow + 1); michael@0: michael@0: *storage++ = avg_sse2_8x2(&a, &b, &c, &d); michael@0: } michael@0: } michael@0: michael@0: uint32_t *unalignedStorage = (uint32_t*)storage; michael@0: // Take care of the final pixels, we know there's an even number of pixels michael@0: // in the source rectangle. We use a 2x2 'simd' implementation for this. michael@0: // michael@0: // Potentially we only have to do this in the last row since overflowing michael@0: // 8 pixels in an earlier row would appear to be harmless as it doesn't michael@0: // touch invalid memory. Even when reading and writing to the same surface. michael@0: // in practice we only do this when doing an additional downscale pass, and michael@0: // in this situation we have unused stride to write into harmlessly. michael@0: // I do not believe the additional code complexity would be worth it though. michael@0: for (; x < aSourceSize.width; x += 2) { michael@0: uint8_t *upperRow = aSource + (y * aSourceStride + x * Bpp); michael@0: uint8_t *lowerRow = aSource + ((y + 1) * aSourceStride + x * Bpp); michael@0: michael@0: *unalignedStorage++ = Avg2x2(*(uint32_t*)upperRow, *((uint32_t*)upperRow + 1), michael@0: *(uint32_t*)lowerRow, *((uint32_t*)lowerRow + 1)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: ImageHalfScaler::HalfImageVertical_SSE2(uint8_t *aSource, int32_t aSourceStride, michael@0: const IntSize &aSourceSize, uint8_t *aDest, michael@0: uint32_t aDestStride) michael@0: { michael@0: for (int y = 0; y < aSourceSize.height; y += 2) { michael@0: __m128i *storage = (__m128i*)(aDest + (y / 2) * aDestStride); michael@0: int x = 0; michael@0: // Run a loop depending on alignment. michael@0: if (!(uintptr_t(aSource + (y * aSourceStride)) % 16) && michael@0: !(uintptr_t(aSource + ((y + 1) * aSourceStride)) % 16)) { michael@0: for (; x < (aSourceSize.width - 3); x += 4) { michael@0: uint8_t *upperRow = aSource + (y * aSourceStride + x * 4); michael@0: uint8_t *lowerRow = aSource + ((y + 1) * aSourceStride + x * 4); michael@0: michael@0: __m128i a = _mm_load_si128((__m128i*)upperRow); michael@0: __m128i b = _mm_load_si128((__m128i*)lowerRow); michael@0: michael@0: *storage++ = avg_sse2_4x2_4x1(a, b); michael@0: } michael@0: } else if (!(uintptr_t(aSource + (y * aSourceStride)) % 16)) { michael@0: // This line doesn't align well. michael@0: for (; x < (aSourceSize.width - 3); x += 4) { michael@0: uint8_t *upperRow = aSource + (y * aSourceStride + x * 4); michael@0: uint8_t *lowerRow = aSource + ((y + 1) * aSourceStride + x * 4); michael@0: michael@0: __m128i a = _mm_load_si128((__m128i*)upperRow); michael@0: __m128i b = loadUnaligned128((__m128i*)lowerRow); michael@0: michael@0: *storage++ = avg_sse2_4x2_4x1(a, b); michael@0: } michael@0: } else if (!(uintptr_t(aSource + (y * aSourceStride)) % 16)) { michael@0: for (; x < (aSourceSize.width - 3); x += 4) { michael@0: uint8_t *upperRow = aSource + (y * aSourceStride + x * 4); michael@0: uint8_t *lowerRow = aSource + ((y + 1) * aSourceStride + x * 4); michael@0: michael@0: __m128i a = loadUnaligned128((__m128i*)upperRow); michael@0: __m128i b = _mm_load_si128((__m128i*)lowerRow); michael@0: michael@0: *storage++ = avg_sse2_4x2_4x1(a, b); michael@0: } michael@0: } else { michael@0: for (; x < (aSourceSize.width - 3); x += 4) { michael@0: uint8_t *upperRow = aSource + (y * aSourceStride + x * 4); michael@0: uint8_t *lowerRow = aSource + ((y + 1) * aSourceStride + x * 4); michael@0: michael@0: __m128i a = loadUnaligned128((__m128i*)upperRow); michael@0: __m128i b = loadUnaligned128((__m128i*)lowerRow); michael@0: michael@0: *storage++ = avg_sse2_4x2_4x1(a, b); michael@0: } michael@0: } michael@0: michael@0: uint32_t *unalignedStorage = (uint32_t*)storage; michael@0: // Take care of the final pixels, we know there's an even number of pixels michael@0: // in the source rectangle. michael@0: // michael@0: // Similar overflow considerations are valid as in the previous function. michael@0: for (; x < aSourceSize.width; x++) { michael@0: uint8_t *upperRow = aSource + (y * aSourceStride + x * 4); michael@0: uint8_t *lowerRow = aSource + ((y + 1) * aSourceStride + x * 4); michael@0: michael@0: *unalignedStorage++ = Avg2(*(uint32_t*)upperRow, *(uint32_t*)lowerRow); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: ImageHalfScaler::HalfImageHorizontal_SSE2(uint8_t *aSource, int32_t aSourceStride, michael@0: const IntSize &aSourceSize, uint8_t *aDest, michael@0: uint32_t aDestStride) michael@0: { michael@0: for (int y = 0; y < aSourceSize.height; y++) { michael@0: __m128i *storage = (__m128i*)(aDest + (y * aDestStride)); michael@0: int x = 0; michael@0: // Run a loop depending on alignment. michael@0: if (!(uintptr_t(aSource + (y * aSourceStride)) % 16)) { michael@0: for (; x < (aSourceSize.width - 7); x += 8) { michael@0: __m128i* pixels = (__m128i*)(aSource + (y * aSourceStride + x * 4)); michael@0: michael@0: __m128i a = _mm_load_si128(pixels); michael@0: __m128i b = _mm_load_si128(pixels + 1); michael@0: michael@0: *storage++ = avg_sse2_8x1_4x1(a, b); michael@0: } michael@0: } else { michael@0: for (; x < (aSourceSize.width - 7); x += 8) { michael@0: __m128i* pixels = (__m128i*)(aSource + (y * aSourceStride + x * 4)); michael@0: michael@0: __m128i a = loadUnaligned128(pixels); michael@0: __m128i b = loadUnaligned128(pixels + 1); michael@0: michael@0: *storage++ = avg_sse2_8x1_4x1(a, b); michael@0: } michael@0: } michael@0: michael@0: uint32_t *unalignedStorage = (uint32_t*)storage; michael@0: // Take care of the final pixels, we know there's an even number of pixels michael@0: // in the source rectangle. michael@0: // michael@0: // Similar overflow considerations are valid as in the previous function. michael@0: for (; x < aSourceSize.width; x += 2) { michael@0: uint32_t *pixels = (uint32_t*)(aSource + (y * aSourceStride + x * 4)); michael@0: michael@0: *unalignedStorage++ = Avg2(*pixels, *(pixels + 1)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: } michael@0: }