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/Sequence.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 
bbf_Sequence_init(struct bbs_Context * cpA,struct bbf_Sequence * ptrA)41 void bbf_Sequence_init( struct bbs_Context* cpA,
42 					    struct bbf_Sequence* ptrA )
43 {
44 	bbs_memset16( ptrA->ftrPtrArrE, 0, bbs_SIZEOF16( ptrA->ftrPtrArrE ) );
45 
46 	bbf_Feature_init( cpA, &ptrA->baseE );
47 	ptrA->baseE.typeE = ( uint32 )bbf_FT_SEQUENCE;
48 	ptrA->baseE.vpActivityE = bbf_Sequence_activity;
49 	ptrA->sizeE = 0;
50 	bbs_Int32Arr_init( cpA, &ptrA->thrArrE );
51 	bbs_UInt16Arr_init( cpA, &ptrA->wgtArrE );
52 	bbs_UInt16Arr_init( cpA, &ptrA->dataArrE );
53 }
54 
55 /* ------------------------------------------------------------------------- */
56 
bbf_Sequence_exit(struct bbs_Context * cpA,struct bbf_Sequence * ptrA)57 void bbf_Sequence_exit( struct bbs_Context* cpA,
58 					    struct bbf_Sequence* ptrA )
59 {
60 	uint16 iL;
61 	for( iL = 0; iL < ptrA->sizeE; iL++ ) bbf_featureExit( cpA, ptrA->ftrPtrArrE[ iL ] );
62 
63 	bbs_memset16( ptrA->ftrPtrArrE, 0, bbs_SIZEOF16( ptrA->ftrPtrArrE ) );
64 	bbf_Feature_exit( cpA, &ptrA->baseE );
65 	ptrA->sizeE = 0;
66 	bbs_Int32Arr_exit( cpA, &ptrA->thrArrE );
67 	bbs_UInt16Arr_exit( cpA, &ptrA->wgtArrE );
68 	bbs_UInt16Arr_exit( cpA, &ptrA->dataArrE );
69 }
70 
71 /* ------------------------------------------------------------------------- */
72 
73 /* ========================================================================= */
74 /*                                                                           */
75 /* ---- \ghd{ operators } -------------------------------------------------- */
76 /*                                                                           */
77 /* ========================================================================= */
78 
79 /* ------------------------------------------------------------------------- */
80 
bbf_Sequence_copy(struct bbs_Context * cpA,struct bbf_Sequence * ptrA,const struct bbf_Sequence * srcPtrA)81 void bbf_Sequence_copy( struct bbs_Context* cpA,
82 					    struct bbf_Sequence* ptrA,
83 					    const struct bbf_Sequence* srcPtrA )
84 {
85 	bbs_ERROR0( "bbf_Sequence_copy:\n Function is not available" );
86 }
87 
88 /* ------------------------------------------------------------------------- */
89 
bbf_Sequence_equal(struct bbs_Context * cpA,const struct bbf_Sequence * ptrA,const struct bbf_Sequence * srcPtrA)90 flag bbf_Sequence_equal( struct bbs_Context* cpA,
91 						 const struct bbf_Sequence* ptrA,
92 						 const struct bbf_Sequence* srcPtrA )
93 {
94 	bbs_ERROR0( "bbf_Sequence_equal:\n Function is not available" );
95 	return TRUE;
96 }
97 
98 /* ------------------------------------------------------------------------- */
99 
100 /* ========================================================================= */
101 /*                                                                           */
102 /* ---- \ghd{ query functions } -------------------------------------------- */
103 /*                                                                           */
104 /* ========================================================================= */
105 
106 /* ------------------------------------------------------------------------- */
107 
108 /* ========================================================================= */
109 /*                                                                           */
110 /* ---- \ghd{ modify functions } ------------------------------------------- */
111 /*                                                                           */
112 /* ========================================================================= */
113 
114 /* ------------------------------------------------------------------------- */
115 
116 /* ========================================================================= */
117 /*                                                                           */
118 /* ---- \ghd{ I/O } -------------------------------------------------------- */
119 /*                                                                           */
120 /* ========================================================================= */
121 
122 /* ------------------------------------------------------------------------- */
123 
bbf_Sequence_memSize(struct bbs_Context * cpA,const struct bbf_Sequence * ptrA)124 uint32 bbf_Sequence_memSize( struct bbs_Context* cpA,
125 						     const struct bbf_Sequence* ptrA )
126 {
127 	uint16 iL;
128 	uint32 memSizeL = bbs_SIZEOF16( uint32 ) +
129 					  bbs_SIZEOF16( uint32 ); /* version */
130 
131 	memSizeL += bbf_Feature_memSize( cpA, &ptrA->baseE );
132 	memSizeL += bbs_SIZEOF16( ptrA->sizeE );
133 	memSizeL += bbs_Int32Arr_memSize( cpA, &ptrA->thrArrE );
134 	memSizeL += bbs_UInt16Arr_memSize( cpA, &ptrA->wgtArrE );
135 	for( iL = 0; iL < ptrA->sizeE; iL++ ) memSizeL += bbf_featureMemSize( cpA, ptrA->ftrPtrArrE[ iL ] );
136 	return memSizeL;
137 }
138 
139 /* ------------------------------------------------------------------------- */
140 
bbf_Sequence_memWrite(struct bbs_Context * cpA,const struct bbf_Sequence * ptrA,uint16 * memPtrA)141 uint32 bbf_Sequence_memWrite( struct bbs_Context* cpA,
142 						      const struct bbf_Sequence* ptrA,
143 							  uint16* memPtrA )
144 {
145 	uint16 iL;
146 	uint32 memSizeL = bbf_Sequence_memSize( cpA, ptrA );
147 	memPtrA += bbs_memWrite32( &memSizeL, memPtrA );
148 	memPtrA += bbs_memWriteUInt32( bbf_SEQUENCE_VERSION, memPtrA );
149 	memPtrA += bbf_Feature_memWrite( cpA, &ptrA->baseE, memPtrA );
150 	memPtrA += bbs_memWrite32( &ptrA->sizeE, memPtrA );
151 	memPtrA += bbs_Int32Arr_memWrite( cpA, &ptrA->thrArrE, memPtrA );
152 	memPtrA += bbs_UInt16Arr_memWrite( cpA, &ptrA->wgtArrE, memPtrA );
153 	for( iL = 0; iL < ptrA->sizeE; iL++ ) memPtrA += bbf_featureMemWrite( cpA, ptrA->ftrPtrArrE[ iL ], memPtrA );
154 	return memSizeL;
155 }
156 
157 /* ------------------------------------------------------------------------- */
158 
bbf_Sequence_memRead(struct bbs_Context * cpA,struct bbf_Sequence * ptrA,const uint16 * memPtrA,struct bbs_MemTbl * mtpA)159 uint32 bbf_Sequence_memRead( struct bbs_Context* cpA,
160 						     struct bbf_Sequence* ptrA,
161 							 const uint16* memPtrA,
162 							 struct bbs_MemTbl* mtpA )
163 {
164 	uint16 iL;
165 	uint32 memSizeL, versionL;
166 	struct bbs_MemTbl memTblL = *mtpA;
167 	struct bbs_MemSeg* espL = bbs_MemTbl_fastestSegPtr( cpA, &memTblL, 0 );
168 	if( bbs_Context_error( cpA ) ) return 0;
169 	memPtrA += bbs_memRead32( &memSizeL, memPtrA );
170 	memPtrA += bbs_memReadVersion32( cpA, &versionL, bbf_SEQUENCE_VERSION, memPtrA );
171 	memPtrA += bbf_Feature_memRead( cpA, &ptrA->baseE, memPtrA );
172 	memPtrA += bbs_memRead32( &ptrA->sizeE, memPtrA );
173 
174 	if( ptrA->sizeE > bbf_SEQUENCE_MAX_SIZE )
175 	{
176 		bbs_ERROR0( "bbf_Sequence_memRead:\n Sequence size exceeds bbf_SEQUENCE_MAX_SIZE" );
177 		return 0;
178 	}
179 
180 	memPtrA += bbs_Int32Arr_memRead( cpA, &ptrA->thrArrE, memPtrA, espL );
181 
182 	if( versionL >= 101 ) memPtrA += bbs_UInt16Arr_memRead( cpA, &ptrA->wgtArrE, memPtrA, espL );
183 
184 	/* check features & allocate data buffer */
185 	{
186 		const uint16* memPtrL = memPtrA;
187 		uint32 dataSizeL = 0;
188 		for( iL = 0; iL < ptrA->sizeE; iL++ )
189 		{
190 			enum bbf_FeatureType typeL = ( enum bbf_FeatureType )bbs_memPeek32( memPtrL + 4 );
191 			dataSizeL += bbf_featureSizeOf16( cpA, typeL );
192 			memPtrL += bbs_memPeek32( memPtrL );
193 		}
194 		bbs_UInt16Arr_create( cpA, &ptrA->dataArrE, dataSizeL, espL );
195 	}
196 
197 	/* load features & initialize pointers */
198 	{
199 		uint16* dataPtrL = ptrA->dataArrE.arrPtrE;
200 		for( iL = 0; iL < ptrA->sizeE; iL++ )
201 		{
202 			enum bbf_FeatureType typeL = ( enum bbf_FeatureType )bbs_memPeek32( memPtrA + 4 );
203 			ptrA->ftrPtrArrE[ iL ] = ( struct bbf_Feature* )dataPtrL;
204 			bbf_featureInit( cpA, ptrA->ftrPtrArrE[ iL ], typeL );
205 			memPtrA += bbf_featureMemRead( cpA, ptrA->ftrPtrArrE[ iL ], memPtrA, &memTblL );
206 			dataPtrL += bbf_featureSizeOf16( cpA, typeL );
207 		}
208 	}
209 
210 /*	if( memSizeL != bbf_Sequence_memSize( cpA, ptrA ) )
211 	{
212 		bbs_ERR0( bbs_ERR_CORRUPT_DATA, "uint32 bbf_Sequence_memRead( struct bem_ScanGradientMove* ptrA, const uint16* memPtrA ):\n"
213 			        "size mismatch" );
214 		return 0;
215 	}
216 */
217 
218 	return memSizeL;
219 }
220 
221 /* ------------------------------------------------------------------------- */
222 
223 /* ========================================================================= */
224 /*                                                                           */
225 /* ---- \ghd{ exec functions } --------------------------------------------- */
226 /*                                                                           */
227 /* ========================================================================= */
228 
229 /* ------------------------------------------------------------------------- */
230 
bbf_Sequence_activity(const struct bbf_Feature * ptrA,const uint32 * patchA)231 int32 bbf_Sequence_activity( const struct bbf_Feature* ptrA, const uint32* patchA )
232 {
233 	const struct bbf_Sequence* ptrL = ( struct bbf_Sequence* )ptrA;
234 
235 	count_t iL;
236 
237 	int32 sizeL = ptrL->sizeE;
238 
239 	/* 12.20 */
240 	int32 actSumL = ( -sizeL ) << 20;
241 
242 	if( sizeL == 0 ) return 0x10000000; /* 1.0 in 4.28 format */
243 
244 	if( ptrL->wgtArrE.sizeE == 0 )
245 	{
246 		for( iL = 0; iL < ptrL->sizeE; iL++ )
247 		{
248 			/* 4.28 */
249 			int32 actL = ptrL->ftrPtrArrE[ iL ]->vpActivityE( ptrL->ftrPtrArrE[ iL ], patchA ) - ptrL->thrArrE.arrPtrE[ iL ];
250 			actSumL += ( actL >> 8 );
251 			if( actL < 0 ) return ( actSumL / sizeL ) << 7; /* return 4.28 */
252 		}
253 	}
254 	else
255 	{
256 		for( iL = 0; iL < ptrL->sizeE; iL++ )
257 		{
258 			/* 4.28 */
259 			int32 actL = ptrL->ftrPtrArrE[ iL ]->vpActivityE( ptrL->ftrPtrArrE[ iL ], patchA ) - ptrL->thrArrE.arrPtrE[ iL ];
260 			int32 wgtL = ptrL->wgtArrE.arrPtrE[ iL ];
261 			actL = ( actL >> 16 ) * wgtL + ( ( ( int32 )( actL & 0x0000FFFF ) * wgtL ) >> 16 );
262 			actSumL += ( actL >> 8 );
263 			if( actL < 0 ) return ( actSumL / sizeL ) << 7; /* return 4.28 */
264 		}
265 	}
266 
267 	actSumL += sizeL << 20;
268 
269     /* positive activity: ] 0, 1 ] */
270 	return ( actSumL / sizeL ) << 7; /* return 4.28 */
271 }
272 
273 /* ------------------------------------------------------------------------- */
274 
275 /* ========================================================================= */
276 
277