Home | History | Annotate | Download | only in b_BasicEm
      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/Complex.h"
     20 #include "b_BasicEm/APh.h"
     21 #include "b_BasicEm/Functions.h"
     22 #include "b_BasicEm/Math.h"
     23 
     24 /* ------------------------------------------------------------------------- */
     25 
     26 /* ========================================================================= */
     27 /*                                                                           */
     28 /* ---- \ghd{ auxiliary functions } ---------------------------------------- */
     29 /*                                                                           */
     30 /* ========================================================================= */
     31 
     32 /* ------------------------------------------------------------------------- */
     33 
     34 /* ========================================================================= */
     35 /*                                                                           */
     36 /* ---- \ghd{ constructor / destructor } ----------------------------------- */
     37 /*                                                                           */
     38 /* ========================================================================= */
     39 
     40 /* ------------------------------------------------------------------------- */
     41 
     42 /* ========================================================================= */
     43 /*                                                                           */
     44 /* ---- \ghd{ operators } -------------------------------------------------- */
     45 /*                                                                           */
     46 /* ========================================================================= */
     47 
     48 /* ------------------------------------------------------------------------- */
     49 
     50 flag bbs_Complex_equal( struct bbs_Complex compl1A, struct bbs_Complex compl2A )
     51 {
     52 	return ( compl1A.realE == compl2A.realE ) && ( compl1A.imagE == compl2A.imagE );
     53 }
     54 
     55 /* ------------------------------------------------------------------------- */
     56 
     57 /* ========================================================================= */
     58 /*                                                                           */
     59 /* ---- \ghd{ query functions } -------------------------------------------- */
     60 /*                                                                           */
     61 /* ========================================================================= */
     62 
     63 /* ------------------------------------------------------------------------- */
     64 
     65 /* ========================================================================= */
     66 /*                                                                           */
     67 /* ---- \ghd{ modify functions } ------------------------------------------- */
     68 /*                                                                           */
     69 /* ========================================================================= */
     70 
     71 /* ------------------------------------------------------------------------- */
     72 
     73 /* ========================================================================= */
     74 /*                                                                           */
     75 /* ---- \ghd{ I/O } -------------------------------------------------------- */
     76 /*                                                                           */
     77 /* ========================================================================= */
     78 
     79 /* ------------------------------------------------------------------------- */
     80 
     81 uint32 bbs_Complex_memSize( struct bbs_Context* cpA,
     82 						    struct bbs_Complex complA )
     83 {
     84 	return bbs_SIZEOF16( complA.realE ) + bbs_SIZEOF16( complA.imagE );
     85 }
     86 
     87 /* ------------------------------------------------------------------------- */
     88 
     89 uint32 bbs_Complex_memWrite( struct bbs_Context* cpA,
     90 							 const struct bbs_Complex* ptrA,
     91 							 uint16* memPtrA )
     92 {
     93 	memPtrA += bbs_memWrite16( &ptrA->realE, memPtrA );
     94 	memPtrA += bbs_memWrite16( &ptrA->imagE, memPtrA );
     95 	return bbs_Complex_memSize( cpA, *ptrA );
     96 }
     97 
     98 /* ------------------------------------------------------------------------- */
     99 
    100 uint32 bbs_Complex_memRead( struct bbs_Context* cpA,
    101 						    struct bbs_Complex* ptrA,
    102 							const uint16* memPtrA )
    103 {
    104 	if( bbs_Context_error( cpA ) ) return 0;
    105 	memPtrA += bbs_memRead16( &ptrA->realE, memPtrA );
    106 	memPtrA += bbs_memRead16( &ptrA->imagE, memPtrA );
    107 	return bbs_Complex_memSize( cpA, *ptrA );
    108 }
    109 
    110 /* ------------------------------------------------------------------------- */
    111 
    112 /* ========================================================================= */
    113 /*                                                                           */
    114 /* ---- \ghd{ exec functions } --------------------------------------------- */
    115 /*                                                                           */
    116 /* ========================================================================= */
    117 
    118 struct bbs_Complex bbs_Complex_conj( struct bbs_Complex complA )
    119 {
    120 	struct bbs_Complex resultL;
    121 	resultL.imagE = - complA.imagE;
    122 	resultL.realE = complA.realE;
    123 	return resultL;
    124 }
    125 
    126 /* ------------------------------------------------------------------------- */
    127 
    128 uint32 bbs_Complex_abs2( struct bbs_Complex complA )
    129 {
    130 	return ( int32 ) complA.realE * complA.realE +
    131 		   ( int32 ) complA.imagE * complA.imagE;
    132 }
    133 
    134 /* ------------------------------------------------------------------------- */
    135 
    136 uint16 bbs_Complex_abs( struct bbs_Complex complA )
    137 {
    138 	return bbs_sqrt32( bbs_Complex_abs2( complA ) );
    139 }
    140 
    141 /* ------------------------------------------------------------------------- */
    142 
    143 phase16 bbs_Complex_phase( struct bbs_Complex complA )
    144 {
    145 	int32 realL, imagL;
    146 	realL = complA.realE;
    147 	imagL = complA.imagE;
    148 
    149 	return bbs_phase16( realL, imagL );
    150 }
    151 
    152 /* ------------------------------------------------------------------------- */
    153 
    154 void bbs_Complex_importAPh( struct bbs_Complex* dstPtrA, const struct bbs_APh* srcPtrA )
    155 {
    156 	dstPtrA->realE = ( ( bbs_cos32( srcPtrA->phaseE ) >> 8 ) * srcPtrA->absE ) >> 16;
    157 	dstPtrA->imagE = ( ( bbs_sin32( srcPtrA->phaseE ) >> 8 ) * srcPtrA->absE ) >> 16;
    158 }
    159 
    160 /* ------------------------------------------------------------------------- */
    161 
    162 /* ========================================================================= */
    163 
    164 
    165