media/libyuv/source/compare_win.cc

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /*
michael@0 2 * Copyright 2012 The LibYuv Project Authors. All rights reserved.
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license
michael@0 5 * that can be found in the LICENSE file in the root of the source
michael@0 6 * tree. An additional intellectual property rights grant can be found
michael@0 7 * in the file PATENTS. All contributing project authors may
michael@0 8 * be found in the AUTHORS file in the root of the source tree.
michael@0 9 */
michael@0 10
michael@0 11 #include "libyuv/basic_types.h"
michael@0 12 #include "libyuv/row.h"
michael@0 13
michael@0 14 #ifdef __cplusplus
michael@0 15 namespace libyuv {
michael@0 16 extern "C" {
michael@0 17 #endif
michael@0 18
michael@0 19 #if !defined(LIBYUV_DISABLE_X86) && defined(_M_IX86) && defined(_MSC_VER)
michael@0 20
michael@0 21 __declspec(naked) __declspec(align(16))
michael@0 22 uint32 SumSquareError_SSE2(const uint8* src_a, const uint8* src_b, int count) {
michael@0 23 __asm {
michael@0 24 mov eax, [esp + 4] // src_a
michael@0 25 mov edx, [esp + 8] // src_b
michael@0 26 mov ecx, [esp + 12] // count
michael@0 27 pxor xmm0, xmm0
michael@0 28 pxor xmm5, xmm5
michael@0 29
michael@0 30 align 4
michael@0 31 wloop:
michael@0 32 movdqa xmm1, [eax]
michael@0 33 lea eax, [eax + 16]
michael@0 34 movdqa xmm2, [edx]
michael@0 35 lea edx, [edx + 16]
michael@0 36 sub ecx, 16
michael@0 37 movdqa xmm3, xmm1 // abs trick
michael@0 38 psubusb xmm1, xmm2
michael@0 39 psubusb xmm2, xmm3
michael@0 40 por xmm1, xmm2
michael@0 41 movdqa xmm2, xmm1
michael@0 42 punpcklbw xmm1, xmm5
michael@0 43 punpckhbw xmm2, xmm5
michael@0 44 pmaddwd xmm1, xmm1
michael@0 45 pmaddwd xmm2, xmm2
michael@0 46 paddd xmm0, xmm1
michael@0 47 paddd xmm0, xmm2
michael@0 48 jg wloop
michael@0 49
michael@0 50 pshufd xmm1, xmm0, 0xee
michael@0 51 paddd xmm0, xmm1
michael@0 52 pshufd xmm1, xmm0, 0x01
michael@0 53 paddd xmm0, xmm1
michael@0 54 movd eax, xmm0
michael@0 55 ret
michael@0 56 }
michael@0 57 }
michael@0 58
michael@0 59 // Visual C 2012 required for AVX2.
michael@0 60 #if _MSC_VER >= 1700
michael@0 61 // C4752: found Intel(R) Advanced Vector Extensions; consider using /arch:AVX.
michael@0 62 #pragma warning(disable: 4752)
michael@0 63 __declspec(naked) __declspec(align(16))
michael@0 64 uint32 SumSquareError_AVX2(const uint8* src_a, const uint8* src_b, int count) {
michael@0 65 __asm {
michael@0 66 mov eax, [esp + 4] // src_a
michael@0 67 mov edx, [esp + 8] // src_b
michael@0 68 mov ecx, [esp + 12] // count
michael@0 69 vpxor ymm0, ymm0, ymm0 // sum
michael@0 70 vpxor ymm5, ymm5, ymm5 // constant 0 for unpck
michael@0 71 sub edx, eax
michael@0 72
michael@0 73 align 4
michael@0 74 wloop:
michael@0 75 vmovdqu ymm1, [eax]
michael@0 76 vmovdqu ymm2, [eax + edx]
michael@0 77 lea eax, [eax + 32]
michael@0 78 sub ecx, 32
michael@0 79 vpsubusb ymm3, ymm1, ymm2 // abs difference trick
michael@0 80 vpsubusb ymm2, ymm2, ymm1
michael@0 81 vpor ymm1, ymm2, ymm3
michael@0 82 vpunpcklbw ymm2, ymm1, ymm5 // u16. mutates order.
michael@0 83 vpunpckhbw ymm1, ymm1, ymm5
michael@0 84 vpmaddwd ymm2, ymm2, ymm2 // square + hadd to u32.
michael@0 85 vpmaddwd ymm1, ymm1, ymm1
michael@0 86 vpaddd ymm0, ymm0, ymm1
michael@0 87 vpaddd ymm0, ymm0, ymm2
michael@0 88 jg wloop
michael@0 89
michael@0 90 vpshufd ymm1, ymm0, 0xee // 3, 2 + 1, 0 both lanes.
michael@0 91 vpaddd ymm0, ymm0, ymm1
michael@0 92 vpshufd ymm1, ymm0, 0x01 // 1 + 0 both lanes.
michael@0 93 vpaddd ymm0, ymm0, ymm1
michael@0 94 vpermq ymm1, ymm0, 0x02 // high + low lane.
michael@0 95 vpaddd ymm0, ymm0, ymm1
michael@0 96 vmovd eax, xmm0
michael@0 97 vzeroupper
michael@0 98 ret
michael@0 99 }
michael@0 100 }
michael@0 101 #endif // _MSC_VER >= 1700
michael@0 102
michael@0 103 #define HAS_HASHDJB2_SSE41
michael@0 104 static uvec32 kHash16x33 = { 0x92d9e201, 0, 0, 0 }; // 33 ^ 16
michael@0 105 static uvec32 kHashMul0 = {
michael@0 106 0x0c3525e1, // 33 ^ 15
michael@0 107 0xa3476dc1, // 33 ^ 14
michael@0 108 0x3b4039a1, // 33 ^ 13
michael@0 109 0x4f5f0981, // 33 ^ 12
michael@0 110 };
michael@0 111 static uvec32 kHashMul1 = {
michael@0 112 0x30f35d61, // 33 ^ 11
michael@0 113 0x855cb541, // 33 ^ 10
michael@0 114 0x040a9121, // 33 ^ 9
michael@0 115 0x747c7101, // 33 ^ 8
michael@0 116 };
michael@0 117 static uvec32 kHashMul2 = {
michael@0 118 0xec41d4e1, // 33 ^ 7
michael@0 119 0x4cfa3cc1, // 33 ^ 6
michael@0 120 0x025528a1, // 33 ^ 5
michael@0 121 0x00121881, // 33 ^ 4
michael@0 122 };
michael@0 123 static uvec32 kHashMul3 = {
michael@0 124 0x00008c61, // 33 ^ 3
michael@0 125 0x00000441, // 33 ^ 2
michael@0 126 0x00000021, // 33 ^ 1
michael@0 127 0x00000001, // 33 ^ 0
michael@0 128 };
michael@0 129
michael@0 130 // 27: 66 0F 38 40 C6 pmulld xmm0,xmm6
michael@0 131 // 44: 66 0F 38 40 DD pmulld xmm3,xmm5
michael@0 132 // 59: 66 0F 38 40 E5 pmulld xmm4,xmm5
michael@0 133 // 72: 66 0F 38 40 D5 pmulld xmm2,xmm5
michael@0 134 // 83: 66 0F 38 40 CD pmulld xmm1,xmm5
michael@0 135 #define pmulld(reg) _asm _emit 0x66 _asm _emit 0x0F _asm _emit 0x38 \
michael@0 136 _asm _emit 0x40 _asm _emit reg
michael@0 137
michael@0 138 __declspec(naked) __declspec(align(16))
michael@0 139 uint32 HashDjb2_SSE41(const uint8* src, int count, uint32 seed) {
michael@0 140 __asm {
michael@0 141 mov eax, [esp + 4] // src
michael@0 142 mov ecx, [esp + 8] // count
michael@0 143 movd xmm0, [esp + 12] // seed
michael@0 144
michael@0 145 pxor xmm7, xmm7 // constant 0 for unpck
michael@0 146 movdqa xmm6, kHash16x33
michael@0 147
michael@0 148 align 4
michael@0 149 wloop:
michael@0 150 movdqu xmm1, [eax] // src[0-15]
michael@0 151 lea eax, [eax + 16]
michael@0 152 pmulld(0xc6) // pmulld xmm0,xmm6 hash *= 33 ^ 16
michael@0 153 movdqa xmm5, kHashMul0
michael@0 154 movdqa xmm2, xmm1
michael@0 155 punpcklbw xmm2, xmm7 // src[0-7]
michael@0 156 movdqa xmm3, xmm2
michael@0 157 punpcklwd xmm3, xmm7 // src[0-3]
michael@0 158 pmulld(0xdd) // pmulld xmm3, xmm5
michael@0 159 movdqa xmm5, kHashMul1
michael@0 160 movdqa xmm4, xmm2
michael@0 161 punpckhwd xmm4, xmm7 // src[4-7]
michael@0 162 pmulld(0xe5) // pmulld xmm4, xmm5
michael@0 163 movdqa xmm5, kHashMul2
michael@0 164 punpckhbw xmm1, xmm7 // src[8-15]
michael@0 165 movdqa xmm2, xmm1
michael@0 166 punpcklwd xmm2, xmm7 // src[8-11]
michael@0 167 pmulld(0xd5) // pmulld xmm2, xmm5
michael@0 168 movdqa xmm5, kHashMul3
michael@0 169 punpckhwd xmm1, xmm7 // src[12-15]
michael@0 170 pmulld(0xcd) // pmulld xmm1, xmm5
michael@0 171 paddd xmm3, xmm4 // add 16 results
michael@0 172 paddd xmm1, xmm2
michael@0 173 sub ecx, 16
michael@0 174 paddd xmm1, xmm3
michael@0 175
michael@0 176 pshufd xmm2, xmm1, 0x0e // upper 2 dwords
michael@0 177 paddd xmm1, xmm2
michael@0 178 pshufd xmm2, xmm1, 0x01
michael@0 179 paddd xmm1, xmm2
michael@0 180 paddd xmm0, xmm1
michael@0 181 jg wloop
michael@0 182
michael@0 183 movd eax, xmm0 // return hash
michael@0 184 ret
michael@0 185 }
michael@0 186 }
michael@0 187
michael@0 188 // Visual C 2012 required for AVX2.
michael@0 189 #if _MSC_VER >= 1700
michael@0 190 __declspec(naked) __declspec(align(16))
michael@0 191 uint32 HashDjb2_AVX2(const uint8* src, int count, uint32 seed) {
michael@0 192 __asm {
michael@0 193 mov eax, [esp + 4] // src
michael@0 194 mov ecx, [esp + 8] // count
michael@0 195 movd xmm0, [esp + 12] // seed
michael@0 196 movdqa xmm6, kHash16x33
michael@0 197
michael@0 198 align 4
michael@0 199 wloop:
michael@0 200 vpmovzxbd xmm3, dword ptr [eax] // src[0-3]
michael@0 201 pmulld xmm0, xmm6 // hash *= 33 ^ 16
michael@0 202 vpmovzxbd xmm4, dword ptr [eax + 4] // src[4-7]
michael@0 203 pmulld xmm3, kHashMul0
michael@0 204 vpmovzxbd xmm2, dword ptr [eax + 8] // src[8-11]
michael@0 205 pmulld xmm4, kHashMul1
michael@0 206 vpmovzxbd xmm1, dword ptr [eax + 12] // src[12-15]
michael@0 207 pmulld xmm2, kHashMul2
michael@0 208 lea eax, [eax + 16]
michael@0 209 pmulld xmm1, kHashMul3
michael@0 210 paddd xmm3, xmm4 // add 16 results
michael@0 211 paddd xmm1, xmm2
michael@0 212 sub ecx, 16
michael@0 213 paddd xmm1, xmm3
michael@0 214 pshufd xmm2, xmm1, 0x0e // upper 2 dwords
michael@0 215 paddd xmm1, xmm2
michael@0 216 pshufd xmm2, xmm1, 0x01
michael@0 217 paddd xmm1, xmm2
michael@0 218 paddd xmm0, xmm1
michael@0 219 jg wloop
michael@0 220
michael@0 221 movd eax, xmm0 // return hash
michael@0 222 ret
michael@0 223 }
michael@0 224 }
michael@0 225 #endif // _MSC_VER >= 1700
michael@0 226
michael@0 227 #endif // !defined(LIBYUV_DISABLE_X86) && defined(_M_IX86) && defined(_MSC_VER)
michael@0 228
michael@0 229 #ifdef __cplusplus
michael@0 230 } // extern "C"
michael@0 231 } // namespace libyuv
michael@0 232 #endif

mercurial