auto import from //depot/cupcake/@135843
[android/platform/external/neven.git] / Embedded / common / src / b_TensorEm / MapSequence.c
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_TensorEm/MapSequence.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
41 void bts_MapSequence_init( struct bbs_Context* cpA,
42                                                struct bts_MapSequence* ptrA )
43 {
44         ptrA->ptrArrE = NULL;
45         bts_Flt16Vec_init( cpA, &ptrA->vecE );
46         bts_VectorMap_init( cpA, &ptrA->baseE );
47         ptrA->baseE.typeE = ( uint32 )bts_VM_MAP_SEQUENCE;
48         ptrA->baseE.vpMapE = bts_MapSequence_map;
49         ptrA->sizeE = 0;
50         ptrA->vecSizeE = 0;
51         bbs_UInt16Arr_init( cpA, &ptrA->objBufE );
52 }
53
54 /* ------------------------------------------------------------------------- */
55
56 void bts_MapSequence_exit( struct bbs_Context* cpA,
57                                                struct bts_MapSequence* ptrA )
58 {
59         uint16 iL;
60         for( iL = 0; iL < ptrA->sizeE; iL++ ) bts_vectorMapExit( cpA, ptrA->ptrArrE[ iL ] );
61         ptrA->ptrArrE = NULL;
62         bts_Flt16Vec_exit( cpA, &ptrA->vecE );
63         ptrA->sizeE = 0;
64         ptrA->vecSizeE = 0;
65         bbs_UInt16Arr_exit( cpA, &ptrA->objBufE );
66
67         bts_VectorMap_exit( cpA, &ptrA->baseE );
68 }
69
70 /* ------------------------------------------------------------------------- */
71
72 /* ========================================================================= */
73 /*                                                                           */
74 /* ---- \ghd{ operators } -------------------------------------------------- */
75 /*                                                                           */
76 /* ========================================================================= */
77
78 /* ------------------------------------------------------------------------- */
79
80 void bts_MapSequence_copy( struct bbs_Context* cpA,
81                                                    struct bts_MapSequence* ptrA, 
82                                                const struct bts_MapSequence* srcPtrA )
83 {
84         bbs_ERROR0( "bts_MapSequence_copy:\n Function is not available" );
85 }
86
87 /* ------------------------------------------------------------------------- */
88
89 flag bts_MapSequence_equal( struct bbs_Context* cpA,
90                                                     const struct bts_MapSequence* ptrA, 
91                                                     const struct bts_MapSequence* srcPtrA )
92 {
93         bbs_ERROR0( "bts_MapSequence_equal:\n Function is not available" );
94         return TRUE;
95 }
96
97 /* ------------------------------------------------------------------------- */
98
99 /* ========================================================================= */
100 /*                                                                           */
101 /* ---- \ghd{ query functions } -------------------------------------------- */
102 /*                                                                           */
103 /* ========================================================================= */
104
105 /* ------------------------------------------------------------------------- */
106
107 /* ========================================================================= */
108 /*                                                                           */
109 /* ---- \ghd{ modify functions } ------------------------------------------- */
110 /*                                                                           */
111 /* ========================================================================= */
112
113 /* ------------------------------------------------------------------------- */
114         
115 /* ========================================================================= */
116 /*                                                                           */
117 /* ---- \ghd{ I/O } -------------------------------------------------------- */
118 /*                                                                           */
119 /* ========================================================================= */
120
121 /* ------------------------------------------------------------------------- */
122         
123 uint32 bts_MapSequence_memSize( struct bbs_Context* cpA,
124                                                                 const struct bts_MapSequence* ptrA )
125 {
126         uint16 iL;
127         uint32 memSizeL = bbs_SIZEOF16( uint32 ) +
128                                           bbs_SIZEOF16( uint32 ); /* version */
129
130         memSizeL += bts_VectorMap_memSize( cpA, &ptrA->baseE );
131         memSizeL += bbs_SIZEOF16( ptrA->sizeE );
132         memSizeL += bbs_SIZEOF16( ptrA->vecSizeE );
133         for( iL = 0; iL < ptrA->sizeE; iL++ ) memSizeL += bts_vectorMapMemSize( cpA, ptrA->ptrArrE[ iL ] );
134
135         return memSizeL; 
136 }
137
138 /* ------------------------------------------------------------------------- */
139         
140 uint32 bts_MapSequence_memWrite( struct bbs_Context* cpA,
141                                                                  const struct bts_MapSequence* ptrA, 
142                                                                  uint16* memPtrA )
143 {
144         uint16 iL;
145         uint32 memSizeL = bts_MapSequence_memSize( cpA, ptrA );
146         memPtrA += bbs_memWrite32( &memSizeL, memPtrA );
147         memPtrA += bbs_memWriteUInt32( bts_MAP_SEQUENCE_VERSION, memPtrA );
148         memPtrA += bts_VectorMap_memWrite( cpA, &ptrA->baseE, memPtrA );
149         memPtrA += bbs_memWrite32( &ptrA->sizeE, memPtrA );
150         memPtrA += bbs_memWrite32( &ptrA->vecSizeE, memPtrA );
151         for( iL = 0; iL < ptrA->sizeE; iL++ ) memPtrA += bts_vectorMapMemWrite( cpA, ptrA->ptrArrE[ iL ], memPtrA );
152         return memSizeL;
153 }
154
155 /* ------------------------------------------------------------------------- */
156
157 uint32 bts_MapSequence_memRead( struct bbs_Context* cpA,
158                                                                 struct bts_MapSequence* ptrA, 
159                                                                 const uint16* memPtrA, 
160                                                                 struct bbs_MemTbl* mtpA )
161 {
162         uint16 iL;
163         uint32 memSizeL, versionL;
164         struct bbs_MemTbl memTblL = *mtpA;
165         struct bbs_MemSeg* espL = bbs_MemTbl_segPtr( cpA, &memTblL, 0 );
166         struct bbs_MemSeg* sspL = bbs_MemTbl_sharedSegPtr( cpA, &memTblL, 0 );
167         if( bbs_Context_error( cpA ) ) return 0;
168         memPtrA += bbs_memRead32( &memSizeL, memPtrA );
169         memPtrA += bbs_memReadVersion32( cpA, &versionL, bts_MAP_SEQUENCE_VERSION, memPtrA );
170         memPtrA += bts_VectorMap_memRead( cpA, &ptrA->baseE, memPtrA );
171         memPtrA += bbs_memRead32( &ptrA->sizeE, memPtrA );
172         memPtrA += bbs_memRead32( &ptrA->vecSizeE, memPtrA );
173
174         /* put buffer vector on shared memory */
175         bts_Flt16Vec_create( cpA, &ptrA->vecE, ptrA->vecSizeE, sspL );
176
177         /* check maps & allocate data buffer */
178         {
179                 const uint16* memPtrL = memPtrA;
180                 uint32 dataSizeL = ptrA->sizeE * bbs_SIZEOF16( struct bts_VectorMap* );
181
182                 for( iL = 0; iL < ptrA->sizeE; iL++ )
183                 {
184                         enum bts_VectorMapType typeL = ( enum bts_VectorMapType )bbs_memPeek32( memPtrL + 4 );
185                         dataSizeL += bts_vectorMapSizeOf16( cpA, typeL );
186                         memPtrL += bbs_memPeek32( memPtrL );
187                 }
188
189                 bbs_UInt16Arr_create( cpA, &ptrA->objBufE, dataSizeL, espL );
190                 if( bbs_Context_error( cpA ) ) return 0;
191         }
192
193         /* load maps & initialize pointers */
194         {
195                 uint16* dataPtrL = ptrA->objBufE.arrPtrE;
196                 ptrA->ptrArrE = ( struct bts_VectorMap** )dataPtrL;
197                 dataPtrL += ptrA->sizeE * bbs_SIZEOF16( struct bts_VectorMap* );
198                 for( iL = 0; iL < ptrA->sizeE; iL++ )
199                 {
200                         enum bts_VectorMapType typeL = ( enum bts_VectorMapType )bbs_memPeek32( memPtrA + 4 );
201                         ptrA->ptrArrE[ iL ] = ( struct bts_VectorMap* )dataPtrL;
202                         bts_vectorMapInit( cpA, ptrA->ptrArrE[ iL ], typeL );
203                         memPtrA += bts_vectorMapMemRead( cpA, ptrA->ptrArrE[ iL ], memPtrA, &memTblL );
204                         dataPtrL += bts_vectorMapSizeOf16( cpA, typeL );
205                 }
206         }
207
208         if( memSizeL != bts_MapSequence_memSize( cpA, ptrA ) )
209         {
210                 bbs_ERR0( bbs_ERR_CORRUPT_DATA, "uint32 bts_MapSequence_memRead( struct bem_ScanGradientMove* ptrA, const uint16* memPtrA ):\n"
211                                 "size mismatch" );
212                 return 0;
213         }
214
215         return memSizeL;
216 }
217
218 /* ------------------------------------------------------------------------- */
219         
220 /* ========================================================================= */
221 /*                                                                           */
222 /* ---- \ghd{ exec functions } --------------------------------------------- */
223 /*                                                                           */
224 /* ========================================================================= */
225         
226 /* ------------------------------------------------------------------------- */
227
228 void bts_MapSequence_map( struct bbs_Context* cpA, 
229                                                   const struct bts_VectorMap* ptrA, 
230                                                   const struct bts_Flt16Vec* inVecPtrA,
231                                                   struct bts_Flt16Vec* outVecPtrA )
232 {
233         struct bts_MapSequence* ptrL = ( struct bts_MapSequence* )ptrA;
234         if( ptrL->sizeE == 0 )
235         {
236                 bts_Flt16Vec_copy( cpA, outVecPtrA, inVecPtrA );
237         }
238         else if( ptrL->sizeE == 1 )
239         {
240                 struct bts_VectorMap* mapPtrL = ptrL->ptrArrE[ 0 ];
241                 mapPtrL->vpMapE( cpA, mapPtrL, inVecPtrA, outVecPtrA );
242         }
243         else
244         {
245                 uint32 iL;
246                 struct bts_Flt16Vec* vp1L = &ptrL->vecE;
247                 struct bts_Flt16Vec* vp2L = outVecPtrA;
248                 struct bts_VectorMap* mapPtrL = ptrL->ptrArrE[ 0 ];
249                 mapPtrL->vpMapE( cpA, mapPtrL, inVecPtrA, vp1L );
250
251                 for( iL = 1; iL < ptrL->sizeE; iL++ )
252                 {
253                         mapPtrL = ptrL->ptrArrE[ iL ];
254                         mapPtrL->vpMapE( cpA, mapPtrL, vp1L, vp2L );
255
256                         /* swap vectors */
257                         {
258                                 struct bts_Flt16Vec* vpL = vp1L;
259                                 vp1L = vp2L;
260                                 vp2L = vpL;
261                         }
262                 }
263
264                 /* vp1 holds output */
265                 if( vp1L != outVecPtrA ) bts_Flt16Vec_copy( cpA, outVecPtrA, vp1L );
266         }
267 }
268
269 /* ------------------------------------------------------------------------- */
270
271 /* ========================================================================= */
272