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/CompactAlt.h" 20 #include "b_TensorEm/Functions.h" 21 #include "b_BasicEm/Math.h" 22 #include "b_BasicEm/Functions.h" 23 #include "b_BasicEm/Memory.h" 24 25 /* ------------------------------------------------------------------------- */ 26 27 /* ========================================================================= */ 28 /* */ 29 /* ---- \ghd{ auxiliary functions } ---------------------------------------- */ 30 /* */ 31 /* ========================================================================= */ 32 33 /* ------------------------------------------------------------------------- */ 34 35 /* ========================================================================= */ 36 /* */ 37 /* ---- \ghd{ constructor / destructor } ----------------------------------- */ 38 /* */ 39 /* ========================================================================= */ 40 41 /* ------------------------------------------------------------------------- */ 42 43 void bts_CompactAlt_init( struct bbs_Context* cpA, 44 struct bts_CompactAlt* ptrA ) 45 { 46 bts_CompactMat_init( cpA, &ptrA->matE ); 47 bbs_Int16Arr_init( cpA, &ptrA->vecE ); 48 ptrA->vecExpE = 0; 49 } 50 51 /* ------------------------------------------------------------------------- */ 52 53 void bts_CompactAlt_exit( struct bbs_Context* cpA, 54 struct bts_CompactAlt* ptrA ) 55 { 56 bts_CompactMat_exit( cpA, &ptrA->matE ); 57 bbs_Int16Arr_exit( cpA, &ptrA->vecE ); 58 ptrA->vecExpE = 0; 59 } 60 /* ------------------------------------------------------------------------- */ 61 62 /* ========================================================================= */ 63 /* */ 64 /* ---- \ghd{ operators } -------------------------------------------------- */ 65 /* */ 66 /* ========================================================================= */ 67 68 /* ------------------------------------------------------------------------- */ 69 70 /* ========================================================================= */ 71 /* */ 72 /* ---- \ghd{ query functions } -------------------------------------------- */ 73 /* */ 74 /* ========================================================================= */ 75 76 /* ------------------------------------------------------------------------- */ 77 78 /* ========================================================================= */ 79 /* */ 80 /* ---- \ghd{ modify functions } ------------------------------------------- */ 81 /* */ 82 /* ========================================================================= */ 83 84 /* ------------------------------------------------------------------------- */ 85 86 void bts_CompactAlt_create( struct bbs_Context* cpA, 87 struct bts_CompactAlt* ptrA, 88 uint32 widthA, 89 uint32 heightA, 90 uint32 bitsA, 91 uint32 maxRowSizeA, 92 struct bbs_MemSeg* mspA ) 93 { 94 bts_CompactMat_create( cpA, &ptrA->matE, widthA, heightA, bitsA, maxRowSizeA, mspA ); 95 bbs_Int16Arr_create( cpA, &ptrA->vecE, heightA, mspA ); 96 bbs_Int16Arr_fill( cpA, &ptrA->vecE, 0 ); 97 ptrA->vecExpE = 0; 98 } 99 100 /* ------------------------------------------------------------------------- */ 101 102 void bts_CompactAlt_copy( struct bbs_Context* cpA, 103 struct bts_CompactAlt* ptrA, 104 const struct bts_CompactAlt* srcPtrA ) 105 { 106 bts_CompactMat_copy( cpA, &ptrA->matE, &srcPtrA->matE ); 107 bbs_Int16Arr_copy( cpA, &ptrA->vecE, &srcPtrA->vecE ); 108 ptrA->vecExpE = srcPtrA->vecExpE; 109 } 110 111 /* ------------------------------------------------------------------------- */ 112 113 /* ========================================================================= */ 114 /* */ 115 /* ---- \ghd{ I/O } -------------------------------------------------------- */ 116 /* */ 117 /* ========================================================================= */ 118 119 /* ------------------------------------------------------------------------- */ 120 121 uint32 bts_CompactAlt_memSize( struct bbs_Context* cpA, 122 const struct bts_CompactAlt *ptrA ) 123 { 124 return bbs_SIZEOF16( uint32 ) 125 + bbs_SIZEOF16( uint32 ) /* version */ 126 + bts_CompactMat_memSize( cpA, &ptrA->matE ) 127 + bbs_Int16Arr_memSize( cpA, &ptrA->vecE ) 128 + bbs_SIZEOF16( ptrA->vecExpE ); 129 } 130 131 /* ------------------------------------------------------------------------- */ 132 133 uint32 bts_CompactAlt_memWrite( struct bbs_Context* cpA, 134 const struct bts_CompactAlt* ptrA, 135 uint16* memPtrA ) 136 { 137 uint32 memSizeL = bts_CompactAlt_memSize( cpA, ptrA ); 138 memPtrA += bbs_memWrite32( &memSizeL, memPtrA ); 139 memPtrA += bbs_memWriteUInt32( bts_COMPACT_ALT_VERSION, memPtrA ); 140 memPtrA += bts_CompactMat_memWrite( cpA, &ptrA->matE, memPtrA ); 141 memPtrA += bbs_Int16Arr_memWrite( cpA, &ptrA->vecE, memPtrA ); 142 memPtrA += bbs_memWrite32( &ptrA->vecExpE, memPtrA ); 143 return memSizeL; 144 } 145 146 /* ------------------------------------------------------------------------- */ 147 148 uint32 bts_CompactAlt_memRead( struct bbs_Context* cpA, 149 struct bts_CompactAlt* ptrA, 150 const uint16* memPtrA, 151 struct bbs_MemSeg* mspA ) 152 { 153 uint32 memSizeL, versionL; 154 if( bbs_Context_error( cpA ) ) return 0; 155 memPtrA += bbs_memRead32( &memSizeL, memPtrA ); 156 memPtrA += bbs_memReadVersion32( cpA, &versionL, bts_COMPACT_ALT_VERSION, memPtrA ); 157 memPtrA += bts_CompactMat_memRead( cpA, &ptrA->matE, memPtrA, mspA ); 158 memPtrA += bbs_Int16Arr_memRead( cpA, &ptrA->vecE, memPtrA, mspA ); 159 memPtrA += bbs_memRead32( &ptrA->vecExpE, memPtrA ); 160 161 if( memSizeL != bts_CompactAlt_memSize( cpA, ptrA ) ) 162 { 163 bbs_ERR0( bbs_ERR_CORRUPT_DATA, "uint32 bts_CompactAlt_memRead( const struct bts_CompactAlt* ptrA, const void* memPtrA ):\n" 164 "size mismatch" ); 165 } 166 167 return memSizeL; 168 } 169 170 /* ------------------------------------------------------------------------- */ 171 172 /* ========================================================================= */ 173 /* */ 174 /* ---- \ghd{ exec functions } --------------------------------------------- */ 175 /* */ 176 /* ========================================================================= */ 177 178 /* ------------------------------------------------------------------------- */ 179 180 void bts_CompactAlt_map( struct bbs_Context* cpA, 181 const struct bts_CompactAlt* ptrA, 182 const int16* inVecA, 183 int16 inExpA, 184 int16* outVecA, 185 int16* outExpPtrA ) 186 { 187 uint32 iL; 188 uint32 sizeL = ptrA->matE.heightE; 189 190 int32 expL = inExpA; 191 int16 mapExpL; 192 bts_CompactMat_map( cpA, &ptrA->matE, inVecA, outVecA, &mapExpL ); 193 expL += mapExpL; 194 195 /* translation */ 196 if( ptrA->vecE.sizeE > 0 ) 197 { 198 const int16* vecL = ptrA->vecE.arrPtrE; 199 if( expL == ptrA->vecExpE ) 200 { 201 for( iL = 0; iL < sizeL; iL++ ) outVecA[ iL ] = ( ( int32 )outVecA[ iL ] + vecL[ iL ] + 1 ) >> 1; 202 expL += 1; 203 } 204 else if( expL > ptrA->vecExpE ) 205 { 206 int32 shrL = expL - ptrA->vecExpE; 207 int32 addL = ( int32 )1 << ( shrL - 1 ); 208 for( iL = 0; iL < sizeL; iL++ ) outVecA[ iL ] = ( ( int32 )outVecA[ iL ] + ( ( ( int32 )vecL[ iL ] + addL ) >> shrL ) + 1 ) >> 1; 209 expL += 1; 210 } 211 else 212 { 213 int32 shrL = ptrA->vecExpE - expL; 214 int32 addL = ( int32 )1 << ( shrL - 1 ); 215 for( iL = 0; iL < sizeL; iL++ ) outVecA[ iL ] = ( ( ( ( int32 )outVecA[ iL ] + addL ) >> shrL ) + vecL[ iL ] + 1 ) >> 1; 216 expL += 1 + shrL; 217 } 218 } 219 220 /* precision underflow */ 221 if( expL < -32767 ) 222 { 223 bbs_memset16( outVecA, 0, ptrA->matE.heightE ); 224 expL = 0; 225 } 226 227 if( outExpPtrA != NULL ) *outExpPtrA = expL; 228 } 229 230 /* ------------------------------------------------------------------------- */ 231 232 /* ========================================================================= */ 233 234