1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 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 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 /* 19 20 Filename: pv_log2.c 21 22 ------------------------------------------------------------------------------ 23 REVISION HISTORY 24 25 26 Who: Date: MM/DD/YYYY 27 Description: 28 29 ------------------------------------------------------------------------------ 30 INPUT AND OUTPUT DEFINITIONS 31 32 Int32 x 32-bit integer input 33 34 35 ------------------------------------------------------------------------------ 36 FUNCTION DESCRIPTION 37 38 Implement the logarithm base 2 of a number 39 ------------------------------------------------------------------------------ 40 REQUIREMENTS 41 42 43 ------------------------------------------------------------------------------ 44 REFERENCES 45 46 ------------------------------------------------------------------------------ 47 PSEUDO-CODE 48 49 ------------------------------------------------------------------------------ 50 */ 51 52 53 /*---------------------------------------------------------------------------- 54 ; INCLUDES 55 ----------------------------------------------------------------------------*/ 56 57 #ifdef AAC_PLUS 58 59 60 #include "pv_log2.h" 61 #include "fxp_mul32.h" 62 63 /*---------------------------------------------------------------------------- 64 ; MACROS 65 ; Define module specific macros here 66 ----------------------------------------------------------------------------*/ 67 68 69 /*---------------------------------------------------------------------------- 70 ; DEFINES 71 ; Include all pre-processor statements here. Include conditional 72 ; compile variables also. 73 ----------------------------------------------------------------------------*/ 74 75 /*---------------------------------------------------------------------------- 76 ; LOCAL FUNCTION DEFINITIONS 77 ; Function Prototype declaration 78 ----------------------------------------------------------------------------*/ 79 80 /*---------------------------------------------------------------------------- 81 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS 82 ; Variable declaration - defined here and used outside this module 83 ----------------------------------------------------------------------------*/ 84 85 #define R_SHIFT 20 86 #define Q_fmt(x) (Int32)(x*((Int32)1<<R_SHIFT) + (x>=0?0.5F:-0.5F)) 87 88 const Int32 log_table[9] = 89 { 90 Q_fmt(-0.00879832091331F), Q_fmt(0.12022974263833F), 91 Q_fmt(-0.72883958314294F), Q_fmt(2.57909824242332F), 92 Q_fmt(-5.90041216630330F), Q_fmt(9.15023342527264F), 93 Q_fmt(-9.90616619500413F), Q_fmt(8.11228968755409F), 94 Q_fmt(-3.41763474309898F) 95 }; 96 97 98 /*---------------------------------------------------------------------------- 99 ; EXTERNAL FUNCTION REFERENCES 100 ; Declare functions defined elsewhere and referenced in this module 101 ----------------------------------------------------------------------------*/ 102 103 /*---------------------------------------------------------------------------- 104 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES 105 ; Declare variables used in this module but defined elsewhere 106 ----------------------------------------------------------------------------*/ 107 108 /*---------------------------------------------------------------------------- 109 ; FUNCTION CODE 110 ----------------------------------------------------------------------------*/ 111 112 113 114 Int32 pv_log2(Int32 z) 115 { 116 const Int32 *pt_table = log_table; 117 Int32 y; 118 Int32 i; 119 120 Int32 int_log2 = 0; 121 122 if (z > Q_fmt(2.0f)) 123 { 124 while (z > Q_fmt(2.0f)) 125 { 126 z >>= 1; 127 int_log2++; 128 } 129 } 130 else if (z < Q_fmt(1.0f)) 131 { 132 { 133 while (z < Q_fmt(1.0f)) 134 { 135 z <<= 1; 136 int_log2--; 137 } 138 } 139 } 140 141 /* 142 * at this point, input limited to 1<x<2 143 */ 144 145 if (z != Q_fmt(1.0f)) 146 { 147 y = fxp_mul32_Q20(*(pt_table++), z); 148 149 for (i = 7; i != 0; i--) 150 { 151 y += *(pt_table++); 152 y = fxp_mul32_Q20(y, z); 153 } 154 155 y += *(pt_table++); 156 } 157 else 158 { 159 y = 0; 160 } 161 162 163 return (y + (int_log2 << 20)); /* Q20 */ 164 } 165 166 167 #endif 168 169