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