1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* ---- includes ----------------------------------------------------------- */
18
19#include "b_BasicEm/Functions.h"
20#include "b_BasicEm/Math.h"
21#include "b_BitFeatureEm/L01Tld2x4Ftr.h"
22
23/* ------------------------------------------------------------------------- */
24
25/* ========================================================================= */
26/*                                                                           */
27/* ---- \ghd{ auxiliary functions } ---------------------------------------- */
28/*                                                                           */
29/* ========================================================================= */
30
31/* ------------------------------------------------------------------------- */
32
33/* ========================================================================= */
34/*                                                                           */
35/* ---- \ghd{ constructor / destructor } ----------------------------------- */
36/*                                                                           */
37/* ========================================================================= */
38
39/* ------------------------------------------------------------------------- */
40
41void bbf_L01Tld2x4Ftr_init( struct bbs_Context* cpA,
42						    struct bbf_L01Tld2x4Ftr* ptrA )
43{
44	bbf_Feature_init( cpA, &ptrA->baseE );
45	ptrA->baseE.typeE = ( uint32 )bbf_FT_L01_TLD_2X4_FTR;
46	ptrA->baseE.vpActivityE = bbf_L01Tld2x4Ftr_activity;
47	bbs_UInt32Arr_init( cpA, &ptrA->dataArrE );
48	ptrA->activityFactorE = 0;
49}
50
51/* ------------------------------------------------------------------------- */
52
53void bbf_L01Tld2x4Ftr_exit( struct bbs_Context* cpA,
54						    struct bbf_L01Tld2x4Ftr* ptrA )
55{
56	bbf_Feature_exit( cpA, &ptrA->baseE );
57	bbs_UInt32Arr_exit( cpA, &ptrA->dataArrE );
58	ptrA->activityFactorE = 0;
59}
60
61/* ------------------------------------------------------------------------- */
62
63/* ========================================================================= */
64/*                                                                           */
65/* ---- \ghd{ operators } -------------------------------------------------- */
66/*                                                                           */
67/* ========================================================================= */
68
69/* ------------------------------------------------------------------------- */
70
71void bbf_L01Tld2x4Ftr_copy( struct bbs_Context* cpA,
72						    struct bbf_L01Tld2x4Ftr* ptrA,
73						    const struct bbf_L01Tld2x4Ftr* srcPtrA )
74{
75	bbf_Feature_copy( cpA, &ptrA->baseE, &srcPtrA->baseE );
76	bbs_UInt32Arr_copy( cpA, &ptrA->dataArrE, &srcPtrA->dataArrE );
77	ptrA->activityFactorE = srcPtrA->activityFactorE;
78}
79
80/* ------------------------------------------------------------------------- */
81
82flag bbf_L01Tld2x4Ftr_equal( struct bbs_Context* cpA,
83						     const struct bbf_L01Tld2x4Ftr* ptrA,
84						     const struct bbf_L01Tld2x4Ftr* srcPtrA )
85{
86	if( !bbf_Feature_equal( cpA, &ptrA->baseE, &srcPtrA->baseE ) ) return FALSE;
87	if( !bbs_UInt32Arr_equal( cpA, &ptrA->dataArrE, &srcPtrA->dataArrE ) ) return FALSE;
88	if( ptrA->activityFactorE != srcPtrA->activityFactorE ) return FALSE;
89	return TRUE;
90}
91
92/* ------------------------------------------------------------------------- */
93
94/* ========================================================================= */
95/*                                                                           */
96/* ---- \ghd{ query functions } -------------------------------------------- */
97/*                                                                           */
98/* ========================================================================= */
99
100/* ------------------------------------------------------------------------- */
101
102/* ========================================================================= */
103/*                                                                           */
104/* ---- \ghd{ modify functions } ------------------------------------------- */
105/*                                                                           */
106/* ========================================================================= */
107
108/* ------------------------------------------------------------------------- */
109
110/* ========================================================================= */
111/*                                                                           */
112/* ---- \ghd{ I/O } -------------------------------------------------------- */
113/*                                                                           */
114/* ========================================================================= */
115
116/* ------------------------------------------------------------------------- */
117
118uint32 bbf_L01Tld2x4Ftr_memSize( struct bbs_Context* cpA,
119							     const struct bbf_L01Tld2x4Ftr* ptrA )
120{
121	uint32 memSizeL = bbs_SIZEOF16( uint32 ) +
122					  bbs_SIZEOF16( uint32 ); /* version */
123
124	memSizeL += bbf_Feature_memSize( cpA, &ptrA->baseE );
125	memSizeL += bbs_UInt32Arr_memSize( cpA, &ptrA->dataArrE );
126	memSizeL += bbs_SIZEOF16( ptrA->activityFactorE );
127
128	return memSizeL;
129}
130
131/* ------------------------------------------------------------------------- */
132
133uint32 bbf_L01Tld2x4Ftr_memWrite( struct bbs_Context* cpA,
134							      const struct bbf_L01Tld2x4Ftr* ptrA,
135								  uint16* memPtrA )
136{
137	uint32 memSizeL = bbf_L01Tld2x4Ftr_memSize( cpA, ptrA );
138	memPtrA += bbs_memWrite32( &memSizeL, memPtrA );
139	memPtrA += bbs_memWriteUInt32( bbf_L01_TLD_2X4_FTR_VERSION, memPtrA );
140	memPtrA += bbf_Feature_memWrite( cpA, &ptrA->baseE, memPtrA );
141	memPtrA += bbs_UInt32Arr_memWrite( cpA, &ptrA->dataArrE, memPtrA );
142	memPtrA += bbs_memWrite32( &ptrA->activityFactorE, memPtrA );
143	return memSizeL;
144}
145
146/* ------------------------------------------------------------------------- */
147
148uint32 bbf_L01Tld2x4Ftr_memRead( struct bbs_Context* cpA,
149							     struct bbf_L01Tld2x4Ftr* ptrA,
150							     const uint16* memPtrA,
151							     struct bbs_MemTbl* mtpA )
152{
153	uint32 memSizeL, versionL;
154	struct bbs_MemTbl memTblL = *mtpA;
155	struct bbs_MemSeg* espL = bbs_MemTbl_fastestSegPtr( cpA, &memTblL, 0 );
156	if( bbs_Context_error( cpA ) ) return 0;
157	memPtrA += bbs_memRead32( &memSizeL, memPtrA );
158	memPtrA += bbs_memReadVersion32( cpA, &versionL, bbf_L01_TLD_2X4_FTR_VERSION, memPtrA );
159	memPtrA += bbf_Feature_memRead( cpA, &ptrA->baseE, memPtrA );
160	memPtrA += bbs_UInt32Arr_memRead( cpA, &ptrA->dataArrE, memPtrA, espL );
161	memPtrA += bbs_memRead32( &ptrA->activityFactorE, memPtrA );
162	if( memSizeL != bbf_L01Tld2x4Ftr_memSize( cpA, ptrA ) )
163	{
164		bbs_ERR0( bbs_ERR_CORRUPT_DATA, "uint32 bbf_L01Tld2x4Ftr_memRead( struct bem_ScanGradientMove* ptrA, const uint16* memPtrA ):\n"
165			        "size mismatch" );
166		return 0;
167	}
168
169	return memSizeL;
170}
171
172/* ------------------------------------------------------------------------- */
173
174/* ========================================================================= */
175/*                                                                           */
176/* ---- \ghd{ exec functions } --------------------------------------------- */
177/*                                                                           */
178/* ========================================================================= */
179
180/* ------------------------------------------------------------------------- */
181
182int32 bbf_L01Tld2x4Ftr_activity( const struct bbf_Feature* ptrA, const uint32* patchA )
183{
184	const struct bbf_L01Tld2x4Ftr* ptrL = ( struct bbf_L01Tld2x4Ftr* )ptrA;
185
186	const uint32* dataPtrL = ptrL->dataArrE.arrPtrE;
187	const uint32* patchL = patchA;
188
189	uint32 iL;
190
191	uint32 bs1L = 0;
192	uint32 bs2L = 0;
193
194	for( iL = ptrL->baseE.patchWidthE >> 3; iL > 0; iL-- )
195	{
196		uint32 vL = ~dataPtrL[ 0 ];
197
198		/* compare with pattern */
199		uint32 s1L = patchL[ 0 ] ^ dataPtrL[ 1 ];
200		uint32 s2L = patchL[ 1 ] ^ dataPtrL[ 2 ];
201
202		/* bit count */
203		s1L = ( s1L & 0x55555555 ) + ( ( s1L >> 1 ) & 0x55555555 );
204		s1L = ( s1L & 0x33333333 ) + ( ( s1L >> 2 ) & 0x33333333 );
205		s2L = ( s2L & 0x55555555 ) + ( ( s2L >> 1 ) & 0x55555555 );
206		s2L = ( s2L & 0x33333333 ) + ( ( s2L >> 2 ) & 0x33333333 );
207
208		/* compare with threshold and store results in vL */
209		vL |= ( ( s1L + s2L + dataPtrL[ 3 ] ) & 0x88888888 ) >> 3;
210
211		/* compare with pattern */
212		s1L = patchL[ 2 ] ^ dataPtrL[ 4 ];
213		s2L = patchL[ 3 ] ^ dataPtrL[ 5 ];
214
215		/* bit count */
216		s1L = ( s1L & 0x55555555 ) + ( ( s1L >> 1 ) & 0x55555555 );
217		s1L = ( s1L & 0x33333333 ) + ( ( s1L >> 2 ) & 0x33333333 );
218		s2L = ( s2L & 0x55555555 ) + ( ( s2L >> 1 ) & 0x55555555 );
219		s2L = ( s2L & 0x33333333 ) + ( ( s2L >> 2 ) & 0x33333333 );
220
221		/* compare with threshold and store results in vL */
222		vL |= ( ( s1L + s2L + dataPtrL[ 6 ] ) & 0x88888888 ) >> 2;
223
224		/* compare with pattern */
225		s1L = patchL[ 4 ] ^ dataPtrL[ 7 ];
226		s2L = patchL[ 5 ] ^ dataPtrL[ 8 ];
227
228		/* bit count */
229		s1L = ( s1L & 0x55555555 ) + ( ( s1L >> 1 ) & 0x55555555 );
230		s1L = ( s1L & 0x33333333 ) + ( ( s1L >> 2 ) & 0x33333333 );
231		s2L = ( s2L & 0x55555555 ) + ( ( s2L >> 1 ) & 0x55555555 );
232		s2L = ( s2L & 0x33333333 ) + ( ( s2L >> 2 ) & 0x33333333 );
233
234		/* compare with threshold and store results in vL */
235		vL |= ( ( s1L + s2L + dataPtrL[ 9 ] ) & 0x88888888 ) >> 1;
236
237		/* compare with pattern */
238		s1L = patchL[ 6 ] ^ dataPtrL[ 10 ];
239		s2L = patchL[ 7 ] ^ dataPtrL[ 11 ];
240
241		/* bit count */
242		s1L = ( s1L & 0x55555555 ) + ( ( s1L >> 1 ) & 0x55555555 );
243		s1L = ( s1L & 0x33333333 ) + ( ( s1L >> 2 ) & 0x33333333 );
244		s2L = ( s2L & 0x55555555 ) + ( ( s2L >> 1 ) & 0x55555555 );
245		s2L = ( s2L & 0x33333333 ) + ( ( s2L >> 2 ) & 0x33333333 );
246
247		/* compare with threshold and store results in vL */
248		vL |= ( ( s1L + s2L + dataPtrL[ 12 ] ) & 0x88888888 );
249
250		vL = ~vL;
251
252		/* add bits */
253		bs1L +=    vL        & 0x55555555;
254		bs2L +=  ( vL >> 1 ) & 0x55555555;
255
256		dataPtrL += 13;
257		patchL  += 8;
258	}
259
260	/* complete partial sums and compute final confidence */
261	bs1L = ( bs1L & 0x33333333 ) + ( ( bs1L >> 2 ) & 0x33333333 ) + ( bs2L & 0x33333333 ) + ( ( bs2L >> 2 ) & 0x33333333 );
262	bs1L = ( bs1L & 0x0F0F0F0F ) + ( ( bs1L >> 4 ) & 0x0F0F0F0F );
263	bs1L += bs1L >> 8;
264
265	/* compute final activity */
266	{
267		uint32 actL =  ( bs1L + ( bs1L >> 16 ) ) & 0x0FF;
268		return actL * ptrL->activityFactorE;
269	}
270}
271
272/* ------------------------------------------------------------------------- */
273
274/* ========================================================================= */
275
276