SphinxBase  5prealpha
fixlog.c
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 2005 Carnegie Mellon University. All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * This work was supported in part by funding from the Defense Advanced
19  * Research Projects Agency and the National Science Foundation of the
20  * United States of America, and the CMU Sphinx Speech Consortium.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * ====================================================================
35  *
36  * File: fixlog.c
37  *
38  * Description: Fast approximate fixed-point logarithms
39  *
40  * Author: David Huggins-Daines <dhuggins@cs.cmu.edu>
41  *
42  */
43 
44 #ifdef HAVE_CONFIG_H
45 #include <config.h>
46 #endif
47 
48 #include "sphinxbase/prim_type.h"
49 #include "sphinxbase/fixpoint.h"
50 
51 #include "fe_internal.h"
52 
53 /* Table of log2(x/128)*(1<<DEFAULT_RADIX) */
54 /* perl -e 'for (0..128) {my $x = 1 + $_/128; $y = 1 + ($_ + 1.) / 128;
55  print " (uint32)(", (log($x) + log($y))/2/log(2)," *(1<<DEFAULT_RADIX)),\n"}' */
56 static uint32 logtable[] = {
57  (uint32)(0.00561362771162706*(1<<DEFAULT_RADIX)),
58  (uint32)(0.0167975342258543*(1<<DEFAULT_RADIX)),
59  (uint32)(0.0278954072829524*(1<<DEFAULT_RADIX)),
60  (uint32)(0.0389085604479519*(1<<DEFAULT_RADIX)),
61  (uint32)(0.0498382774298215*(1<<DEFAULT_RADIX)),
62  (uint32)(0.060685812979481*(1<<DEFAULT_RADIX)),
63  (uint32)(0.0714523937543017*(1<<DEFAULT_RADIX)),
64  (uint32)(0.0821392191505851*(1<<DEFAULT_RADIX)),
65  (uint32)(0.0927474621054331*(1<<DEFAULT_RADIX)),
66  (uint32)(0.103278269869348*(1<<DEFAULT_RADIX)),
67  (uint32)(0.113732764750838*(1<<DEFAULT_RADIX)),
68  (uint32)(0.124112044834237*(1<<DEFAULT_RADIX)),
69  (uint32)(0.13441718467188*(1<<DEFAULT_RADIX)),
70  (uint32)(0.144649235951738*(1<<DEFAULT_RADIX)),
71  (uint32)(0.154809228141536*(1<<DEFAULT_RADIX)),
72  (uint32)(0.164898169110351*(1<<DEFAULT_RADIX)),
73  (uint32)(0.174917045728623*(1<<DEFAULT_RADIX)),
74  (uint32)(0.184866824447476*(1<<DEFAULT_RADIX)),
75  (uint32)(0.194748451858191*(1<<DEFAULT_RADIX)),
76  (uint32)(0.204562855232657*(1<<DEFAULT_RADIX)),
77  (uint32)(0.214310943045556*(1<<DEFAULT_RADIX)),
78  (uint32)(0.223993605479021*(1<<DEFAULT_RADIX)),
79  (uint32)(0.23361171491048*(1<<DEFAULT_RADIX)),
80  (uint32)(0.243166126384332*(1<<DEFAULT_RADIX)),
81  (uint32)(0.252657678068119*(1<<DEFAULT_RADIX)),
82  (uint32)(0.262087191693777*(1<<DEFAULT_RADIX)),
83  (uint32)(0.271455472984569*(1<<DEFAULT_RADIX)),
84  (uint32)(0.280763312068243*(1<<DEFAULT_RADIX)),
85  (uint32)(0.290011483876938*(1<<DEFAULT_RADIX)),
86  (uint32)(0.299200748534365*(1<<DEFAULT_RADIX)),
87  (uint32)(0.308331851730729*(1<<DEFAULT_RADIX)),
88  (uint32)(0.317405525085859*(1<<DEFAULT_RADIX)),
89  (uint32)(0.32642248650099*(1<<DEFAULT_RADIX)),
90  (uint32)(0.335383440499621*(1<<DEFAULT_RADIX)),
91  (uint32)(0.344289078557851*(1<<DEFAULT_RADIX)),
92  (uint32)(0.353140079424581*(1<<DEFAULT_RADIX)),
93  (uint32)(0.36193710943195*(1<<DEFAULT_RADIX)),
94  (uint32)(0.37068082279637*(1<<DEFAULT_RADIX)),
95  (uint32)(0.379371861910488*(1<<DEFAULT_RADIX)),
96  (uint32)(0.388010857626406*(1<<DEFAULT_RADIX)),
97  (uint32)(0.396598429530472*(1<<DEFAULT_RADIX)),
98  (uint32)(0.405135186209943*(1<<DEFAULT_RADIX)),
99  (uint32)(0.4136217255118*(1<<DEFAULT_RADIX)),
100  (uint32)(0.422058634793998*(1<<DEFAULT_RADIX)),
101  (uint32)(0.430446491169411*(1<<DEFAULT_RADIX)),
102  (uint32)(0.438785861742727*(1<<DEFAULT_RADIX)),
103  (uint32)(0.447077303840529*(1<<DEFAULT_RADIX)),
104  (uint32)(0.455321365234813*(1<<DEFAULT_RADIX)),
105  (uint32)(0.463518584360147*(1<<DEFAULT_RADIX)),
106  (uint32)(0.471669490524698*(1<<DEFAULT_RADIX)),
107  (uint32)(0.479774604115327*(1<<DEFAULT_RADIX)),
108  (uint32)(0.487834436796966*(1<<DEFAULT_RADIX)),
109  (uint32)(0.49584949170644*(1<<DEFAULT_RADIX)),
110  (uint32)(0.503820263640951*(1<<DEFAULT_RADIX)),
111  (uint32)(0.511747239241369*(1<<DEFAULT_RADIX)),
112  (uint32)(0.519630897170528*(1<<DEFAULT_RADIX)),
113  (uint32)(0.527471708286662*(1<<DEFAULT_RADIX)),
114  (uint32)(0.535270135812172*(1<<DEFAULT_RADIX)),
115  (uint32)(0.543026635497834*(1<<DEFAULT_RADIX)),
116  (uint32)(0.550741655782637*(1<<DEFAULT_RADIX)),
117  (uint32)(0.558415637949355*(1<<DEFAULT_RADIX)),
118  (uint32)(0.56604901627601*(1<<DEFAULT_RADIX)),
119  (uint32)(0.573642218183348*(1<<DEFAULT_RADIX)),
120  (uint32)(0.581195664378452*(1<<DEFAULT_RADIX)),
121  (uint32)(0.588709768994618*(1<<DEFAULT_RADIX)),
122  (uint32)(0.596184939727604*(1<<DEFAULT_RADIX)),
123  (uint32)(0.603621577968369*(1<<DEFAULT_RADIX)),
124  (uint32)(0.61102007893241*(1<<DEFAULT_RADIX)),
125  (uint32)(0.618380831785792*(1<<DEFAULT_RADIX)),
126  (uint32)(0.625704219767993*(1<<DEFAULT_RADIX)),
127  (uint32)(0.632990620311629*(1<<DEFAULT_RADIX)),
128  (uint32)(0.640240405159187*(1<<DEFAULT_RADIX)),
129  (uint32)(0.647453940476827*(1<<DEFAULT_RADIX)),
130  (uint32)(0.654631586965362*(1<<DEFAULT_RADIX)),
131  (uint32)(0.661773699968486*(1<<DEFAULT_RADIX)),
132  (uint32)(0.668880629578336*(1<<DEFAULT_RADIX)),
133  (uint32)(0.675952720738471*(1<<DEFAULT_RADIX)),
134  (uint32)(0.682990313344332*(1<<DEFAULT_RADIX)),
135  (uint32)(0.689993742341272*(1<<DEFAULT_RADIX)),
136  (uint32)(0.696963337820209*(1<<DEFAULT_RADIX)),
137  (uint32)(0.703899425110987*(1<<DEFAULT_RADIX)),
138  (uint32)(0.710802324873503*(1<<DEFAULT_RADIX)),
139  (uint32)(0.717672353186654*(1<<DEFAULT_RADIX)),
140  (uint32)(0.724509821635192*(1<<DEFAULT_RADIX)),
141  (uint32)(0.731315037394519*(1<<DEFAULT_RADIX)),
142  (uint32)(0.738088303313493*(1<<DEFAULT_RADIX)),
143  (uint32)(0.744829917995304*(1<<DEFAULT_RADIX)),
144  (uint32)(0.751540175876464*(1<<DEFAULT_RADIX)),
145  (uint32)(0.758219367303974*(1<<DEFAULT_RADIX)),
146  (uint32)(0.764867778610703*(1<<DEFAULT_RADIX)),
147  (uint32)(0.77148569218905*(1<<DEFAULT_RADIX)),
148  (uint32)(0.778073386562917*(1<<DEFAULT_RADIX)),
149  (uint32)(0.784631136458046*(1<<DEFAULT_RADIX)),
150  (uint32)(0.791159212870769*(1<<DEFAULT_RADIX)),
151  (uint32)(0.797657883135205*(1<<DEFAULT_RADIX)),
152  (uint32)(0.804127410988954*(1<<DEFAULT_RADIX)),
153  (uint32)(0.810568056637321*(1<<DEFAULT_RADIX)),
154  (uint32)(0.816980076816112*(1<<DEFAULT_RADIX)),
155  (uint32)(0.823363724853051*(1<<DEFAULT_RADIX)),
156  (uint32)(0.829719250727828*(1<<DEFAULT_RADIX)),
157  (uint32)(0.836046901130843*(1<<DEFAULT_RADIX)),
158  (uint32)(0.84234691952066*(1<<DEFAULT_RADIX)),
159  (uint32)(0.848619546180216*(1<<DEFAULT_RADIX)),
160  (uint32)(0.854865018271815*(1<<DEFAULT_RADIX)),
161  (uint32)(0.861083569890926*(1<<DEFAULT_RADIX)),
162  (uint32)(0.867275432118842*(1<<DEFAULT_RADIX)),
163  (uint32)(0.873440833074202*(1<<DEFAULT_RADIX)),
164  (uint32)(0.879579997963421*(1<<DEFAULT_RADIX)),
165  (uint32)(0.88569314913005*(1<<DEFAULT_RADIX)),
166  (uint32)(0.891780506103101*(1<<DEFAULT_RADIX)),
167  (uint32)(0.897842285644346*(1<<DEFAULT_RADIX)),
168  (uint32)(0.903878701794633*(1<<DEFAULT_RADIX)),
169  (uint32)(0.90988996591924*(1<<DEFAULT_RADIX)),
170  (uint32)(0.915876286752278*(1<<DEFAULT_RADIX)),
171  (uint32)(0.921837870440188*(1<<DEFAULT_RADIX)),
172  (uint32)(0.927774920584334*(1<<DEFAULT_RADIX)),
173  (uint32)(0.933687638282728*(1<<DEFAULT_RADIX)),
174  (uint32)(0.939576222170905*(1<<DEFAULT_RADIX)),
175  (uint32)(0.945440868461959*(1<<DEFAULT_RADIX)),
176  (uint32)(0.951281770985776*(1<<DEFAULT_RADIX)),
177  (uint32)(0.957099121227478*(1<<DEFAULT_RADIX)),
178  (uint32)(0.962893108365084*(1<<DEFAULT_RADIX)),
179  (uint32)(0.968663919306429*(1<<DEFAULT_RADIX)),
180  (uint32)(0.974411738725344*(1<<DEFAULT_RADIX)),
181  (uint32)(0.980136749097113*(1<<DEFAULT_RADIX)),
182  (uint32)(0.985839130733238*(1<<DEFAULT_RADIX)),
183  (uint32)(0.991519061815512*(1<<DEFAULT_RADIX)),
184  (uint32)(0.997176718429429*(1<<DEFAULT_RADIX)),
185  (uint32)(1.00281227459694*(1<<DEFAULT_RADIX)),
186 };
187 
188 int32
189 fixlog2(uint32 x)
190 {
191  uint32 y;
192 
193  if (x == 0)
194  return MIN_FIXLOG2;
195 
196  /* Get the exponent. */
197 #if ((defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5T__) || \
198  defined(__ARM_ARCH_5TE__) || defined(__ARM_ARCH_7A__)) && !defined(__thumb__))
199  __asm__("clz %0, %1\n": "=r"(y):"r"(x));
200  x <<= y;
201  y = 31 - y;
202 #elif defined(__ppc__)
203  __asm__("cntlzw %0, %1\n": "=r"(y):"r"(x));
204  x <<= y;
205  y = 31 - y;
206 #elif __GNUC__ >= 4
207  y = __builtin_clz(x);
208  x <<= y;
209  y = (31 - y);
210 #else
211  for (y = 31; y > 0; --y) {
212  if (x & 0x80000000)
213  break;
214  x <<= 1;
215  }
216 #endif
217  y <<= DEFAULT_RADIX;
218  /* Do a table lookup for the MSB of the mantissa. */
219  x = (x >> 24) & 0x7f;
220  return y + logtable[x];
221 }
222 
223 int
224 fixlog(uint32 x)
225 {
226  int32 y;
227  y = fixlog2(x);
228  return FIXMUL(y, FIXLN_2);
229 }
Basic type definitions used in Sphinx.