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_TensorEm/Int16Mat2D.h"
20#include "b_TensorEm/Functions.h"
21#include "b_BasicEm/Math.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/* ========================================================================= */
42/*                                                                           */
43/* ---- \ghd{ operators } -------------------------------------------------- */
44/*                                                                           */
45/* ========================================================================= */
46
47/* ------------------------------------------------------------------------- */
48
49/* ========================================================================= */
50/*                                                                           */
51/* ---- \ghd{ query functions } -------------------------------------------- */
52/*                                                                           */
53/* ========================================================================= */
54
55/* ------------------------------------------------------------------------- */
56
57/* ========================================================================= */
58/*                                                                           */
59/* ---- \ghd{ modify functions } ------------------------------------------- */
60/*                                                                           */
61/* ========================================================================= */
62
63/* ------------------------------------------------------------------------- */
64
65/* ========================================================================= */
66/*                                                                           */
67/* ---- \ghd{ I/O } -------------------------------------------------------- */
68/*                                                                           */
69/* ========================================================================= */
70
71/* ------------------------------------------------------------------------- */
72
73/* ========================================================================= */
74/*                                                                           */
75/* ---- \ghd{ exec functions } --------------------------------------------- */
76/*                                                                           */
77/* ========================================================================= */
78
79/* ------------------------------------------------------------------------- */
80
81struct bts_Int16Mat2D bts_Int16Mat2D_createIdentity()
82{
83	struct bts_Int16Mat2D matL = { 1 << 14, 0, 0, 1 << 14, 14 };
84	return matL;
85}
86
87/* ------------------------------------------------------------------------- */
88
89struct bts_Int16Mat2D bts_Int16Mat2D_createRotation( phase16 angleA )
90{
91	int16 cL = bbs_cos16( angleA );
92	int16 sL = bbs_sin16( angleA );
93	struct bts_Int16Mat2D matL;
94	matL.xxE =  cL;
95	matL.xyE = -sL;
96	matL.yxE =  sL;
97	matL.yyE =  cL;
98	matL.bbpE = 14;
99	return matL;
100}
101
102/* ------------------------------------------------------------------------- */
103
104struct bts_Int16Mat2D bts_Int16Mat2D_createRigid( phase16 angleA, struct flt16 scaleA )
105{
106	struct bts_Int16Mat2D matL = bts_Int16Mat2D_createRotation( angleA );
107	bts_Int16Mat2D_scale( &matL, scaleA );
108	return matL;
109}
110
111/* ------------------------------------------------------------------------- */
112
113void bts_Int16Mat2D_scale( struct bts_Int16Mat2D* ptrA, struct flt16 scaleA )
114{
115	int32 xxL = ( int32 ) ptrA->xxE * scaleA.valE;
116	int32 xyL = ( int32 ) ptrA->xyE * scaleA.valE;
117	int32 yxL = ( int32 ) ptrA->yxE * scaleA.valE;
118	int32 yyL = ( int32 ) ptrA->yyE * scaleA.valE;
119
120	uint32 shiftL = bts_maxAbsIntLog2Of4( xxL, xyL, yxL, yyL ) - 15;
121
122	ptrA->xxE = xxL >> shiftL;
123	ptrA->xyE = xyL >> shiftL;
124	ptrA->yxE = yxL >> shiftL;
125	ptrA->yyE = yyL >> shiftL;
126
127	ptrA->bbpE += scaleA.bbpE - shiftL;
128}
129
130/* ------------------------------------------------------------------------- */
131
132struct bts_Int16Vec2D bts_Int16Mat2D_map( const struct bts_Int16Mat2D* matPtrA,
133								          const struct bts_Int16Vec2D* vecPtrA )
134{
135	struct bts_Int16Vec2D vecL;
136	vecL.xE = ( ( int32 ) matPtrA->xxE * vecPtrA->xE + ( int32 ) matPtrA->xyE * vecPtrA->yE ) >> matPtrA->bbpE;
137	vecL.yE = ( ( int32 ) matPtrA->yxE * vecPtrA->xE + ( int32 ) matPtrA->yyE * vecPtrA->yE ) >> matPtrA->bbpE;
138	return vecL;
139}
140
141/* ------------------------------------------------------------------------- */
142
143struct bts_Int16Mat2D bts_Int16Mat2D_mul( const struct bts_Int16Mat2D* mat1PtrA,
144								          const struct bts_Int16Mat2D* mat2PtrA )
145{
146	struct bts_Int16Mat2D matL;
147	int32 xxL = ( int32 ) mat1PtrA->xxE * mat2PtrA->xxE + ( int32 ) mat1PtrA->xyE * mat2PtrA->yxE;
148	int32 xyL = ( int32 ) mat1PtrA->xxE * mat2PtrA->xyE + ( int32 ) mat1PtrA->xyE * mat2PtrA->yyE;
149	int32 yxL = ( int32 ) mat1PtrA->yxE * mat2PtrA->xxE + ( int32 ) mat1PtrA->yyE * mat2PtrA->yxE;
150	int32 yyL = ( int32 ) mat1PtrA->yxE * mat2PtrA->xyE + ( int32 ) mat1PtrA->yyE * mat2PtrA->yyE;
151
152	uint32 shiftL = bts_maxAbsIntLog2Of4( xxL, xyL, yxL, yyL ) - 15;
153
154	matL.xxE = xxL >> shiftL;
155	matL.xyE = xyL >> shiftL;
156	matL.yxE = yxL >> shiftL;
157	matL.yyE = yyL >> shiftL;
158
159	matL.bbpE = mat1PtrA->bbpE + mat2PtrA->bbpE - shiftL;
160
161	return matL;
162}
163
164/* ------------------------------------------------------------------------- */
165
166/* ========================================================================= */
167
168
169