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/SubVecMap.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 bts_SubVecMap_init( struct bbs_Context* cpA,
42					      struct bts_SubVecMap* ptrA )
43{
44	bts_VectorMap_init( cpA, &ptrA->baseE );
45	ptrA->baseE.typeE = ( uint32 )bts_VM_SUB_VEC_MAP;
46	ptrA->baseE.vpMapE = bts_SubVecMap_map;
47	ptrA->offsetE = 0;
48	ptrA->sizeE = -1;
49}
50
51/* ------------------------------------------------------------------------- */
52
53void bts_SubVecMap_exit( struct bbs_Context* cpA,
54					      struct bts_SubVecMap* ptrA )
55{
56	bts_VectorMap_exit( cpA, &ptrA->baseE );
57	ptrA->offsetE = 0;
58	ptrA->sizeE = -1;
59}
60
61/* ------------------------------------------------------------------------- */
62
63/* ========================================================================= */
64/*                                                                           */
65/* ---- \ghd{ operators } -------------------------------------------------- */
66/*                                                                           */
67/* ========================================================================= */
68
69/* ------------------------------------------------------------------------- */
70
71void bts_SubVecMap_copy( struct bbs_Context* cpA,
72						 struct bts_SubVecMap* ptrA,
73					     const struct bts_SubVecMap* srcPtrA )
74{
75	ptrA->offsetE = srcPtrA->offsetE;
76	ptrA->sizeE = srcPtrA->sizeE;
77}
78
79/* ------------------------------------------------------------------------- */
80
81flag bts_SubVecMap_equal( struct bbs_Context* cpA,
82						   const struct bts_SubVecMap* ptrA,
83						   const struct bts_SubVecMap* srcPtrA )
84{
85	if( ptrA->offsetE != srcPtrA->offsetE ) return FALSE;
86	if( ptrA->sizeE   != srcPtrA->sizeE   ) return FALSE;
87	return TRUE;
88}
89
90/* ------------------------------------------------------------------------- */
91
92/* ========================================================================= */
93/*                                                                           */
94/* ---- \ghd{ query functions } -------------------------------------------- */
95/*                                                                           */
96/* ========================================================================= */
97
98/* ------------------------------------------------------------------------- */
99
100/* ========================================================================= */
101/*                                                                           */
102/* ---- \ghd{ modify functions } ------------------------------------------- */
103/*                                                                           */
104/* ========================================================================= */
105
106/* ------------------------------------------------------------------------- */
107
108/* ========================================================================= */
109/*                                                                           */
110/* ---- \ghd{ I/O } -------------------------------------------------------- */
111/*                                                                           */
112/* ========================================================================= */
113
114/* ------------------------------------------------------------------------- */
115
116uint32 bts_SubVecMap_memSize( struct bbs_Context* cpA,
117							   const struct bts_SubVecMap* ptrA )
118{
119	uint32 memSizeL = bbs_SIZEOF16( uint32 ) +
120					  bbs_SIZEOF16( uint32 ); /* version */
121	memSizeL += bts_VectorMap_memSize( cpA, &ptrA->baseE );
122	memSizeL += bbs_SIZEOF16( ptrA->offsetE );
123	memSizeL += bbs_SIZEOF16( ptrA->sizeE );
124
125	return memSizeL;
126}
127
128/* ------------------------------------------------------------------------- */
129
130uint32 bts_SubVecMap_memWrite( struct bbs_Context* cpA,
131								const struct bts_SubVecMap* ptrA,
132								uint16* memPtrA )
133{
134	uint32 memSizeL = bts_SubVecMap_memSize( cpA, ptrA );
135	memPtrA += bbs_memWrite32( &memSizeL, memPtrA );
136	memPtrA += bbs_memWriteUInt32( bts_SUB_VEC_MAP_VERSION, memPtrA );
137	memPtrA += bts_VectorMap_memWrite( cpA, &ptrA->baseE, memPtrA );
138	memPtrA += bbs_memWrite32( &ptrA->offsetE, memPtrA );
139	memPtrA += bbs_memWrite32( &ptrA->sizeE, memPtrA );
140	return memSizeL;
141}
142
143/* ------------------------------------------------------------------------- */
144
145uint32 bts_SubVecMap_memRead( struct bbs_Context* cpA,
146							   struct bts_SubVecMap* ptrA,
147							   const uint16* memPtrA,
148							   struct bbs_MemTbl* mtpA )
149{
150	uint32 memSizeL, versionL;
151	if( bbs_Context_error( cpA ) ) return 0;
152	memPtrA += bbs_memRead32( &memSizeL, memPtrA );
153	memPtrA += bbs_memReadVersion32( cpA, &versionL, bts_SUB_VEC_MAP_VERSION, memPtrA );
154	memPtrA += bts_VectorMap_memRead( cpA, &ptrA->baseE, memPtrA );
155	memPtrA += bbs_memRead32( &ptrA->offsetE, memPtrA );
156	memPtrA += bbs_memRead32( &ptrA->sizeE, memPtrA );
157
158	if( memSizeL != bts_SubVecMap_memSize( cpA, ptrA ) )
159	{
160		bbs_ERR0( bbs_ERR_CORRUPT_DATA, "uint32 bts_SubVecMap_memRead( struct bem_ScanGradientMove* ptrA, const uint16* memPtrA ):\n"
161			        "size mismatch" );
162		return 0;
163	}
164
165	return memSizeL;
166}
167
168/* ------------------------------------------------------------------------- */
169
170/* ========================================================================= */
171/*                                                                           */
172/* ---- \ghd{ exec functions } --------------------------------------------- */
173/*                                                                           */
174/* ========================================================================= */
175
176/* ------------------------------------------------------------------------- */
177
178void bts_SubVecMap_map( struct bbs_Context* cpA,
179						const struct bts_VectorMap* ptrA,
180						const struct bts_Flt16Vec* inVecPtrA,
181						struct bts_Flt16Vec* outVecPtrA )
182{
183	bbs_DEF_fNameL( "bts_SubVecMap_map" )
184	struct bts_SubVecMap* ptrL = ( struct bts_SubVecMap* )ptrA;
185
186	int32 sizeL = ( ptrL->sizeE != -1 ) ? ptrL->sizeE : ( int32 )inVecPtrA->arrE.sizeE - ptrL->offsetE;
187	if( sizeL < 0 ) sizeL = 0;
188
189	if( ( ptrL->offsetE + sizeL ) > ( int32 )inVecPtrA->arrE.sizeE )
190	{
191		bbs_ERROR1( "%s:\ninput vector too small", fNameL );
192		return;
193	}
194
195	if( outVecPtrA->arrE.allocatedSizeE < ( uint32 )sizeL )
196	{
197		bbs_ERROR1( "%s:\noutput vector is insufficiently allocated", fNameL );
198		return;
199	}
200
201	bts_Flt16Vec_size( cpA, outVecPtrA, sizeL );
202	outVecPtrA->expE = inVecPtrA->expE;
203	bbs_memcpy16( outVecPtrA->arrE.arrPtrE, inVecPtrA->arrE.arrPtrE + ptrL->offsetE, sizeL );
204
205	bts_Flt16Vec_maximizeMantisse( cpA, outVecPtrA );
206}
207
208/* ------------------------------------------------------------------------- */
209
210/* ========================================================================= */
211
212